Create buildDiff function

This commit is contained in:
James Allen 2014-03-03 17:39:59 +00:00
parent 8d044b7f13
commit 20d70859aa
2 changed files with 32 additions and 0 deletions

View file

@ -28,6 +28,10 @@ module.exports = DiffGenerator =
return content return content
buildDiff: (initialContent, updates) -> buildDiff: (initialContent, updates) ->
diff = [ u: initialContent ]
for update in updates
diff = DiffGenerator.applyUpdateToDiff diff, update
return diff
applyUpdateToDiff: (diff, update) -> applyUpdateToDiff: (diff, update) ->
position = 0 position = 0

View file

@ -50,6 +50,34 @@ describe "DiffGenerator", ->
rewoundContent = @DiffGenerator.rewindUpdates content, updates rewoundContent = @DiffGenerator.rewindUpdates content, updates
rewoundContent.should.equal "aaa" 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 "applyUpdateToDiff", ->
describe "an insert", -> describe "an insert", ->
it "should insert into the middle of (u)nchanged text", -> it "should insert into the middle of (u)nchanged text", ->