OLD | NEW |
1 description("createDocument tests modeled after mozilla's testing"); | 1 description("createDocument tests modeled after mozilla's testing"); |
2 | 2 |
3 function stringForExceptionCode(c) | 3 shouldThrow("document.implementation.createDocumentType('foo')", "'TypeError: No
t enough arguments'"); |
4 { | 4 shouldThrow("document.implementation.createDocumentType('foo', null)", "'TypeErr
or: Not enough arguments'"); |
5 var exceptionName; | 5 shouldThrow("document.implementation.createDocumentType(undefined, undefined)",
"'TypeError: Not enough arguments'"); |
6 switch(c) { | 6 shouldThrow("document.implementation.createDocumentType(null, undefined)", "'Typ
eError: Not enough arguments'"); |
7 case DOMException.INVALID_CHARACTER_ERR: | 7 shouldThrow("document.implementation.createDocumentType(undefined, null)", "'Typ
eError: Not enough arguments'"); |
8 exceptionName = "INVALID_CHARACTER_ERR"; | 8 shouldNotThrow("document.implementation.createDocumentType(undefined, undefined,
null)"); |
9 break; | 9 shouldThrow("document.implementation.createDocumentType(null, null)", "'TypeErro
r: Not enough arguments'"); |
10 case DOMException.NAMESPACE_ERR: | 10 shouldThrow("document.implementation.createDocumentType(null, '')", "'TypeError:
Not enough arguments'"); |
11 exceptionName = "NAMESPACE_ERR"; | 11 shouldThrow("document.implementation.createDocumentType('', null)", "'TypeError:
Not enough arguments'"); |
12 } | 12 shouldThrow("document.implementation.createDocumentType('', '')", "'TypeError: N
ot enough arguments'"); |
13 if (exceptionName) | 13 shouldThrow("document.implementation.createDocumentType('a:', null, null)", "'Na
mespaceError: An attempt was made to create or change an object in a way which i
s incorrect with regard to namespaces.'"); |
14 return exceptionName; // + "(" + c + ")"; | 14 shouldThrow("document.implementation.createDocumentType(':foo', null, null)", "'
NamespaceError: An attempt was made to create or change an object in a way which
is incorrect with regard to namespaces.'"); |
15 return c; | 15 shouldThrow("document.implementation.createDocumentType(':', null, null)", "'Nam
espaceError: An attempt was made to create or change an object in a way which is
incorrect with regard to namespaces.'"); |
16 } | 16 shouldNotThrow("document.implementation.createDocumentType('foo', null, null)"); |
| 17 shouldNotThrow("document.implementation.createDocumentType('foo:bar', null, null
)"); |
| 18 shouldThrow("document.implementation.createDocumentType('foo::bar', null, null)"
, "'NamespaceError: An attempt was made to create or change an object in a way w
hich is incorrect with regard to namespaces.'"); |
| 19 shouldThrow("document.implementation.createDocumentType('\t:bar', null, null)",
"'InvalidCharacterError: The string contains invalid characters.'"); |
| 20 shouldThrow("document.implementation.createDocumentType('foo:\t', null, null)",
"'InvalidCharacterError: The string contains invalid characters.'"); |
| 21 shouldThrow("document.implementation.createDocumentType('foo :bar', null, null)"
, "'InvalidCharacterError: The string contains invalid characters.'"); |
| 22 shouldThrow("document.implementation.createDocumentType('foo: bar', null, null)"
, "'InvalidCharacterError: The string contains invalid characters.'"); |
| 23 shouldThrow("document.implementation.createDocumentType('a:b:c', null, null)", "
'NamespaceError: An attempt was made to create or change an object in a way whic
h is incorrect with regard to namespaces.'"); |
17 | 24 |
18 function assertEquals(actual, expect, m) | |
19 { | |
20 if (actual !== expect) { | |
21 m += "; expected " + stringForExceptionCode(expect) + ", threw " + strin
gForExceptionCode(actual); | |
22 testFailed(m); | |
23 } else { | |
24 m += "; threw " + stringForExceptionCode(actual);; | |
25 testPassed(m); | |
26 } | |
27 } | |
28 | |
29 var allTests = [ | |
30 { args: [undefined, undefined], code: 5 }, | |
31 { args: [null, undefined], code: 5 }, | |
32 { args: [undefined, null], code: 5 }, | |
33 { args: [undefined, undefined, null], code: 5 }, | |
34 { args: [null, null], code: 5 }, | |
35 { args: [null, null, null], code: 5 }, | |
36 { args: [null, ""], code: 5 }, | |
37 { args: ["", null], code: 5 }, | |
38 { args: ["", ""], code: 5 }, | |
39 { args: ["a:", null, null], code: 14 }, | |
40 { args: [":foo", null, null], code: 14 }, | |
41 { args: [":", null, null], code: 14 }, | |
42 { args: ["foo", null, null] }, | |
43 { args: ["foo:bar", null, null] }, | |
44 { args: ["foo::bar", null, null], code: 14 }, | |
45 { args: ["\t:bar", null, null], code: 5 }, | |
46 { args: ["foo:\t", null, null], code: 5 }, | |
47 { args: ["foo :bar", null, null], code: 5 }, | |
48 { args: ["foo: bar", null, null], code: 5 }, | |
49 { args: ["a:b:c", null, null], code: 14, message: "valid XML name, invalid QN
ame" }, | |
50 ]; | |
51 | |
52 function sourceify(v) | |
53 { | |
54 switch (typeof v) { | |
55 case "undefined": | |
56 return v; | |
57 case "string": | |
58 return '"' + v.replace('"', '\\"') + '"'; | |
59 default: | |
60 return String(v); | |
61 } | |
62 } | |
63 | |
64 function sourceifyArgs(args) | |
65 { | |
66 var copy = new Array(args.length); | |
67 for (var i = 0, sz = args.length; i < sz; i++) | |
68 copy[i] = sourceify(args[i]); | |
69 | |
70 return copy.join(", "); | |
71 } | |
72 | |
73 function runTests(tests, createFunctionName) | |
74 { | |
75 for (var i = 0, sz = tests.length; i < sz; i++) { | |
76 var test = tests[i]; | |
77 | |
78 var code = -1; | |
79 var argStr = sourceifyArgs(test.args); | |
80 var msg = createFunctionName + "(" + argStr + ")"; | |
81 if ("message" in test) | |
82 msg += "; " + test.message; | |
83 try { | |
84 document.implementation[createFunctionName].apply(document.implement
ation, test.args); | |
85 if ('code' in test) | |
86 testFailed(msg + " expected exception: " + test.code); | |
87 else | |
88 testPassed(msg); | |
89 } catch (e) { | |
90 assertEquals(e.code, test.code || "expected no exception", msg); | |
91 } | |
92 } | |
93 } | |
94 | |
95 // Moz throws a "Not enough arguments" exception in these, we don't: | |
96 shouldBeEqualToString("document.implementation.createDocumentType('foo').toStrin
g()", "[object DocumentType]"); | |
97 shouldBeEqualToString("document.implementation.createDocumentType('foo', null).t
oString()", "[object DocumentType]"); | |
98 runTests(allTests, "createDocumentType"); | |
OLD | NEW |