summaryrefslogtreecommitdiff
path: root/cask/test/src/test/cask/DispatchTrieTests.scala
blob: 068d7d040bab87d94c28f7338c5d537832f6691e (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
package test.cask
import cask.internal.DispatchTrie
import utest._

object DispatchTrieTests extends TestSuite {
  val tests = Tests{

    'hello - {
      val x = DispatchTrie.construct(0,
        Seq((Vector("hello"), 1, false))
      )

      assert(
        x.lookup(List("hello"), Map()) == Some((1, Map(), Nil)),
        x.lookup(List("hello", "world"), Map()) == None,
        x.lookup(List("world"), Map()) == None
      )
    }
    'nested - {
      val x = DispatchTrie.construct(0,
        Seq(
          (Vector("hello", "world"), 1, false),
          (Vector("hello", "cow"), 2, false)
        )
      )
      assert(
        x.lookup(List("hello", "world"), Map()) == Some((1, Map(), Nil)),
        x.lookup(List("hello", "cow"), Map()) == Some((2, Map(), Nil)),
        x.lookup(List("hello"), Map()) == None,
        x.lookup(List("hello", "moo"), Map()) == None,
        x.lookup(List("hello", "world", "moo"), Map()) == None
      )
    }
    'bindings - {
      val x = DispatchTrie.construct(0,
        Seq((Vector(":hello", ":world"), 1, false))
      )
      assert(
        x.lookup(List("hello", "world"), Map()) == Some((1, Map("hello" -> "hello", "world" -> "world"), Nil)),
        x.lookup(List("world", "hello"), Map()) == Some((1, Map("hello" -> "world", "world" -> "hello"), Nil)),

        x.lookup(List("hello", "world", "cow"), Map()) == None,
        x.lookup(List("hello"), Map()) == None
      )
    }

    'path - {
      val x = DispatchTrie.construct(0,
        Seq((Vector("hello"), 1, true))
      )

      assert(
        x.lookup(List("hello", "world"), Map()) ==  Some((1,Map(), Seq("world"))),
        x.lookup(List("hello", "world", "cow"), Map()) ==  Some((1,Map(), Seq("world", "cow"))),
        x.lookup(List("hello"), Map()) == Some((1,Map(), Seq())),
        x.lookup(List(), Map()) == None
      )
    }

    'errors - {
      intercept[Exception]{
        DispatchTrie.construct(0,
          Seq(
            (Vector("hello", ":world"), 1, false),
            (Vector("hello", "world"),  2, false)
          )
        )
      }
      intercept[Exception]{
        DispatchTrie.construct(0,
          Seq(
            (Vector("hello", ":world"), 1, false),
            (Vector("hello", "world", "omg"), 2, false)
          )
        )
      }
      intercept[Exception]{
        DispatchTrie.construct(0,
          Seq(
            (Vector("hello"), 1, true),
            (Vector("hello", "cow", "omg"), 2, false)
          )
        )
      }
      intercept[Exception]{
        DispatchTrie.construct(0,
          Seq(
            (Vector("hello", ":world"), 1, false),
            (Vector("hello", ":cow"), 2, false)
          )
        )
      }
      intercept[Exception]{
        DispatchTrie.construct(0,
          Seq(
            (Vector("hello", "world"), 1, false),
            (Vector("hello", "world"), 2, false)
          )
        )
      }
    }
  }
}