|
1 | 1 | module Test.Main where |
2 | 2 |
|
3 | | -import Prelude (class Show, Unit, discard, show, ($), (<$>), (*>), (<*>), (<>)) |
| 3 | +import Prelude (class Show, Unit, discard, show, ($), (<$>), (*>), (<*), (<*>), (<>), append) |
4 | 4 | import Control.Monad.Eff (Eff) |
5 | | -import Control.Monad.Eff.Console (CONSOLE(), logShow) |
| 5 | +import Control.Monad.Eff.Console (CONSOLE(), log) |
6 | 6 | import Control.Alt ((<|>)) |
7 | 7 | import Data.List (List) |
8 | 8 | import Data.Map as M |
9 | 9 |
|
10 | 10 |
|
11 | 11 | import Routing (match) |
12 | 12 | import Routing.Match (Match, list) |
13 | | -import Routing.Match.Class (num, int, param, bool, lit, params) |
| 13 | +import Routing.Match.Class (bool, end, int, lit, num, param, params) |
14 | 14 |
|
15 | 15 | data FooBar |
16 | 16 | = Foo Number (M.Map String String) |
17 | 17 | | Bar Boolean String |
18 | 18 | | Baz (List Number) |
19 | 19 | | Quux Int |
| 20 | + | End Int |
20 | 21 |
|
21 | 22 | instance showFooBar :: Show FooBar where |
22 | 23 | show (Foo num q) = "(Foo " <> show num <> " " <> show q <> ")" |
23 | 24 | show (Bar bool str) = "(Bar " <> show bool <> " " <> show str <> ")" |
24 | 25 | show (Baz lst) = "(Baz " <> show lst <> ")" |
25 | 26 | show (Quux i) = "(Quux " <> show i <> ")" |
| 27 | + show (End i) = "(End " <> show i <> ")" |
26 | 28 |
|
27 | 29 | routing :: Match FooBar |
28 | 30 | routing = |
29 | 31 | Foo <$> (lit "foo" *> num) <*> params |
30 | 32 | <|> Bar <$> (lit "bar" *> bool) <*> (param "baz") |
31 | 33 | <|> Quux <$> (lit "" *> lit "quux" *> int) |
| 34 | + -- Order matters here. `list` is greedy, and `end` wont match after it |
| 35 | + <|> End <$> (lit "" *> int <* end) |
32 | 36 | <|> Baz <$> (list num) |
33 | 37 |
|
34 | 38 |
|
35 | 39 | main :: Eff (console :: CONSOLE) Unit |
36 | 40 | main = do |
37 | | - logShow $ match routing "foo/12/?welp='hi'&b=false" |
38 | | - logShow $ match routing "/quux/42" |
| 41 | + print "Foo: " $ match routing "foo/12/?welp='hi'&b=false" -- foo |
| 42 | + print "Quux: " $ match routing "/quux/42" -- quux |
| 43 | + print "Baz: " $ match routing "/123/" -- baz |
| 44 | + print "End: " $ match routing "/1" -- end |
| 45 | + |
| 46 | + where print s e = log $ append s $ show e |
39 | 47 |
|
40 | 48 | -- (minimal test for browser) |
41 | 49 |
|
|
0 commit comments