summaryrefslogtreecommitdiffstats
path: root/src/test/scala/org/perl8/router/BasicTest.scala
blob: b00d1aecef3b067e1b10592dd662216ba466ed66 (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
127
package org.perl8.router

import org.scalatest.FunSuite

import org.perl8.router.test._

class BasicTest 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"
      )
    )
  }
}