From c41990b793b7c24f2075672f5c545bccdfbf0605 Mon Sep 17 00:00:00 2001 From: Joshua Bemenderfer Date: Fri, 10 Feb 2023 13:07:10 -0500 Subject: [PATCH] All JS and Python tests passing. --- packages/python/test/index.py | 15 ++++++++---- test/{test-runner.test.js => helpers.js} | 30 ++++++++++++------------ test/lineData.test.js | 3 +++ test/{tests.tce => lineData.test.tce} | 0 4 files changed, 28 insertions(+), 20 deletions(-) rename test/{test-runner.test.js => helpers.js} (83%) create mode 100644 test/lineData.test.js rename test/{tests.tce => lineData.test.tce} (100%) diff --git a/packages/python/test/index.py b/packages/python/test/index.py index 4e49e18..d0f738c 100644 --- a/packages/python/test/index.py +++ b/packages/python/test/index.py @@ -6,12 +6,17 @@ sys.path.insert(1, os.path.join(sys.path[0], '..')) from parser import createLineData, parseLine def next(): - return sys.stdin.readline().rstrip('\n') + # For blank lines, readline will return a newline. + # For no result, readline will return a string of length 0. + line = sys.stdin.readline() + # Since we strip trailing newlines resulting in an empty string for empty lines, + # return None if there actually is no result. + return line.rstrip('\n') if len(line) > 0 else None def linedata_basic (): lineData = createLineData('') - while l := next(): + while (l := next()) != None: lineData['line'] = l parseLine(lineData) print("| level {level} | indent {indent} | offsetHead {offsetHead} | offsetTail {offsetTail} | line {line} |".format( @@ -21,7 +26,7 @@ def linedata_basic (): def linedata_tabs (): lineData = createLineData('', '\t') - while l := next(): + while (l := next()) != None: lineData['line'] = l parseLine(lineData) print("| level {level} | indent {indent} | offsetHead {offsetHead} | offsetTail {offsetTail} | line {line} |".format( @@ -31,10 +36,10 @@ def linedata_tabs (): def linedata_head_tail (): lineData = createLineData('') - while l := next(): + while (l := next()) != None: lineData['line'] = l parseLine(lineData) - head = lineData['line'][lineData['offsetHead']:lineData['offsetTail']] if len(lineData['line']) > lineData['offsetTail'] else '' + head = lineData['line'][lineData['offsetHead']:lineData['offsetTail']] if len(lineData['line']) > lineData['offsetHead'] else '' tail = lineData['line'][lineData['offsetTail'] + 1:] if len(lineData['line']) > lineData['offsetTail'] + 1 else '' print("| head {head} | tail {tail} |".format( diff --git a/test/test-runner.test.js b/test/helpers.js similarity index 83% rename from test/test-runner.test.js rename to test/helpers.js index 9cea699..eb70084 100644 --- a/test/test-runner.test.js +++ b/test/helpers.js @@ -4,7 +4,7 @@ import { execSync } from 'node:child_process' import { useDocument } from '@terrace/core' import { createFileReader } from '@terrace/core/readers/node-readline' -async function loadTests(path) { +export async function loadTestMap(path) { const { next, level, head, tail, line, match } = useDocument(createFileReader(path)) const descriptions = {} @@ -58,6 +58,20 @@ async function loadTests(path) { return descriptions } +export function defineTests(testMap) { + for (const [name, tests] of Object.entries(testMap)) { + 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) + }) + }) + } + }) + } +} + function callTest(pkg, name, input) { return new Promise((resolve, reject) => { const stdout = execSync(`npm run --silent test ${name}`, { @@ -69,17 +83,3 @@ function callTest(pkg, name, input) { resolve(stdout.toString().trim()) }) } - -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) - }) - }) - } - }) -} diff --git a/test/lineData.test.js b/test/lineData.test.js new file mode 100644 index 0000000..5215180 --- /dev/null +++ b/test/lineData.test.js @@ -0,0 +1,3 @@ +import { loadTestMap, defineTests } from './helpers.js' + +defineTests(await loadTestMap('./lineData.test.tce')) diff --git a/test/tests.tce b/test/lineData.test.tce similarity index 100% rename from test/tests.tce rename to test/lineData.test.tce