michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: /** michael@0: * Test that functions throw the appropriate exceptions. michael@0: */ michael@0: michael@0: "use strict"; michael@0: michael@0: let EXISTING_FILE = do_get_file("xpcshell.ini").path; michael@0: michael@0: michael@0: // Tests on |open| michael@0: michael@0: add_test_pair(function test_typeerror() { michael@0: let exn; michael@0: try { michael@0: let fd = yield OS.File.open("/tmp", {no_such_key: 1}); michael@0: do_print("Fd: " + fd); michael@0: } catch (ex) { michael@0: exn = ex; michael@0: } michael@0: do_print("Exception: " + exn); michael@0: do_check_true(exn.constructor.name == "TypeError"); michael@0: }); michael@0: michael@0: // Tests on |read| michael@0: michael@0: add_test_pair(function* test_bad_encoding() { michael@0: do_print("Testing with a wrong encoding"); michael@0: try { michael@0: yield OS.File.read(EXISTING_FILE, { encoding: "baby-speak-encoded" }); michael@0: do_throw("Should have thrown with an ex.becauseInvalidArgument"); michael@0: } catch (ex if ex.becauseInvalidArgument) { michael@0: do_print("Wrong encoding caused the correct exception"); michael@0: } michael@0: michael@0: try { michael@0: yield OS.File.read(EXISTING_FILE, { encoding: 4 }); michael@0: do_throw("Should have thrown a TypeError"); michael@0: } catch (ex if ex.constructor.name == "TypeError") { michael@0: // Note that TypeError doesn't carry across compartments michael@0: do_print("Non-string encoding caused the correct exception"); michael@0: } michael@0: }); michael@0: michael@0: add_test_pair(function* test_bad_compression() { michael@0: do_print("Testing with a non-existing compression"); michael@0: try { michael@0: yield OS.File.read(EXISTING_FILE, { compression: "mmmh-crunchy" }); michael@0: do_throw("Should have thrown with an ex.becauseInvalidArgument"); michael@0: } catch (ex if ex.becauseInvalidArgument) { michael@0: do_print("Wrong encoding caused the correct exception"); michael@0: } michael@0: michael@0: do_print("Testing with a bad type for option compression"); michael@0: try { michael@0: yield OS.File.read(EXISTING_FILE, { compression: 5 }); michael@0: do_throw("Should have thrown a TypeError"); michael@0: } catch (ex if ex.constructor.name == "TypeError") { michael@0: // Note that TypeError doesn't carry across compartments michael@0: do_print("Non-string encoding caused the correct exception"); michael@0: } michael@0: }); michael@0: michael@0: add_test_pair(function* test_bad_bytes() { michael@0: do_print("Testing with a bad type for option bytes"); michael@0: try { michael@0: yield OS.File.read(EXISTING_FILE, { bytes: "five" }); michael@0: do_throw("Should have thrown a TypeError"); michael@0: } catch (ex if ex.constructor.name == "TypeError") { michael@0: // Note that TypeError doesn't carry across compartments michael@0: do_print("Non-number bytes caused the correct exception"); michael@0: } michael@0: }); michael@0: michael@0: add_test_pair(function* read_non_existent() { michael@0: do_print("Testing with a non-existent file"); michael@0: try { michael@0: yield OS.File.read("I/do/not/exist"); michael@0: do_throw("Should have thrown with an ex.becauseNoSuchFile"); michael@0: } catch (ex if ex.becauseNoSuchFile) { michael@0: do_print("Correct exceptions"); michael@0: } michael@0: }); michael@0: michael@0: function run_test() { michael@0: run_next_test(); michael@0: }