From 0f9d939b00324ab3dd8601ed54b33e6b46c4a109 Mon Sep 17 00:00:00 2001 From: Gabriel Wicke Date: Tue, 5 Jun 2012 16:10:13 +0200 Subject: [PATCH] Use word diff if --color is enabled Change-Id: Ib8d3de75ac306974abfdaca22bfc7b69bc62891d --- tests/parser/parserTests.js | 71 ++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 36 deletions(-) diff --git a/tests/parser/parserTests.js b/tests/parser/parserTests.js index b61fafbb49..cd999ab009 100644 --- a/tests/parser/parserTests.js +++ b/tests/parser/parserTests.js @@ -418,6 +418,39 @@ ParserTests.prototype.processResult = function ( index, item, doc ) { process.nextTick( this.processCase.bind( this, index + 1 ) ); }; +ParserTests.prototype.diff = function ( a, b ) { + if ( this.argv.color ) { + return jsDiff.diffWords( a, b ).map( function ( change ) { + if ( change.added ) { + return change.value.green; + } else if ( change.removed ) { + return change.value.red; + } else { + return change.value; + } + }).join(''); + } else { + var patch = jsDiff.createPatch('wikitext.txt', a, b, 'before', 'after'); + + console.log('DIFF'.cyan +': '); + + // Strip the header from the patch, we know how diffs work.. + patch = patch.replace(/^[^\n]*\n[^\n]*\n[^\n]*\n[^\n]*\n/, ''); + + return patch.split( '\n' ).map( function(line) { + // Add some colors to diff output + switch( line.charAt(0) ) { + case '-': + return line.red; + case '+': + return line.blue; + default: + return line; + } + }).join( "\n" ); + } +} + ParserTests.prototype.checkResult = function ( item, out ) { var normalizedOut = this.normalizeOut(out); var normalizedExpected = this.normalizeHTML(item.result); @@ -450,26 +483,10 @@ ParserTests.prototype.checkResult = function ( item, out ) { console.log('NORMALIZED RENDERED'.magenta + ':'); console.log(this.formatHTML(this.normalizeOut(out)) + "\n"); - var patch = jsDiff.createPatch('wikitext.txt', a, b, 'before', 'after'); console.log('DIFF'.cyan +': '); - // Strip the header from the patch, we know how diffs work.. - patch = patch.replace(/^[^\n]*\n[^\n]*\n[^\n]*\n[^\n]*\n/, ''); - - var colored_diff = patch.split( '\n' ).map( function(line) { - // Add some colors to diff output - switch( line.charAt(0) ) { - case '-': - return line.red; - case '+': - return line.blue; - default: - return line; - } - }).join( "\n" ); - - + var colored_diff = this.diff( a, b ); console.log( colored_diff ); if(this.argv.printwhitelist) { @@ -507,26 +524,8 @@ ParserTests.prototype.checkRoundTripResult = function ( item, out ) { console.log('NORMALIZED RENDERED'.magenta + ':'); console.log(normalizedOut + "\n"); - var patch = jsDiff.createPatch('wikitext.txt', normalizedExpected, normalizedOut, 'before', 'after'); - console.log('DIFF'.cyan +': '); - - // Strip the header from the patch, we know how diffs work.. - patch = patch.replace(/^[^\n]*\n[^\n]*\n[^\n]*\n[^\n]*\n/, ''); - - var colored_diff = patch.split( '\n' ).map( function(line) { - // Add some colors to diff output - switch( line.charAt(0) ) { - case '-': - return line.red; - case '+': - return line.blue; - default: - return line; - } - }).join( "\n" ); - - + var colored_diff = this.diff ( normalizedExpected, normalizedOut ); console.log( colored_diff ); } } else {