Skip to content

Commit 688d16f

Browse files
committed
Add tests for the arithmetic proposal
1 parent 77c4a72 commit 688d16f

File tree

7 files changed

+675
-18
lines changed

7 files changed

+675
-18
lines changed

compatible.test.js

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ for (const file of files) {
1313
}
1414
}
1515

16+
function correction (x) {
17+
// eslint-disable-next-line no-compare-neg-zero
18+
if (x === -0) return 0
19+
return x
20+
}
21+
1622
// eslint-disable-next-line no-labels
1723
inline: {
1824
const logic = new LogicEngine(undefined, { compatible: true })
@@ -25,13 +31,13 @@ inline: {
2531
test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
2632
testCase[1]
2733
)}`, () => {
28-
expect(logic.run(testCase[0], testCase[1])).toStrictEqual(testCase[2])
34+
expect(correction(logic.run(testCase[0], testCase[1]))).toStrictEqual(testCase[2])
2935
})
3036

3137
test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
3238
testCase[1]
3339
)} (async)`, async () => {
34-
expect(await asyncLogic.run(testCase[0], testCase[1])).toStrictEqual(
40+
expect(correction(await asyncLogic.run(testCase[0], testCase[1]))).toStrictEqual(
3541
testCase[2]
3642
)
3743
})
@@ -40,26 +46,26 @@ inline: {
4046
testCase[1]
4147
)} (built)`, () => {
4248
const f = logic.build(testCase[0])
43-
expect(f(testCase[1])).toStrictEqual(testCase[2])
49+
expect(correction(f(testCase[1]))).toStrictEqual(testCase[2])
4450
})
4551

4652
test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
4753
testCase[1]
4854
)} (asyncBuilt)`, async () => {
4955
const f = await asyncLogic.build(testCase[0])
50-
expect(await f(testCase[1])).toStrictEqual(testCase[2])
56+
expect(correction(await f(testCase[1]))).toStrictEqual(testCase[2])
5157
})
5258

5359
test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
5460
testCase[1]
5561
)} (noOptimization)`, () => {
56-
expect(logicWithoutOptimization.run(testCase[0], testCase[1])).toStrictEqual(testCase[2])
62+
expect(correction(logicWithoutOptimization.run(testCase[0], testCase[1]))).toStrictEqual(testCase[2])
5763
})
5864

5965
test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
6066
testCase[1]
6167
)} (asyncNoOptimization)`, async () => {
62-
expect(await asyncLogicWithoutOptimization.run(testCase[0], testCase[1])).toStrictEqual(
68+
expect(correction(await asyncLogicWithoutOptimization.run(testCase[0], testCase[1]))).toStrictEqual(
6369
testCase[2]
6470
)
6571
})
@@ -68,14 +74,14 @@ inline: {
6874
testCase[1]
6975
)} (builtNoOptimization)`, () => {
7076
const f = logicWithoutOptimization.build(testCase[0])
71-
expect(f(testCase[1])).toStrictEqual(testCase[2])
77+
expect(correction(f(testCase[1]))).toStrictEqual(testCase[2])
7278
})
7379

7480
test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
7581
testCase[1]
7682
)} (asyncBuiltNoOptimization)`, async () => {
7783
const f = await asyncLogicWithoutOptimization.build(testCase[0])
78-
expect(await f(testCase[1])).toStrictEqual(testCase[2])
84+
expect(correction(await f(testCase[1]))).toStrictEqual(testCase[2])
7985
})
8086
})
8187
})
@@ -97,13 +103,13 @@ notInline: {
97103
test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
98104
testCase[1]
99105
)}`, () => {
100-
expect(logic.run(testCase[0], testCase[1])).toStrictEqual(testCase[2])
106+
expect(correction(logic.run(testCase[0], testCase[1]))).toStrictEqual(testCase[2])
101107
})
102108

103109
test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
104110
testCase[1]
105111
)} (async)`, async () => {
106-
expect(await asyncLogic.run(testCase[0], testCase[1])).toStrictEqual(
112+
expect(correction(await asyncLogic.run(testCase[0], testCase[1]))).toStrictEqual(
107113
testCase[2]
108114
)
109115
})
@@ -112,26 +118,26 @@ notInline: {
112118
testCase[1]
113119
)} (built)`, () => {
114120
const f = logic.build(testCase[0])
115-
expect(f(testCase[1])).toStrictEqual(testCase[2])
121+
expect(correction(f(testCase[1]))).toStrictEqual(testCase[2])
116122
})
117123

118124
test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
119125
testCase[1]
120126
)} (asyncBuilt)`, async () => {
121127
const f = await asyncLogic.build(testCase[0])
122-
expect(await f(testCase[1])).toStrictEqual(testCase[2])
128+
expect(correction(await f(testCase[1]))).toStrictEqual(testCase[2])
123129
})
124130

125131
test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
126132
testCase[1]
127133
)} (noOptimization)`, () => {
128-
expect(logicWithoutOptimization.run(testCase[0], testCase[1])).toStrictEqual(testCase[2])
134+
expect(correction(logicWithoutOptimization.run(testCase[0], testCase[1]))).toStrictEqual(testCase[2])
129135
})
130136

131137
test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
132138
testCase[1]
133139
)} (asyncNoOptimization)`, async () => {
134-
expect(await asyncLogicWithoutOptimization.run(testCase[0], testCase[1])).toStrictEqual(
140+
expect(correction(await asyncLogicWithoutOptimization.run(testCase[0], testCase[1]))).toStrictEqual(
135141
testCase[2]
136142
)
137143
})
@@ -140,14 +146,14 @@ notInline: {
140146
testCase[1]
141147
)} (builtNoOptimization)`, () => {
142148
const f = logicWithoutOptimization.build(testCase[0])
143-
expect(f(testCase[1])).toStrictEqual(testCase[2])
149+
expect(correction(f(testCase[1]))).toStrictEqual(testCase[2])
144150
})
145151

146152
test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
147153
testCase[1]
148154
)} (asyncBuiltNoOptimization)`, async () => {
149155
const f = await asyncLogicWithoutOptimization.build(testCase[0])
150-
expect(await f(testCase[1])).toStrictEqual(testCase[2])
156+
expect(correction(await f(testCase[1]))).toStrictEqual(testCase[2])
151157
})
152158
})
153159
}

defaultMethods.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ const defaultMethods = {
5454
'+': (data) => {
5555
if (typeof data === 'string') return +data
5656
if (typeof data === 'number') return +data
57+
if (typeof data === 'boolean') return +data
5758
let res = 0
5859
for (let i = 0; i < data.length; i++) res += +data[i]
5960
return res
@@ -64,20 +65,21 @@ const defaultMethods = {
6465
return res
6566
},
6667
'/': (data) => {
67-
let res = data[0]
68+
let res = +data[0]
6869
for (let i = 1; i < data.length; i++) res /= +data[i]
6970
return res
7071
},
7172
'-': (data) => {
7273
if (typeof data === 'string') return -data
7374
if (typeof data === 'number') return -data
75+
if (typeof data === 'boolean') return -data
7476
if (data.length === 1) return -data[0]
7577
let res = data[0]
7678
for (let i = 1; i < data.length; i++) res -= +data[i]
7779
return res
7880
},
7981
'%': (data) => {
80-
let res = data[0]
82+
let res = +data[0]
8183
for (let i = 1; i < data.length; i++) res %= +data[i]
8284
return res
8385
},

suites/divide.json

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
[
2+
"# Collection of Divide Operator Tests",
3+
{
4+
"description": "Divide",
5+
"rule": { "/": [4, 2] },
6+
"result": 2,
7+
"data": null
8+
},
9+
{
10+
"description": "Divide to Decimal",
11+
"rule": { "/": [2, 4] },
12+
"result": 0.5,
13+
"data": null,
14+
"decimal": true
15+
},
16+
{
17+
"description": "Divide with Multiple Operands",
18+
"rule": { "/": [8, 2, 2] },
19+
"result": 2,
20+
"data": null
21+
},
22+
{
23+
"description": "Divide with Multiple Operands (2)",
24+
"rule": { "/": [2, 2, 1] },
25+
"result": 1,
26+
"data": null
27+
},
28+
{
29+
"description": "Divide with Negative Numbers",
30+
"rule": { "/": [-1, 2] },
31+
"result": -0.5,
32+
"data": null,
33+
"decimal": true
34+
},
35+
{
36+
"description": "Divide with Strings",
37+
"rule": { "/": ["8", "2", "2"] },
38+
"result": 2,
39+
"data": null
40+
},
41+
{
42+
"description": "Divide with Booleans",
43+
"rule": { "/": [false, true] },
44+
"result": 0,
45+
"data": null
46+
},
47+
{
48+
"description": "Divide with Multiple Value Types",
49+
"rule": { "/": ["8", true, 2] },
50+
"result": 4,
51+
"data": null
52+
},
53+
{
54+
"description": "Divide with Multiple Value Types (2)",
55+
"rule": { "/": ["1", 1] },
56+
"result": 1,
57+
"data": null
58+
},
59+
{
60+
"description": "Divide with Single Operand (Number)",
61+
"rule": { "/": [1] },
62+
"result": 1,
63+
"data": null
64+
},
65+
{
66+
"description": "Divide with Single Operand, Direct (Number)",
67+
"rule": { "/": 1 },
68+
"result": 1,
69+
"data": null
70+
},
71+
{
72+
"description": "Divide with Single Operand, Direct (0)",
73+
"rule": { "/": 0 },
74+
"result": 0,
75+
"data": null
76+
},
77+
{
78+
"description": "Divide Operator with Single Operand (Number)",
79+
"rule": { "/": [1] },
80+
"result": 1,
81+
"data": null
82+
},
83+
{
84+
"description": "Divide Operator with Single Operand (Negative Number)",
85+
"rule": { "/": [-1] },
86+
"result": -1,
87+
"data": null
88+
},
89+
{
90+
"description": "Divide Operator with Single Operand, Direct (Number)",
91+
"rule": { "/": 1 },
92+
"result": 1,
93+
"data": null
94+
},
95+
{
96+
"description": "Divide Operator with Single Operand, Direct (0)",
97+
"rule": { "/": 0 },
98+
"result": 0,
99+
"data": null
100+
},
101+
{
102+
"description": "Divide Operator with Single Operand (String)",
103+
"rule": { "/": ["1"] },
104+
"result": 1,
105+
"data": null
106+
},
107+
{
108+
"description": "Divide Operator with Single Operand, Direct (Negative Number String)",
109+
"rule": { "/": "-1" },
110+
"result": -1,
111+
"data": null
112+
},
113+
114+
{
115+
"description": "Divide Operator with Single Operand, Direct (String 0)",
116+
"rule": { "/": "0" },
117+
"result": 0,
118+
"data": null
119+
},
120+
{
121+
"description": "Divide Operator with Single Operand, Direct (true)",
122+
"rule": { "/": true },
123+
"result": 1,
124+
"data": null
125+
},
126+
{
127+
"description": "Divide Operator with Single Operand, Direct (false)",
128+
"rule": { "/": false },
129+
"result": 0,
130+
"data": null
131+
},
132+
{
133+
"description": "Divide with val",
134+
"rule": { "/": [{ "val": "x" }, { "val": "y" }] },
135+
"data": { "x": 8, "y": 2 },
136+
"result": 4
137+
}
138+
]

0 commit comments

Comments
 (0)