Commit ba82bc6c authored by Caleb C. Sander's avatar Caleb C. Sander
Browse files

Improve test diffs

parent 64448884
Showing with 22 additions and 19 deletions
+22 -19
......@@ -6,7 +6,5 @@ The spec for this project can be found at [grep.md](https://gitlab.caltech.edu/c
## Testing infrastructure
Your submission will be run against the tests in the `tests` directory.
If your submission passes all the tests and you have not intentionally written your program to defeat the test cases, you will receive full credit on the project.
To run the tests, you need to run `npm install` once and then `npm test` each time you want to run the tests.
Several tests are provided in the `tests` directory.
To run them, you need to run `npm install` once and then `npm test` each time you want to run the tests.
const fs = require('fs')
const test = require('ava')
const {execFilePromise, matchOutput, reorderings, toLines} = require('./util')
const {execFilePromise, matchOutput, reorderings} = require('./util')
process.chdir(__dirname)
......@@ -558,7 +558,7 @@ for (const {args, expected} of TESTS) {
for (const args of reorderings(parameters, flags)) {
test(`grep ${args.join(' ')}`, async t => {
const {stdout} = await execFilePromise('node', ['../grep.js', ...args])
matchOutput(t, toLines(stdout), expected)
matchOutput(t, stdout, expected)
})
}
}
......@@ -569,8 +569,8 @@ test('grep no matches', async t => {
try {
({stdout} = await execFilePromise('node', ['../grep.js', 'no-such-string', 'files/independence.txt']))
}
catch (e) { stdout = e.stdout }
t.deepEqual(stdout, '')
catch (e) { ({stdout} = e) }
matchOutput(t, stdout, [])
})
// Test grepping the standard input
......@@ -581,7 +581,7 @@ test('grep stdin', async t => {
.pipe(grepPromise.child.stdin)
.on('error', _ => t.fail('grep.js did not read stdin'))
const {stdout} = await grepPromise
t.deepEqual(toLines(stdout), [
matchOutput(t, stdout, [[
'# The Rust Programming Language',
'[The Rust Programming Language](title-page.md)',
'## Basic Rust Literacy',
......@@ -589,7 +589,7 @@ test('grep stdin', async t => {
'- [Object Oriented Programming Features of Rust](ch17-00-oop.md)',
' - [Unsafe Rust](ch19-01-unsafe-rust.md)',
' - [G - How Rust is Made and “Nightly Rust”](appendix-07-nightly-rust.md)'
])
]])
})
// Ensure that the streams deal with lines spread across chunks correctly
......@@ -598,6 +598,7 @@ test('multiple chunk input file', async t => {
for (let i = 0; i < 10; i++) {
const search = `${i}`
const {stdout} = await execFilePromise('node', ['../grep.js', search, 'files/long.txt'])
t.deepEqual(toLines(stdout), lines.filter(line => line.includes(search)))
const expectedLines = lines.filter(line => line.includes(search))
t.deepEqual(stdout, expectedLines.map(line => `${line}\n`).join(''))
}
})
......@@ -3,12 +3,6 @@ const {promisify} = require('util')
exports.execFilePromise = promisify(execFile)
exports.toLines = text => {
const lines = text.split('\n')
if (lines.length && !lines[lines.length - 1].length) lines.pop()
return lines
}
function* reorderings(fixed, reorderable) {
if (!reorderable.length) {
yield fixed
......@@ -22,6 +16,12 @@ function* reorderings(fixed, reorderable) {
}
exports.reorderings = reorderings
function toLines(text) {
const lines = text.split('\n')
if (lines.length && !lines[lines.length - 1]) lines.pop()
return lines
}
/**
* Checks that grep.js's output matches an array of expected outputs for each file.
* Each file's lines must appear in order,
......@@ -42,7 +42,7 @@ exports.matchOutput = (t, actual, expected) => {
for (const expectedLines of expected) addNextLine(new Set(expectedLines))
const expectedOutput = []
for (const line of actual) {
for (const line of toLines(actual)) {
const expectedRest = nextLines.get(line)
// If this was not the next line in any file, skip it
if (!expectedRest) continue
......@@ -57,5 +57,9 @@ exports.matchOutput = (t, actual, expected) => {
for (const [firstLine, remainingLines] of nextLines) {
expectedOutput.push(firstLine, ...remainingLines)
}
t.deepEqual(actual, expectedOutput, 'Unexpected grep.js output')
t.deepEqual(
actual,
expectedOutput.map(line => `${line}\n`).join(''),
'Unexpected grep.js output'
)
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment