toolkit/components/osfile/tests/xpcshell/test_exception.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/osfile/tests/xpcshell/test_exception.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,89 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +/**
     1.8 + * Test that functions throw the appropriate exceptions.
     1.9 + */
    1.10 +
    1.11 +"use strict";
    1.12 +
    1.13 +let EXISTING_FILE = do_get_file("xpcshell.ini").path;
    1.14 +
    1.15 +
    1.16 +// Tests on |open|
    1.17 +
    1.18 +add_test_pair(function test_typeerror() {
    1.19 +  let exn;
    1.20 +  try {
    1.21 +    let fd = yield OS.File.open("/tmp", {no_such_key: 1});
    1.22 +    do_print("Fd: " + fd);
    1.23 +  } catch (ex) {
    1.24 +    exn = ex;
    1.25 +  }
    1.26 +  do_print("Exception: " + exn);
    1.27 +  do_check_true(exn.constructor.name == "TypeError");
    1.28 +});
    1.29 +
    1.30 +// Tests on |read|
    1.31 +
    1.32 +add_test_pair(function* test_bad_encoding() {
    1.33 +  do_print("Testing with a wrong encoding");
    1.34 +  try {
    1.35 +    yield OS.File.read(EXISTING_FILE, { encoding: "baby-speak-encoded" });
    1.36 +    do_throw("Should have thrown with an ex.becauseInvalidArgument");
    1.37 +  } catch (ex if ex.becauseInvalidArgument) {
    1.38 +    do_print("Wrong encoding caused the correct exception");
    1.39 +  }
    1.40 +
    1.41 +  try {
    1.42 +    yield OS.File.read(EXISTING_FILE, { encoding: 4 });
    1.43 +    do_throw("Should have thrown a TypeError");
    1.44 +  } catch (ex if ex.constructor.name == "TypeError") {
    1.45 +    // Note that TypeError doesn't carry across compartments
    1.46 +    do_print("Non-string encoding caused the correct exception");
    1.47 +  }
    1.48 + });
    1.49 +
    1.50 +add_test_pair(function* test_bad_compression() {
    1.51 +  do_print("Testing with a non-existing compression");
    1.52 +  try {
    1.53 +    yield OS.File.read(EXISTING_FILE, { compression: "mmmh-crunchy" });
    1.54 +    do_throw("Should have thrown with an ex.becauseInvalidArgument");
    1.55 +  } catch (ex if ex.becauseInvalidArgument) {
    1.56 +    do_print("Wrong encoding caused the correct exception");
    1.57 +  }
    1.58 +
    1.59 +  do_print("Testing with a bad type for option compression");
    1.60 +  try {
    1.61 +    yield OS.File.read(EXISTING_FILE, { compression: 5 });
    1.62 +    do_throw("Should have thrown a TypeError");
    1.63 +  } catch (ex if ex.constructor.name == "TypeError") {
    1.64 +    // Note that TypeError doesn't carry across compartments
    1.65 +    do_print("Non-string encoding caused the correct exception");
    1.66 +  }
    1.67 +});
    1.68 +
    1.69 +add_test_pair(function* test_bad_bytes() {
    1.70 +  do_print("Testing with a bad type for option bytes");
    1.71 +  try {
    1.72 +    yield OS.File.read(EXISTING_FILE, { bytes: "five" });
    1.73 +    do_throw("Should have thrown a TypeError");
    1.74 +  } catch (ex if ex.constructor.name == "TypeError") {
    1.75 +    // Note that TypeError doesn't carry across compartments
    1.76 +    do_print("Non-number bytes caused the correct exception");
    1.77 +  }
    1.78 +});
    1.79 +
    1.80 +add_test_pair(function* read_non_existent() {
    1.81 +  do_print("Testing with a non-existent file");
    1.82 +  try {
    1.83 +    yield OS.File.read("I/do/not/exist");
    1.84 +    do_throw("Should have thrown with an ex.becauseNoSuchFile");
    1.85 +  } catch (ex if ex.becauseNoSuchFile) {
    1.86 +    do_print("Correct exceptions");
    1.87 +  }
    1.88 +});
    1.89 +
    1.90 +function run_test() {
    1.91 +  run_next_test();
    1.92 +}

mercurial