@@ -2,68 +2,111 @@ module Test.Main where
2
2
3
3
import Prelude
4
4
5
- import Control.Alt ((<|>))
6
5
import Control.Monad.Eff (Eff )
7
- import Control.Monad.Eff.Console (CONSOLE , log )
6
+ import Control.Monad.Eff.Console (CONSOLE )
7
+ import Data.Bifunctor (lmap )
8
8
import Data.Either (Either (..))
9
+ import Data.Foldable (oneOf )
10
+ import Data.Generic.Rep (class Generic )
11
+ import Data.Generic.Rep.Show (genericShow )
9
12
import Data.List (List )
10
13
import Data.List as L
11
14
import Data.Map as M
15
+ import Data.String.NonEmpty (NonEmptyString )
16
+ import Data.String.NonEmpty as NES
12
17
import Data.Tuple (Tuple (..))
18
+ import Partial.Unsafe (unsafePartial )
13
19
import Routing (match )
14
- import Routing.Match (Match , list )
15
- import Routing.Match.Class (bool , end , int , lit , num , str , param , params )
16
- import Test.Assert (ASSERT , assert' )
20
+ import Routing.Match (Match , bool , end , int , list , lit , nonempty , num , param , params , str )
21
+ import Test.Assert (ASSERT , assertEqual )
17
22
18
- data FooBar
23
+ data MyRoutes
19
24
= Foo Number (M.Map String String )
20
25
| Bar Boolean String
21
26
| Baz (List Number )
22
27
| Quux Int
23
28
| Corge String
29
+ | Corge' NonEmptyString
24
30
| End Int
25
31
26
- derive instance eqFooBar :: Eq FooBar
27
-
28
- instance showFooBar :: Show FooBar where
29
- show (Foo num q) = " (Foo " <> show num <> " " <> show q <> " )"
30
- show (Bar bool str) = " (Bar " <> show bool <> " " <> show str <> " )"
31
- show (Baz lst) = " (Baz " <> show lst <> " )"
32
- show (Quux i) = " (Quux " <> show i <> " )"
33
- show (Corge str) = " (Corge " <> show str <> " )"
34
- show (End i) = " (End " <> show i <> " )"
35
-
36
- routing :: Match FooBar
37
- routing =
38
- Foo <$> (lit " foo" *> num) <*> params
39
- <|> Bar <$> (lit " bar" *> bool) <*> (param " baz" )
40
- <|> Quux <$> (lit " " *> lit " quux" *> int)
41
- <|> Corge <$> (lit " corge" *> str)
42
- -- Order matters here. `list` is greedy, and `end` wont match after it
43
- <|> End <$> (lit " " *> int <* end)
44
- <|> Baz <$> (list num)
32
+ derive instance eqMyRoutes :: Eq MyRoutes
33
+ derive instance genericMyRoutes :: Generic MyRoutes _
34
+ instance showMyRoutes :: Show MyRoutes where show = genericShow
45
35
36
+ routing :: Match MyRoutes
37
+ routing = oneOf
38
+ [ Foo <$> (lit " foo" *> num) <*> params
39
+ , Bar <$> (lit " bar" *> bool) <*> (param " baz" )
40
+ , Baz <$> (lit " list" *> list num)
41
+ , Quux <$> (lit " " *> lit " quux" *> int)
42
+ , Corge <$> (lit " corge" *> str)
43
+ , Corge' <$> (lit " corge'" *> nonempty)
44
+ , End <$> (lit " " *> int <* end)
45
+ ]
46
46
47
47
main :: Eff (assert :: ASSERT , console :: CONSOLE ) Unit
48
48
main = do
49
- assertEq (match routing " foo/12/?welp='hi'&b=false" ) (Right (Foo 12.0 (M .fromFoldable [Tuple " welp" " 'hi'" , Tuple " b" " false" ])))
50
- assertEq (match routing " foo/12?welp='hi'&b=false" ) (Right (Foo 12.0 (M .fromFoldable [Tuple " welp" " 'hi'" , Tuple " b" " false" ])))
51
- assertEq (match routing " bar/true?baz=test" ) (Right (Bar true " test" ))
52
- assertEq (match routing " bar/false?baz=%D0%B2%D1%80%D0%B5%D0%BC%D0%B5%D0%BD%D0%BD%D1%8B%D0%B9%20%D1%84%D0%B0%D0%B9%D0%BB" ) (Right (Bar false " временный файл" ))
53
- assertEq (match routing " corge/test" ) (Right (Corge " test" ))
54
- assertEq (match routing " corge/%D0%B2%D1%80%D0%B5%D0%BC%D0%B5%D0%BD%D0%BD%D1%8B%D0%B9%20%D1%84%D0%B0%D0%B9%D0%BB" ) (Right (Corge " временный файл" ))
55
- assertEq (match routing " /quux/42" ) (Right (Quux 42 ))
56
- assertEq (match routing " 123/" ) (Right (Baz (L .fromFoldable [123.0 ])))
57
- assertEq (match routing " /1" ) (Right (End 1 ))
58
- assertEq (match routing " foo/0/?test=a/b/c" ) (Right (Foo 0.0 (M .fromFoldable [Tuple " test" " a/b/c" ])))
59
-
60
- assertEq
61
- :: forall a eff
62
- . Eq a
63
- => Show a
64
- => a
65
- -> a
66
- -> Eff (assert :: ASSERT , console :: CONSOLE | eff ) Unit
67
- assertEq actual expected
68
- | actual /= expected = assert' (" Equality assertion failed\n\n Actual: " <> show actual <> " \n\n Expected: " <> show expected) false
69
- | otherwise = log (" Equality assertion passed for " <> show actual)
49
+ assertEqual
50
+ { actual: match routing " foo/12/?welp='hi'&b=false"
51
+ , expected: Right (Foo 12.0 (M .fromFoldable [Tuple " welp" " 'hi'" , Tuple " b" " false" ]))
52
+ }
53
+ assertEqual
54
+ { actual: match routing " foo/12?welp='hi'&b=false"
55
+ , expected: Right (Foo 12.0 (M .fromFoldable [Tuple " welp" " 'hi'" , Tuple " b" " false" ]))
56
+ }
57
+ assertEqual
58
+ { actual: match routing " bar/true?baz=test"
59
+ , expected: Right (Bar true " test" )
60
+ }
61
+ assertEqual
62
+ { actual: match routing " bar/false?baz=%D0%B2%D1%80%D0%B5%D0%BC%D0%B5%D0%BD%D0%BD%D1%8B%D0%B9%20%D1%84%D0%B0%D0%B9%D0%BB"
63
+ , expected: Right (Bar false " временный файл" )
64
+ }
65
+ assertEqual
66
+ { actual: match routing " corge/test"
67
+ , expected: Right (Corge " test" )
68
+ }
69
+ assertEqual
70
+ { actual: match routing " corge/%D0%B2%D1%80%D0%B5%D0%BC%D0%B5%D0%BD%D0%BD%D1%8B%D0%B9%20%D1%84%D0%B0%D0%B9%D0%BB"
71
+ , expected: Right (Corge " временный файл" )
72
+ }
73
+ assertEqual
74
+ { actual: match routing " corge'/test"
75
+ , expected: Right (Corge' (unsafePartial NES .unsafeFromString " test" ))
76
+ }
77
+ assertEqual
78
+ { actual: match routing " corge'/%D0%B2%D1%80%D0%B5%D0%BC%D0%B5%D0%BD%D0%BD%D1%8B%D0%B9%20%D1%84%D0%B0%D0%B9%D0%BB"
79
+ , expected: Right (Corge' (unsafePartial NES .unsafeFromString " временный файл" ))
80
+ }
81
+ assertEqual
82
+ { actual: lmap (const unit) (match routing " corge'/" )
83
+ , expected: Left unit
84
+ }
85
+ assertEqual
86
+ { actual: match routing " /quux/42"
87
+ , expected: Right (Quux 42 )
88
+ }
89
+ assertEqual
90
+ { actual: match routing " list/123/"
91
+ , expected: Right (Baz (L .fromFoldable [123.0 ]))
92
+ }
93
+ assertEqual
94
+ { actual: match routing " list/123/456"
95
+ , expected: Right (Baz (L .fromFoldable [123.0 , 456.0 ]))
96
+ }
97
+ assertEqual
98
+ { actual: match routing " list/"
99
+ , expected: Right (Baz (L .fromFoldable [] ))
100
+ }
101
+ assertEqual
102
+ { actual: match routing " list"
103
+ , expected: Right (Baz (L .fromFoldable [] ))
104
+ }
105
+ assertEqual
106
+ { actual: match routing " /1"
107
+ , expected: Right (End 1 )
108
+ }
109
+ assertEqual
110
+ { actual: match routing " foo/0/?test=a/b/c"
111
+ , expected: Right (Foo 0.0 (M .fromFoldable [Tuple " test" " a/b/c" ]))
112
+ }
0 commit comments