Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy over fast-stable-stringify tests from source #2501

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions packages/fast-stable-stringify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
],
"scripts": {
"compile:js": "tsup --config build-scripts/tsup.config.package.ts",
"dev": "jest -c ../../node_modules/@solana/test-config/jest-dev.config.ts --rootDir . --watch",
"prepublishOnly": "pnpm pkg delete devDependencies",
"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",
"publish-packages": "pnpm prepublishOnly && pnpm publish-impl",
Expand All @@ -46,8 +47,12 @@
"test:treeshakability:browser": "agadoo dist/index.browser.js",
"test:treeshakability:native": "agadoo dist/index.native.js",
"test:treeshakability:node": "agadoo dist/index.node.js",
"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",
"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"
Comment on lines -49 to -50
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This never needed the test validator.

"test:unit:browser": "jest -c ../../node_modules/@solana/test-config/jest-unit.config.browser.ts --rootDir . --silent",
"test:unit:node": "jest -c ../../node_modules/@solana/test-config/jest-unit.config.node.ts --rootDir . --silent"
},
"devDependencies": {
"@types/json-stable-stringify": "^1.0.36",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious why it needs the types, too?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

json-stable-stringify doesn't use TypeScript. You have to buy the types on the second hand market.

"json-stable-stringify": "^1.1.1"
},
"author": "Solana Labs Maintainers <[email protected]>",
"license": "MIT",
Expand Down
201 changes: 201 additions & 0 deletions packages/fast-stable-stringify/src/__tests__/index-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
import jsonStableStringify from 'json-stable-stringify';

import stringify from '../index';

class ImplementingToJSON {
toJSON() {
return 'dummy!';
}
}

class NotImplementingToJSON {}

describe('fastStableStringify', function () {
it.each([
[
'strings',
{
BACKSPACE: '\b',
CARRIAGE_RETURN: '\r',
EMPTY_STRING: '',
ESCAPE_RANGE: '\u0000\u001F',
FORM_FEED: '\f',
LINE_FEED: '\n',
LOWERCASE: 'abc',
MIXED: 'Aa1 Bb2 Cc3 \u0000\u001F\u0020\uFFFF☃"\\/\f\n\r\t\b',
NON_ESCAPE_RANGE: '\u0020\uFFFF',
NUMBER_ONLY: '123',
QUOTATION_MARK: '"',
REVERSE_SOLIDUS: '\\',
SOLIDUS: '/',
TAB: '\t',
UPPERCASE: 'ABC',
UTF16: '☃',
VALUES_WITH_SPACES: 'a b c',
},
],
[
'object keys',
{
'': 'EMPTY_STRING',
'\u0000\u001F': 'ESCAPE_RANGE',
'\b': 'BACKSPACE',
'\t': 'TAB',
'\n': 'LINE_FEED',
'\f': 'FORM_FEED',
'\r': 'CARRIAGE_RETURN',
'\u0020\uFFFF': 'NON_ESCAPE_RANGE',
'"': 'QUOTATION_MARK',
'/': 'SOLIDUS',
ABC: 'UPPERCASE',
'Aa1 Bb2 Cc3 \u0000\u001F\u0020\uFFFF☃"\\/\f\n\r\t\b': 'MIXED',
NUMBER_ONLY: '123',
'\\': 'REVERSE_SOLIDUS',
'a b c': 'VALUES_WITH_SPACES',
abc: 'LOWERCASE',
'☃': 'UTF16',
},
],
[
'numbers',
{
FALSY: 0,
FLOAT: 0.1234567,
INFINITY: Infinity,
MAX_SAFE_INTEGER: 9007199254740991,
MAX_VALUE: 1.7976931348623157e308,
MIN_SAFE_INTEGER: -9007199254740991,
MIN_VALUE: 5e-324,
NAN: NaN,
NEGATIVE: -1,
NEGATIVE_FLOAT: -0.9876543,
NEGATIVE_MAX_VALUE: -1.7976931348623157e308,
NEGATIVE_MIN_VALUE: -5e-324,
NEG_INFINITY: -Infinity,
},
],
['true', true],
['false', false],
['undefined', undefined],
['null', null],
['objects of undefineds', { ONE: undefined, THREE: undefined, TWO: undefined }],
['objects of null', { NULL: null }],
['a Date instance', new Date('2017')],
['a function', function () {}],
['object that implements `toJSON`', new ImplementingToJSON()],
['objects that does not implements `toJSON`', new NotImplementingToJSON()],
[
'objects of mixed values',
{
'Aa1 Bb2 Cc3 \u0000\u001F\u0020\uFFFF☃"\\/\f\n\r\t\b': 'MIXED',
FALSE: false,
MAX_VALUE: 1.7976931348623157e308,
MIN_VALUE: 5e-324,
MIXED: 'Aa1 Bb2 Cc3 \u0000\u001F\u0020\uFFFF☃"\\/\f\n\r\t\b',
NEGATIVE_MAX_VALUE: -1.7976931348623157e308,
NEGATIVE_MIN_VALUE: -5e-324,
NULL: null,
TRUE: true,
UNDEFINED: undefined,
zzz: 'ending',
},
],
[
'arrays of numbers',
[
9007199254740991,
-9007199254740991,
0,
-1,
0.1234567,
-0.9876543,
1.7976931348623157e308,
5e-324,
-1.7976931348623157e308,
-5e-324,
Infinity,
-Infinity,
NaN,
],
],
[
'arrays of strings',
[
'a b c',
'abc',
'ABC',
'NUMBER_ONLY',
'',
'\u0000\u001F',
'\u0020\uFFFF',
'☃',
'"',
'\\',
'/',
'\f',
'\n',
'\r',
'\t',
'\b',
'Aa1 Bb2 Cc3 \u0000\u001F\u0020\uFFFF☃"\\/\f\n\r\t\b',
],
],
['arrays of booleans ', [true, false]],
['arrays of null', [null]],
['arrays of undefined', [undefined]],
['arrays of Date instances', [new Date('2017')]],
['arrays of instances that implement `toJSON`', [new ImplementingToJSON()]],
['arrays of instances that do not implement `toJSON`', [new NotImplementingToJSON()]],
['arrays of functions', [function () {}]],
[
'arrays of mixed values',
[
-1.7976931348623157e308,
-5e-324,
'Aa1 Bb2 Cc3 \u0000\u001F\u0020\uFFFF☃"\\/\f\n\r\t\b',
true,
false,
null,
undefined,
],
],
[
'mixed values',
[
{
'Aa1 Bb2 Cc3 \u0000\u001F\u0020\uFFFF☃"\\/\f\n\r\t\b': 'MIXED',
DATE: new Date('2017'),
FALSE: false,
FUNCTION: function () {},
IMPLEMENTING_TO_JSON: new ImplementingToJSON(),
MAX_VALUE: 1.7976931348623157e308,
MIN_VALUE: 5e-324,
MIXED: 'Aa1 Bb2 Cc3 \u0000\u001F\u0020\uFFFF☃"\\/\f\n\r\t\b',
NEGATIVE_MAX_VALUE: -1.7976931348623157e308,
NEGATIVE_MIN_VALUE: -5e-324,
NOT_IMPLEMENTING_TO_JSON: new NotImplementingToJSON(),
NULL: null,
TRUE: true,
UNDEFINED: undefined,
zzz: 'ending',
},
-1.7976931348623157e308,
-5e-324,
'Aa1 Bb2 Cc3 \u0000\u001F\u0020\uFFFF☃"\\/\f\n\r\t\b',
true,
false,
null,
undefined,
new Date('2017'),
function () {},
new ImplementingToJSON(),
new NotImplementingToJSON(),
],
],
])('matches the output of `json-stable-stringify` when hashing %s (`%s`)', (_, value) => {
expect(stringify(value)).toBe(jsonStableStringify(value));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to bench this against anything else, like faster-stable-stringify or JSON.stringify?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original fast-stable-stringify tests verified its output against json-stable-stringify for correctness only.

I at least wanted to use that as a starting point so that as we make changes we can observe how they make ours diverge.

});
it('hashes bigints', function () {
expect(stringify(200n)).toMatch('"200n"');
});
});
55 changes: 0 additions & 55 deletions packages/fast-stable-stringify/src/__tests__/json_test.ts

This file was deleted.

27 changes: 26 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading