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

amend: added consistency test, access counts #1753

Merged
merged 1 commit into from
Jul 24, 2022
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
1 change: 1 addition & 0 deletions .structignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ test/comparison/benchmark.spec/gen-table.spec.js
test/comparison/benchmark.spec/get-color-for-value.spec.js
test/comparison/benchmark.spec/ok-logo.spec.js
test/comparison/benchmark.spec/run-benchmark.spec.js
test/consistency/access-counts.spec.js
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![NPM](https://img.shields.io/npm/v/object-scan.svg)](https://www.npmjs.com/package/object-scan)
[![Downloads](https://img.shields.io/npm/dt/object-scan.svg)](https://www.npmjs.com/package/object-scan)
[![Size](https://shields.io/badge/min%20+%20gz-4.88%20KB-informational)](https://cdn.jsdelivr.net/npm/object-scan/lib/)
[![Test Ratio](https://shields.io/badge/test%20:%20code-9.8%20:%201-informational)](./test/readme/replace-variables/ratio-badge.js)
[![Test Ratio](https://shields.io/badge/test%20:%20code-9.9%20:%201-informational)](./test/readme/replace-variables/ratio-badge.js)

Traverse object hierarchies using matching and callbacks.

Expand Down
52 changes: 52 additions & 0 deletions test/consistency/access-counts.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { expect } from 'chai';
import Nimma from 'nimma';
import objectScan from '../../src/index.js';
import { AccessLogger } from './access-counts/access-logger.js';
import fixture from './access-counts/fixture.js';

const needlesToNimma = (needles) => needles.map(
(n) => `$.${n}`
.replace(/\.\*\*\./g, '..')
);

describe('Testing access counts', () => {
it('Testing objectScan', () => {
const al = new AccessLogger(fixture.obj);
const result = objectScan(fixture.needles, {
joined: true,
filterFn: ({
matchedBy, key, value, context
}) => {
matchedBy.forEach((m) => {
context.push([key, m, value]);
});
}
})(al.obj, []);
expect(al.reset()).to.deep.equal([
'a', 'b', 'e', 'e', 'd', 'd', 'c', 'c', 'b', 'a'
]);
expect(result).to.deep.equal(fixture.expected);
});

it('Testing nimma', () => {
const al = new AccessLogger(fixture.obj);
const needles = needlesToNimma(fixture.needles);

const result = [];
new Nimma(needles).query(al.obj, needles.reduce((p, k) => ({
...p,
[k]({ value, path }) {
result.push([path.join('.'), k, value]);
}
}), {}));
expect(al.reset()).to.deep.equal([
'a', 'b', 'a', 'b', 'c', 'a', 'b', 'a', 'b', 'd',
'a', 'b', 'a', 'b', 'e', 'a', 'a', 'b', 'a', 'a',
'b', 'b', 'c', 'c', 'd', 'd', 'e', 'e'
]);
const expected = fixture.expected
.map(([path, needle, value]) => [path, needlesToNimma([needle])[0], value])
.sort();
expect(result.sort()).to.deep.equal(expected);
});
});
27 changes: 27 additions & 0 deletions test/consistency/access-counts/access-logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export class AccessLogger {
constructor(obj) {
const logs = [];
const prepare = (o) => {
const entries = Object.entries(o);
const result = {};
for (let i = 0; i < entries.length; i += 1) {
const [k, v] = entries[i];
const value = prepare(v);
result[k] = value;
Object.defineProperty(result, k, {
get() {
logs.push(k);
return value;
}
});
}
return result;
};
this.logs = logs;
this.obj = prepare(obj);
}

reset() {
return this.logs.splice(0);
}
}
37 changes: 37 additions & 0 deletions test/consistency/access-counts/fixture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export default {
obj: {
a: {
b: {
c: {},
d: {},
e: {}
}
}
},
needles: [
'a.b.c',
'**.*',
'a.b.d',
'a.b.e',
'a.*.e',
'**.e',
'*.*',
'**.b.c',
'a.b'
],
expected: [
['a.b.e', 'a.b.e', {}],
['a.b.e', 'a.*.e', {}],
['a.b.e', '**.*', {}],
['a.b.e', '**.e', {}],
['a.b.d', 'a.b.d', {}],
['a.b.d', '**.*', {}],
['a.b.c', 'a.b.c', {}],
['a.b.c', '**.*', {}],
['a.b.c', '**.b.c', {}],
['a.b', 'a.b', { c: {}, d: {}, e: {} }],
['a.b', '**.*', { c: {}, d: {}, e: {} }],
['a.b', '*.*', { c: {}, d: {}, e: {} }],
['a', '**.*', { b: { c: {}, d: {}, e: {} } }]
]
};