Skip to content

Commit 8105897

Browse files
committed
Add tests
1 parent 8989640 commit 8105897

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package mill.api.opt
2+
3+
import utest.*
4+
import mill.api.opt.*
5+
6+
class OptsTests extends TestSuite {
7+
8+
val homeDir = os.home
9+
val workDir = homeDir / "work"
10+
val outDir = workDir / "out"
11+
12+
val srcDir1 = workDir / "src1"
13+
val srcDir2 = workDir / "src2"
14+
val sources1 = Seq(srcDir1, srcDir2)
15+
16+
val srcDir3 = workDir / "src3"
17+
val srcDir4 = workDir / "src4"
18+
val sources2 = Seq(srcDir3, srcDir4)
19+
20+
val plugin1 = homeDir / ".cache" / "plugin1"
21+
22+
val opts1 = Opts(
23+
// single arg
24+
Opt("-deprecation"),
25+
// implicit single args
26+
"-verbose",
27+
// two args as group
28+
OptGroup("--release", "17"),
29+
// an option including a file via ArgParts
30+
Opt("-Xplugin=", plugin1),
31+
// an option including a file via arg string interpolator
32+
opt"-Xplugin:${plugin1}",
33+
// some files
34+
sources1,
35+
// some files as ArgGroup
36+
OptGroup(sources2*),
37+
// Mixed ArgGroup
38+
OptGroup(opt"--extra", opt"-Xplugin=${plugin1}") ++ OptGroup(sources1*)
39+
)
40+
41+
val expectedOpts1 = Opts(
42+
Opt("-deprecation"),
43+
Opt("-verbose"),
44+
OptGroup(
45+
Opt("--release"),
46+
Opt("17")
47+
),
48+
Opt("-Xplugin=", plugin1),
49+
Opt("-Xplugin:", plugin1),
50+
Opt(srcDir1),
51+
Opt(srcDir2),
52+
Opt(srcDir3),
53+
Opt(srcDir4),
54+
OptGroup(
55+
Opt("--extra"),
56+
Opt("-Xplugin=", plugin1),
57+
Opt(srcDir1),
58+
Opt(srcDir2)
59+
)
60+
)
61+
62+
val expectedSeq1 = Seq(
63+
"-deprecation",
64+
"-verbose",
65+
"--release",
66+
"17",
67+
s"-Xplugin=${plugin1.toString()}",
68+
// an option including a file via arg string interpolator
69+
s"-Xplugin:${plugin1.toString()}"
70+
) ++
71+
sources1.map(_.toString())
72+
++
73+
sources2.map(_.toString())
74+
++
75+
Seq(
76+
"--extra",
77+
s"-Xplugin=${plugin1.toString()}"
78+
) ++
79+
sources1.map(_.toString())
80+
81+
override def tests: Tests = Tests {
82+
test("structure") {
83+
assert(opts1 == expectedOpts1)
84+
}
85+
test("toStringSeq") {
86+
val str = opts1.toStringSeq
87+
assert(str == expectedSeq1)
88+
}
89+
test("jsonify") {
90+
test("without-mapping") {
91+
val json = upickle.write(opts1)
92+
assertGoldenLiteral(
93+
json,
94+
"{\"value\":[{\"value\":[[[\"-deprecation\",null]],[[\"-verbose\",null]],[[\"--release\",null]],[[\"17\",null]],[[\"-Xplugin=\",null],[null,\"/home/lefou/.cache/plugin1\"]],[[\"-Xplugin:\",null],[null,\"/home/lefou/.cache/plugin1\"]],[[null,\"/home/lefou/work/src1\"]],[[null,\"/home/lefou/work/src2\"]],[[null,\"/home/lefou/work/src3\"]],[[null,\"/home/lefou/work/src4\"]],[[\"--extra\",null]],[[\"-Xplugin=\",null],[null,\"/home/lefou/.cache/plugin1\"]],[[null,\"/home/lefou/work/src1\"]],[[null,\"/home/lefou/work/src2\"]]]}]}"
95+
)
96+
assert(json.split("\\Q$HOME\\E").size == 1)
97+
val back = upickle.read[Opts](json)
98+
assert(opts1 == back)
99+
}
100+
// test("with-mapping-home") {
101+
// val json = upickle.write(opts1)
102+
// assertGoldenLiteral(
103+
// json,
104+
// "{\"value\":[{\"value\":[[[\"-deprecation\",null]],[[\"-verbose\",null]],[[\"--release\",null]],[[\"17\",null]],[[\"-Xplugin=\",null],[null,\"/home/lefou/.cache/plugin1\"]],[[\"-Xplugin:\",null],[null,\"/home/lefou/.cache/plugin1\"]],[[null,\"/home/lefou/work/src1\"]],[[null,\"/home/lefou/work/src2\"]],[[null,\"/home/lefou/work/src3\"]],[[null,\"/home/lefou/work/src4\"]],[[\"--extra\",null]],[[\"-Xplugin=\",null],[null,\"/home/lefou/.cache/plugin1\"]],[[null,\"/home/lefou/work/src1\"]],[[null,\"/home/lefou/work/src2\"]]]}]}"
105+
// )
106+
// assert(json.split("\\Q$HOME\\E").size == 10)
107+
// val back = upickle.read[Opts](json)
108+
// assert(opts1 == back)
109+
// }
110+
}
111+
}
112+
}

0 commit comments

Comments
 (0)