1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/ecma_5/String/replace-throws-nonwritable-lastIndex-global.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,126 @@ 1.4 +/* 1.5 + * Any copyright is dedicated to the Public Domain. 1.6 + * http://creativecommons.org/licenses/publicdomain/ 1.7 + */ 1.8 + 1.9 +var BUGNUMBER = 501739; 1.10 +var summary = 1.11 + "String.prototype.replace should throw when called with a global RegExp " + 1.12 + "whose .lastIndex is non-writable"; 1.13 + 1.14 +print(BUGNUMBER + ": " + summary); 1.15 + 1.16 +/************** 1.17 + * BEGIN TEST * 1.18 + **************/ 1.19 + 1.20 +var s = '0x2x4x6x8'; 1.21 + 1.22 +// First time with .lastIndex === 0, replacing to '' 1.23 + 1.24 +var p1 = /x/g; 1.25 +Object.defineProperty(p1, "lastIndex", { writable: false }); 1.26 + 1.27 +try 1.28 +{ 1.29 + s.replace(p1, ''); 1.30 + throw "didn't throw"; 1.31 +} 1.32 +catch (e) 1.33 +{ 1.34 + assertEq(e instanceof TypeError, true, 1.35 + "should have thrown a TypeError, instead got: " + e); 1.36 + assertEq(p1.lastIndex, 0); 1.37 +} 1.38 + 1.39 +// Second time with .lastIndex !== 0, replacing to '' 1.40 + 1.41 +var p2 = /x/g; 1.42 +Object.defineProperty(p2, "lastIndex", { writable: false, value: 3 }); 1.43 + 1.44 +try 1.45 +{ 1.46 + s.replace(p2, ''); 1.47 + throw "didn't throw"; 1.48 +} 1.49 +catch (e) 1.50 +{ 1.51 + assertEq(e instanceof TypeError, true, 1.52 + "should have thrown a TypeError, instead got: " + e); 1.53 + assertEq(p2.lastIndex, 3); 1.54 +} 1.55 + 1.56 +// Third time with .lastIndex === 0, replacing to 'y' 1.57 + 1.58 +var p3 = /x/g; 1.59 +Object.defineProperty(p3, "lastIndex", { writable: false }); 1.60 + 1.61 +try 1.62 +{ 1.63 + s.replace(p3, 'y'); 1.64 + throw "didn't throw"; 1.65 +} 1.66 +catch (e) 1.67 +{ 1.68 + assertEq(e instanceof TypeError, true, 1.69 + "should have thrown a TypeError, instead got: " + e); 1.70 + assertEq(p3.lastIndex, 0); 1.71 +} 1.72 + 1.73 +// Fourth time with .lastIndex !== 0, replacing to 'y' 1.74 + 1.75 +var p4 = /x/g; 1.76 +Object.defineProperty(p4, "lastIndex", { writable: false, value: 3 }); 1.77 + 1.78 +try 1.79 +{ 1.80 + s.replace(p4, ''); 1.81 + throw "didn't throw"; 1.82 +} 1.83 +catch (e) 1.84 +{ 1.85 + assertEq(e instanceof TypeError, true, 1.86 + "should have thrown a TypeError, instead got: " + e); 1.87 + assertEq(p4.lastIndex, 3); 1.88 +} 1.89 + 1.90 +// Fifth time with .lastIndex === 0, replacing to 'y', but no match 1.91 + 1.92 +var p5 = /q/g; 1.93 +Object.defineProperty(p5, "lastIndex", { writable: false }); 1.94 + 1.95 +try 1.96 +{ 1.97 + s.replace(p5, 'y'); 1.98 + throw "didn't throw"; 1.99 +} 1.100 +catch (e) 1.101 +{ 1.102 + assertEq(e instanceof TypeError, true, 1.103 + "should have thrown a TypeError, instead got: " + e); 1.104 + assertEq(p5.lastIndex, 0); 1.105 +} 1.106 + 1.107 +// Sixth time with .lastIndex !== 0, replacing to 'y', but no match 1.108 + 1.109 +var p6 = /q/g; 1.110 +Object.defineProperty(p6, "lastIndex", { writable: false, value: 3 }); 1.111 + 1.112 +try 1.113 +{ 1.114 + s.replace(p6, ''); 1.115 + throw "didn't throw"; 1.116 +} 1.117 +catch (e) 1.118 +{ 1.119 + assertEq(e instanceof TypeError, true, 1.120 + "should have thrown a TypeError, instead got: " + e); 1.121 + assertEq(p6.lastIndex, 3); 1.122 +} 1.123 + 1.124 +/******************************************************************************/ 1.125 + 1.126 +if (typeof reportCompare === "function") 1.127 + reportCompare(true, true); 1.128 + 1.129 +print("Tests complete");