Skip to content

Commit a72d987

Browse files
committed
test(validation): add test suite for arra-location validation
1 parent 57fdb5d commit a72d987

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

src/test/array-dash.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const grammar = new Grammar();
66
const parser = new Parser();
77

88
const testArrayDash = (referenceToken) => {
9+
if (typeof referenceToken !== 'string') return false;
10+
911
try {
1012
return parser.parse(grammar, 'array-dash', referenceToken).success;
1113
} catch {

src/test/array-location.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const grammar = new Grammar();
66
const parser = new Parser();
77

88
const testArrayLocation = (referenceToken) => {
9+
if (typeof referenceToken !== 'string') return false;
10+
911
try {
1012
return parser.parse(grammar, 'array-location', referenceToken).success;
1113
} catch {

test/test/array-location.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { assert } from 'chai';
2+
import { testArrayLocation } from '../../src/index.js';
3+
4+
describe('testArrayLocation', function () {
5+
context('valid array locations', function () {
6+
specify('should return true for valid array locations (rfc 6901)', function () {
7+
const validLocations = [
8+
'0', // minimal valid index
9+
'1', // single-digit positive number
10+
'9', // highest single digit
11+
'10', // multi-digit number
12+
'42', // arbitrary number
13+
'999', // large index
14+
'123456789', // long valid number
15+
'-', // valid array-dash
16+
];
17+
18+
validLocations.forEach((location) => {
19+
assert.isTrue(testArrayLocation(location), `failed for location: ${location}`);
20+
});
21+
});
22+
});
23+
24+
context('invalid array locations', function () {
25+
specify('should return false for locations with disallowed characters', function () {
26+
const invalidLocations = [
27+
'-1', // negative number
28+
'01', // leading zero
29+
'00', // multiple leading zeros
30+
'0123', // leading zero on a multi-digit number
31+
'1.0', // floating-point number
32+
'1,000', // comma-separated
33+
'1e3', // scientific notation
34+
'ten', // non-numeric string
35+
'1a', // mixed alphanumeric
36+
'1-', // trailing dash
37+
'', // empty string
38+
' ', // space only
39+
' 10', // leading space
40+
'10 ', // trailing space
41+
'0x1', // hexadecimal notation
42+
'--', // multiple dashes
43+
'-0', // dash followed by zero
44+
' - ', // dash with spaces
45+
];
46+
47+
invalidLocations.forEach((location) => {
48+
assert.isFalse(testArrayLocation(location), `failed for location: ${location}`);
49+
});
50+
});
51+
});
52+
53+
context('edge cases', function () {
54+
specify('should return false for non-string inputs', function () {
55+
assert.isFalse(testArrayLocation(42)); // number instead of string
56+
assert.isFalse(testArrayLocation(null));
57+
assert.isFalse(testArrayLocation(undefined));
58+
assert.isFalse(testArrayLocation({}));
59+
assert.isFalse(testArrayLocation([]));
60+
});
61+
62+
specify('should allow exactly "0"', function () {
63+
assert.isTrue(testArrayLocation('0'));
64+
});
65+
66+
specify('should allow exactly "-"', function () {
67+
assert.isTrue(testArrayLocation('-'));
68+
});
69+
70+
specify('should allow numbers without leading zeros', function () {
71+
assert.isTrue(testArrayLocation('1'));
72+
assert.isTrue(testArrayLocation('10'));
73+
assert.isTrue(testArrayLocation('123'));
74+
});
75+
76+
specify('should disallow numbers with leading zeros', function () {
77+
assert.isFalse(testArrayLocation('01'));
78+
assert.isFalse(testArrayLocation('007'));
79+
});
80+
});
81+
});

0 commit comments

Comments
 (0)