Start working on tests.

This commit is contained in:
Joshua Bemenderfer
2023-02-07 13:33:23 -05:00
parent ea6eb7bd94
commit ac821e448d
29 changed files with 154 additions and 121 deletions

View File

@@ -2,7 +2,7 @@
"name": "@terrace/test",
"type": "module",
"scripts": {
"test": "NODE_OPTIONS=--experimental-vm-modules jest"
"test": "NODE_OPTIONS=--experimental-vm-modules NODE_NO_WARNINGS=1 jest"
},
"dependencies": {
"@terrace/core": "workspace:*"

View File

@@ -1,43 +1,83 @@
import { expect } from 'chai'
import fs from 'node:fs/promises'
import { execSync } from 'node:child_process'
import { useDocument } from '@terrace/core'
import { createReadlineReader } from '@terrace/core/readers/node-readline'
import { createFileReader } from '@terrace/core/readers/node-readline'
async function loadTests(path) {
const { next, level, head, tail, line, match } = useDocument(createReadlineReader(path))
const { next, level, head, tail, line, match } = useDocument(createFileReader(path))
const tests = {}
const descriptions = {}
while (await next()) {
if (!head() || match('#schema')) continue
const tests = descriptions[tail()] = []
const rootLevel = level()
const test = tests[tail().trim()] = { input: [], output: [] }
while (await next(rootLevel)) {
const testLevel = level()
if (match('input')) {
while (await next(testLevel)) {
test.input.push(line(testLevel))
}
}
if (!match('it')) continue
if (match('output')) {
while (await next(testLevel)) {
test.output.push(line(testLevel))
const test = { it: tail(), packages: [], key: '', input: [], output: [] }
tests.push(test)
const testLevel = level()
while (await next(testLevel)) {
if (match('key')) test.key = tail()
if (match('packages')) test.packages = tail().split(' ')
const propertyLevel = level()
if (match('input')) {
// TODO: Find a way to handle newlines better.
if (tail().startsWith('literal')) {
test.input = tail()
.split('literal ').join('')
.replaceAll('\\n', '\n')
.replaceAll('\\t', '\t')
continue
}
while (await next(propertyLevel)) test.input.push(line(propertyLevel))
test.input = test.input.join('\n').trim()
}
if (match('output')) {
// TODO: Find a way to handle newlines better.
if (tail().startsWith('literal')) {
test.output = tail().split('literal ').join('').replace('\\n', '\n')
continue
}
while (await next(propertyLevel)) test.output.push(line())
}
}
test.output = test.output.join('\n').trim()
}
}
return tests
return descriptions
}
it('Runs a basic test', async () => {
const tests = await loadTests('./tests.tce', 'utf-8')
function callTest(pkg, name, input) {
return new Promise((resolve, reject) => {
const stdout = execSync(`npm run --silent test ${name}`, {
cwd: `../packages/${pkg}`,
input
})
console.log(tests)
resolve(stdout.toString().trim())
})
}
expect(1).to.equal(1)
})
const descriptions = await loadTests('./tests.tce')
for (const [name, tests] of Object.entries(descriptions)) {
describe(name, () => {
for (const test of tests) {
test.packages.forEach(pkg => {
it(`${pkg}: ${test.it}`, async () => {
expect(await callTest(pkg, test.key, test.input)).to.equal(test.output)
})
})
}
})
}

View File

@@ -1,8 +1,54 @@
#schema test
test Basic
input
key value
describe LineData
it Handles a blank line at indent level 0
key linedata:basic
packages js
input literal \n
output
level: 0 | head: | tail: | line:
output
level: 0 | head: key | tail: value | line: key value
it Handles a blank line with a single space at indent level 1
key linedata:basic
packages js
input literal \n
output
level: 1 | head: | tail: | line:
it Handles a blank line with two spaces
key linedata:basic
packages js
input literal \n
output
level: 2 | head: | tail: | line:
it Handles a normal line at indent level 1
key linedata:basic
packages js
input literal line 1
output
level: 1 | head: line | tail: 1 | line: line 1
it Handles a normal line at indent level 1 indented with tabs
key linedata:tabs
packages js
input literal \tline 1
output
level: 1 | head: line | tail: 1 | line: line 1
it Handles a normal line at indent level 2 indented with tabs
key linedata:tabs
packages js
input literal \t\tline 1
output
level: 2 | head: line | tail: 1 | line: line 1
it Nests a normal line under a preceding normal line
key linedata:basic
packages js
input
line 1
line 2
output
level: 0 | head: line | tail: 1 | line: line 1
level: 1 | head: line | tail: 2 | line: line 2