Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epidemian
GitHub Repository: epidemian/eslint-plugin-import
Path: blob/main/tests/src/core/hash.js
829 views
1
import { expect } from 'chai';
2
3
import hashify, { hashArray, hashObject } from 'eslint-module-utils/hash';
4
5
const createHash = require('crypto').createHash;
6
7
function expectHash(actualHash, expectedString) {
8
const expectedHash = createHash('sha256');
9
expectedHash.update(expectedString);
10
expect(actualHash.digest('hex'), 'to be a hex digest of sha256 hash of string <' + expectedString + '>').to.equal(expectedHash.digest('hex'));
11
}
12
13
describe('hash', function () {
14
describe('hashify', function () {
15
it('handles null', function () {
16
expectHash(hashify(null), 'null');
17
});
18
19
it('handles undefined', function () {
20
expectHash(hashify(undefined), 'undefined');
21
});
22
23
it('handles numbers', function () {
24
expectHash(hashify(123.456), '123.456');
25
});
26
27
it('handles strings', function () {
28
expectHash(hashify('a string'), '"a string"');
29
});
30
31
it('handles Array instances', function () {
32
expectHash(hashify([ 'a string' ]), '["a string",]');
33
});
34
35
it('handles empty Array instances', function () {
36
expectHash(hashify([]), '[]');
37
});
38
39
it('handles Object instances', function () {
40
expectHash(hashify({ foo: 123.456, 'a key': 'a value' }), '{"a key":"a value","foo":123.456,}');
41
});
42
43
it('handles nested Object instances', function () {
44
expectHash(hashify({ foo: 123.456, 'a key': 'a value', obj: { abc: { def: 'ghi' } } }), '{"a key":"a value","foo":123.456,"obj":{"abc":{"def":"ghi",},},}');
45
});
46
47
it('handles nested Object and Array instances', function () {
48
expectHash(hashify({ foo: 123.456, 'a key': 'a value', obj: { arr: [ { def: 'ghi' } ] } }), '{"a key":"a value","foo":123.456,"obj":{"arr":[{"def":"ghi",},],},}');
49
});
50
});
51
52
describe('hashArray', function () {
53
it('handles Array instances', function () {
54
expectHash(hashArray([ 'a string' ]), '["a string",]');
55
});
56
57
it('handles empty Array instances', function () {
58
expectHash(hashArray([]), '[]');
59
});
60
});
61
62
describe('hashObject', function () {
63
it('handles Object instances', function () {
64
expectHash(hashObject({ foo: 123.456, 'a key': 'a value' }), '{"a key":"a value","foo":123.456,}');
65
});
66
67
it('handles nested Object instances', function () {
68
expectHash(hashObject({ foo: 123.456, 'a key': 'a value', obj: { abc: { def: 'ghi' } } }), '{"a key":"a value","foo":123.456,"obj":{"abc":{"def":"ghi",},},}');
69
});
70
71
it('handles nested Object and Array instances', function () {
72
expectHash(hashObject({ foo: 123.456, 'a key': 'a value', obj: { arr: [ { def: 'ghi' } ] } }), '{"a key":"a value","foo":123.456,"obj":{"arr":[{"def":"ghi",},],},}');
73
});
74
});
75
76
});
77
78