summaryrefslogtreecommitdiff
path: root/src/test/scala/forge/GraphTests.scala
blob: 86955fdaacc572c208d673b750e4aeda6e1d3c86 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package forge

import utest._
import Target.test

object GraphTests extends TestSuite{

  val tests = Tests{


    val graphs = new TestGraphs()
    import graphs._

    'discovery{
      class CanNest{
        val single = test()
        val invisible: Any = test()
      }
      object outer {
        val single = test()
        val invisible: Any = test()
        object nested{
          val single = test()
          val invisible: Any = test()

        }
        val classInstance = new CanNest

      }
      val discovered = Discovered[outer.type].apply(outer)
      val expected = Seq(
        (List("classInstance", "single"), outer.classInstance.single),
        (List("nested", "single"), outer.nested.single),
        (List("single"), outer.single)
      )
      assert(discovered == expected)
    }


    'topoSortedTransitiveTargets - {
      def check(targets: OSet[Target[_]], expected: OSet[Target[_]]) = {
        val result = Evaluator.topoSortedTransitiveTargets(targets).values
        TestUtil.checkTopological(result)
        assert(result == expected)
      }

      'singleton - check(
        targets = OSet(singleton.single),
        expected = OSet(singleton.single)
      )
      'pair - check(
        targets = OSet(pair.down),
        expected = OSet(pair.up, pair.down)
      )
      'anonTriple - check(
        targets = OSet(anonTriple.down),
        expected = OSet(anonTriple.up, anonTriple.down.inputs(0), anonTriple.down)
      )
      'diamond - check(
        targets = OSet(diamond.down),
        expected = OSet(diamond.up, diamond.left, diamond.right, diamond.down)
      )
      'anonDiamond - check(
        targets = OSet(diamond.down),
        expected = OSet(
          diamond.up,
          diamond.down.inputs(0),
          diamond.down.inputs(1),
          diamond.down
        )
      )
      'bigSingleTerminal - {
        val result = Evaluator.topoSortedTransitiveTargets(OSet(bigSingleTerminal.j)).values
        TestUtil.checkTopological(result)
        assert(result.size == 28)
      }
    }

    'groupAroundNamedTargets - {
      def check[T: Discovered](base: T,
                               target: Target.Test,
                               expected: OSet[(OSet[Target.Test], Int)]) = {

        val mapping: Map[Target[_], Seq[String]] = {
          implicitly[Discovered[T]].apply(base).map(_.swap).toMap
        }
        val grouped = Evaluator.groupAroundNamedTargets(
          Evaluator.topoSortedTransitiveTargets(OSet(target)),
          mapping
        )
        TestUtil.checkTopological(grouped.flatMap(_.items))
        for(((expectedPresent, expectedSize), i) <- expected.items.zipWithIndex){
          val grouping = grouped.items(i)
          assert(
            grouping.size == expectedSize,
            grouping.filter(mapping.contains) == expectedPresent
          )
        }
      }
      'singleton - check(
        singleton,
        singleton.single,
        OSet(OSet(singleton.single) -> 1)
      )
      'pair - check(
        pair,
        pair.down,
        OSet(OSet(pair.up) -> 1, OSet(pair.down) -> 1)
      )
      'anonTriple - check(
        anonTriple,
        anonTriple.down,
        OSet(OSet(anonTriple.up) -> 1, OSet(anonTriple.down) -> 2)
      )
      'diamond - check(
        diamond,
        diamond.down,
        OSet(
          OSet(diamond.up) -> 1,
          OSet(diamond.left) -> 1,
          OSet(diamond.right) -> 1,
          OSet(diamond.down) -> 1
        )
      )
      'anonDiamond - check(
        anonDiamond,
        anonDiamond.down,
        OSet(
          OSet(anonDiamond.up) -> 1,
          OSet(anonDiamond.down) -> 3
        )
      )
      'bigSingleTerminal - check(
        bigSingleTerminal,
        bigSingleTerminal.j,
        OSet(
          OSet(bigSingleTerminal.a) -> 3,
          OSet(bigSingleTerminal.b) -> 2,
          OSet(bigSingleTerminal.e) -> 9,
          OSet(bigSingleTerminal.i) -> 6,
          OSet(bigSingleTerminal.f) -> 4,
          OSet(bigSingleTerminal.j) -> 4
        )
      )
    }

    'labeling - {

      def check[T: Discovered](base: T, t: Target[_], relPath: Option[String]) = {


        val names: Seq[(Target[_], Seq[String])] =
          implicitly[Discovered[T]].apply(base).map(_.swap)
        val nameMap = names.toMap

        val targetLabel = nameMap.get(t).map(_.mkString("."))
        assert(targetLabel == relPath)
      }
      'singleton - check(singleton, singleton.single, Some("single"))
      'pair - {
        check(pair, pair.up, Some("up"))
        check(pair, pair.down, Some("down"))
      }

      'anonTriple - {
        check(anonTriple, anonTriple.up, Some("up"))
        check(anonTriple, anonTriple.down.inputs(0), None)
        check(anonTriple, anonTriple.down, Some("down"))
      }

      'diamond - {
        check(diamond, diamond.up, Some("up"))
        check(diamond, diamond.left, Some("left"))
        check(diamond, diamond.right, Some("right"))
        check(diamond, diamond.down, Some("down"))
      }

      'anonDiamond - {
        check(anonDiamond, anonDiamond.up, Some("up"))
        check(anonDiamond, anonDiamond.down.inputs(0), None)
        check(anonDiamond, anonDiamond.down.inputs(1), None)
        check(anonDiamond, anonDiamond.down, Some("down"))
      }

    }

  }
}