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

Improve test diffs

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