From 756cb2e96d1352a9539570dbb75c00b9a0c3902a Mon Sep 17 00:00:00 2001 From: pangratz Date: Wed, 16 May 2012 16:55:11 +0200 Subject: [PATCH 1/2] Add undoCount and redoCount --- app/lib/memento.js | 14 ++++++++++ app/tests/memento_tests.js | 53 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/app/lib/memento.js b/app/lib/memento.js index a083474..b4a701b 100644 --- a/app/lib/memento.js +++ b/app/lib/memento.js @@ -1,4 +1,5 @@ Ember.Memento = Ember.Mixin.create({ + // holds all history items _memento: [], @@ -9,6 +10,16 @@ Ember.Memento = Ember.Mixin.create({ */ _mementoIndex: -1, + undoCount: function() { + return this.get('_mementoIndex') + 1; + }.property('_mementoIndex').cacheable(), + + redoCount: function() { + var length = this.getPath('_memento.length'); + var mementoIndex = this.get('_mementoIndex'); + return length - mementoIndex - 1; + }.property('_mementoIndex', '_memento.length').cacheable(), + _mementoSizeChanged: function() { this._updateMemento(); }.observes('mementoSize'), @@ -123,6 +134,9 @@ Ember.Memento = Ember.Mixin.create({ }, init: function() { + this.set('_memento', []); + this.set('_mementoIndex', -1); + // iterate over all mementoProperties and add observers var props = this.get('mementoProperties'); props.forEach(function(item) { diff --git a/app/tests/memento_tests.js b/app/tests/memento_tests.js index 69074eb..1c928c8 100644 --- a/app/tests/memento_tests.js +++ b/app/tests/memento_tests.js @@ -505,6 +505,59 @@ function() { deepEqual(get(obj, 'array'), [1, 2, 3], 'redo changes array'); }); +module("undoCount AND redoCount", { + teardown: cleanObj +}); + +test("are initially 0", +function() { + obj = Ember.Object.create(Ember.Memento, { + mementoProperties: 'str'.w(), + + str: 'hansi' + }); + + equal(get(obj, 'undoCount'), 0, 'undoCount is initially 0'); + equal(get(obj, 'redoCount'), 0, 'redoCount is initially 0'); +}); + +test("change when a property is set", +function() { + obj = Ember.Object.create(Ember.Memento, { + mementoProperties: 'str'.w(), + str: 'hansi' + }); + + equal(get(obj, 'undoCount'), 0, 'precond - undoCount is initially 0'); + equal(get(obj, 'redoCount'), 0, 'precond - redoCount is initially 0'); + + set(obj, 'str', 'frozen banana'); + + equal(get(obj, 'undoCount'), 1, 'undoCount is 1 when a undo can be done'); + equal(get(obj, 'redoCount'), 0, 'redoCount stays 0'); +}); + +test("change when a undo is done", +function() { + obj = Ember.Object.create(Ember.Memento, { + mementoProperties: 'str'.w(), + str: 'hansi' + }); + + equal(get(obj, 'undoCount'), 0, 'precond - undoCount is initially 0'); + equal(get(obj, 'redoCount'), 0, 'precond - redoCount is initially 0'); + + set(obj, 'str', 'frozen banana'); + + equal(get(obj, 'undoCount'), 1, 'precond - undoCount is 1 when a undo can be done'); + equal(get(obj, 'redoCount'), 0, 'precond - redoCount stays 0'); + + obj.undo(); + + equal(get(obj, 'undoCount'), 0, 'undoCount is 0 after an undo'); + equal(get(obj, 'redoCount'), 1, 'redoCount is 1 after an undo'); +}); + module("clearHistory", { teardown: cleanObj }); From e49a84292e5d4f21466b4c105456f32d14d45981 Mon Sep 17 00:00:00 2001 From: pangratz Date: Wed, 16 May 2012 16:59:30 +0200 Subject: [PATCH 2/2] Add tests for complex example and when mementoSize changes --- app/tests/memento_tests.js | 77 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/app/tests/memento_tests.js b/app/tests/memento_tests.js index 1c928c8..e638a96 100644 --- a/app/tests/memento_tests.js +++ b/app/tests/memento_tests.js @@ -558,6 +558,83 @@ function() { equal(get(obj, 'redoCount'), 1, 'redoCount is 1 after an undo'); }); +test("complex example", +function() { + obj = Ember.Object.create(Ember.Memento, { + mementoProperties: 'str'.w(), + str: 'hansi' + }); + + equal(get(obj, 'undoCount'), 0, 'precond - undoCount is initially 0'); + equal(get(obj, 'redoCount'), 0, 'precond - redoCount is initially 0'); + + set(obj, 'str', 'frozen banana'); + + equal(get(obj, 'undoCount'), 1, 'undoCount is 1 when a undo can be done'); + equal(get(obj, 'redoCount'), 0, 'redoCount stays 0'); + + set(obj, 'str', 'hubert'); + + equal(get(obj, 'undoCount'), 2, 'undoCount is increased when a property changes'); + equal(get(obj, 'redoCount'), 0, 'redoCount stays 0'); + + obj.undo(); + + equal(get(obj, 'undoCount'), 1, 'undoCount is 1 after an undo'); + equal(get(obj, 'redoCount'), 1, 'redoCount is 1 after an undo'); + + obj.undo(); + + equal(get(obj, 'undoCount'), 0, 'undoCount is 0 after an undo'); + equal(get(obj, 'redoCount'), 2, 'redoCount is 2 after an undo'); + + obj.undo(); + + equal(get(obj, 'undoCount'), 0, 'undoCount is 0 after an undo'); + equal(get(obj, 'redoCount'), 2, 'redoCount is 2 after an undo'); + + obj.redo(); + + equal(get(obj, 'undoCount'), 1, 'undoCount is 1 after an undo'); + equal(get(obj, 'redoCount'), 1, 'redoCount is 1 after an undo'); + + obj.redo(); + + equal(get(obj, 'undoCount'), 2, 'undoCount is 2 after an undo'); + equal(get(obj, 'redoCount'), 0, 'redoCount is 0 after an undo'); + + obj.redo(); + + equal(get(obj, 'undoCount'), 2, 'undoCount is 2 after an undo'); + equal(get(obj, 'redoCount'), 0, 'redoCount is 0 after an undo'); +}); + +test("change when mementoSize is changed", +function() { + obj = Ember.Object.create(Ember.Memento, { + mementoProperties: 'str'.w(), + str: 'hansi' + }); + + equal(get(obj, 'undoCount'), 0, 'precond - undoCount is initially 0'); + equal(get(obj, 'redoCount'), 0, 'precond - redoCount is initially 0'); + + set(obj, 'str', 'frozen banana'); + + equal(get(obj, 'undoCount'), 1, 'undoCount is 1 when a undo can be done'); + equal(get(obj, 'redoCount'), 0, 'redoCount stays 0'); + + set(obj, 'str', 'hubert'); + + equal(get(obj, 'undoCount'), 2, 'undoCount is increased when a property changes'); + equal(get(obj, 'redoCount'), 0, 'redoCount stays 0'); + + set(obj, 'mementoSize', 1); + + equal(get(obj, 'undoCount'), 1, 'undoCount is decreased to 1'); + equal(get(obj, 'redoCount'), 0, 'redoCount stays 0'); +}); + module("clearHistory", { teardown: cleanObj });