Skip to content
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
40 changes: 23 additions & 17 deletions src/compile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,31 @@ function go(data: unknown, query: JSONQuery, options?: JSONQueryCompileOptions)
}

const groupByCategory = compile(['groupBy', ['get', 'category']])
const testsByCategory = groupByCategory(suite.tests) as Record<string, CompileTestSuite['tests']>
const testsByCategory = groupByCategory(suite.groups) as Record<string, CompileTestSuite['groups']>

for (const [category, tests] of Object.entries(testsByCategory)) {
for (const [category, testGroups] of Object.entries(testsByCategory)) {
describe(category, () => {
for (const currentTest of tests) {
if (isTestException(currentTest)) {
test(currentTest.description, () => {
const { input, query, throws } = currentTest

expect(() => compile(query)(input)).toThrow(throws)
})
} else {
test(currentTest.description, () => {
const { input, query, output } = currentTest
const actualOutput = compile(query)(input)

expect({ input, query, output: actualOutput }).toEqual({ input, query, output })
})
}
for (const group of testGroups) {
describe(group.description, () => {
for (const currentTest of group.tests) {
const description = `input = '${currentTest.input}'`

if (isTestException(currentTest)) {
test(description, () => {
const { input, query, throws } = currentTest

expect(() => compile(query)(input)).toThrow(throws)
})
} else {
test(description, () => {
const { input, query, output } = currentTest
const actualOutput = compile(query)(input)

expect({ input, query, output: actualOutput }).toEqual({ input, query, output })
})
}
}
})
}
})
}
Expand Down
12 changes: 6 additions & 6 deletions test-suite/compile.test.d.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import type { JSONQuery } from '../src/types'

export interface CompileTestOutput {
category: string
description: string
input: unknown
query: JSONQuery
output: unknown
}

export interface CompileTestException {
category: string
description: string
input: unknown
query: JSONQuery
throws: string
}

export type CompileTest = CompileTestOutput | CompileTestException
export interface CompileTestGroup {
category: string
description: string
tests: Array<CompileTestOutput | CompileTestException>
}

export interface CompileTestSuite {
updated: string
tests: CompileTest[]
groups: CompileTestGroup[]
}
Loading