From 82d4550ff31a8767ac9dc6ed4cb2477438f12441 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki & Nathan Sobo Date: Fri, 4 Jan 2013 13:02:56 -0700 Subject: [PATCH] Throw exception when aborting/committing without a transaction --- spec/app/undo-manager-spec.coffee | 11 +++++++++++ src/app/undo-manager.coffee | 10 ++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/spec/app/undo-manager-spec.coffee b/spec/app/undo-manager-spec.coffee index df1c9c690..b9807090e 100644 --- a/spec/app/undo-manager-spec.coffee +++ b/spec/app/undo-manager-spec.coffee @@ -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') diff --git a/src/app/undo-manager.coffee b/src/app/undo-manager.coffee index aa025668b..f13b46983 100644 --- a/src/app/undo-manager.coffee +++ b/src/app/undo-manager.coffee @@ -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()