Skip to content

Commit e8300f7

Browse files
committed
Commit length tests and updates
1 parent 9605498 commit e8300f7

File tree

4 files changed

+94
-20
lines changed

4 files changed

+94
-20
lines changed

async.test.js

-8
Original file line numberDiff line numberDiff line change
@@ -753,14 +753,6 @@ modes.forEach((logic) => {
753753
expect(answer).toStrictEqual(0)
754754
})
755755

756-
test('length object (null)', async () => {
757-
const answer = await logic.run({
758-
length: { preserve: null }
759-
})
760-
761-
expect(answer).toStrictEqual(0)
762-
})
763-
764756
test('merge', async () => {
765757
const answer = await logic.run({
766758
merge: [{ preserve: ['b'] }, { preserve: ['c'] }]

defaultMethods.js

+19-4
Original file line numberDiff line numberDiff line change
@@ -445,10 +445,25 @@ const defaultMethods = {
445445
}
446446
return string.substr(from, end)
447447
},
448-
length: ([i]) => {
449-
if (typeof i === 'string' || Array.isArray(i)) return i.length
450-
if (i && typeof i === 'object') return Object.keys(i).length
451-
return 0
448+
length: {
449+
method: (data, context, above, engine) => {
450+
if (!data) throw INVALID_ARGUMENTS
451+
const parsed = runOptimizedOrFallback(data, engine, context, above)
452+
const i = Array.isArray(data) ? parsed[0] : parsed
453+
if (typeof i === 'string' || Array.isArray(i)) return i.length
454+
if (i && typeof i === 'object') return Object.keys(i).length
455+
throw INVALID_ARGUMENTS
456+
},
457+
asyncMethod: async (data, context, above, engine) => {
458+
if (!data) throw INVALID_ARGUMENTS
459+
const parsed = await runOptimizedOrFallback(data, engine, context, above)
460+
const i = Array.isArray(data) ? parsed[0] : parsed
461+
if (typeof i === 'string' || Array.isArray(i)) return i.length
462+
if (i && typeof i === 'object') return Object.keys(i).length
463+
throw INVALID_ARGUMENTS
464+
},
465+
deterministic: (data, buildState) => isDeterministic(data, buildState.engine, buildState),
466+
lazy: true
452467
},
453468
exists: {
454469
method: (key, context, above, engine) => {

suites/length.json

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
[
2+
"# Length operator tests",
3+
{
4+
"description": "Length of a basic array",
5+
"rule": { "length": {"val": "array"} },
6+
"data": { "array": [1, 2, 3, 4, 5] },
7+
"result": 5
8+
},
9+
{
10+
"description": "Length of an empty array returns 0",
11+
"rule": { "length": {"val": "emptyArray"} },
12+
"data": { "emptyArray": [] },
13+
"result": 0
14+
},
15+
{
16+
"description": "Length of a string counts characters",
17+
"rule": { "length": {"val": "str"} },
18+
"data": { "str": "hello" },
19+
"result": 5
20+
},
21+
{
22+
"description": "Length of an empty string returns 0",
23+
"rule": { "length": {"val": "empty"} },
24+
"data": { "empty": "" },
25+
"result": 0
26+
},
27+
{
28+
"description": "Length of missing variable returns null",
29+
"rule": { "length": {"val": "missing"} },
30+
"data": {},
31+
"error": { "type": "Invalid Arguments" }
32+
},
33+
{
34+
"description": "Length operator with null argument throws error",
35+
"rule": { "length": null },
36+
"data": {},
37+
"error": { "type": "Invalid Arguments" }
38+
},
39+
{
40+
"description": "Length operator with numeric argument throws error",
41+
"rule": { "length": 123 },
42+
"data": {},
43+
"error": { "type": "Invalid Arguments" }
44+
},
45+
{
46+
"description": "Length operator with boolean argument throws error",
47+
"rule": { "length": true },
48+
"data": {},
49+
"error": { "type": "Invalid Arguments" }
50+
},
51+
{
52+
"description": "Conditional based on array length (longer than 3)",
53+
"rule": { "if": [{ ">": [{ "length": {"val": "array"} }, 3] }, "long", "short"] },
54+
"data": { "array": [1, 2, 3, 4] },
55+
"result": "long"
56+
},
57+
{
58+
"description": "Conditional based on array length (not longer than 3)",
59+
"rule": { "if": [{ ">": [{ "length": {"val": "array"} }, 3] }, "long", "short"] },
60+
"data": { "array": [1, 2, 3] },
61+
"result": "short"
62+
},
63+
{
64+
"description": "Length of nested array counts top level elements",
65+
"rule": { "length": {"val": "nestedArray"} },
66+
"data": { "nestedArray": [[1, 2], [3, 4], [5, 6]] },
67+
"result": 3
68+
},
69+
{
70+
"description": "Length of array of objects counts objects",
71+
"rule": { "length": {"val": "objectArray"} },
72+
"data": { "objectArray": [{"a": 1}, {"b": 2}, {"c": 3}] },
73+
"result": 3
74+
}
75+
]

test.js

-8
Original file line numberDiff line numberDiff line change
@@ -730,14 +730,6 @@ modes.forEach((logic) => {
730730
expect(answer).toStrictEqual(0)
731731
})
732732

733-
test('length object (null)', () => {
734-
const answer = logic.run({
735-
length: { preserve: null }
736-
})
737-
738-
expect(answer).toStrictEqual(0)
739-
})
740-
741733
test('get from array', () => {
742734
const answer = logic.run({
743735
get: [['hi'], 'length']

0 commit comments

Comments
 (0)