Split lines on Windows line endings too

This commit is contained in:
James Allen 2014-04-10 12:44:46 +01:00
parent 375427bf5e
commit c2ebaaa338
2 changed files with 57 additions and 99 deletions

View file

@ -44,10 +44,7 @@ module.exports = ShareJsUpdateManager =
if error?
@_sendError(project_id, doc_id, error)
return callback(error)
if typeof data.snapshot == "string"
docLines = data.snapshot.split("\n")
else
docLines = data.snapshot.lines
docLines = data.snapshot.split(/\r\n|\n|\r/)
callback(null, docLines, data.v)
_listenForOps: (model) ->

View file

@ -29,113 +29,74 @@ describe "ShareJsUpdateManager", ->
@ShareJsUpdateManager.getNewShareJsModel = sinon.stub().returns(@model)
@ShareJsUpdateManager._listenForOps = sinon.stub()
@ShareJsUpdateManager.removeDocFromCache = sinon.stub().callsArg(1)
@updates = [
{p: 4, t: "foo"}
{p: 6, t: "bar"}
]
@updatedDocLines = ["one", "two"]
describe "with a text document", ->
beforeEach ->
@updates = [
{p: 4, t: "foo"}
{p: 6, t: "bar"}
]
@updatedDocLines = ["one", "two"]
describe "successfully", ->
beforeEach (done) ->
@model.getSnapshot.callsArgWith(1, null, {snapshot: @updatedDocLines.join("\n"), v: @version})
@ShareJsUpdateManager.applyUpdates @project_id, @doc_id, @updates, (err, docLines, version) =>
@callback(err, docLines, version)
done()
describe "successfully", ->
beforeEach (done) ->
@model.getSnapshot.callsArgWith(1, null, {snapshot: @updatedDocLines.join("\n"), v: @version})
@ShareJsUpdateManager.applyUpdates @project_id, @doc_id, @updates, (err, docLines, version) =>
@callback(err, docLines, version)
done()
it "should create a new ShareJs model", ->
@ShareJsUpdateManager.getNewShareJsModel
.called.should.equal true
it "should create a new ShareJs model", ->
@ShareJsUpdateManager.getNewShareJsModel
.called.should.equal true
it "should listen for ops on the model", ->
@ShareJsUpdateManager._listenForOps
.calledWith(@model)
.should.equal true
it "should listen for ops on the model", ->
@ShareJsUpdateManager._listenForOps
.calledWith(@model)
.should.equal true
it "should send each update to ShareJs", ->
for update in @updates
@model.applyOp
.calledWith("#{@project_id}:#{@doc_id}", update).should.equal true
it "should send each update to ShareJs", ->
for update in @updates
@model.applyOp
.calledWith("#{@project_id}:#{@doc_id}", update).should.equal true
it "should get the updated doc lines", ->
@model.getSnapshot
.calledWith("#{@project_id}:#{@doc_id}")
.should.equal true
it "should get the updated doc lines", ->
@model.getSnapshot
.calledWith("#{@project_id}:#{@doc_id}")
.should.equal true
it "should return the updated doc lines", ->
@callback.calledWith(null, @updatedDocLines, @version).should.equal true
it "should return the updated doc lines", ->
@callback.calledWith(null, @updatedDocLines, @version).should.equal true
describe "when applyOp fails", ->
beforeEach (done) ->
@error = new Error("Something went wrong")
@ShareJsUpdateManager._sendError = sinon.stub()
@model.applyOp = sinon.stub().callsArgWith(2, @error)
@ShareJsUpdateManager.applyUpdates @project_id, @doc_id, @updates, (err, docLines, version) =>
@callback(err, docLines, version)
done()
describe "when applyOp fails", ->
beforeEach (done) ->
@error = new Error("Something went wrong")
@ShareJsUpdateManager._sendError = sinon.stub()
@model.applyOp = sinon.stub().callsArgWith(2, @error)
@ShareJsUpdateManager.applyUpdates @project_id, @doc_id, @updates, (err, docLines, version) =>
@callback(err, docLines, version)
done()
it "should call sendError with the error", ->
@ShareJsUpdateManager._sendError
.calledWith(@project_id, @doc_id, @error)
.should.equal true
it "should call sendError with the error", ->
@ShareJsUpdateManager._sendError
.calledWith(@project_id, @doc_id, @error)
.should.equal true
it "should call the callback with the error", ->
@callback.calledWith(@error).should.equal true
it "should call the callback with the error", ->
@callback.calledWith(@error).should.equal true
describe "when getSnapshot fails", ->
beforeEach (done) ->
@error = new Error("Something went wrong")
@ShareJsUpdateManager._sendError = sinon.stub()
@model.getSnapshot.callsArgWith(1, @error)
@ShareJsUpdateManager.applyUpdates @project_id, @doc_id, @updates, (err, docLines, version) =>
@callback(err, docLines, version)
done()
describe "when getSnapshot fails", ->
beforeEach (done) ->
@error = new Error("Something went wrong")
@ShareJsUpdateManager._sendError = sinon.stub()
@model.getSnapshot.callsArgWith(1, @error)
@ShareJsUpdateManager.applyUpdates @project_id, @doc_id, @updates, (err, docLines, version) =>
@callback(err, docLines, version)
done()
it "should call sendError with the error", ->
@ShareJsUpdateManager._sendError
.calledWith(@project_id, @doc_id, @error)
.should.equal true
it "should call sendError with the error", ->
@ShareJsUpdateManager._sendError
.calledWith(@project_id, @doc_id, @error)
.should.equal true
it "should call the callback with the error", ->
@callback.calledWith(@error).should.equal true
describe "with a JSON document", ->
beforeEach ->
@updates = [
{p: ["lines", 0], dl: { foo: "bar "}}
]
@docLines = [text: "one", text: "two"]
describe "successfully", ->
beforeEach (done) ->
@model.getSnapshot.callsArgWith(1, null, {snapshot: {lines: @docLines}, v: @version})
@ShareJsUpdateManager.applyUpdates @project_id, @doc_id, @updates, (err, docLines, version) =>
@callback(err, docLines, version)
done()
it "should create a new ShareJs model", ->
@ShareJsUpdateManager.getNewShareJsModel
.called.should.equal true
it "should listen for ops on the model", ->
@ShareJsUpdateManager._listenForOps
.calledWith(@model)
.should.equal true
it "should send each update to ShareJs", ->
for update in @updates
@model.applyOp
.calledWith("#{@project_id}:#{@doc_id}", update).should.equal true
it "should get the updated doc lines", ->
@model.getSnapshot
.calledWith("#{@project_id}:#{@doc_id}")
.should.equal true
it "should return the updated doc lines", ->
@callback.calledWith(null, @docLines, @version).should.equal true
it "should call the callback with the error", ->
@callback.calledWith(@error).should.equal true
describe "_listenForOps", ->
beforeEach ->