Skip to content

Commit

Permalink
Add unit tests for parseHash and parsePath (#69)
Browse files Browse the repository at this point in the history
Co-authored-by: Melvyn Laïly <[email protected]>
  • Loading branch information
mlaily and Melvyn Laïly authored Oct 18, 2023
1 parent 7e08d41 commit 6f9f245
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 2 deletions.
5 changes: 3 additions & 2 deletions build.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ let projects =
!! "src/**.fsproj"
++ "netstandard/**.fsproj"

System.Environment.GetCommandLineArgs()
System.Environment.GetCommandLineArgs()
|> Array.skip 2 // fsi.exe; build.fsx
|> Array.toList
|> Context.FakeExecutionContext.Create false __SOURCE_FILE__
Expand Down Expand Up @@ -128,9 +128,10 @@ Target.create "Publish" ignore
==> "Meta"
==> "Restore"
==> "Build"
==> "Test"
==> "Package"
==> "PublishNuget"
==> "Publish"

// start build
Target.runOrDefault "Build"
Target.runOrDefault "Test"
21 changes: 21 additions & 0 deletions tests/Fable.Elmish.Browser.Tests.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="../src/Fable.Elmish.Browser.fsproj" />
</ItemGroup>
<ItemGroup>
<Compile Include="ParserTests.fs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="FsCheck" Version="2.*" />
<PackageReference Include="FsCheck.NUnit" Version="2.*" />
<PackageReference Include="NUnit3TestAdapter" Version="3.16.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Unquote" Version="4.*" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
</ItemGroup>
</Project>
127 changes: 127 additions & 0 deletions tests/ParserTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
module Elmish.ParserTests

open Swensen.Unquote
open NUnit.Framework
open UrlParser

let log (state: State<_>) : State<_> =
NUnit.Framework.TestContext.WriteLine("UrlParser state:",
{|
unvisited = state.unvisited |> Array.ofList
visited = state.visited |> Array.ofList
queryParams = state.args |> Map.toArray
|})
state

/// Created using `document.location` on url:
/// https://github.com/elmish/src/parser.fs?key1=value1&key2&key3=value3#L16-L33
let testPathLocation =
{ new Browser.Types.Location with
member this.assign(url: string): unit = raise (System.NotImplementedException())
member this.hash
with get (): string = "#L16-L33"
and set (v: string): unit = raise (System.NotImplementedException())
member this.host
with get (): string = "github.com"
and set (v: string): unit = raise (System.NotImplementedException())
member this.hostname
with get (): string = "github.com"
and set (v: string): unit = raise (System.NotImplementedException())
member this.href
with get (): string = "https://github.com/elmish/src/parser.fs?key1=value1&key2&key3=value3#L16-L33"
and set (v: string): unit = raise (System.NotImplementedException())
member this.origin
with get (): string = "https://github.com"
and set (v: string): unit = raise (System.NotImplementedException())
member this.password
with get (): string = raise (System.NotImplementedException())
and set (v: string): unit = raise (System.NotImplementedException())
member this.pathname
with get (): string = "/elmish/src/parser.fs"
and set (v: string): unit = raise (System.NotImplementedException())
member this.port
with get (): string = ""
and set (v: string): unit = raise (System.NotImplementedException())
member this.protocol
with get (): string = "https:"
and set (v: string): unit = raise (System.NotImplementedException())
member this.reload(forcedReload: bool option): unit = raise (System.NotImplementedException())
member this.replace(url: string): unit = raise (System.NotImplementedException())
member this.search
with get (): string = "?key1=value1&key2&key3=value3"
and set (v: string): unit = raise (System.NotImplementedException())
member this.toString(): string = raise (System.NotImplementedException())
member this.username
with get (): string = raise (System.NotImplementedException())
and set (v: string): unit = raise (System.NotImplementedException()) }

/// Created using `document.location` on url:
/// https://github.com/root/#/elmish/src/parser.fs?key1=value1&key2&key3=value3
let testHashLocation =
{ new Browser.Types.Location with
member this.assign(url: string): unit = raise (System.NotImplementedException())
member this.hash
with get (): string = "#/elmish/src/parser.fs?key1=value1&key2&key3=value3"
and set (v: string): unit = raise (System.NotImplementedException())
member this.host
with get (): string = "github.com"
and set (v: string): unit = raise (System.NotImplementedException())
member this.hostname
with get (): string = "github.com"
and set (v: string): unit = raise (System.NotImplementedException())
member this.href
with get (): string = "https://github.com/root/#/elmish/src/parser.fs?key1=value1&key2&key3=value3"
and set (v: string): unit = raise (System.NotImplementedException())
member this.origin
with get (): string = "https://github.com"
and set (v: string): unit = raise (System.NotImplementedException())
member this.password
with get (): string = raise (System.NotImplementedException())
and set (v: string): unit = raise (System.NotImplementedException())
member this.pathname
with get (): string = "/root/"
and set (v: string): unit = raise (System.NotImplementedException())
member this.port
with get (): string = ""
and set (v: string): unit = raise (System.NotImplementedException())
member this.protocol
with get (): string = "https:"
and set (v: string): unit = raise (System.NotImplementedException())
member this.reload(forcedReload: bool option): unit = raise (System.NotImplementedException())
member this.replace(url: string): unit = raise (System.NotImplementedException())
member this.search
with get (): string = ""
and set (v: string): unit = raise (System.NotImplementedException())
member this.toString(): string = raise (System.NotImplementedException())
member this.username
with get (): string = raise (System.NotImplementedException())
and set (v: string): unit = raise (System.NotImplementedException()) }

type GitHubUrl =
| GitHubUrl of key1: string option * key3: string option

[<Test>]
let ``parseHash works``() =
let parser : Parser<GitHubUrl -> GitHubUrl, GitHubUrl> =
log >>
oneOf [
(s "elmish" </> s "src" </> s"parser.fs" <?> stringParam "key1" <?> stringParam "key3")
|> map (fun key1 key3 -> GitHubUrl(key1, key3))
]

let parsedHash = Elmish.UrlParser.parseHash parser testHashLocation

parsedHash =! Some (GitHubUrl(Some "value1", Some "value3"))

[<Test>]
let ``parsePath works``() =
let parser : Parser<GitHubUrl -> GitHubUrl, GitHubUrl> =
log >>
oneOf [
(s "elmish" </> s "src" </> s"parser.fs" <?> stringParam "key1" <?> stringParam "key3")
|> map (fun key1 key3 -> GitHubUrl(key1, key3))
]

let parsedHash = Elmish.UrlParser.parsePath parser testPathLocation

parsedHash =! Some (GitHubUrl(Some "value1", Some "value3"))

0 comments on commit 6f9f245

Please sign in to comment.