PageRenderTime 25ms CodeModel.GetById 18ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/services/sync/tests/unit/test_utils_catch.js

http://github.com/zpao/v8monkey
JavaScript | 86 lines | 77 code | 9 blank | 0 comment | 7 complexity | dc922aaf943e3c7062a63e7a34eeffd8 MD5 | raw file
 1Cu.import("resource://services-sync/util.js");
 2Cu.import("resource://services-sync/service.js");
 3
 4function run_test() {
 5  _("Make sure catch when copied to an object will correctly catch stuff");
 6  let ret, rightThis, didCall, didThrow, wasTen, wasLocked;
 7  let obj = {
 8    catch: Utils.catch,
 9    _log: {
10      debug: function(str) {
11        didThrow = str.search(/^Exception: /) == 0;
12      },
13      info: function(str) {
14        wasLocked = str.indexOf("Cannot start sync: already syncing?") == 0;
15      }
16    },
17
18    func: function() this.catch(function() {
19      rightThis = this == obj;
20      didCall = true;
21      return 5;
22    })(),
23
24    throwy: function() this.catch(function() {
25      rightThis = this == obj;
26      didCall = true;
27      throw 10;
28    })(),
29    
30    callbacky: function() this.catch(function() {
31      rightThis = this == obj;
32      didCall = true;
33      throw 10;
34    }, function(ex) {
35      wasTen = (ex == 10)
36    })(),
37    
38    lockedy: function() this.catch(function() {
39      rightThis = this == obj;
40      didCall = true;
41      throw("Could not acquire lock.");
42    })()
43  };
44
45  _("Make sure a normal call will call and return");
46  rightThis = didCall = didThrow = wasLocked = false;
47  ret = obj.func();
48  do_check_eq(ret, 5);
49  do_check_true(rightThis);
50  do_check_true(didCall);
51  do_check_false(didThrow);
52  do_check_eq(wasTen, undefined);
53  do_check_false(wasLocked);
54
55  _("Make sure catch/throw results in debug call and caller doesn't need to handle exception");
56  rightThis = didCall = didThrow = wasLocked = false;
57  ret = obj.throwy();
58  do_check_eq(ret, undefined);
59  do_check_true(rightThis);
60  do_check_true(didCall);
61  do_check_true(didThrow);
62  do_check_eq(wasTen, undefined);
63  do_check_false(wasLocked);
64  
65  _("Test callback for exception testing.");
66  rightThis = didCall = didThrow = wasLocked = false;
67  ret = obj.callbacky();
68  do_check_eq(ret, undefined);
69  do_check_true(rightThis);
70  do_check_true(didCall);
71  do_check_true(didThrow);
72  do_check_true(wasTen);
73  do_check_false(wasLocked);
74  
75  _("Test the lock-aware catch that Service uses.");
76  obj.catch = Service._catch;
77  rightThis = didCall = didThrow = wasLocked = false;
78  wasTen = undefined;
79  ret = obj.lockedy();
80  do_check_eq(ret, undefined);
81  do_check_true(rightThis);
82  do_check_true(didCall);
83  do_check_true(didThrow);
84  do_check_eq(wasTen, undefined);
85  do_check_true(wasLocked);
86}