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) { |
| 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 |
| 21 assert_throws(new TypeError(), () => { |
| 22 recordTest.setStringLongRecord(undefined); |
| 23 }, "Converting undefined to record should be rejected."); |
| 24 |
| 25 recordTest.setNullableStringLongRecord(null); |
| 26 assert_equals(recordTest.getNullableStringLongRecord(), |
| 27 null, "Passing null to a nullable record works"); |
| 28 |
| 29 recordTest.setNullableStringLongRecord(undefined); |
| 30 assert_equals(recordTest.getNullableStringLongRecord(), |
| 31 null, "Passing undefined to a nullable record works"); |
| 32 }, "Test handling of null and undefined records"); |
| 33 |
| 34 test(() => { |
| 35 let recordTest = internals.recordTest(); |
| 36 |
| 37 recordTest.setStringLongRecord({a: true, false: null, c: "foo"}); |
| 38 assert_record_equals(recordTest.getStringLongRecord(), |
| 39 [['a', 1], ['false', 0], ['c', 0]], |
| 40 "Types are properly coerced to the record's types"); |
| 41 }, "Test type conversion"); |
| 42 |
| 43 test(() => { |
| 44 let recordTest = internals.recordTest(); |
| 45 |
| 46 recordTest.setStringLongRecord({false: 1, [false]: 42}); |
| 47 assert_record_equals(recordTest.getStringLongRecord(), [['false', 42]], |
| 48 "Key types are coerced and never repeated"); |
| 49 }, "Test duplicate keys"); |
| 50 |
| 51 test(() => { |
| 52 let recordTest = internals.recordTest(); |
| 53 |
| 54 recordTest.setStringLongRecord({z: 42, foo: -5, ABC: 0}); |
| 55 assert_record_equals(recordTest.getStringLongRecord(), |
| 56 [['z', 42], ['foo', -5], ['ABC', 0]], |
| 57 "Keys are not sorted"); |
| 58 }, "Test mapping order"); |
| 59 |
| 60 test(() => { |
| 61 let recordTest = internals.recordTest(); |
| 62 |
| 63 assert_throws(new TypeError(), () => { |
| 64 recordTest.setByteStringByteStringRecord({'a': 'bc', '\uFFFF': 'foo'}); |
| 65 }, "Invalid ByteString key must throw a TypeError"); |
| 66 |
| 67 assert_throws(new TypeError(), () => { |
| 68 recordTest.setByteStringByteStringRecord({'xy': 'z', 'foo': '\uFFFF'}); |
| 69 }, "Invalid ByteString value must throw a TypeError"); |
| 70 }, "Test ByteString validation"); |
| 71 |
| 72 test(() => { |
| 73 let recordTest = internals.recordTest(); |
| 74 |
| 75 recordTest.setStringLongRecord({'foo': 0}); |
| 76 let record = recordTest.getStringLongRecord(); |
| 77 assert_record_equals(record, [['foo', 0]]); |
| 78 |
| 79 record.baz = 'quux'; |
| 80 assert_equals(Object.keys(recordTest.getStringLongRecord()).length, 1, |
| 81 "Changing a returned record does not change the record"); |
| 82 assert_record_equals(recordTest.getStringLongRecord(), [['foo', 0]], |
| 83 "Changing a returned record does not change the record
"); |
| 84 |
| 85 assert_record_equals(recordTest.getStringLongRecord(), |
| 86 Object.entries(recordTest.getStringLongRecord()), |
| 87 "Record getters always return the same elements"); |
| 88 assert_not_equals(recordTest.getStringLongRecord(), recordTest.getStringLon
gRecord(), |
| 89 "Record getters always return a new copy"); |
| 90 }, "Test records are passed by value"); |
| 91 |
| 92 test(() => { |
| 93 let recordTest = internals.recordTest(); |
| 94 |
| 95 assert_false(recordTest.unionReceivedARecord(true), |
| 96 "Passing 'true' should convert the union to boolean"); |
| 97 assert_false(recordTest.unionReceivedARecord(false), |
| 98 "Passing 'false' should convert the union to boolean"); |
| 99 assert_false(recordTest.unionReceivedARecord(42), |
| 100 "Passing a number should convert the union to boolean"); |
| 101 assert_true(recordTest.unionReceivedARecord([1, 2, 3]), |
| 102 "Passing an array should convert the union to a record"); |
| 103 assert_true(recordTest.unionReceivedARecord({}), |
| 104 "Passing an object should conver the union to a record"); |
| 105 }, "Test unions resolve records"); |
| 106 |
| 107 test(() => { |
| 108 let recordTest = internals.recordTest(); |
| 109 |
| 110 let elem = document.createElement('p'); |
| 111 recordTest.setStringElementRecord({'elem': elem}); |
| 112 assert_equals(recordTest.getStringElementRecord().elem, elem, |
| 113 "The same Oilpan-based value was stored in the record"); |
| 114 assert_not_equals(recordTest.getStringElementRecord().elem, |
| 115 document.createElement('p'), |
| 116 "The same Oilpan-based value was stored in the record"); |
| 117 |
| 118 elem = document.createElement('br'); |
| 119 assert_not_equals(recordTest.getStringElementRecord().elem, elem, |
| 120 "Changing the original value does not change the record v
alue"); |
| 121 }, "Test Oilpan-based types"); |
| 122 |
| 123 test(() => { |
| 124 let recordTest = internals.recordTest(); |
| 125 let getterAlias = recordTest.getUSVStringUSVStringBooleanRecordRecord; |
| 126 |
| 127 recordTest.setUSVStringUSVStringBooleanRecordRecord({'foo': {'bar': true},
'quux': {'baz': false}}); |
| 128 let records = recordTest.getUSVStringUSVStringBooleanRecordRecord(); |
| 129 assert_array_equals(Object.keys(records), ['foo', 'quux'], |
| 130 "Nested record types have the correct keys"); |
| 131 assert_record_equals(records.foo, [['bar', true]]); |
| 132 assert_record_equals(records.quux, [['baz', false]]); |
| 133 }, "Test nested record types"); |
| 134 </script> |
OLD | NEW |