PageRenderTime 72ms CodeModel.GetById 31ms app.highlight 35ms RepoModel.GetById 2ms app.codeStats 0ms

/js/jetpack/tests/unit/test_jetpack.js

http://github.com/zpao/v8monkey
JavaScript | 204 lines | 184 code | 20 blank | 0 comment | 1 complexity | e783e2af4725c92a6ae5072127c3ae9a MD5 | raw file
  1var jetpack = null;
  2
  3load("handle_tests.js");
  4function createHandle() {
  5  return jetpack.createHandle();
  6}
  7
  8function run_test() {
  9  jetpack = createJetpack({
 10    skipRegisterError: true,
 11    scriptFile: do_get_file("impl.js")
 12  });
 13  run_handle_tests();
 14
 15  var circ1 = {},
 16      circ2 = {},
 17      circ3 = {},
 18      ok = false;
 19  ((circ1.next = circ2).next = circ3).next = circ1;
 20  try {
 21    jetpack.sendMessage("ignored", circ3, circ1);
 22    ok = true;
 23  } catch (x) {
 24    do_check_false(x);
 25  }
 26  do_check_true(ok);
 27
 28  var echoHandle = jetpack.createHandle();
 29  echoHandle.payload = { weight: 10 };
 30  jetpack.registerReceiver("echo",
 31                           function(msgName, data, handle) {
 32                             do_check_eq(arguments.length, 3);
 33                             do_check_eq(msgName, "echo");
 34                             do_check_eq(data, "echo this");
 35                             do_check_true(handle.isValid);
 36                             do_check_eq(handle, echoHandle);
 37                             do_check_eq(handle.payload.weight, 10);
 38                             do_test_finished();
 39                           });
 40
 41  jetpack.registerReceiver("callback",
 42                           function(msgName, data) {
 43                             do_check_eq(msgName, "callback");
 44                             return "called back: " + data;
 45                           });
 46
 47  var callbackHandle = echoHandle.createHandle();
 48  jetpack.registerReceiver("sendback",
 49                           function(msgName, data, handle) {
 50                             do_check_eq(msgName, "sendback");
 51                             do_check_eq(data, "called back: call me back");
 52                             do_check_eq(handle, callbackHandle);
 53                             do_test_finished();
 54                           });
 55
 56  var obj;
 57  jetpack.registerReceiver("recvHandle",
 58                           function(msgName, data, handle) {
 59                             handle.mark = obj = {};
 60                             jetpack.sendMessage("kthx", data + data, handle.createHandle());
 61                           });
 62  jetpack.registerReceiver("recvHandleAgain",
 63                           function(msgName, data, handle) {
 64                             do_check_eq(data, "okokokok");
 65                             do_check_eq(handle.mark, obj);
 66                             do_test_finished();
 67                           });
 68  var obj1 = {
 69    id: Math.random() + ""
 70  }, obj2 = {
 71    id: Math.random() + "",
 72    obj: obj1
 73  };
 74  jetpack.registerReceiver("echo2",
 75                           function(msgName, a, b) {
 76                             do_check_neq(obj1, a);
 77                             do_check_neq(obj2, b);
 78                             do_check_eq(obj1.id, a.id);
 79                             do_check_eq(obj2.id, b.id);
 80                             do_check_eq(obj1.id, obj2.obj.id);
 81                             do_test_finished();
 82                           });
 83
 84  jetpack.registerReceiver("multireturn", function() { return obj1 });
 85  jetpack.registerReceiver("multireturn", function() { return circ1 });
 86  jetpack.registerReceiver("multireturn", function() { return obj2 });
 87  jetpack.registerReceiver("multireturn check",
 88                           function(msgName, rval1, rval2, rval3) {
 89                             do_check_eq(rval1.id, obj1.id);
 90                             do_check_eq(rval2.next.next.next, rval2);
 91                             do_check_eq(rval3.id, obj2.id);
 92                             do_check_eq(rval3.obj.id, obj1.id);
 93                             do_test_finished();
 94                           });
 95
 96  var testarray = [1, 1, 2, 3, 5, 8, 13];
 97  jetpack.registerReceiver("testarray",
 98                           function(msgName, reversed) {
 99                             for (var i = 0; i < testarray.length; ++i)
100                               do_check_eq(testarray[i],
101                                           reversed[reversed.length - i - 1]);
102                             do_test_finished();
103                           });
104
105  var undefined;
106  jetpack.registerReceiver("test primitive types",
107                           function(msgName,
108                                    void_val, null_val,
109                                    bool_true, bool_false,
110                                    one, two, nine99,
111                                    one_quarter,
112                                    oyez_str)
113                           {
114                             do_check_true(void_val === undefined);
115                             do_check_true(null_val === null);
116                             do_check_true(bool_true === true);
117                             do_check_true(bool_false === false);
118                             do_check_eq(one, 1);
119                             do_check_eq(two, 2);
120                             do_check_eq(nine99, 999);
121                             do_check_eq(one_quarter, 0.25);
122                             do_check_eq(oyez_str, "oyez");
123
124                             do_test_finished();
125                           });
126
127  var drop = {
128    nested: {
129      method: function() { return this.value },
130      value: 42
131    }
132  };
133  jetpack.registerReceiver("drop methods",
134                           function(msgName, echoed) {
135                             do_check_true(!echoed.nested.method);
136                             do_check_eq(echoed.nested.value, 42);
137                             do_test_finished();
138                           });
139
140  var coped = "did not cope";
141  jetpack.registerReceiver("exception coping",
142                           function(msgName) { throw coped = "did cope" });
143  jetpack.registerReceiver("exception coping",
144                           function(msgName) {
145                             do_check_eq(coped, "did cope");
146                             do_test_finished();
147                           });
148
149  var calls = "";
150  function countCalls() { calls += "." }
151  jetpack.registerReceiver("duplicate receivers", countCalls);
152  jetpack.registerReceiver("duplicate receivers", countCalls);
153  jetpack.registerReceiver("duplicate receivers",
154                           function() { do_check_eq(calls, ".") });
155  jetpack.registerReceiver("duplicate receivers", countCalls);
156  jetpack.registerReceiver("duplicate receivers",
157                           function() {
158                             do_check_eq(calls, ".");
159                             jetpack.unregisterReceivers("duplicate receivers");
160                           });
161  jetpack.registerReceiver("duplicate receivers",
162                           function() { do_test_finished() });
163
164  jetpack.registerReceiver("test result", function(name, c, msg) {
165    dump("TEST-INFO | test_jetpack.js | remote check '" + msg + "' result: " + c + "\n");
166    do_check_true(c);
167  });
168  jetpack.registerReceiver("sandbox done", do_test_finished);
169
170  jetpack.registerReceiver("core:exception",
171			   function(msgName, e) {
172			       do_check_true(/throwing on request/.test(e.message));
173			       do_test_finished();
174			   });
175
176  do_test_pending();
177  do_test_pending();
178  do_test_pending();
179  do_test_pending();
180  do_test_pending();
181  do_test_pending();
182  do_test_pending();
183  do_test_pending();
184  do_test_pending();
185  do_test_pending();
186  do_test_pending();
187  do_test_pending();
188
189  jetpack.sendMessage("echo", "echo this", echoHandle);
190  jetpack.sendMessage("callback", "call me back", callbackHandle);
191  jetpack.sendMessage("gimmeHandle");
192  jetpack.sendMessage("echo2", obj1, obj2);
193  jetpack.sendMessage("multireturn begin");
194  jetpack.sendMessage("testarray", testarray);
195  jetpack.sendMessage("test primitive types",
196                      undefined, null, true, false, 1, 2, 999, 1/4, "oyez");
197  jetpack.sendMessage("drop methods", drop);
198  jetpack.sendMessage("exception coping");
199
200  jetpack.sendMessage("duplicate receivers");
201
202  jetpack.sendMessage("test sandbox");
203  jetpack.sendMessage("throw");
204}