1
1
import fs from 'fs'
2
2
import { LogicEngine , AsyncLogicEngine } from './index.js'
3
+ import { configurePrecision } from './precision/index.js'
4
+ import Decimal from 'decimal.js'
5
+
3
6
const tests = [ ]
4
7
5
8
// get all json files from "suites" directory
@@ -13,141 +16,55 @@ for (const file of files) {
13
16
}
14
17
}
15
18
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 = [ ]
52
20
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
+ }
66
46
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 (
68
51
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 ] )
72
57
} )
73
58
74
- test ( `${ JSON . stringify ( testCase [ 0 ] ) } ${ JSON . stringify (
59
+ test ( `${ engine [ 1 ] } ${ JSON . stringify ( testCase [ 0 ] ) } ${ JSON . stringify (
75
60
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 ] )
79
67
} )
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
+ } )
0 commit comments