Skip to content
This repository has been archived by the owner on Jul 1, 2020. It is now read-only.

Commit

Permalink
Merge branch 'add_undoCount_redoCount', this closes #5
Browse files Browse the repository at this point in the history
  • Loading branch information
pangratz committed May 16, 2012
2 parents 26248b6 + e49a842 commit c1af27e
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 0 deletions.
14 changes: 14 additions & 0 deletions app/lib/memento.js
@@ -1,4 +1,5 @@
Ember.Memento = Ember.Mixin.create({

// holds all history items
_memento: [],

Expand All @@ -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'),
Expand Down Expand Up @@ -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) {
Expand Down
130 changes: 130 additions & 0 deletions app/tests/memento_tests.js
Expand Up @@ -505,6 +505,136 @@ 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');
});

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
});
Expand Down

0 comments on commit c1af27e

Please sign in to comment.