##// END OF EJS Templates
Added test for pack and unpack Model functions
Jonathan Frederic -
Show More
@@ -1,67 +1,134 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 31 // Test the widget framework.
2 32 casper.notebook_test(function () {
3 33 var index;
4 34
5 35 this.then(function () {
6 36
7 37 // Check if the WidgetManager class is defined.
8 38 this.test.assert(this.evaluate(function() {
9 39 return IPython.WidgetManager !== undefined;
10 40 }), 'WidgetManager class is defined');
11 41 });
12 42
13 43 index = this.append_cell(
14 44 'from IPython.html import widgets\n' +
15 45 'from IPython.display import display, clear_output\n' +
16 46 'print("Success")');
17 47 this.execute_cell_then(index);
18 48
19 49 this.then(function () {
20 50 // Check if the widget manager has been instantiated.
21 51 this.test.assert(this.evaluate(function() {
22 52 return IPython.notebook.kernel.widget_manager !== undefined;
23 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 93 var textbox = {};
27 94 throttle_index = this.append_cell(
28 95 'import time\n' +
29 96 'textbox = widgets.TextWidget()\n' +
30 97 'display(textbox)\n' +
31 98 'textbox.add_class("my-throttle-textbox")\n' +
32 99 'def handle_change(name, old, new):\n' +
33 100 ' print(len(new))\n' +
34 101 ' time.sleep(0.5)\n' +
35 102 'textbox.on_trait_change(handle_change, "value")\n' +
36 103 'print(textbox.model_id)');
37 104 this.execute_cell_then(throttle_index, function(index){
38 105 textbox.model_id = this.get_output_cell(index).text.trim();
39 106
40 107 this.test.assert(this.cell_element_exists(index,
41 108 '.widget-area .widget-subarea'),
42 109 'Widget subarea exists.');
43 110
44 111 this.test.assert(this.cell_element_exists(index,
45 112 '.my-throttle-textbox'), 'Textbox exists.');
46 113
47 114 // Send 20 characters
48 115 this.sendKeys('.my-throttle-textbox', '....................');
49 116 });
50 117
51 118 this.wait_for_widget(textbox);
52 119
53 120 this.then(function () {
54 121 var outputs = this.evaluate(function(i) {
55 122 return IPython.notebook.get_cell(i).output_area.outputs;
56 123 }, {i : throttle_index});
57 124
58 125 // Only 4 outputs should have printed, but because of timing, sometimes
59 126 // 5 outputs will print. All we need to do is verify num outputs <= 5
60 127 // because that is much less than 20.
61 128 this.test.assert(outputs.length <= 5, 'Messages throttled.');
62 129
63 130 // We also need to verify that the last state sent was correct.
64 131 var last_state = outputs[outputs.length-1].text;
65 132 this.test.assertEquals(last_state, "20\n", "Last state sent when throttling.");
66 133 });
67 134 });
General Comments 0
You need to be logged in to leave comments. Login now