-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path_test.pony
140 lines (114 loc) · 4.62 KB
/
_test.pony
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
use "ponytest"
actor Main is TestList
new create(env: Env) =>PonyTest(env, this)
fun tag tests(test: PonyTest) =>
test(TestLensAssign)
test(TestLensChoice)
test(TestLensArray)
test(TestObjectMerge)
test(TestJsonEquality)
class TestJsonEquality is UnitTest
fun name(): String => "JsonEquality"
fun ref apply(h: TestHelper) =>
h.assert_true(JEq(I64(5), I64(5)))
h.assert_true(JEq(F64(5.3), F64(5.3)))
h.assert_true(JEq(None, None))
h.assert_true(JEq(NotSet, NotSet))
h.assert_true(JEq("hello", "hello"))
h.assert_true(JEq(true, true))
h.assert_true(JEq(JObj, JObj))
h.assert_true(JEq(JArr, JArr))
h.assert_true(JEq(JObj + ("a", I64(5)), JObj + ("a", I64(5))))
h.assert_true(JEq(JArr + I64(5), JArr + I64(5)))
h.assert_false(JEq(I64(1), I64(2)))
h.assert_false(JEq(F64(1), F64(2)))
h.assert_false(JEq(None, false))
h.assert_false(JEq(NotSet, false))
h.assert_false(JEq("hello", "bye"))
h.assert_false(JEq(JObj, JObj + ("foo", "bar")))
h.assert_false(JEq(JObj + ("foo", "bar"), JObj))
h.assert_false(JEq(JObj, JArr))
h.assert_false(JEq(JArr + None, JArr))
h.assert_false(JEq(JObj + ("foo", "baz"), JObj + ("foo", "bar")))
h.assert_false(JEq(
JObj
+ ("foo", JObj
+ ("bar", false)
)
, JObj
+ ("foo", JObj
+ ("bar", true)
)))
h.assert_false(JEq(
JObj
+ ("foo", JObj
+ ("bar", false)
)
, JObj
+ ("foo", JObj
+ ("bar", false)
+ ("baz", None)
)))
h.assert_true(JEq(
JObj
+ ("foo", JObj
+ ("bar", false)
)
, JObj
+ ("foo", JObj
+ ("bar", false)
)))
let a = "dummy"
let b = JObj + ("value", "dummy")
let abLens = JLens("value") / JLens // try obtaining the "value" field of an object, if that doesnt work return the whole thing
h.assert_true(abLens.equals(a, b), "Expected (" + a.string() + ") `((JLens * 'value') / JLens).equals` (" + b.string() + ")")
class TestLensAssign is UnitTest
fun name(): String => "LensAssign"
fun ref apply(h: TestHelper) =>
let x = JObj
+ ("foo", "bar")
+ ("deep", JObj + ("space", I64(9)))
let foo' = JLens("foo")
let foobazz = (JLens("foo") = "bazz").json(x)
try
h.assert_eq[String](foo'.json(foobazz as J) as String, "bazz")
else h.fail(foobazz.string())
end
class TestLensChoice is UnitTest
fun name(): String => "LensChoice"
fun ref apply(h: TestHelper) =>
let x = JObj + ("value", "bar")
let y = "foo"
let valueLens = JLens("value") / JLens
try
h.assert_eq[String](valueLens.json(x) as String, "bar")
h.assert_eq[String](valueLens.json(y) as String, "foo")
else h.fail("failed")
end
primitive AssertJson
fun eq(h: TestHelper, a: (J | NotSet), b: (J | NotSet)) =>
h.assert_true(JEq(a, b), "Expected (" + a.string() + ") == (" + b.string() + ")")
fun ne(h: TestHelper, a: (J | NotSet), b: (J | NotSet)) =>
h.assert_false(JEq(a, b), "Expected (" + a.string() + ") != (" + b.string() + ")")
class TestLensArray is UnitTest
fun name(): String => "LensArray"
fun ref apply(h: TestHelper) =>
let x = JArr + "markdown" + (JObj + ("language", "en") + ("value", "plain"))
let set1true = JLens(1) = true
AssertJson.eq(h, set1true.json(x), JArr + "markdown" + true)
let obj = JObj + ("one", JObj + ("two", "three"))
let set_three_3 = JLens("one")("two") = I64(3)
let remove_two = JLens("one")("two") = NotSet
let expect = JObj
+ ("one", JObj
+ ("two", I64(3))
)
AssertJson.eq(h, set_three_3.json(obj), expect)
AssertJson.eq(h, remove_two.json(obj), JObj + ("one", JObj))
class TestObjectMerge is UnitTest
fun name(): String => "ObjectMerge"
fun ref apply(h: TestHelper) =>
let a = JObj + ("rootPath", ".")
let b = JObj + ("rootUri", "file:///") + ("rootPath", None)
let expected = JObj + ("rootPath", None) + ("rootUri", "file:///")
AssertJson.eq(h, a * b, expected)