Skip to content

Commit ad71d24

Browse files
committed
Add basic test
1 parent c85afdb commit ad71d24

File tree

3 files changed

+84
-4
lines changed

3 files changed

+84
-4
lines changed

bower.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"purescript-psci-support": "^4.0.0",
2424
"purescript-console": "^4.1.0",
2525
"purescript-effect": "^2.0.0",
26-
"purescript-assert": "^4.0.0"
26+
"purescript-assert": "^4.0.0",
27+
"purescript-quickcheck": "^5.0.0"
2728
}
2829
}

src/Routing/Duplex/Printer.purs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@ run = printPath <<< applyFlipped emptyRouteState <<< unwrap
4747

4848
printPath :: RouteState -> String
4949
printPath { segments, params, hash: hash' } =
50-
joinWith "/" segments <> printParams params <> printHash hash'
50+
printSegments segments <> printParams params <> printHash hash'
5151
where
52-
printSegments =
53-
joinWith "/" <<< map unsafeEncodeURIComponent
52+
printSegments = case _ of
53+
[""] -> "/"
54+
xs -> joinWith "/" $ map unsafeEncodeURIComponent xs
5455

5556
printParams [] = ""
5657
printParams ps = "?" <> joinWith "&" (uncurry printParam <$> ps)

test/Main.purs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
module Test.Main where
2+
3+
import Prelude hiding ((/))
4+
5+
import Data.Either (Either(..))
6+
import Data.Generic.Rep (class Generic)
7+
import Data.Generic.Rep.Show (genericShow)
8+
import Data.String.Gen (genAlphaString)
9+
import Data.Symbol (SProxy(..))
10+
import Effect (Effect)
11+
import Routing.Duplex (RouteDuplex', flag, int, param, parse, print, record, rest, root, segment, string, (:=))
12+
import Routing.Duplex.Generic (noArgs)
13+
import Routing.Duplex.Generic as RDG
14+
import Routing.Duplex.Generic.Syntax ((/), (?))
15+
import Test.QuickCheck (Result(..), arbitrary, quickCheckGen, (===))
16+
import Test.QuickCheck.Gen (Gen, arrayOf, chooseInt)
17+
18+
data TestRoute
19+
= Root
20+
| Foo String Int String { a :: String, b :: Boolean }
21+
| Bar { id :: String, search :: String }
22+
| Baz String (Array String)
23+
24+
derive instance eqTestRoute :: Eq TestRoute
25+
derive instance genericTestRoute :: Generic TestRoute _
26+
instance showTestRoute :: Show TestRoute where show = genericShow
27+
28+
genTestRoute :: Gen TestRoute
29+
genTestRoute = do
30+
chooseInt 1 4 >>= case _ of
31+
1 -> pure Root
32+
2 ->
33+
Foo
34+
<$> genAlphaString
35+
<*> arbitrary
36+
<*> genAlphaString
37+
<*> ({ a: _, b: _ } <$> genAlphaString <*> arbitrary)
38+
3 -> Bar <$> ({ id: _, search: _ } <$> genAlphaString <*> genAlphaString)
39+
_ -> Baz <$> genAlphaString <*> (arrayOf genAlphaString)
40+
41+
_id = SProxy :: SProxy "id"
42+
_search = SProxy :: SProxy "search"
43+
44+
route :: RouteDuplex' TestRoute
45+
route =
46+
root $ RDG.sum
47+
{ "Root": noArgs
48+
, "Foo": fooRoute
49+
, "Bar": barRoute
50+
, "Baz": bazRoute
51+
}
52+
where
53+
fooRoute =
54+
segment / int segment / segment ? { a: string, b: flag }
55+
56+
barRoute =
57+
record
58+
# _id := segment
59+
# _search := param "search"
60+
61+
bazRoute =
62+
segment / rest
63+
64+
main :: Effect Unit
65+
main = do
66+
quickCheckGen do
67+
r <- genTestRoute
68+
let
69+
url = print route r
70+
res = parse route url
71+
pure $ case res of
72+
Left err ->
73+
Failed $
74+
show err <> ":"
75+
<> "\n " <> show r
76+
<> "\n " <> show url
77+
Right r' ->
78+
r === r'

0 commit comments

Comments
 (0)