##// END OF EJS Templates
Added test for pack and unpack Model functions
Jonathan Frederic -
Show More
@@ -1,3 +1,33 b''
1 var xor = function (a, b) {return !a ^ !b;};
2 var isArray = function (a) {return toString.call(a) === "[object Array]";};
3 var recursive_compare = function(a, b) {
4 // Recursively compare two objects.
5 var same = true;
6 same = same && !xor(a instanceof Object, b instanceof Object);
7 same = same && !xor(isArray(a), isArray(b));
8
9 if (same) {
10 if (a instanceof Object) {
11 for (var key in a) {
12 if (a.hasOwnProperty(key) && !recursive_compare(a[key], b[key])) {
13 same = false;
14 break;
15 }
16 }
17 for (var key in b) {
18 if (b.hasOwnProperty(key) && !recursive_compare(a[key], b[key])) {
19 same = false;
20 break;
21 }
22 }
23 } else {
24 return a === b;
25 }
26 }
27
28 return same;
29 }
30
1 // Test the widget framework.
31 // Test the widget framework.
2 casper.notebook_test(function () {
32 casper.notebook_test(function () {
3 var index;
33 var index;
@@ -21,6 +51,43 b' casper.notebook_test(function () {'
21 this.test.assert(this.evaluate(function() {
51 this.test.assert(this.evaluate(function() {
22 return IPython.notebook.kernel.widget_manager !== undefined;
52 return IPython.notebook.kernel.widget_manager !== undefined;
23 }), 'Notebook widget manager instantiated');
53 }), 'Notebook widget manager instantiated');
54
55 // Functions that can be used to test the packing and unpacking APIs
56 var that = this;
57 var test_pack = function (input) {
58 var output = that.evaluate(function(input) {
59 var model = new IPython.WidgetModel(IPython.notebook.kernel.widget_manager, undefined);
60 var results = model._pack_models(input);
61 delete model;
62 return results;
63 }, {input: input});
64 that.test.assert(recursive_compare(input, output),
65 JSON.stringify(input) + ' passed through Model._pack_model unchanged');
66 };
67 var test_unpack = function (input) {
68 var output = that.evaluate(function(input) {
69 var model = new IPython.WidgetModel(IPython.notebook.kernel.widget_manager, undefined);
70 var results = model._unpack_models(input);
71 delete model;
72 return results;
73 }, {input: input});
74 that.test.assert(recursive_compare(input, output),
75 JSON.stringify(input) + ' passed through Model._unpack_model unchanged');
76 };
77 var test_packing = function(input) {
78 test_pack(input);
79 test_unpack(input);
80 };
81
82 test_packing({0: 'hi', 1: 'bye'})
83 test_packing(['hi', 'bye'])
84 test_packing(['hi', 5])
85 test_packing(['hi', '5'])
86 test_packing([1.0, 0])
87 test_packing([1.0, false])
88 test_packing([1, false])
89 test_packing([1, false, {a: 'hi'}])
90 test_packing([1, false, ['hi']])
24 });
91 });
25
92
26 var textbox = {};
93 var textbox = {};
General Comments 0
You need to be logged in to leave comments. Login now