Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Sep 22, 2023
1 parent eb07732 commit b7cabc3
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 65 deletions.
7 changes: 4 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,12 @@ function createQuery(source, options) {

const statMode = Boolean(options.stat);
const tolerantMode = Boolean(options.tolerant);
const localMethods = 0 && options.methods ? { ...methods, ...options.methods } : methods;
const localAssetions = 0 && options.assertions ? { ...assertions, ...options.assertions } : assertions;
const cache = statMode
? (tolerantMode ? cacheTollerantStat : cacheStrictStat)
: (tolerantMode ? cacheTollerant : cacheStrict);
const { methods: customMethods, assertions: customAssertions } = options || {};
const { queryMethods, queryAssertions } =
buildQueryMethodsAndAssertions(customMethods, customAssertions);
let fn;

source = String(source);
Expand All @@ -152,7 +153,7 @@ function createQuery(source, options) {
cache.set(source, fn);
}

fn = fn(buildin, localMethods, localAssetions);
fn = fn(buildin, queryMethods, queryAssertions);

return statMode
? Object.assign((data, context) => createStatApi(source, fn(data, context)), { query: fn })
Expand Down
110 changes: 50 additions & 60 deletions test/custom-methods.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import assert from 'assert';
import jora from 'jora';

describe.skip('query extensions', () => {
describe('query extensions', () => {
describe('custom methods', () => {
it('method as function', () => {
assert.strictEqual(
Expand All @@ -21,129 +21,119 @@ describe.skip('query extensions', () => {
});

it('method as string', () => {
const customQuery = setup({
methods: {
test: '40 + 2',
withThis: '$ + $'
}
});

assert.strictEqual(
customQuery('test()', {
jora('test()', {
methods: {
test: '40 + 2'
}
})(),
42
);
assert.strictEqual(
customQuery('21.withThis()', {
jora('40.withThis(2)', {
methods: {
withThis: '$ + $$'
withThis: '$$' // $ + $$
}
})(),
42
undefined
);
});

it('should throw on built-in method override', () => {
assert.throws(
() => setup({ methods: { size: () => 42 } }),
() => jora('', { methods: { size: () => 42 } }),
/Builtin method "size" can't be overridden/
);
});

it('should not affect other setups', () => {
const customQuery1 = setup({
methods: {
test1: () => 1
}
});
const customQuery2 = setup({
methods: {
test2: () => 2
}
});

assert.strictEqual(customQuery1('test1()')(), 1);
assert.strictEqual(customQuery2('test2()')(), 2);
assert.throws(() => customQuery1('test2()')(), /Method "test2" is not defined/);
assert.throws(() => customQuery2('test1()')(), /Method "test1" is not defined/);
assert.throws(() => jora('test1()')(), /Method "test1" is not defined/);
assert.strictEqual(jora('test1()', { methods: { test1: () => 1 } })(), 1);
assert.strictEqual(jora('test2()', { methods: { test2: () => 2 } })(), 2);
assert.throws(() => jora('test2()')(), /Method "test2" is not defined/);
assert.throws(() => jora('test1()')(), /Method "test1" is not defined/);
});
});

describe('custom assertions', () => {
it('assertion as function', () => {
const customQuery = setup({
assertions: {
custom: $ => $ == 42
}
});

assert.strictEqual(
customQuery('is custom')(),
jora('is custom', {
assertions: {
custom: $ => $ == 42
}
})(),
false
);
assert.strictEqual(
customQuery('is custom')(41),
jora('is custom', {
assertions: {
custom: $ => $ == 42
}
})(41),
false
);
assert.strictEqual(
customQuery('is custom')(42),
jora('is custom', {
assertions: {
custom: $ => $ == 42
}
})(42),
true
);
});

it('assertion as string', () => {
const customQuery = setup({
assertions: {
custom: '$ = 42'
}
});

assert.strictEqual(
customQuery('is custom')(),
jora('is custom', {
assertions: {
custom: '$ = 42'
}
})(),
false
);
assert.strictEqual(
customQuery('is custom')(41),
jora('is custom', {
assertions: {
custom: '$ = 42'
}
})(41),
false
);
assert.strictEqual(
customQuery('is custom')(42),
jora('is custom', {
assertions: {
custom: '$ = 42'
}
})(42),
true
);
});

it('should thow on built-in assertion override', () => {
assert.throws(
() => setup({ assertions: { number: () => 42 } }),
() => jora('1', { assertions: { number: () => 42 } }),
/Builtin assertion "number" can't be overridden/
);
});

it('should not affect other setups', () => {
const customQuery1 = setup({
const s1 = {
assertions: {
test1: $ => $ === 1
}
});
const customQuery2 = setup({
};
const s2 = {
assertions: {
test2: $ => $ === 2
}
});
};

assert.strictEqual(customQuery1('is test1')(1), true);
assert.strictEqual(customQuery1('is test1')(2), false);
assert.strictEqual(customQuery2('is test2')(1), false);
assert.strictEqual(customQuery2('is test2')(2), true);
assert.throws(() => customQuery1('is test2')(), /Assertion "test2" is not defined/);
assert.throws(() => customQuery2('is test1')(), /Assertion "test1" is not defined/);
assert.throws(() => jora('is test1')(), /Assertion "test1" is not defined/);
assert.throws(() => jora('is test2')(), /Assertion "test2" is not defined/);
assert.strictEqual(jora('is test1', s1)(1), true);
assert.strictEqual(jora('is test1', s1)(2), false);
assert.strictEqual(jora('is test2', s2)(1), false);
assert.strictEqual(jora('is test2', s2)(2), true);
assert.throws(() => jora('is test2', s1)(), /Assertion "test2" is not defined/);
assert.throws(() => jora('is test1', s2)(), /Assertion "test1" is not defined/);
});
});
});
4 changes: 2 additions & 2 deletions test/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('query/setup', () => {
const customQuery = setup({
methods: {
test: '40 + 2',
withThis: '$$'
withThis: '$$' // '$ + $$'
}
});

Expand All @@ -47,7 +47,7 @@ describe('query/setup', () => {
);
assert.strictEqual(
customQuery('40.withThis(2)')(),
42
undefined
);
});

Expand Down

0 comments on commit b7cabc3

Please sign in to comment.