1/** 2 * Runs tests for <http://heycam.github.io/webidl/#es-stringifier>. 3 * @param {Object} aObject - object to test 4 * @param {string} aAttribute - IDL attribute name that is annotated with `stringifier` 5 * @param {boolean} aIsUnforgeable - whether the IDL attribute is `[LegacyUnforgeable]` 6 */ 7function test_stringifier_attribute(aObject, aAttribute, aIsUnforgeable) { 8 // Step 1. 9 test(function() { 10 [null, undefined].forEach(function(v) { 11 assert_throws_js(TypeError, function() { 12 aObject.toString.call(v); 13 }); 14 }); 15 }); 16 17 // TODO Step 2: security check. 18 19 // Step 3. 20 test(function() { 21 assert_false("Window" in window && aObject instanceof window.Window); 22 [{}, window].forEach(function(v) { 23 assert_throws_js(TypeError, function() { 24 aObject.toString.call(v) 25 }); 26 }); 27 }); 28 29 // Step 4-6. 30 var expected_value; 31 test(function() { 32 expected_value = aObject[aAttribute]; 33 assert_equals(aObject[aAttribute], expected_value, 34 "The attribute " + aAttribute + " should be pure."); 35 }); 36 37 var test_error = { name: "test" }; 38 test(function() { 39 if (!aIsUnforgeable) { 40 Object.defineProperty(aObject, aAttribute, { 41 configurable: true, 42 get: function() { throw test_error; } 43 }); 44 } 45 assert_equals(aObject.toString(), expected_value); 46 }); 47 48 test(function() { 49 if (!aIsUnforgeable) { 50 Object.defineProperty(aObject, aAttribute, { 51 configurable: true, 52 value: { toString: function() { throw test_error; } } 53 }); 54 } 55 assert_equals(aObject.toString(), expected_value); 56 }); 57} 58