Jelajahi Sumber

wip update tests

Steven Silvester 8 tahun lalu
induk
melakukan
b7ad6acbff

+ 242 - 0
test/src/notebook/common/undo.spec.ts

@@ -0,0 +1,242 @@
+// Copyright (c) Jupyter Development Team.
+// Distributed under the terms of the Modified BSD License.
+
+import expect = require('expect.js');
+
+import {
+  JSONObject
+} from 'phosphor/lib/algorithm/json';
+
+import {
+  ObservableUndoableVector, ISerializable
+} from '../../../../lib/notebook/common/undo';
+
+
+
+class Test implements ISerializable {
+
+  constructor(value: JSONObject) {
+    this._value = value;
+  }
+
+  toJSON(): JSONObject {
+    return this._value;
+  }
+
+  private _value: JSONObject;
+}
+
+
+let count = 0;
+
+function factory(value: JSONObject): Test {
+  value['count'] = count++;
+  return new Test(value);
+}
+
+
+const value: JSONObject = { name: 'foo' };
+
+
+describe('notebook/common/undo', () => {
+
+  describe('ObservableUndoableVector', () => {
+
+    describe('#constructor', () => {
+
+      it('should create a new ObservableUndoableVector', () => {
+        let vector = new ObservableUndoableVector(factory);
+        expect(vector).to.be.an(ObservableUndoableVector);
+      });
+
+    });
+
+    describe('#canRedo', () => {
+
+      it('should return false if there is no history', () => {
+        let vector = new ObservableUndoableVector(factory);
+        expect(vector.canRedo).to.be(false);
+      });
+
+      it('should return true if there is an undo that can be redone', () => {
+        let vector = new ObservableUndoableVector(factory);
+        vector.pushBack(new Test(value));
+        vector.undo();
+        expect(vector.canRedo).to.be(true);
+      });
+
+    });
+
+    describe('#canUndo', () => {
+
+      it('should return false if there is no history', () => {
+        let vector = new ObservableUndoableVector(factory);
+        expect(vector.canUndo).to.be(false);
+      });
+
+      it('should return true if there is a change that can be undone', () => {
+        let vector = new ObservableUndoableVector(factory);
+        vector.pushBack(factory(value));
+        expect(vector.canUndo).to.be(true);
+      });
+
+    });
+
+    describe('#dispose()', () => {
+
+      it('should dispose of the resources used by the vector', () => {
+        let vector = new ObservableUndoableVector(factory);
+        vector.dispose();
+        expect(vector.isDisposed).to.be(true);
+        vector.dispose();
+        expect(vector.isDisposed).to.be(true);
+      });
+
+    });
+
+    describe('#beginCompoundOperation()', () => {
+
+      it('should begin a compound operation', () => {
+        let vector = new ObservableUndoableVector(factory);
+        vector.beginCompoundOperation();
+        vector.pushBack(factory(value));
+        vector.pushBack(factory(value));
+        vector.endCompoundOperation();
+        expect(vector.canUndo).to.be(true);
+        vector.undo();
+        expect(vector.canUndo).to.be(false);
+      });
+
+      it('should not be undoable if isUndoAble is set to false', () => {
+        let vector = new ObservableUndoableVector(factory);
+        vector.beginCompoundOperation(false);
+        vector.pushBack(factory(value));
+        vector.pushBack(factory(value));
+        vector.endCompoundOperation();
+        expect(vector.canUndo).to.be(false);
+      });
+
+    });
+
+    describe('#endCompoundOperation()', () => {
+
+      it('should end a compound operation', () => {
+        let vector = new ObservableUndoableVector(factory);
+        vector.beginCompoundOperation();
+        vector.pushBack(factory(value));
+        vector.pushBack(factory(value));
+        vector.endCompoundOperation();
+        expect(vector.canUndo).to.be(true);
+        vector.undo();
+        expect(vector.canUndo).to.be(false);
+      });
+
+    });
+
+    describe('#undo()', () => {
+
+      it('should undo a pushBack', () => {
+        let vector = new ObservableUndoableVector(factory);
+        vector.pushBack(factory(value));
+        vector.undo();
+        expect(vector.length).to.be(0);
+      });
+
+      it('should undo a pushAll', () => {
+        let vector = new ObservableUndoableVector(factory);
+        vector.pushAll([factory(value), factory(value)]);
+        vector.undo();
+        expect(vector.length).to.be(0);
+      });
+
+      it('should undo a remove', () => {
+         let vector = new ObservableUndoableVector(factory);
+         vector.pushAll([factory(value), factory(value)]);
+         vector.removeAt(0);
+         vector.undo();
+         expect(vector.length).to.be(2);
+      });
+
+      it('should undo a removeRange', () => {
+        let vector = new ObservableUndoableVector(factory);
+        vector.pushAll([factory(value), factory(value), factory(value),
+          factory(value), factory(value), factory(value)]);
+        vector.removeRange(1, 3);
+        vector.undo();
+        expect(vector.length).to.be(6);
+      });
+
+      it('should undo a move', () => {
+        let items = [factory(value), factory(value), factory(value)];
+        let vector = new ObservableUndoableVector(factory);
+        vector.pushAll(items);
+        vector.move(1, 2);
+        vector.undo();
+        expect((vector.at(1) as any)['count']).to.be((items[1] as any)['count']);
+      });
+
+    });
+
+    describe('#redo()', () => {
+
+      it('should redo a pushBack', () => {
+        let vector = new ObservableUndoableVector(factory);
+        vector.pushBack(factory(value));
+        vector.undo();
+        vector.redo();
+        expect(vector.length).to.be(1);
+      });
+
+      it('should redo a pushAll', () => {
+        let vector = new ObservableUndoableVector(factory);
+        vector.pushAll([factory(value), factory(value)]);
+        vector.undo();
+        vector.redo();
+        expect(vector.length).to.be(2);
+      });
+
+      it('should redo a remove', () => {
+         let vector = new ObservableUndoableVector(factory);
+         vector.pushAll([factory(value), factory(value)]);
+         vector.removeAt(0);
+         vector.undo();
+         vector.redo();
+         expect(vector.length).to.be(1);
+      });
+
+      it('should redo a removeRange', () => {
+        let vector = new ObservableUndoableVector(factory);
+        vector.pushAll([factory(value), factory(value), factory(value),
+          factory(value), factory(value), factory(value)]);
+        vector.removeRange(1, 3);
+        vector.undo();
+        vector.redo();
+        expect(vector.length).to.be(4);
+      });
+
+      it('should undo a move', () => {
+        let items = [factory(value), factory(value), factory(value)];
+        let vector = new ObservableUndoableVector(factory);
+        vector.pushAll(items);
+        vector.move(1, 2);
+        vector.undo();
+        vector.redo();
+        expect((vector.at(2) as any)['count']).to.be((items[1] as any)['count']);
+      });
+
+    });
+
+    describe('#clearUndo()', () => {
+
+      it('should clear the undo stack', () => {
+        let vector = new ObservableUndoableVector(factory);
+        vector.pushBack(factory(value));
+        vector.clearUndo();
+        expect(vector.canUndo).to.be(false);
+      });
+
+    });
+
+  });
+
+});

+ 1 - 0
test/src/notebook/notebook/actions.spec.ts

@@ -171,6 +171,7 @@ describe('notebook/notebook/actions', () => {
         widget.select(next);
         source += next.model.source;
         let count = widget.childCount();
+        debugger;
         NotebookActions.mergeCells(widget);
         expect(widget.childCount()).to.be(count - 2);
         expect(widget.activeCell.model.source).to.be(source);

+ 5 - 10
test/src/notebook/notebook/model.spec.ts

@@ -4,12 +4,12 @@
 import expect = require('expect.js');
 
 import {
-  CodeCellModel
-} from '../../../../lib/notebook/cells/model';
+  indexOf
+} from 'phosphor/lib/algorithm/searching';
 
 import {
-  ObservableUndoableList
-} from '../../../../lib/notebook/common/undo';
+  CodeCellModel
+} from '../../../../lib/notebook/cells/model';
 
 import {
   nbformat
@@ -87,11 +87,6 @@ describe('notebook/notebook/model', () => {
 
     describe('#cells', () => {
 
-      it('should be an observable undoable list', () => {
-        let model = new NotebookModel();
-        expect(model.cells).to.be.an(ObservableUndoableList);
-      });
-
       it('should add an empty code cell by default', () => {
         let model = new NotebookModel();
         expect(model.cells.length).to.be(1);
@@ -103,7 +98,7 @@ describe('notebook/notebook/model', () => {
         let cell = model.factory.createCodeCell();
         model.cells.pushBack(cell);
         model.fromJSON(DEFAULT_CONTENT);
-        expect(model.cells.indexOf(cell)).to.be(-1);
+        expect(indexOf(model.cells, cell)).to.be(-1);
         expect(model.cells.length).to.be(6);
       });
 

+ 3 - 3
test/src/notebook/notebook/widget.spec.ts

@@ -316,10 +316,10 @@ describe('notebook/notebook/widget', () => {
           expect(widget.childAt(2)).to.be(child);
         });
 
-        it('should handle a replace', () => {
+        it('should handle a clear', () => {
           let cell = widget.model.factory.createCodeCell();
-          widget.model.cells.replace(0, 6, [cell]);
-          expect(widget.childCount()).to.be(1);
+          widget.model.cells.clear();
+          expect(widget.childCount()).to.be(0);
         });
 
       });

+ 2 - 2
test/src/notebook/output-area/model.spec.ts

@@ -92,8 +92,8 @@ describe('notebook/output-area/model', () => {
           expect(args.type).to.be('add');
           expect(args.oldIndex).to.be(-1);
           expect(args.newIndex).to.be(0);
-          expect(args.oldValue).to.be(void 0);
-          expect(deepEqual(args.newValue as nbformat.IOutput, DEFAULT_OUTPUTS[0]));
+          expect(args.oldValues.next()).to.be(void 0);
+          expect(deepEqual(args.newValues.next() as nbformat.IOutput, DEFAULT_OUTPUTS[0]));
           called = true;
         });
         model.add(DEFAULT_OUTPUTS[0]);