Skip to content

Commit 013e9b3

Browse files
committed
Copy over fast-stable-stringify tests from source
1 parent 96a4894 commit 013e9b3

File tree

4 files changed

+234
-58
lines changed

4 files changed

+234
-58
lines changed

packages/fast-stable-stringify/package.json

+7-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
],
3838
"scripts": {
3939
"compile:js": "tsup --config build-scripts/tsup.config.package.ts",
40+
"dev": "jest -c ../../node_modules/@solana/test-config/jest-dev.config.ts --rootDir . --watch",
4041
"prepublishOnly": "pnpm pkg delete devDependencies",
4142
"publish-impl": "npm view $npm_package_name@$npm_package_version > /dev/null 2>&1 || pnpm publish --tag ${PUBLISH_TAG:-preview} --access public --no-git-checks",
4243
"publish-packages": "pnpm prepublishOnly && pnpm publish-impl",
@@ -46,8 +47,12 @@
4647
"test:treeshakability:browser": "agadoo dist/index.browser.js",
4748
"test:treeshakability:native": "agadoo dist/index.native.js",
4849
"test:treeshakability:node": "agadoo dist/index.node.js",
49-
"test:unit:browser": "jest -c ../../node_modules/@solana/test-config/jest-unit.config.browser.ts --globalSetup @solana/test-config/test-validator-setup.js --globalTeardown @solana/test-config/test-validator-teardown.js --rootDir . --silent",
50-
"test:unit:node": "jest -c ../../node_modules/@solana/test-config/jest-unit.config.node.ts --globalSetup @solana/test-config/test-validator-setup.js --globalTeardown @solana/test-config/test-validator-teardown.js --rootDir . --silent"
50+
"test:unit:browser": "jest -c ../../node_modules/@solana/test-config/jest-unit.config.browser.ts --rootDir . --silent",
51+
"test:unit:node": "jest -c ../../node_modules/@solana/test-config/jest-unit.config.node.ts --rootDir . --silent"
52+
},
53+
"devDependencies": {
54+
"@types/json-stable-stringify": "^1.0.36",
55+
"json-stable-stringify": "^1.1.1"
5156
},
5257
"author": "Solana Labs Maintainers <[email protected]>",
5358
"license": "MIT",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
import jsonStableStringify from 'json-stable-stringify';
2+
3+
import stringify from '../index';
4+
5+
class ImplementingToJSON {
6+
toJSON() {
7+
return 'dummy!';
8+
}
9+
}
10+
11+
class NotImplementingToJSON {}
12+
13+
describe('fastStableStringify', function () {
14+
it.each([
15+
[
16+
'strings',
17+
{
18+
BACKSPACE: '\b',
19+
CARRIAGE_RETURN: '\r',
20+
EMPTY_STRING: '',
21+
ESCAPE_RANGE: '\u0000\u001F',
22+
FORM_FEED: '\f',
23+
LINE_FEED: '\n',
24+
LOWERCASE: 'abc',
25+
MIXED: 'Aa1 Bb2 Cc3 \u0000\u001F\u0020\uFFFF☃"\\/\f\n\r\t\b',
26+
NON_ESCAPE_RANGE: '\u0020\uFFFF',
27+
NUMBER_ONLY: '123',
28+
QUOTATION_MARK: '"',
29+
REVERSE_SOLIDUS: '\\',
30+
SOLIDUS: '/',
31+
TAB: '\t',
32+
UPPERCASE: 'ABC',
33+
UTF16: '☃',
34+
VALUES_WITH_SPACES: 'a b c',
35+
},
36+
],
37+
[
38+
'object keys',
39+
{
40+
'': 'EMPTY_STRING',
41+
'\u0000\u001F': 'ESCAPE_RANGE',
42+
'\b': 'BACKSPACE',
43+
'\t': 'TAB',
44+
'\n': 'LINE_FEED',
45+
'\f': 'FORM_FEED',
46+
'\r': 'CARRIAGE_RETURN',
47+
'\u0020\uFFFF': 'NON_ESCAPE_RANGE',
48+
'"': 'QUOTATION_MARK',
49+
'/': 'SOLIDUS',
50+
ABC: 'UPPERCASE',
51+
'Aa1 Bb2 Cc3 \u0000\u001F\u0020\uFFFF☃"\\/\f\n\r\t\b': 'MIXED',
52+
NUMBER_ONLY: '123',
53+
'\\': 'REVERSE_SOLIDUS',
54+
'a b c': 'VALUES_WITH_SPACES',
55+
abc: 'LOWERCASE',
56+
'☃': 'UTF16',
57+
},
58+
],
59+
[
60+
'numbers',
61+
{
62+
FALSY: 0,
63+
FLOAT: 0.1234567,
64+
INFINITY: Infinity,
65+
MAX_SAFE_INTEGER: 9007199254740991,
66+
MAX_VALUE: 1.7976931348623157e308,
67+
MIN_SAFE_INTEGER: -9007199254740991,
68+
MIN_VALUE: 5e-324,
69+
NAN: NaN,
70+
NEGATIVE: -1,
71+
NEGATIVE_FLOAT: -0.9876543,
72+
NEGATIVE_MAX_VALUE: -1.7976931348623157e308,
73+
NEGATIVE_MIN_VALUE: -5e-324,
74+
NEG_INFINITY: -Infinity,
75+
},
76+
],
77+
['true', true],
78+
['false', false],
79+
['undefined', undefined],
80+
['null', null],
81+
['objects of undefineds', { ONE: undefined, THREE: undefined, TWO: undefined }],
82+
['objects of null', { NULL: null }],
83+
['a Date instance', new Date('2017')],
84+
['a function', function () {}],
85+
['object that implements `toJSON`', new ImplementingToJSON()],
86+
['objects that does not implements `toJSON`', new NotImplementingToJSON()],
87+
[
88+
'objects of mixed values',
89+
{
90+
'Aa1 Bb2 Cc3 \u0000\u001F\u0020\uFFFF☃"\\/\f\n\r\t\b': 'MIXED',
91+
FALSE: false,
92+
MAX_VALUE: 1.7976931348623157e308,
93+
MIN_VALUE: 5e-324,
94+
MIXED: 'Aa1 Bb2 Cc3 \u0000\u001F\u0020\uFFFF☃"\\/\f\n\r\t\b',
95+
NEGATIVE_MAX_VALUE: -1.7976931348623157e308,
96+
NEGATIVE_MIN_VALUE: -5e-324,
97+
NULL: null,
98+
TRUE: true,
99+
UNDEFINED: undefined,
100+
zzz: 'ending',
101+
},
102+
],
103+
[
104+
'arrays of numbers',
105+
[
106+
9007199254740991,
107+
-9007199254740991,
108+
0,
109+
-1,
110+
0.1234567,
111+
-0.9876543,
112+
1.7976931348623157e308,
113+
5e-324,
114+
-1.7976931348623157e308,
115+
-5e-324,
116+
Infinity,
117+
-Infinity,
118+
NaN,
119+
],
120+
],
121+
[
122+
'arrays of strings',
123+
[
124+
'a b c',
125+
'abc',
126+
'ABC',
127+
'NUMBER_ONLY',
128+
'',
129+
'\u0000\u001F',
130+
'\u0020\uFFFF',
131+
'☃',
132+
'"',
133+
'\\',
134+
'/',
135+
'\f',
136+
'\n',
137+
'\r',
138+
'\t',
139+
'\b',
140+
'Aa1 Bb2 Cc3 \u0000\u001F\u0020\uFFFF☃"\\/\f\n\r\t\b',
141+
],
142+
],
143+
['arrays of booleans ', [true, false]],
144+
['arrays of null', [null]],
145+
['arrays of undefined', [undefined]],
146+
['arrays of Date instances', [new Date('2017')]],
147+
['arrays of instances that implement `toJSON`', [new ImplementingToJSON()]],
148+
['arrays of instances that do not implement `toJSON`', [new NotImplementingToJSON()]],
149+
['arrays of functions', [function () {}]],
150+
[
151+
'arrays of mixed values',
152+
[
153+
-1.7976931348623157e308,
154+
-5e-324,
155+
'Aa1 Bb2 Cc3 \u0000\u001F\u0020\uFFFF☃"\\/\f\n\r\t\b',
156+
true,
157+
false,
158+
null,
159+
undefined,
160+
],
161+
],
162+
[
163+
'mixed values',
164+
[
165+
{
166+
'Aa1 Bb2 Cc3 \u0000\u001F\u0020\uFFFF☃"\\/\f\n\r\t\b': 'MIXED',
167+
DATE: new Date('2017'),
168+
FALSE: false,
169+
FUNCTION: function () {},
170+
IMPLEMENTING_TO_JSON: new ImplementingToJSON(),
171+
MAX_VALUE: 1.7976931348623157e308,
172+
MIN_VALUE: 5e-324,
173+
MIXED: 'Aa1 Bb2 Cc3 \u0000\u001F\u0020\uFFFF☃"\\/\f\n\r\t\b',
174+
NEGATIVE_MAX_VALUE: -1.7976931348623157e308,
175+
NEGATIVE_MIN_VALUE: -5e-324,
176+
NOT_IMPLEMENTING_TO_JSON: new NotImplementingToJSON(),
177+
NULL: null,
178+
TRUE: true,
179+
UNDEFINED: undefined,
180+
zzz: 'ending',
181+
},
182+
-1.7976931348623157e308,
183+
-5e-324,
184+
'Aa1 Bb2 Cc3 \u0000\u001F\u0020\uFFFF☃"\\/\f\n\r\t\b',
185+
true,
186+
false,
187+
null,
188+
undefined,
189+
new Date('2017'),
190+
function () {},
191+
new ImplementingToJSON(),
192+
new NotImplementingToJSON(),
193+
],
194+
],
195+
])('matches the output of `json-stable-stringify` when hashing %s (`%s`)', (_, value) => {
196+
expect(stringify(value)).toBe(jsonStableStringify(value));
197+
});
198+
it('hashes bigints', function () {
199+
expect(stringify(200n)).toMatch('"200n"');
200+
});
201+
});

packages/fast-stable-stringify/src/__tests__/json_test.ts

-55
This file was deleted.

pnpm-lock.yaml

+26-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)