Explorar o código

Create buildDiff function

James Allen %!s(int64=12) %!d(string=hai) anos
pai
achega
20d70859aa

+ 4 - 0
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

+ 28 - 0
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", ->