Skip to content

Commit

Permalink
update single-line diff
Browse files Browse the repository at this point in the history
  • Loading branch information
tbeseda committed Oct 27, 2023
1 parent 288fd65 commit 784fd97
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
Binary file modified screen-shot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 10 additions & 5 deletions src/_make-diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,20 @@ export default function ({ actual, expected }){
*/
function diffLine (a, e) {
const diff = Diff.diffWordsWithSpace(a, e)
const output = []

let aLine = 'Actual: '
let eLine = 'Expected: '

for (const d of diff) {
if (d.added) output.push(`${expected(d.value)}`)
else if (d.removed) output.push(`${actual(d.value)}`)
else output.push(d.value)
if (d.added) eLine += expected(d.value)
else if (d.removed) aLine += actual(d.value)
else {
eLine += d.value
aLine += d.value
}
}

return output.join('').split('\n')
return [ aLine, eLine ]
}

/**
Expand Down
17 changes: 12 additions & 5 deletions test/formatting-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Readable } from 'stream'
import stripAnsi from 'strip-ansi'
import test from 'tape'
import tapArc from '../src/tap-arc.js'

Expand Down Expand Up @@ -122,7 +123,8 @@ const TAP = {
'\x1B[2m \x1B[22m\x1B[2m \x1B[22m\x1B[2m \x1B[22m\x1B[2m \x1B[22mExpected \x1B[33m666\x1B[39m but got \x1B[34m66\x1B[39m',
'\x1B[2m \x1B[22m\x1B[2m \x1B[22m\x1B[2m \x1B[22m\x1B[2m \x1B[22m\x1B[2mAt: Test.<anonymous> (/test/mock/create-simple-tap.cjs:27:5)\x1B[22m',
'\x1B[2m \x1B[22m\x1B[2m \x1B[22m\x1B[1m\x1B[31m✗\x1B[39m\x1B[22m [9] \x1B[31mshould be strictly equal\x1B[39m',
'\x1B[2m \x1B[22m\x1B[2m \x1B[22m\x1B[2m \x1B[22m\x1B[2m \x1B[22m\x1B[34mBad\x1B[39m\x1B[33mGood\x1B[39m dog',
'\x1B[2m \x1B[22m\x1B[2m \x1B[22m\x1B[2m \x1B[22m\x1B[2m \x1B[22mActual: \x1B[34mBad\x1B[39m dog',
'\x1B[2m \x1B[22m\x1B[2m \x1B[22m\x1B[2m \x1B[22m\x1B[2m \x1B[22mExpected: \x1B[33mGood\x1B[39m dog',
'\x1B[2m \x1B[22m\x1B[2m \x1B[22m\x1B[2m \x1B[22m\x1B[2m \x1B[22m\x1B[2mAt: Test.<anonymous> (/test/mock/create-simple-tap.cjs:28:5)\x1B[22m',
'\x1B[2m \x1B[22m\x1B[2m \x1B[22m\x1B[1m\x1B[31m✗\x1B[39m\x1B[22m [10] \x1B[31mRegex: match fail\x1B[39m',
'\x1B[2m \x1B[22m\x1B[2m \x1B[22m\x1B[2m \x1B[22m\x1B[2m \x1B[22mExpected "\x1B[34matreides\x1B[39m" to match \x1B[33m/^A/\x1B[39m',
Expand Down Expand Up @@ -188,12 +190,17 @@ test('basic output formatting', (t) => {
input.push(null)

parser.on('end', () => {
const actual = chunks.trim().split('\n').slice(0, -1).filter(l => l.length > 0) // remove timer
t.plan(2)

// console.log({ actual, expected: TAP.OUT })
console.log(actual.join('\n') )
const actual = chunks.trim().split('\n').slice(0, -1).filter(l => l.length > 0) // remove timer

t.deepEqual(actual, TAP.OUT, 'should print expected output')
t.end()

// this test helps visualize when output differs without the ANSI codes
const actualNoAnsi = actual.map(stripAnsi)
const expectedNoAnsi = TAP.OUT.map(stripAnsi)
// console.log('=========\n', actualNoAnsi.join('\n'), '\n=========')

t.deepEqual(actualNoAnsi, expectedNoAnsi, 'should print expected output without ANSI codes')
})
})

0 comments on commit 784fd97

Please sign in to comment.