summaryrefslogtreecommitdiffstats
path: root/toolkit/components/osfile/tests/xpcshell/test_exception.js
diff options
context:
space:
mode:
authorMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
committerMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
commit5f8de423f190bbb79a62f804151bc24824fa32d8 (patch)
tree10027f336435511475e392454359edea8e25895d /toolkit/components/osfile/tests/xpcshell/test_exception.js
parent49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff)
downloadUXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip
Add m-esr52 at 52.6.0
Diffstat (limited to 'toolkit/components/osfile/tests/xpcshell/test_exception.js')
-rw-r--r--toolkit/components/osfile/tests/xpcshell/test_exception.js89
1 files changed, 89 insertions, 0 deletions
diff --git a/toolkit/components/osfile/tests/xpcshell/test_exception.js b/toolkit/components/osfile/tests/xpcshell/test_exception.js
new file mode 100644
index 000000000..1282adb3e
--- /dev/null
+++ b/toolkit/components/osfile/tests/xpcshell/test_exception.js
@@ -0,0 +1,89 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+/**
+ * Test that functions throw the appropriate exceptions.
+ */
+
+"use strict";
+
+var EXISTING_FILE = do_get_file("xpcshell.ini").path;
+
+
+// Tests on |open|
+
+add_test_pair(function test_typeerror() {
+ let exn;
+ try {
+ let fd = yield OS.File.open("/tmp", {no_such_key: 1});
+ do_print("Fd: " + fd);
+ } catch (ex) {
+ exn = ex;
+ }
+ do_print("Exception: " + exn);
+ do_check_true(exn.constructor.name == "TypeError");
+});
+
+// Tests on |read|
+
+add_test_pair(function* test_bad_encoding() {
+ do_print("Testing with a wrong encoding");
+ try {
+ yield OS.File.read(EXISTING_FILE, { encoding: "baby-speak-encoded" });
+ do_throw("Should have thrown with an ex.becauseInvalidArgument");
+ } catch (ex if ex.becauseInvalidArgument) {
+ do_print("Wrong encoding caused the correct exception");
+ }
+
+ try {
+ yield OS.File.read(EXISTING_FILE, { encoding: 4 });
+ do_throw("Should have thrown a TypeError");
+ } catch (ex if ex.constructor.name == "TypeError") {
+ // Note that TypeError doesn't carry across compartments
+ do_print("Non-string encoding caused the correct exception");
+ }
+ });
+
+add_test_pair(function* test_bad_compression() {
+ do_print("Testing with a non-existing compression");
+ try {
+ yield OS.File.read(EXISTING_FILE, { compression: "mmmh-crunchy" });
+ do_throw("Should have thrown with an ex.becauseInvalidArgument");
+ } catch (ex if ex.becauseInvalidArgument) {
+ do_print("Wrong encoding caused the correct exception");
+ }
+
+ do_print("Testing with a bad type for option compression");
+ try {
+ yield OS.File.read(EXISTING_FILE, { compression: 5 });
+ do_throw("Should have thrown a TypeError");
+ } catch (ex if ex.constructor.name == "TypeError") {
+ // Note that TypeError doesn't carry across compartments
+ do_print("Non-string encoding caused the correct exception");
+ }
+});
+
+add_test_pair(function* test_bad_bytes() {
+ do_print("Testing with a bad type for option bytes");
+ try {
+ yield OS.File.read(EXISTING_FILE, { bytes: "five" });
+ do_throw("Should have thrown a TypeError");
+ } catch (ex if ex.constructor.name == "TypeError") {
+ // Note that TypeError doesn't carry across compartments
+ do_print("Non-number bytes caused the correct exception");
+ }
+});
+
+add_test_pair(function* read_non_existent() {
+ do_print("Testing with a non-existent file");
+ try {
+ yield OS.File.read("I/do/not/exist");
+ do_throw("Should have thrown with an ex.becauseNoSuchFile");
+ } catch (ex if ex.becauseNoSuchFile) {
+ do_print("Correct exceptions");
+ }
+});
+
+function run_test() {
+ run_next_test();
+}