Skip to content

Commit

Permalink
Add diffview tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Spencer Thang committed Sep 11, 2015
1 parent fe3b415 commit 9d99aa5
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
5 changes: 5 additions & 0 deletions difflib.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF S
DAMAGE.
***/
/* Author: Chas Emerick <cemerick@snowtide.com> */

exports = module.exports = {};

var __whitespace = {" ":true, "\t":true, "\n":true, "\f":true, "\r":true};

var difflib = {
Expand Down Expand Up @@ -411,3 +414,5 @@ var difflib = {
}
};

exports.stringAsLines = difflib.stringAsLines;
exports.SequenceMatcher = difflib.SequenceMatcher;
4 changes: 4 additions & 0 deletions diffview.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ The views and conclusions contained in the software and documentation are those
authors and should not be interpreted as representing official policies, either expressed
or implied, of Chas Emerick.
*/
exports = module.exports = {};
var difflib = require("./difflib");

diffview = {
/**
* Builds and returns a visual diff view. The single parameter, `params', should contain
Expand Down Expand Up @@ -207,3 +210,4 @@ diffview = {
}
};

exports.buildView = diffview.buildView;
28 changes: 28 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "jsdifflib",
"version": "1.0.0",
"description": "A javascript library to diff two files.",
"main": "difflib.js",
"directories": {
"test": "test"
},
"dependencies": {},
"devDependencies": {
"mocha": "^2.3.2",
"jsdom": "^3.1.2",
"chai": "^3.2.0"
},
"scripts": {
"test": "mocha"
},
"repository": {
"type": "git",
"url": "git+https://github.com/red-gate/jsdifflib.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/red-gate/jsdifflib/issues"
},
"homepage": "https://github.com/red-gate/jsdifflib#readme"
}
63 changes: 63 additions & 0 deletions test/diffview_tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
var expect = require("chai").expect;
var diffview = require("../diffview.js");
var difflib = require("../difflib.js");
var jsdom = require("jsdom").jsdom;
var document = jsdom("<html></html>");
global.document = document;

describe("diffview", function() {
describe(".buildView()", function() {
it("can build view with a difference", function () {
var view = testBuildView('a', 'b');
expect(view.differences).to.equal(1);
expect(view.diffOutput.outerHTML).to.contain('diffview_difference_1');
expect(view.diffOutput.outerHTML).not.to.contain('diffview_difference_2');
});

it("can build view with no differences", function () {
var view = testBuildView('a', 'a');
expect(view.differences).to.equal(0);
expect(view.diffOutput.outerHTML).not.to.contain('diffview_difference_1');
});

it("can build view with multi-line difference blocks", function () {
var source = '<ruleEntry>' +
'\r\n<assignedTo>spencerthang@gmail.com</assignedTo>' +
'\r\n<criteriaItems>' +
'\r\n <field>Lead.Country</field>' +
'\r\n <value>US,USA,United States,United States of America</value>' +
'\r\n</criteriaItems>';

var target = '<ruleEntry>' +
'\r\n<assignedTo>spencerthang@red-gate.com</assignedTo>' +
'\r\n<criteriaItems>' +
'\r\n <field>Lead.City</field>' +
'\r\n <value>SG,Singapore</value>' +
'\r\n</criteriaItems>';

var view = testBuildView(source, target);
expect(view.differences).to.equal(2);
expect(view.diffOutput.outerHTML).to.contain('diffview_difference_1');
expect(view.diffOutput.outerHTML).to.contain('diffview_difference_2');
expect(view.diffOutput.outerHTML).not.to.contain('diffview_difference_3');
});
});
});

var testBuildView = function(sourceText, targetText) {
var source = difflib.stringAsLines(sourceText);
var target = difflib.stringAsLines(targetText);
var sm = new difflib.SequenceMatcher(source, target);
var opcodes = sm.get_opcodes();

var diffResult = diffview.buildView({
baseTextLines: source,
newTextLines: target,
opcodes: opcodes,
baseTextName: 'Source',
newTextName: 'Target',
viewType: 0
});

return diffResult;
}

0 comments on commit 9d99aa5

Please sign in to comment.