-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
35a8235
commit 2925e6b
Showing
5 changed files
with
117 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
const _ = require('lodash') | ||
|
||
const createComparableLines = text => text | ||
.replace(/\r/g, '¶') | ||
.replace(/\t/g, '→') | ||
.replace(/^\s+/gm, spaces => _.repeat('·', spaces.length)) | ||
.split('\n') | ||
|
||
function compareContent(actualContent, expectContent) { | ||
if (actualContent === expectContent) { | ||
return | ||
} | ||
|
||
const resultLines = createComparableLines(actualContent) | ||
const expectLines = createComparableLines(expectContent) | ||
|
||
let lineIndex = -1 | ||
while (++lineIndex < Math.min(resultLines.length, expectLines.length)) { | ||
if (resultLines[lineIndex] !== expectLines[lineIndex]) { | ||
let markers = '' | ||
let charIndex = -1 | ||
const charLimit = Math.max(resultLines[lineIndex].length, expectLines[lineIndex].length) | ||
while (++charIndex < charLimit) { | ||
if (resultLines[lineIndex][charIndex] !== expectLines[lineIndex][charIndex]) { | ||
markers += '^' | ||
} else { | ||
markers += ' ' | ||
} | ||
} | ||
|
||
return [ | ||
'The first mismatched was at line ' + (lineIndex + 1) + '.', | ||
' Actual: ' + resultLines[lineIndex], | ||
' Expect: ' + expectLines[lineIndex], | ||
' ' + markers | ||
].join('\n') | ||
} | ||
} | ||
|
||
return 'It was not clear to show the difference.' | ||
} | ||
|
||
module.exports = compareContent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters