From 20d70859aa79b21b8a432d38ad1f113e434b4fc9 Mon Sep 17 00:00:00 2001 From: James Allen Date: Mon, 3 Mar 2014 17:39:59 +0000 Subject: [PATCH] Create buildDiff function --- .../app/coffee/DiffGenerator.coffee | 4 +++ .../DiffGenerator/DiffGeneratorTests.coffee | 28 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/services/track-changes/app/coffee/DiffGenerator.coffee b/services/track-changes/app/coffee/DiffGenerator.coffee index 9ea4682e31..fdec79d6f4 100644 --- a/services/track-changes/app/coffee/DiffGenerator.coffee +++ b/services/track-changes/app/coffee/DiffGenerator.coffee @@ -28,6 +28,10 @@ module.exports = DiffGenerator = return content buildDiff: (initialContent, updates) -> + diff = [ u: initialContent ] + for update in updates + diff = DiffGenerator.applyUpdateToDiff diff, update + return diff applyUpdateToDiff: (diff, update) -> position = 0 diff --git a/services/track-changes/test/unit/coffee/DiffGenerator/DiffGeneratorTests.coffee b/services/track-changes/test/unit/coffee/DiffGenerator/DiffGeneratorTests.coffee index 47c4561074..18b2e637e1 100644 --- a/services/track-changes/test/unit/coffee/DiffGenerator/DiffGeneratorTests.coffee +++ b/services/track-changes/test/unit/coffee/DiffGenerator/DiffGeneratorTests.coffee @@ -50,6 +50,34 @@ describe "DiffGenerator", -> rewoundContent = @DiffGenerator.rewindUpdates content, updates rewoundContent.should.equal "aaa" + describe "buildDiff", -> + beforeEach -> + @diff = [ u: "mock-diff" ] + @content = "Hello world" + @updates = [ + { i: "mock-update-1" } + { i: "mock-update-2" } + { i: "mock-update-3" } + ] + @DiffGenerator.applyUpdateToDiff = sinon.stub().returns(@diff) + @result = @DiffGenerator.buildDiff(@content, @updates) + + it "should return the diff", -> + @result.should.deep.equal @diff + + it "should build the content into an initial diff", -> + @DiffGenerator.applyUpdateToDiff + .calledWith([{ + u: @content + }], @updates[0]) + .should.equal true + + it "should apply each update", -> + for update in @updates + @DiffGenerator.applyUpdateToDiff + .calledWith(sinon.match.any, update) + .should.equal true + describe "applyUpdateToDiff", -> describe "an insert", -> it "should insert into the middle of (u)nchanged text", ->