Skip to content

Commit 7a74c0d

Browse files
committed
Add decimal precision to tests
1 parent eb05083 commit 7a74c0d

File tree

4 files changed

+54
-178
lines changed

4 files changed

+54
-178
lines changed

compatible.test.js

+48-131
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import fs from 'fs'
22
import { LogicEngine, AsyncLogicEngine } from './index.js'
3+
import { configurePrecision } from './precision/index.js'
4+
import Decimal from 'decimal.js'
5+
36
const tests = []
47

58
// get all json files from "suites" directory
@@ -13,141 +16,55 @@ for (const file of files) {
1316
}
1417
}
1518

16-
// eslint-disable-next-line no-labels
17-
inline: {
18-
const logic = new LogicEngine(undefined, { compatible: true })
19-
const asyncLogic = new AsyncLogicEngine(undefined, { compatible: true })
20-
const logicWithoutOptimization = new LogicEngine(undefined, { compatible: true })
21-
const asyncLogicWithoutOptimization = new AsyncLogicEngine(undefined, { compatible: true })
22-
23-
describe('All of the compatible tests', () => {
24-
tests.forEach((testCase) => {
25-
test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
26-
testCase[1]
27-
)}`, () => {
28-
expect(logic.run(testCase[0], testCase[1])).toStrictEqual(testCase[2])
29-
})
30-
31-
test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
32-
testCase[1]
33-
)} (async)`, async () => {
34-
expect(await asyncLogic.run(testCase[0], testCase[1])).toStrictEqual(
35-
testCase[2]
36-
)
37-
})
38-
39-
test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
40-
testCase[1]
41-
)} (built)`, () => {
42-
const f = logic.build(testCase[0])
43-
expect(f(testCase[1])).toStrictEqual(testCase[2])
44-
})
45-
46-
test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
47-
testCase[1]
48-
)} (asyncBuilt)`, async () => {
49-
const f = await asyncLogic.build(testCase[0])
50-
expect(await f(testCase[1])).toStrictEqual(testCase[2])
51-
})
19+
const engines = []
5220

53-
test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
54-
testCase[1]
55-
)} (noOptimization)`, () => {
56-
expect(logicWithoutOptimization.run(testCase[0], testCase[1])).toStrictEqual(testCase[2])
57-
})
58-
59-
test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
60-
testCase[1]
61-
)} (asyncNoOptimization)`, async () => {
62-
expect(await asyncLogicWithoutOptimization.run(testCase[0], testCase[1])).toStrictEqual(
63-
testCase[2]
64-
)
65-
})
21+
for (let i = 0; i < 16; i++) {
22+
let res = 'sync'
23+
let engine = new LogicEngine(undefined, { compatible: true })
24+
// sync / async
25+
if (i & 1) {
26+
engine = new AsyncLogicEngine(undefined, { compatible: true })
27+
res = 'async'
28+
}
29+
// inline / disabled
30+
if (i & 2) {
31+
engine.disableInline = true
32+
res += ' no-inline'
33+
}
34+
// optimized / not optimized
35+
if (i & 4) {
36+
engine.disableInterpretedOptimization = true
37+
res += ' no-optimized'
38+
}
39+
// ieee754 / decimal
40+
if (i & 8) {
41+
configurePrecision(engine, Decimal)
42+
res += ' decimal'
43+
}
44+
engines.push([engine, res])
45+
}
6646

67-
test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
47+
describe('All of the compatible tests', () => {
48+
for (const testCase of tests) {
49+
for (const engine of engines) {
50+
test(`${engine[1]} ${JSON.stringify(testCase[0])} ${JSON.stringify(
6851
testCase[1]
69-
)} (builtNoOptimization)`, () => {
70-
const f = logicWithoutOptimization.build(testCase[0])
71-
expect(f(testCase[1])).toStrictEqual(testCase[2])
52+
)}`, async () => {
53+
let result = await engine[0].run(testCase[0], testCase[1])
54+
if ((result || 0).toNumber) result = Number(result)
55+
if (Array.isArray(result)) result = result.map(i => (i || 0).toNumber ? Number(i) : i)
56+
expect(result).toStrictEqual(testCase[2])
7257
})
7358

74-
test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
59+
test(`${engine[1]} ${JSON.stringify(testCase[0])} ${JSON.stringify(
7560
testCase[1]
76-
)} (asyncBuiltNoOptimization)`, async () => {
77-
const f = await asyncLogicWithoutOptimization.build(testCase[0])
78-
expect(await f(testCase[1])).toStrictEqual(testCase[2])
61+
)} (built)`, async () => {
62+
const f = await engine[0].build(testCase[0])
63+
let result = await f(testCase[1])
64+
if ((result || 0).toNumber) result = Number(result)
65+
if (Array.isArray(result)) result = result.map(i => i.toNumber ? Number(i) : i)
66+
expect(result).toStrictEqual(testCase[2])
7967
})
80-
})
81-
})
82-
}
83-
// eslint-disable-next-line no-labels
84-
notInline: {
85-
const logic = new LogicEngine(undefined, { compatible: true })
86-
const asyncLogic = new AsyncLogicEngine(undefined, { compatible: true })
87-
const logicWithoutOptimization = new LogicEngine(undefined, { compatible: true })
88-
const asyncLogicWithoutOptimization = new AsyncLogicEngine(undefined, { compatible: true })
89-
90-
logicWithoutOptimization.disableInline = true
91-
logic.disableInline = true
92-
asyncLogic.disableInline = true
93-
asyncLogicWithoutOptimization.disableInline = true
94-
95-
// using a loop to disable the inline compilation mechanism.
96-
tests.forEach((testCase) => {
97-
test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
98-
testCase[1]
99-
)}`, () => {
100-
expect(logic.run(testCase[0], testCase[1])).toStrictEqual(testCase[2])
101-
})
102-
103-
test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
104-
testCase[1]
105-
)} (async)`, async () => {
106-
expect(await asyncLogic.run(testCase[0], testCase[1])).toStrictEqual(
107-
testCase[2]
108-
)
109-
})
110-
111-
test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
112-
testCase[1]
113-
)} (built)`, () => {
114-
const f = logic.build(testCase[0])
115-
expect(f(testCase[1])).toStrictEqual(testCase[2])
116-
})
117-
118-
test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
119-
testCase[1]
120-
)} (asyncBuilt)`, async () => {
121-
const f = await asyncLogic.build(testCase[0])
122-
expect(await f(testCase[1])).toStrictEqual(testCase[2])
123-
})
124-
125-
test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
126-
testCase[1]
127-
)} (noOptimization)`, () => {
128-
expect(logicWithoutOptimization.run(testCase[0], testCase[1])).toStrictEqual(testCase[2])
129-
})
130-
131-
test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
132-
testCase[1]
133-
)} (asyncNoOptimization)`, async () => {
134-
expect(await asyncLogicWithoutOptimization.run(testCase[0], testCase[1])).toStrictEqual(
135-
testCase[2]
136-
)
137-
})
138-
139-
test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
140-
testCase[1]
141-
)} (builtNoOptimization)`, () => {
142-
const f = logicWithoutOptimization.build(testCase[0])
143-
expect(f(testCase[1])).toStrictEqual(testCase[2])
144-
})
145-
146-
test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
147-
testCase[1]
148-
)} (asyncBuiltNoOptimization)`, async () => {
149-
const f = await asyncLogicWithoutOptimization.build(testCase[0])
150-
expect(await f(testCase[1])).toStrictEqual(testCase[2])
151-
})
152-
})
153-
}
68+
}
69+
}
70+
})

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"build:default": "rm -rf dist && rm -f *.d.ts && rm -f utilities/*.d.ts && rollup index.js --file dist/cjs/index.js --format cjs --exports named && rollup index.js --file dist/esm/index.js --format esm && echo '{ \"type\": \"module\" }' > dist/esm/package.json && echo '{ \"type\": \"commonjs\" }' > dist/cjs/package.json && cd dist && standard --fix */*.js && tsc ../index.js --declaration --allowJs --emitDeclarationOnly --target ESNext --moduleResolution node"
1919
},
2020
"devDependencies": {
21+
"decimal.js": "^10.4.3",
2122
"coveralls": "^3.1.1",
2223
"cross-env": "^7.0.3",
2324
"eslint": "^7.32.0",

precision/test.js

-47
This file was deleted.

yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -1209,6 +1209,11 @@ decimal.js@^10.2.1:
12091209
resolved "https://registry.yarnpkg.com/decimal.js/-/decimal.js-10.3.1.tgz#d8c3a444a9c6774ba60ca6ad7261c3a94fd5e783"
12101210
integrity sha512-V0pfhfr8suzyPGOx3nmq4aHqabehUZn6Ch9kyFpV79TGDTWFmHqUqXdabR7QHqxzrYolF4+tVmJhUG4OURg5dQ==
12111211

1212+
decimal.js@^10.4.3:
1213+
version "10.4.3"
1214+
resolved "https://registry.yarnpkg.com/decimal.js/-/decimal.js-10.4.3.tgz#1044092884d245d1b7f65725fa4ad4c6f781cc23"
1215+
integrity sha512-VBBaLc1MgL5XpzgIP7ny5Z6Nx3UrRkIViUkPUdtl9aya5amy3De1gsUUSB1g3+3sExYNjCAsAznmukyxCb1GRA==
1216+
12121217
dedent@^0.7.0:
12131218
version "0.7.0"
12141219
resolved "https://registry.yarnpkg.com/dedent/-/dedent-0.7.0.tgz#2495ddbaf6eb874abb0e1be9df22d2e5a544326c"

0 commit comments

Comments
 (0)