Package list node-nth-check / 62bc206
Update test Xavier Guimard 11 months ago
7 changed file(s) with 116 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
44 Uploaders: Thorsten Alteholz <debian@alteholz.de>
55 Build-Depends: debhelper-compat (= 13)
66 , dh-sequence-nodejs
7 , mocha <!nocheck>
78 , node-assert <!nocheck>
89 , node-boolbase <!nocheck>
10 , node-expect.js <!nocheck>
11 , node-typescript
12 , node-typescript-types
913 Standards-Version: 4.5.1
1014 Vcs-Browser: https://salsa.debian.org/js-team/node-nth-check
1115 Vcs-Git: https://salsa.debian.org/js-team/node-nth-check.git
0 debian/tests/test
0 node test.js
0 mocha debian/tests/test/*.js
0 const valid = [
1 ["1", [0, 1]],
2 ["2", [0, 2]],
3 ["3", [0, 3]],
4 ["5", [0, 5]],
5 [" 1 ", [0, 1]],
6 [" 5 ", [0, 5]],
7 ["+2n + 1", [2, 1]],
8 ["-1", [0, -1]],
9 ["-1n + 3", [-1, 3]],
10 ["-1n+3", [-1, 3]],
11 ["-n+2", [-1, 2]],
12 ["-n+3", [-1, 3]],
13 ["0n+3", [0, 3]],
14 ["1n", [1, 0]],
15 ["1n+0", [1, 0]],
16 ["2n", [2, 0]],
17 ["2n + 1", [2, 1]],
18 ["2n+1", [2, 1]],
19 ["3n", [3, 0]],
20 ["3n+0", [3, 0]],
21 ["3n+1", [3, 1]],
22 ["3n+2", [3, 2]],
23 ["3n+3", [3, 3]],
24 ["3n-1", [3, -1]],
25 ["3n-2", [3, -2]],
26 ["3n-3", [3, -3]],
27 ["even", [2, 0]],
28 ["n", [1, 0]],
29 ["n+2", [1, 2]],
30 ["odd", [2, 1]],
31
32 // Surprisingly, neither sizzle, qwery or nwmatcher cover these cases
33 ["-4n+13", [-4, 13]],
34 ["-2n + 12", [-2, 12]],
35 ];
36
37 const invalid = [
38 "-",
39 "- 1n",
40 "-1 n",
41 "2+0",
42 "2n+-0",
43 "an+b",
44 "asdf",
45 "b",
46 "expr",
47 "odd|even|x",
48 ];
49
50 module.exports = {valid, invalid};
0 const expect = require('expect.js');
1 const nthCheck = require('../../../lib');
2 const compile = nthCheck.compile;
3 const { valid } = require("./__fixtures__/rules");
4
5 const valArray = new Array(...Array(2e3)).map((_, i) => i);
6
7 /**
8 * Iterate through all possible values. This is adapted from qwery,
9 * and uses a more intuitive way to process all elements.
10 */
11 function slowNth([a, b]) {
12 if (a === 0 && b > 0) return [b - 1];
13
14 return valArray.filter((val) => {
15 for (let i = b; a > 0 ? i <= valArray.length : i >= 1; i += a) {
16 if (val === valArray[i - 1]) return true;
17 }
18 return false;
19 });
20 }
21
22 describe("parse", () => {
23 it("compile & run all valid", () => {
24 for (const [_, parsed] of valid) {
25 const filtered = valArray.filter(compile(parsed));
26 const iterated = slowNth(parsed);
27
28 expect(filtered).to.eql(iterated);
29 }
30 });
31
32 it("parse, compile & run all valid", () => {
33 for (const [rule, parsed] of valid) {
34 const filtered = valArray.filter(nthCheck.default(rule));
35 const iterated = slowNth(parsed);
36
37 expect([filtered, rule]).to.eql([iterated, rule]);
38 }
39 });
40 });
0 const expect = require('expect.js');
1 const { parse } = require("../../../lib/parse");
2 const { valid, invalid } = require("./__fixtures__/rules");
3
4 describe("parse", () => {
5 it("parse invalid", () => {
6 for (const formula of invalid) {
7 expect(() => parse(formula)).to.throwError(Error);
8 }
9 });
10
11 it("parse valid", () => {
12 for (const [formula, result] of valid) {
13 expect(parse(formula)).to.eql(result);
14 }
15 });
16 });