summaryrefslogtreecommitdiffstats
path: root/toolkit/components/osfile/tests/xpcshell/test_exception.js
blob: 1282adb3e17a546b6afa600b64b3d4e17a4c1420 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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();
}