|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 * http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 /** |
|
5 * Test that functions throw the appropriate exceptions. |
|
6 */ |
|
7 |
|
8 "use strict"; |
|
9 |
|
10 let EXISTING_FILE = do_get_file("xpcshell.ini").path; |
|
11 |
|
12 |
|
13 // Tests on |open| |
|
14 |
|
15 add_test_pair(function test_typeerror() { |
|
16 let exn; |
|
17 try { |
|
18 let fd = yield OS.File.open("/tmp", {no_such_key: 1}); |
|
19 do_print("Fd: " + fd); |
|
20 } catch (ex) { |
|
21 exn = ex; |
|
22 } |
|
23 do_print("Exception: " + exn); |
|
24 do_check_true(exn.constructor.name == "TypeError"); |
|
25 }); |
|
26 |
|
27 // Tests on |read| |
|
28 |
|
29 add_test_pair(function* test_bad_encoding() { |
|
30 do_print("Testing with a wrong encoding"); |
|
31 try { |
|
32 yield OS.File.read(EXISTING_FILE, { encoding: "baby-speak-encoded" }); |
|
33 do_throw("Should have thrown with an ex.becauseInvalidArgument"); |
|
34 } catch (ex if ex.becauseInvalidArgument) { |
|
35 do_print("Wrong encoding caused the correct exception"); |
|
36 } |
|
37 |
|
38 try { |
|
39 yield OS.File.read(EXISTING_FILE, { encoding: 4 }); |
|
40 do_throw("Should have thrown a TypeError"); |
|
41 } catch (ex if ex.constructor.name == "TypeError") { |
|
42 // Note that TypeError doesn't carry across compartments |
|
43 do_print("Non-string encoding caused the correct exception"); |
|
44 } |
|
45 }); |
|
46 |
|
47 add_test_pair(function* test_bad_compression() { |
|
48 do_print("Testing with a non-existing compression"); |
|
49 try { |
|
50 yield OS.File.read(EXISTING_FILE, { compression: "mmmh-crunchy" }); |
|
51 do_throw("Should have thrown with an ex.becauseInvalidArgument"); |
|
52 } catch (ex if ex.becauseInvalidArgument) { |
|
53 do_print("Wrong encoding caused the correct exception"); |
|
54 } |
|
55 |
|
56 do_print("Testing with a bad type for option compression"); |
|
57 try { |
|
58 yield OS.File.read(EXISTING_FILE, { compression: 5 }); |
|
59 do_throw("Should have thrown a TypeError"); |
|
60 } catch (ex if ex.constructor.name == "TypeError") { |
|
61 // Note that TypeError doesn't carry across compartments |
|
62 do_print("Non-string encoding caused the correct exception"); |
|
63 } |
|
64 }); |
|
65 |
|
66 add_test_pair(function* test_bad_bytes() { |
|
67 do_print("Testing with a bad type for option bytes"); |
|
68 try { |
|
69 yield OS.File.read(EXISTING_FILE, { bytes: "five" }); |
|
70 do_throw("Should have thrown a TypeError"); |
|
71 } catch (ex if ex.constructor.name == "TypeError") { |
|
72 // Note that TypeError doesn't carry across compartments |
|
73 do_print("Non-number bytes caused the correct exception"); |
|
74 } |
|
75 }); |
|
76 |
|
77 add_test_pair(function* read_non_existent() { |
|
78 do_print("Testing with a non-existent file"); |
|
79 try { |
|
80 yield OS.File.read("I/do/not/exist"); |
|
81 do_throw("Should have thrown with an ex.becauseNoSuchFile"); |
|
82 } catch (ex if ex.becauseNoSuchFile) { |
|
83 do_print("Correct exceptions"); |
|
84 } |
|
85 }); |
|
86 |
|
87 function run_test() { |
|
88 run_next_test(); |
|
89 } |