Skip to content

Commit 288a0d5

Browse files
committed
Change format of test and errors
1 parent 433387b commit 288a0d5

9 files changed

+50
-45
lines changed

compatible.test.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable no-ex-assign */
12
import fs from 'fs'
23
import { LogicEngine, AsyncLogicEngine } from './index.js'
34

@@ -57,7 +58,9 @@ describe('All of the compatible tests', () => {
5758
expect(correction(result)).toStrictEqual(testCase.result)
5859
} catch (err) {
5960
if (err.message && err.message.includes('expect')) throw err
60-
expect(testCase.error).toStrictEqual(true)
61+
if (Number.isNaN(err)) err = { type: 'NaN' }
62+
else if (err.message) err = { type: err.message }
63+
expect(err).toMatchObject(testCase.error)
6164
}
6265
})
6366

@@ -72,7 +75,9 @@ describe('All of the compatible tests', () => {
7275
expect(correction(result)).toStrictEqual(testCase.result)
7376
} catch (err) {
7477
if (err.message && err.message.includes('expect')) throw err
75-
expect(testCase.error).toStrictEqual(true)
78+
if (Number.isNaN(err)) err = { type: 'NaN' }
79+
else if (err.message) err = { type: err.message }
80+
expect(err).toMatchObject(testCase.error)
7681
}
7782
})
7883
}

defaultMethods.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ const defaultMethods = {
117117
throw: (type) => {
118118
if (Array.isArray(type)) type = type[0]
119119
if (typeof type === 'object') throw type
120-
throw { error: type }
120+
throw { type }
121121
},
122122
max: (data) => Math.max(...data),
123123
min: (data) => Math.min(...data),
@@ -345,7 +345,7 @@ const defaultMethods = {
345345
for (let i = 0; i < arr.length; i++) {
346346
try {
347347
// Todo: make this message thing more robust.
348-
if (lastError) item = engine.run(arr[i], { error: lastError.error || lastError.message || lastError.constructor.name }, { above: [null, _1, _2] })
348+
if (lastError) item = engine.run(arr[i], { type: lastError.type || lastError.error || lastError.message || lastError.constructor.name }, { above: [null, _1, _2] })
349349
else item = executeInLoop ? engine.run(arr[i], _1, { above: _2 }) : arr[i]
350350
return item
351351
} catch (e) {
@@ -366,7 +366,7 @@ const defaultMethods = {
366366
for (let i = 0; i < arr.length; i++) {
367367
try {
368368
// Todo: make this message thing more robust.
369-
if (lastError) item = await engine.run(arr[i], { error: lastError.error || lastError.message || lastError.constructor.name }, { above: [null, _1, _2] })
369+
if (lastError) item = await engine.run(arr[i], { type: lastError.type || lastError.error || lastError.message || lastError.constructor.name }, { above: [null, _1, _2] })
370370
else item = executeInLoop ? await engine.run(arr[i], _1, { above: _2 }) : arr[i]
371371
return item
372372
} catch (e) {

suites/divide.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -150,31 +150,31 @@
150150
{
151151
"description": "Divide by Zero",
152152
"rule": { "/": [0, 0] },
153-
"error": true,
153+
"error": { "type": "NaN" },
154154
"data": null
155155
},
156156
{
157157
"description": "Divide with String produces NaN",
158158
"rule": { "/": [1, "a"] },
159-
"error": true,
159+
"error": { "type": "NaN" },
160160
"data": null
161161
},
162162
{
163163
"description": "Divide with Array produces NaN",
164164
"rule": { "/": [1, [1]] },
165-
"error": true,
165+
"error": { "type": "NaN" },
166166
"data": null
167167
},
168168
{
169169
"description": "Any division by zero should return NaN",
170170
"rule": { "/": [1, 0] },
171-
"error": true,
171+
"error": { "type": "NaN" },
172172
"data": null
173173
},
174174
{
175175
"description": "Any division by zero should return NaN (2)",
176176
"rule": { "/": [8, 2, 0] },
177-
"error": true,
177+
"error": { "type": "NaN" },
178178
"data": null
179179
}
180180
]

suites/minus.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,13 @@
118118
{
119119
"description": "Subtraction with string produces NaN",
120120
"rule": { "-": ["Hey", 1] },
121-
"error": true,
121+
"error": { "type": "NaN" },
122122
"data": null
123123
},
124124
{
125125
"description": "Subtraction with Array produces NaN",
126126
"rule": { "-": [[1], 1] },
127-
"error": true,
127+
"error": { "type": "NaN" },
128128
"data": null
129129
}
130130
]

suites/modulo.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,13 @@
161161
{
162162
"description": "Modulo with string produces NaN",
163163
"rule": { "%": ["Hey", 1] },
164-
"error": true,
164+
"error": { "type": "NaN" },
165165
"data": null
166166
},
167167
{
168168
"description": "Modulo with array produces NaN",
169169
"rule": { "%": [[1], 1] },
170-
"error": true,
170+
"error": { "type": "NaN" },
171171
"data": null
172172
}
173173
]

suites/multiply.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -148,19 +148,19 @@
148148
{
149149
"description": "Multiply with string produces NaN",
150150
"rule": { "*": ["Hey", 1] },
151-
"error": true,
151+
"error": { "type": "NaN" },
152152
"data": null
153153
},
154154
{
155155
"description": "Multiply with a single string produces NaN",
156156
"rule": { "*": ["Hey"] },
157-
"error": true,
157+
"error": { "type": "NaN" },
158158
"data": null
159159
},
160160
{
161161
"description": "Multiply with Array produces NaN",
162162
"rule": { "*": [[1], 1] },
163-
"error": true,
163+
"error": { "type": "NaN" },
164164
"data": null
165165
}
166166
]

suites/plus.json

+8-8
Original file line numberDiff line numberDiff line change
@@ -144,49 +144,49 @@
144144
{
145145
"description": "Addition with string produces NaN",
146146
"rule": { "+": ["Hey", 1] },
147-
"error": true,
147+
"error": { "type": "NaN" },
148148
"data": null
149149
},
150150
{
151151
"description": "Addition with Array produces NaN",
152152
"rule": { "+": [[1], 1] },
153-
"error": true,
153+
"error": { "type": "NaN" },
154154
"data": null
155155
},
156156
{
157157
"description": "Addition with Array from context produces NaN",
158158
"rule": { "+": [{ "val": "x" }, 1] },
159-
"error": true,
159+
"error": { "type": "NaN" },
160160
"data": { "x": [1] }
161161
},
162162
{
163163
"description": "Addition with Object produces NaN",
164164
"rule": { "+": [{ "val": "x" }, 1] },
165-
"error": true,
165+
"error": { "type": "NaN" },
166166
"data": { "x": {} }
167167
},
168168
{
169169
"description": "Plus Operator with Single Operand, Invalid String Produces NaN",
170170
"rule": { "+": "Hello" },
171-
"error": true,
171+
"error": { "type": "NaN" },
172172
"data": null
173173
},
174174
{
175175
"description": "Plus Operator with Single Operand, Array Input Produces NaN",
176176
"rule": { "+": [[1]] },
177-
"error": true,
177+
"error": { "type": "NaN" },
178178
"data": null
179179
},
180180
{
181181
"description": "Plus Operator with Single Operand, Object Input Produces NaN",
182182
"rule": { "+": [{}] },
183-
"error": true,
183+
"error": { "type": "NaN" },
184184
"data": null
185185
},
186186
{
187187
"description": "Plus Operator with Single Operand, Direct Object Input Produces NaN",
188188
"rule": { "+": {} },
189-
"error": true,
189+
"error": { "type": "NaN" },
190190
"data": null
191191
}
192192
]

suites/throw.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33
"description": "Creates an error object",
44
"rule": { "throw": "hello" },
55
"data": null,
6-
"error": true
6+
"error": { "type": "hello" }
77
},
88
{
99
"description": "NaN creates an error object or measured equivalent (equivalence class test)",
1010
"rule": { "throw": "NaN" },
1111
"data": null,
12-
"error": true
12+
"error": { "type": "NaN" }
1313
},
1414
{
1515
"description": "Can throw an error object",
1616
"rule": { "throw": { "val": "x" } },
17-
"data": { "x": { "error": "Some error" }},
18-
"error": true
17+
"data": { "x": { "type": "Some error" }},
18+
"error": { "type": "Some error" }
1919
}
2020
]

suites/try.json

+16-16
Original file line numberDiff line numberDiff line change
@@ -21,44 +21,44 @@
2121
{
2222
"description": "Panics if none of the values are valid",
2323
"rule": { "try": [{ "throw": "Some error" }, { "throw": "Some other error" }] },
24-
"error": true,
24+
"error": { "type": "Some other error" },
2525
"data": null
2626
},
2727
{
2828
"description": "Panics if none of the values are valid (2)",
2929
"rule": { "try": [{ "throw": "Some error" }, { "/": [0, 0] }] },
30-
"error": true,
30+
"error": { "type": "NaN" },
3131
"data": null
3232
},
3333
{
3434
"description": "Panics if none of the values are valid (3)",
3535
"rule": { "try": [{ "/": [0, 0] }, { "/": [1, 0] }, { "/": [2, 0] }] },
36-
"error": true,
36+
"error": { "type": "NaN" },
3737
"data": null
3838
},
3939
{
4040
"description": "Panics when the only argument is an error",
4141
"rule": { "try": { "throw": "Some error" } },
42-
"error": true,
42+
"error": { "type": "Some error" },
4343
"data": null
4444
},
4545
{
4646
"description": "Panic with an error emitted from an operator",
4747
"rule": { "try": [{ "/": [1, 0] }] },
48-
"error": true,
48+
"error": { "type": "NaN" },
4949
"data": null
5050
},
5151
{
5252
"description": "Panic within an iterator",
5353
"rule": { "map": [[1, 2, 3], { "try": [{ "/": [0,0] }] }] },
54-
"error": true,
54+
"error": { "type": "NaN" },
5555
"data": null
5656
},
5757
{
5858
"description": "Panic based on an error emitted from an if",
5959
"rule": { "try": [{ "if": [{"val": ["user", "admin"]}, true, { "throw": "Not an admin" }] }] },
6060
"data": { "user": { "admin": false } },
61-
"error": true
61+
"error": { "type": "Not an admin" }
6262
},
6363
{
6464
"description": "Try can work further up the AST with Exceptions",
@@ -79,7 +79,7 @@
7979
"rule": {
8080
"try": [
8181
{ "throw": "Some error" },
82-
{ "val": "error" }
82+
{ "val": "type" }
8383
]
8484
},
8585
"result": "Some error",
@@ -90,7 +90,7 @@
9090
"rule": {
9191
"try": [
9292
{ "if": [true, { "throw": "Some error" }, null] },
93-
{ "val": "error" }
93+
{ "val": "type" }
9494
]
9595
},
9696
"result": "Some error",
@@ -102,7 +102,7 @@
102102
"try": [
103103
{ "throw": "A" },
104104
{ "throw": "B" },
105-
{ "val": "error" }
105+
{ "val": "type" }
106106
]
107107
},
108108
"result": "B",
@@ -111,9 +111,9 @@
111111
{
112112
"description": "Error can pull from an error object",
113113
"rule": {
114-
"try": [{ "throw": { "val": "x" } }, { "val": "error" }]
114+
"try": [{ "throw": { "val": "x" } }, { "val": "type" }]
115115
},
116-
"data": { "x": { "error": "Some error" }},
116+
"data": { "x": { "type": "Some error" }},
117117
"result": "Some error"
118118
},
119119
{
@@ -125,7 +125,7 @@
125125
{ "map": [[1,2,3], {"/": [0, 0] }]},
126126
null
127127
]
128-
}, { "val": "error" }]
128+
}, { "val": "type" }]
129129
},
130130
"result": "NaN",
131131
"data": null
@@ -136,7 +136,7 @@
136136
"try": [
137137
{ "if": [{ "/": [1, { "val": "x" }] }, { "throw": "Some error" }, null] },
138138
{
139-
"if": [{ "===": [{ "val": "error" }, "NaN"]}, "Handled", { "throw": { "val": [] } }]
139+
"if": [{ "===": [{ "val": "type" }, "NaN"]}, "Handled", { "throw": { "val": [] } }]
140140
}
141141
]
142142
},
@@ -148,10 +148,10 @@
148148
"rule": {
149149
"try": [
150150
{ "if": [{ "/": [1, { "val": "x" }] }, { "throw": "Some error" }, null] },
151-
{ "if": [{ "===": [{ "val": "error" }, "NaN"]}, "Handled", { "throw": { "val": [] } }] }
151+
{ "if": [{ "===": [{ "val": "type" }, "NaN"]}, "Handled", { "throw": { "val": [] } }] }
152152
]
153153
},
154-
"error": true,
154+
"error": { "type": "Some error" },
155155
"data": { "x": 1 }
156156
},
157157
"# Not Proposed",

0 commit comments

Comments
 (0)