OLD | NEW |
---|---|
(Empty) | |
1 <!DOCTYPE html> | |
2 <script src="../resources/testharness.js"></script> | |
3 <script src="../resources/testharnessreport.js"></script> | |
4 <script> | |
5 /* assert_array_equals() uses same_value() to compare each item, but | |
6 * Object.entries() returns an array of arrays, so we need to compare | |
7 * each item with assert_array_equals instead. */ | |
8 function assert_record_equals(obj, expected, description) { | |
Yuki
2017/03/09 10:10:08
nit: s/obj/actual/
Raphael Kubo da Costa (rakuco)
2017/03/09 16:08:34
Done.
| |
9 for (let i = 0; i < expected.length; ++i) { | |
10 assert_array_equals(Object.entries(obj)[i], expected[i], description); | |
11 } | |
12 } | |
13 | |
14 test(() => { | |
15 let recordTest = internals.recordTest(); | |
16 | |
17 assert_throws(new TypeError(), () => { | |
18 recordTest.setStringLongRecord(null); | |
19 }, "Converting null to record should be rejected."); | |
20 assert_throws(new TypeError(), () => { | |
21 recordTest.setStringLongRecord(undefined); | |
22 }, "Converting undefined to record should be rejected."); | |
23 recordTest.setNullableStringLongRecord(null); | |
24 assert_equals(recordTest.getNullableStringLongRecord(), | |
25 null, "Passing null to a nullable record works"); | |
26 recordTest.setNullableStringLongRecord(undefined); | |
27 assert_equals(recordTest.getNullableStringLongRecord(), | |
28 null, "Passing undefined to a nullable record works"); | |
29 }, "Test handling of null and undefined records"); | |
30 | |
31 test(() => { | |
32 let recordTest = internals.recordTest(); | |
33 | |
34 recordTest.setStringLongRecord({a: true, false: null, c: "foo"}); | |
35 assert_record_equals(recordTest.getStringLongRecord(), | |
36 [['a', 1], ['false', 0], ['c', 0]], | |
37 "Types are properly coerced to the record's types"); | |
38 }, "Test type conversion"); | |
39 | |
40 test(() => { | |
41 let recordTest = internals.recordTest(); | |
42 | |
43 recordTest.setStringLongRecord({false: 1, [false]: 42}); | |
44 assert_record_equals(recordTest.getStringLongRecord(), [['false', 42]], | |
45 "Key types are coerced and never repeated"); | |
46 }, "Test duplicate keys"); | |
47 | |
48 test(() => { | |
49 let recordTest = internals.recordTest(); | |
50 | |
51 recordTest.setStringLongRecord({z: 42, foo: -5, ABC: 0}); | |
52 assert_record_equals(recordTest.getStringLongRecord(), | |
53 [['z', 42], ['foo', -5], ['ABC', 0]], | |
54 "Keys are not sorted"); | |
55 }, "Test mapping order"); | |
56 | |
57 test(() => { | |
58 let recordTest = internals.recordTest(); | |
59 | |
60 assert_throws(new TypeError(), () => { | |
61 recordTest.setByteStringByteStringRecord({'a': 'bc', '\uFFFF': 'foo'}); | |
62 }, "Invalid ByteString key must throw a TypeError"); | |
63 assert_throws(new TypeError(), () => { | |
64 recordTest.setByteStringByteStringRecord({'xy': 'z', 'foo': '\uFFFF'}); | |
65 }, "Invalid ByteString value must throw a TypeError"); | |
66 }, "Test ByteString validation"); | |
67 | |
68 test(() => { | |
69 let recordTest = internals.recordTest(); | |
70 | |
71 recordTest.setStringLongRecord({'foo': 0}); | |
72 let record = recordTest.getStringLongRecord(); | |
73 assert_record_equals(record, [['foo', 0]]); | |
74 | |
75 record.baz = 'quux'; | |
76 assert_equals(Object.keys(recordTest.getStringLongRecord()).length, 1, | |
77 "Changing a returned record does not change the record"); | |
78 assert_record_equals(recordTest.getStringLongRecord(), [['foo', 0]], | |
79 "Changing a returned record does not change the record") ; | |
80 | |
81 assert_record_equals(recordTest.getStringLongRecord(), | |
82 Object.entries(recordTest.getStringLongRecord()), | |
83 "Record getters always return the same elements"); | |
84 assert_not_equals(recordTest.getStringLongRecord(), recordTest.getStringLongR ecord(), | |
85 "Record getters always return a new copy"); | |
86 }, "Test records are passed by value"); | |
87 | |
88 test(() => { | |
89 let recordTest = internals.recordTest(); | |
90 | |
91 assert_false(recordTest.unionReceivedARecord(true), | |
92 "Passing 'true' should convert the union to boolean"); | |
93 assert_false(recordTest.unionReceivedARecord(false), | |
94 "Passing 'false' should convert the union to boolean"); | |
95 assert_false(recordTest.unionReceivedARecord(42), | |
96 "Passing a number should convert the union to boolean"); | |
97 assert_true(recordTest.unionReceivedARecord([1, 2, 3]), | |
98 "Passing an array should convert the union to a record"); | |
99 assert_true(recordTest.unionReceivedARecord({}), | |
100 "Passing an object should conver the union to a record"); | |
101 }, "Test unions resolve records"); | |
102 | |
103 test(() => { | |
104 let recordTest = internals.recordTest(); | |
105 | |
106 let elem = document.createElement('p'); | |
107 recordTest.setStringElementRecord({'elem': elem}); | |
108 assert_equals(recordTest.getStringElementRecord().elem, elem, | |
109 "The same Oilpan-based value was stored in the record"); | |
Yuki
2017/03/09 10:10:08
nit: s/Oilpan-based value/DOM object/
Raphael Kubo da Costa (rakuco)
2017/03/09 16:08:34
Done.
| |
110 assert_not_equals(recordTest.getStringElementRecord().elem, | |
111 document.createElement('p'), | |
112 "The same Oilpan-based value was stored in the record"); | |
113 | |
114 elem = document.createElement('br'); | |
115 assert_not_equals(recordTest.getStringElementRecord().elem, elem, | |
116 "Changing the original value does not change the record val ue"); | |
117 }, "Test Oilpan-based types"); | |
Yuki
2017/03/09 10:10:08
nit: s/Oilpan-based/DOM Object/
Raphael Kubo da Costa (rakuco)
2017/03/09 16:08:34
Done.
| |
118 | |
119 test(() => { | |
120 let recordTest = internals.recordTest(); | |
121 let getterAlias = recordTest.getUSVStringUSVStringBooleanRecordRecord; | |
122 | |
123 recordTest.setUSVStringUSVStringBooleanRecordRecord({'foo': {'bar': true}, 'q uux': {'baz': false}}); | |
124 let records = recordTest.getUSVStringUSVStringBooleanRecordRecord(); | |
125 assert_array_equals(Object.keys(records), ['foo', 'quux'], | |
126 "Nested record types have the correct keys"); | |
127 assert_record_equals(records.foo, [['bar', true]]); | |
128 assert_record_equals(records.quux, [['baz', false]]); | |
129 }, "Test nested record types"); | |
130 | |
131 test(() => { | |
132 let recordTest = internals.recordTest(); | |
133 | |
134 let record = recordTest.returnStringByteStringSequenceRecord(); | |
135 assert_array_equals(Object.keys(record), ['foo', 'bar']); | |
136 assert_array_equals(record.foo, ['hello, world', 'hi, mom']); | |
137 assert_array_equals(record.bar, ['goodbye, mom']); | |
138 | |
139 let other_record = recordTest.returnStringByteStringSequenceRecord(); | |
140 assert_not_equals(record, other_record, "A new object is returned each time") ; | |
141 }, "Test sequences in records"); | |
142 </script> | |
OLD | NEW |