Skip to content

Commit

Permalink
test: Shim/port to Deno.test
Browse files Browse the repository at this point in the history
  • Loading branch information
danopia committed Oct 3, 2020
1 parent 228868f commit 56a85da
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 13 deletions.
27 changes: 16 additions & 11 deletions test/compliance.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* eslint-disable @typescript-eslint/no-unsafe-call */
import { readdirSync, statSync, readFileSync } from 'fs';
import { basename } from 'path';
import { search } from '../src';
import { search } from '../src/index.ts';

import { basename, join } from 'https://deno.land/[email protected]/path/mod.ts';
import { describe, it, expect, each } from './deno-shim.js';

// Compliance tests that aren't supported yet.
const notImplementedYet = [];
Expand All @@ -10,10 +11,10 @@ function endsWith(str, suffix) {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
}

const listing = readdirSync('test/compliance');
for (let i = 0; i < listing.length; i++) {
const filename = 'test/compliance/' + listing[i];
if (statSync(filename).isFile() && endsWith(filename, '.json') && !notImplementedYet.includes(basename(filename))) {
const listing = Deno.readDirSync(join('test', 'compliance'));
for (const entry of listing) {
const filename = join('test', 'compliance', entry.name);
if (entry.isFile && endsWith(filename, '.json') && !notImplementedYet.includes(basename(filename))) {
addTestSuitesFromFile(filename);
}
}
Expand All @@ -24,14 +25,13 @@ for (let i = 0; i < listing.length; i++) {
*/
function addTestSuitesFromFile(filename) {
describe(filename, () => {
const spec = JSON.parse(readFileSync(filename, 'utf-8'));
const spec = JSON.parse(Deno.readTextFileSync(filename));
for (let i = 0; i < spec.length; i++) {
const msg = `suite ${i} for filename ${filename}`;
const msg = `suite ${i}`;
describe(msg, () => {
const given = spec[i].given;
const cases = spec[i].cases.map(c => [c.expression, c.result, c.error]);

test.each(cases)('should pass test %# %s', (expression, result, error) => {
each(spec[i].cases, titleFunc, ({expression, result, error}) => {
if (error !== undefined) {
expect(() => search(given, expression)).toThrow(error);
} else {
Expand All @@ -42,3 +42,8 @@ function addTestSuitesFromFile(filename) {
}
});
}

// wrapper to reduce code impact from rehighlighting
function titleFunc({comment}) {
return `should pass case` + (comment ? ` '${comment}'` : '');
}
36 changes: 36 additions & 0 deletions test/deno-shim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {
assertEquals, assertStrictEquals,
assertThrows, assertStringContains,
} from "https://deno.land/[email protected]/testing/asserts.ts";

const suiteStack = [];

// shim the test framework API expected by this file to use Deno.test
export function describe(label, func) {
suiteStack.push(label);
func();
suiteStack.pop();
}

export function it(label, func) {
Deno.test([...suiteStack, label].join(' / '), func);
}

export function expect(actual) {
return {
toMatchObject(expected) { assertEquals(actual, expected) },
toEqual(expected) { assertEquals(actual, expected) },
toStrictEqual(expected) { assertStrictEquals(actual, expected) },
toThrow(message) { assertThrows(actual, Error, message) },
toContain(slice) { assertStringContains(actual, slice) },
not: {
toThrow() { actual() }, // the test fails if anything throws anyway...
},
};
}

export function each(cases, labelFunc, testFunc) {
for (const testCase of cases) {
it(labelFunc(testCase), () => testFunc(testCase));
}
}
6 changes: 4 additions & 2 deletions test/jmespath.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import jmespath, { search, tokenize, compile, registerFunction, TreeInterpreter } from '../src';
import { strictDeepEqual } from '../src/utils';
import jmespath, { search, tokenize, compile, registerFunction, TreeInterpreter } from '../src/index.ts';
import { strictDeepEqual } from '../src/utils/index.ts';

import { describe, it, expect } from './deno-shim.js';

describe('tokenize', () => {
it('should tokenize unquoted identifier', () => {
Expand Down

0 comments on commit 56a85da

Please sign in to comment.