Skip to content

Commit

Permalink
Move test output to separate files (fix #55)
Browse files Browse the repository at this point in the history
  • Loading branch information
rrthomas committed Jul 23, 2024
1 parent a8fe3eb commit 64f77b7
Show file tree
Hide file tree
Showing 32 changed files with 347 additions and 325 deletions.
1 change: 1 addition & 0 deletions rosettacode/Accumulator factory.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
8.3
3 changes: 3 additions & 0 deletions rosettacode/Ackermann function.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
1
125
13
6 changes: 6 additions & 0 deletions rosettacode/Anagrams.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[ 'abel', 'able', 'bale', 'bela', 'elba' ]
[ 'alger', 'glare', 'lager', 'large', 'regal' ]
[ 'angel', 'angle', 'galen', 'glean', 'lange' ]
[ 'caret', 'carte', 'cater', 'crate', 'trace' ]
[ 'elan', 'lane', 'lean', 'lena', 'neal' ]
[ 'evil', 'levi', 'live', 'veil', 'vile' ]
100 changes: 100 additions & 0 deletions rosettacode/FizzBuzz.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
Fizz
22
23
Fizz
Buzz
26
Fizz
28
29
FizzBuzz
31
32
Fizz
34
Buzz
Fizz
37
38
Fizz
Buzz
41
Fizz
43
44
FizzBuzz
46
47
Fizz
49
Buzz
Fizz
52
53
Fizz
Buzz
56
Fizz
58
59
FizzBuzz
61
62
Fizz
64
Buzz
Fizz
67
68
Fizz
Buzz
71
Fizz
73
74
FizzBuzz
76
77
Fizz
79
Buzz
Fizz
82
83
Fizz
Buzz
86
Fizz
88
89
FizzBuzz
91
92
Fizz
94
Buzz
Fizz
97
98
Fizz
Buzz
10 changes: 10 additions & 0 deletions rosettacode/Generator-Exponential.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
529
576
625
676
784
841
900
961
1024
1089
6 changes: 6 additions & 0 deletions rosettacode/Hailstone sequence.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
The Hailstone sequence for 27 starts with:
[ 27, 82, 41, 124 ]
and ends with:
[ 8, 4, 2, 1 ]
and its length is:
112
1 change: 1 addition & 0 deletions rosettacode/Hello world-Text.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello woods!
2 changes: 2 additions & 0 deletions rosettacode/Loops-Continue.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1, 2, 3, 4, 5
6, 7, 8, 9, 10
62 changes: 39 additions & 23 deletions src/testutil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ import test, {ExecutionContext, Macro} from 'ava'
import {ExecaError, Options as ExecaOptions, execa} from 'execa'
import {compareSync, Difference} from 'dir-compare'

import {flatToJs, evalArkJs} from './ark/compiler/index.js'
import {expToInsts} from './ark/flatten.js'
import {
debug, ArkState, ArkExp, ArkObject, toJs,
} from './ark/interpreter.js'
import {compile as doArkCompile} from './ark/reader.js'
import {valToJs} from './ark/serialize.js'
import {compile as ursaCompile} from './ursa/compiler.js'
import {format} from './ursa/fmt.js'
import {flatToJs, evalArkJs} from './ark/compiler/index.js'
import {expToInsts} from './ark/flatten.js'
import version from './version.js'

const command = process.env.NODE_ENV === 'coverage' ? './bin/test-run.sh' : './bin/run.js'

Expand Down Expand Up @@ -91,13 +92,21 @@ async function doCliTest(
inputBasename: string,
realSourceBasename?: string,
extraArgs?: string[],
expectedStdout?: string,
expectedStderr?: string,
useRepl?: boolean,
target: string = 'ark',
) {
const resultJsonFilename = `${inputBasename}.result.json`
const actualSourceBasename = realSourceBasename ?? inputBasename
const resultJsonFilename = `${actualSourceBasename}.result.json`
const stdoutFilename = `${actualSourceBasename}.stdout`
let expectedStdout
if (fs.existsSync(stdoutFilename)) {
expectedStdout = fs.readFileSync(stdoutFilename, {encoding: 'utf-8'})
}
const stderrFilename = `${actualSourceBasename}.stderr`
let expectedStderr
if (fs.existsSync(stderrFilename)) {
expectedStderr = fs.readFileSync(stderrFilename, {encoding: 'utf-8'})
}
const inputFile = `${actualSourceBasename}.${syntax}`
const args = [`--syntax=${syntax}`, `--target=${target}`]
let tempFile: tmp.FileResult
Expand All @@ -111,7 +120,10 @@ async function doCliTest(
[...args, ...extraArgs ?? []],
{inputFile: useRepl ? inputFile : undefined},
)
if (!useRepl) {
let processedStdout = stdout
if (useRepl) {
processedStdout = processedStdout?.slice(`Welcome to Ursa ${version}.\n`.length)
} else {
const result: unknown = JSON.parse(fs.readFileSync(tempFile!.name, {encoding: 'utf-8'}))
const expected: unknown = fs.existsSync(resultJsonFilename)
? JSON.parse(fs.readFileSync(resultJsonFilename, {encoding: 'utf-8'}))
Expand All @@ -126,7 +138,7 @@ async function doCliTest(
t.deepEqual(valToJs(compiled), JSON.parse(source))
}
if (expectedStdout !== undefined) {
t.is(stdout, expectedStdout)
t.is(processedStdout, expectedStdout)
}
if (expectedStderr !== undefined) {
t.is(deleteErrorExtent(stderr!.toString()), deleteErrorExtent(expectedStderr))
Expand All @@ -137,8 +149,12 @@ async function doCliTest(
deleteErrorExtent(((error as ExecaError).stderr as string).slice('run.js: '.length)),
deleteErrorExtent(expectedStderr),
)
let processedStdout = (error as ExecaError).stdout
if (useRepl) {
processedStdout = processedStdout?.slice(`Welcome to Ursa ${version}.\n`.length)
}
if (expectedStdout !== undefined) {
t.is((error as ExecaError).stdout, expectedStdout)
t.is(processedStdout, expectedStdout)
}
} else {
throw error
Expand Down Expand Up @@ -186,12 +202,26 @@ function makeReformattedSource(t: ExecutionContext, inputBasename: string) {
const tempDir = tmp.dirSync({unsafeCleanup: true})
t.teardown(tempDir.removeCallback)
// Copy optional related files into temporary directory.
for (const extraExt of ['.result.json']) {
const extraExts = ['.result.json', '.stdout']
for (const extraExt of extraExts) {
const extraFile = `${inputBasename}${extraExt}`
if (fs.existsSync(extraFile)) {
fs.copyFileSync(extraFile, path.join(tempDir.name, path.parse(extraFile).base))
}
}
const stderrFile = `${inputBasename}.stderr`
const reformattedStderrFile = `${inputBasename}.reformatted-stderr`
if (fs.existsSync(reformattedStderrFile)) {
fs.copyFileSync(
reformattedStderrFile,
path.join(tempDir.name, `${path.parse(reformattedStderrFile).name}.stderr`),
)
} else if (fs.existsSync(stderrFile)) {
fs.copyFileSync(
stderrFile,
path.join(tempDir.name, `${path.parse(stderrFile).name}.stderr`),
)
}
const tempSourceFile = path.join(tempDir.name, path.basename(sourceFile))
fs.writeFileSync(tempSourceFile, reformattedSource)
const tempSourcePath = path.parse(tempSourceFile)
Expand All @@ -203,10 +233,7 @@ const reformattingCliTest = test.macro(async (
t: ExecutionContext,
inputBasename: string,
extraArgs?: string[],
expectedStdout?: string,
expectedStderr?: string,
useRepl?: boolean,
expectedReformattedStderr?: string,
syntaxErrorExpected?: boolean,
) => {
for (const target of arkTargets) {
Expand All @@ -216,8 +243,6 @@ const reformattingCliTest = test.macro(async (
inputBasename,
undefined,
extraArgs,
expectedStdout,
expectedStderr,
useRepl,
target,
)
Expand All @@ -229,8 +254,6 @@ const reformattingCliTest = test.macro(async (
inputBasename,
makeReformattedSource(t, inputBasename),
extraArgs,
expectedStdout,
expectedReformattedStderr ?? expectedStderr,
useRepl,
)
}
Expand All @@ -241,10 +264,7 @@ const reformattingCliDirTest = test.macro(async (
inputBasename: string,
expectedDirPath: string,
extraArgs?: string[],
expectedStdout?: string,
expectedStderr?: string,
useRepl?: boolean,
expectedReformattedStderr?: string,
syntaxErrorExpected?: boolean,
) => {
for (const target of arkTargets) {
Expand All @@ -258,8 +278,6 @@ const reformattingCliDirTest = test.macro(async (
inputBasename,
undefined,
[tmpDirPath, ...extraArgs ?? []],
expectedStdout,
expectedStderr,
useRepl,
target,
)
Expand All @@ -277,8 +295,6 @@ const reformattingCliDirTest = test.macro(async (
inputBasename,
makeReformattedSource(t, inputBasename),
[tmpDirPath, ...extraArgs ?? []],
expectedStdout,
expectedReformattedStderr ?? expectedStderr,
useRepl,
)
),
Expand Down
31 changes: 3 additions & 28 deletions src/ursa/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,8 @@
// © Reuben Thomas 2023-2024
// Released under the GPL version 3, or (at your option) any later version.

import version from '../version.js'

import {ursaTest} from '../testutil.js'

ursaTest('Evaluate a number', 'test/repl-number', [], `\
Welcome to Ursa ${version}.
> 4
> `, undefined, true)

ursaTest('Test let followed by reference', 'test/repl-let-val', [], `\
Welcome to Ursa ${version}.
> null
> 3
> `, undefined, true)

ursaTest(
'Test syntax error',
'test/repl-syntax-error',
[],
`\
Welcome to Ursa ${version}.
> > `,
`Line 1, col 4:
> 1 | 4 +
^
Expected "(", "gen", "fn", "{", "[", "_", a letter, a digit, ".", "r####\\"", "r###\\"", "r##\\"", "r#\\"", "r\\"", "\\"", "true", "false", "null", "-", "+", or "~"`,
true,
undefined,
true,
)
ursaTest('Evaluate a number', 'test/repl-number', [], true)
ursaTest('Test let followed by reference', 'test/repl-let-val', [], true)
ursaTest('Test syntax error', 'test/repl-syntax-error', [], true, true)
Loading

0 comments on commit 64f77b7

Please sign in to comment.