Throw exception when aborting/committing without a transaction

This commit is contained in:
Kevin Sawicki & Nathan Sobo
2013-01-04 13:02:56 -07:00
parent da095cdfe9
commit 82d4550ff3
2 changed files with 17 additions and 4 deletions

View File

@@ -157,6 +157,12 @@ describe "UndoManager", ->
undoManager.redo()
expect(buffer.getText()).toBe '12'
describe "commit", ->
it "throws an exception if there is no current transaction", ->
expect(->
buffer.commit()
).toThrow()
describe "abort", ->
it "does not affect the undo stack when the current transaction is empty", ->
buffer.setText('')
@@ -167,6 +173,11 @@ describe "UndoManager", ->
buffer.undo()
expect(buffer.getText()).toBe ''
it "throws an exception if there is no current transaction", ->
expect(->
buffer.abort()
).toThrow()
describe "when a `do` operation throws an exception", ->
it "clears the stack", ->
spyOn(console, 'error')

View File

@@ -39,16 +39,18 @@ class UndoManager
isNewTransaction
commit: ->
@undoHistory.push(@currentTransaction) if @currentTransaction?.length
unless @currentTransaction?
throw new Error("Trying to commit when there is no current transaction")
empty = @currentTransaction.length is 0
@undoHistory.push(@currentTransaction) unless empty
@currentTransaction = null
not empty
abort: ->
@commit()
@undo()
@redoHistory.pop()
unless @currentTransaction?
throw new Error("Trying to abort when there is no current transaction")
if @commit()
@undo()
@redoHistory.pop()