forked from nanoporetech/jmespath-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
56 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = []; | ||
|
@@ -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); | ||
} | ||
} | ||
|
@@ -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 { | ||
|
@@ -42,3 +42,8 @@ function addTestSuitesFromFile(filename) { | |
} | ||
}); | ||
} | ||
|
||
// wrapper to reduce code impact from rehighlighting | ||
function titleFunc({comment}) { | ||
return `should pass case` + (comment ? ` '${comment}'` : ''); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters