summaryrefslogtreecommitdiff
path: root/core/src/test/scala/mill/define/GraphTests.scala
blob: a83f7a7b61c5434dc01df38521c8e28a82012e96 (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
package mill.define

import mill.discover.Discovered
import mill.eval.Evaluator
import mill.util.{OSet, TestGraphs, TestUtil}
import utest._

object GraphTests extends TestSuite{

  val tests = Tests{


    val graphs = new TestGraphs()
    import graphs._

    'topoSortedTransitiveTargets - {
      def check(targets: OSet[Task[_]], expected: OSet[Task[_]]) = {
        val result = Evaluator.topoSorted(Evaluator.transitiveTargets(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
        )
      )
      'defCachedDiamond - check(
        targets = OSet(defCachedDiamond.down),
        expected = OSet(
          defCachedDiamond.up.inputs(0),
          defCachedDiamond.up,
          defCachedDiamond.down.inputs(0).inputs(0).inputs(0),
          defCachedDiamond.down.inputs(0).inputs(0),
          defCachedDiamond.down.inputs(0).inputs(1).inputs(0),
          defCachedDiamond.down.inputs(0).inputs(1),
          defCachedDiamond.down.inputs(0),
          defCachedDiamond.down
        )
      )
      'bigSingleTerminal - {
        val result = Evaluator.topoSorted(Evaluator.transitiveTargets(OSet(bigSingleTerminal.j))).values
        TestUtil.checkTopological(result)
        assert(result.size == 28)
      }
    }

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

        val mapping = Discovered.mapping(base)
        val topoSorted = Evaluator.topoSorted(Evaluator.transitiveTargets(OSet(target)))

        val grouped = Evaluator.groupAroundImportantTargets(topoSorted) {
          case t: Target[_] if mapping.contains(t) => t
        }
        val flattened = OSet.from(grouped.values().flatMap(_.items))

        TestUtil.checkTopological(flattened)
        for((terminal, expectedSize) <- expected){
          val grouping = grouped.lookupKey(terminal)
          assert(
            grouping.size == expectedSize,
            grouping.flatMap(_.asTarget: Option[Target[_]]).filter(mapping.contains) == OSet(terminal)
          )
        }
      }
      'singleton - check(
        singleton,
        singleton.single,
        OSet(singleton.single -> 1)
      )
      'pair - check(
        pair,
        pair.down,
        OSet(pair.up -> 1, pair.down -> 1)
      )
      'anonTriple - check(
        anonTriple,
        anonTriple.down,
        OSet(anonTriple.up -> 1, anonTriple.down -> 2)
      )
      'diamond - check(
        diamond,
        diamond.down,
        OSet(
          diamond.up -> 1,
          diamond.left -> 1,
          diamond.right -> 1,
          diamond.down -> 1
        )
      )

      'defCachedDiamond - check(
        defCachedDiamond,
        defCachedDiamond.down,
        OSet(
          defCachedDiamond.up -> 2,
          defCachedDiamond.left -> 2,
          defCachedDiamond.right -> 2,
          defCachedDiamond.down -> 2
        )
      )

      'anonDiamond - check(
        anonDiamond,
        anonDiamond.down,
        OSet(
          anonDiamond.up -> 1,
          anonDiamond.down -> 3
        )
      )
      'bigSingleTerminal - check(
        bigSingleTerminal,
        bigSingleTerminal.j,
        OSet(
          bigSingleTerminal.a -> 3,
          bigSingleTerminal.b -> 2,
          bigSingleTerminal.e -> 9,
          bigSingleTerminal.i -> 6,
          bigSingleTerminal.f -> 4,
          bigSingleTerminal.j -> 4
        )
      )
    }


  }
}