summaryrefslogtreecommitdiffstats
path: root/src/test/scala/basic.scala
blob: 56655e47f002696faa01c00b5c3e920ba6f883fc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import org.scalatest.FunSuite
import router.test._

import router.Router

class Basic extends FunSuite {
  val yearRx  = """\d{4}""".r
  val monthRx = """\d|10|11|12""".r
  val dayRx   = """\d|[12]\d|30|31""".r

  val router = new Router[Boolean]

  router addRoute (
    "blog",
    true,
    defaults = Map(
      "controller" -> "blog",
      "action"     -> "index"
    )
  )

  router addRoute (
    "blog/:year/:month/:day",
    true,
    defaults = Map(
      "controller" -> "blog",
      "action"     -> "show_date"
    ),
    validations = Map(
      "year"  -> yearRx,
      "month" -> monthRx,
      "day"   -> dayRx
    )
  )

  router addRoute (
    "blog/:action/:id",
    true,
    defaults = Map(
      "controller" -> "blog"
    ),
    validations = Map(
      "action" -> """\D+""".r,
      "id"     -> """\d+""".r
    )
  )

  router addRoute (
    "test/?:x/?:y",
    true,
    defaults = Map(
      "controller" -> "test",
      "x"          -> "x",
      "y"          -> "y"
    )
  )

  test ("routes match properly") {
    assert(
      router matches "blog", Map(
        "controller" -> "blog",
        "action"     -> "index"
      )
    )

    assert(
      router matches "blog/2006/12/5", Map(
        "controller" -> "blog",
        "action"     -> "show_date",
        "year"       -> "2006",
        "month"      -> "12",
        "day"        -> "5"
      )
    )

    assert(
      router matches "blog/1920/12/10", Map(
        "controller" -> "blog",
        "action"     -> "show_date",
        "year"       -> "1920",
        "month"      -> "12",
        "day"        -> "10"
      )
    )

    assert(
      router matches "blog/edit/5", Map(
        "controller" -> "blog",
        "action"     -> "edit",
        "id"         -> "5"
      )
    )

    assert(
      router matches "blog/show/123", Map(
        "controller" -> "blog",
        "action"     -> "show",
        "id"         -> "123"
      )
    )

    assert(
      router matches "blog/some_crazy_long_winded_action_name/12356789101112131151", Map(
        "controller" -> "blog",
        "action"     -> "some_crazy_long_winded_action_name",
        "id"         -> "12356789101112131151"
      )
    )

    assert(
      router matches "blog/delete/5", Map(
        "controller" -> "blog",
        "action"     -> "delete",
        "id"         -> "5"
      )
    )

    assert(
      router matches "test/x1", Map(
        "controller" -> "test",
        "x"          -> "x1",
        "y"          -> "y"
      )
    )
  }
}