Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1 | // Copyright 2014 the V8 project authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // Tests taken from: |
| 6 | // http://mathias.html5.org/tests/javascript/string/ |
| 7 | |
| 8 | assertEquals('_'.anchor('b'), '<a name="b">_</a>'); |
| 9 | assertEquals('<'.anchor('<'), '<a name="<"><</a>'); |
| 10 | assertEquals('_'.anchor(0x2A), '<a name="42">_</a>'); |
| 11 | assertEquals('_'.anchor('\x22'), '<a name=""">_</a>'); |
| 12 | assertEquals(String.prototype.anchor.call(0x2A, 0x2A), '<a name="42">42</a>'); |
| 13 | assertThrows(function() { |
| 14 | String.prototype.anchor.call(undefined); |
| 15 | }, TypeError); |
| 16 | assertThrows(function() { |
| 17 | String.prototype.anchor.call(null); |
| 18 | }, TypeError); |
| 19 | assertEquals(String.prototype.anchor.length, 1); |
| 20 | |
| 21 | assertEquals('_'.big(), '<big>_</big>'); |
| 22 | assertEquals('<'.big(), '<big><</big>'); |
| 23 | assertEquals(String.prototype.big.call(0x2A), '<big>42</big>'); |
| 24 | assertThrows(function() { |
| 25 | String.prototype.big.call(undefined); |
| 26 | }, TypeError); |
| 27 | assertThrows(function() { |
| 28 | String.prototype.big.call(null); |
| 29 | }, TypeError); |
| 30 | assertEquals(String.prototype.big.length, 0); |
| 31 | |
| 32 | assertEquals('_'.blink(), '<blink>_</blink>'); |
| 33 | assertEquals('<'.blink(), '<blink><</blink>'); |
| 34 | assertEquals(String.prototype.blink.call(0x2A), '<blink>42</blink>'); |
| 35 | assertThrows(function() { |
| 36 | String.prototype.blink.call(undefined); |
| 37 | }, TypeError); |
| 38 | assertThrows(function() { |
| 39 | String.prototype.blink.call(null); |
| 40 | }, TypeError); |
| 41 | assertEquals(String.prototype.blink.length, 0); |
| 42 | |
| 43 | assertEquals('_'.bold(), '<b>_</b>'); |
| 44 | assertEquals('<'.bold(), '<b><</b>'); |
| 45 | assertEquals(String.prototype.bold.call(0x2A), '<b>42</b>'); |
| 46 | assertThrows(function() { |
| 47 | String.prototype.bold.call(undefined); |
| 48 | }, TypeError); |
| 49 | assertThrows(function() { |
| 50 | String.prototype.bold.call(null); |
| 51 | }, TypeError); |
| 52 | assertEquals(String.prototype.bold.length, 0); |
| 53 | |
| 54 | assertEquals('_'.fixed(), '<tt>_</tt>'); |
| 55 | assertEquals('<'.fixed(), '<tt><</tt>'); |
| 56 | assertEquals(String.prototype.fixed.call(0x2A), '<tt>42</tt>'); |
| 57 | assertThrows(function() { |
| 58 | String.prototype.fixed.call(undefined); |
| 59 | }, TypeError); |
| 60 | assertThrows(function() { |
| 61 | String.prototype.fixed.call(null); |
| 62 | }, TypeError); |
| 63 | assertEquals(String.prototype.fixed.length, 0); |
| 64 | |
| 65 | assertEquals('_'.fontcolor('b'), '<font color="b">_</font>'); |
| 66 | assertEquals('<'.fontcolor('<'), '<font color="<"><</font>'); |
| 67 | assertEquals('_'.fontcolor(0x2A), '<font color="42">_</font>'); |
| 68 | assertEquals('_'.fontcolor('\x22'), '<font color=""">_</font>'); |
| 69 | assertEquals(String.prototype.fontcolor.call(0x2A, 0x2A), |
| 70 | '<font color="42">42</font>'); |
| 71 | assertThrows(function() { |
| 72 | String.prototype.fontcolor.call(undefined); |
| 73 | }, TypeError); |
| 74 | assertThrows(function() { |
| 75 | String.prototype.fontcolor.call(null); |
| 76 | }, TypeError); |
| 77 | assertEquals(String.prototype.fontcolor.length, 1); |
| 78 | |
| 79 | assertEquals('_'.fontsize('b'), '<font size="b">_</font>'); |
| 80 | assertEquals('<'.fontsize('<'), '<font size="<"><</font>'); |
| 81 | assertEquals('_'.fontsize(0x2A), '<font size="42">_</font>'); |
| 82 | assertEquals('_'.fontsize('\x22'), '<font size=""">_</font>'); |
| 83 | assertEquals(String.prototype.fontsize.call(0x2A, 0x2A), |
| 84 | '<font size="42">42</font>'); |
| 85 | assertThrows(function() { |
| 86 | String.prototype.fontsize.call(undefined); |
| 87 | }, TypeError); |
| 88 | assertThrows(function() { |
| 89 | String.prototype.fontsize.call(null); |
| 90 | }, TypeError); |
| 91 | assertEquals(String.prototype.fontsize.length, 1); |
| 92 | |
| 93 | assertEquals('_'.italics(), '<i>_</i>'); |
| 94 | assertEquals('<'.italics(), '<i><</i>'); |
| 95 | assertEquals(String.prototype.italics.call(0x2A), '<i>42</i>'); |
| 96 | assertThrows(function() { |
| 97 | String.prototype.italics.call(undefined); |
| 98 | }, TypeError); |
| 99 | assertThrows(function() { |
| 100 | String.prototype.italics.call(null); |
| 101 | }, TypeError); |
| 102 | assertEquals(String.prototype.italics.length, 0); |
| 103 | |
| 104 | assertEquals('_'.link('b'), '<a href="b">_</a>'); |
| 105 | assertEquals('<'.link('<'), '<a href="<"><</a>'); |
| 106 | assertEquals('_'.link(0x2A), '<a href="42">_</a>'); |
| 107 | assertEquals('_'.link('\x22'), '<a href=""">_</a>'); |
| 108 | assertEquals(String.prototype.link.call(0x2A, 0x2A), '<a href="42">42</a>'); |
| 109 | assertThrows(function() { |
| 110 | String.prototype.link.call(undefined); |
| 111 | }, TypeError); |
| 112 | assertThrows(function() { |
| 113 | String.prototype.link.call(null); |
| 114 | }, TypeError); |
| 115 | assertEquals(String.prototype.link.length, 1); |
| 116 | |
| 117 | assertEquals('_'.small(), '<small>_</small>'); |
| 118 | assertEquals('<'.small(), '<small><</small>'); |
| 119 | assertEquals(String.prototype.small.call(0x2A), '<small>42</small>'); |
| 120 | assertThrows(function() { |
| 121 | String.prototype.small.call(undefined); |
| 122 | }, TypeError); |
| 123 | assertThrows(function() { |
| 124 | String.prototype.small.call(null); |
| 125 | }, TypeError); |
| 126 | assertEquals(String.prototype.small.length, 0); |
| 127 | |
| 128 | assertEquals('_'.strike(), '<strike>_</strike>'); |
| 129 | assertEquals('<'.strike(), '<strike><</strike>'); |
| 130 | assertEquals(String.prototype.strike.call(0x2A), '<strike>42</strike>'); |
| 131 | assertThrows(function() { |
| 132 | String.prototype.strike.call(undefined); |
| 133 | }, TypeError); |
| 134 | assertThrows(function() { |
| 135 | String.prototype.strike.call(null); |
| 136 | }, TypeError); |
| 137 | assertEquals(String.prototype.strike.length, 0); |
| 138 | |
| 139 | assertEquals('_'.sub(), '<sub>_</sub>'); |
| 140 | assertEquals('<'.sub(), '<sub><</sub>'); |
| 141 | assertEquals(String.prototype.sub.call(0x2A), '<sub>42</sub>'); |
| 142 | assertThrows(function() { |
| 143 | String.prototype.sub.call(undefined); |
| 144 | }, TypeError); |
| 145 | assertThrows(function() { |
| 146 | String.prototype.sub.call(null); |
| 147 | }, TypeError); |
| 148 | assertEquals(String.prototype.sub.length, 0); |
| 149 | |
| 150 | assertEquals('_'.sup(), '<sup>_</sup>'); |
| 151 | assertEquals('<'.sup(), '<sup><</sup>'); |
| 152 | assertEquals(String.prototype.sup.call(0x2A), '<sup>42</sup>'); |
| 153 | assertThrows(function() { |
| 154 | String.prototype.sup.call(undefined); |
| 155 | }, TypeError); |
| 156 | assertThrows(function() { |
| 157 | String.prototype.sup.call(null); |
| 158 | }, TypeError); |
| 159 | assertEquals(String.prototype.sup.length, 0); |
Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 160 | |
| 161 | |
| 162 | (function TestToString() { |
| 163 | var calls = 0; |
| 164 | var obj = { |
| 165 | toString() { |
| 166 | calls++; |
| 167 | return 'abc'; |
| 168 | }, |
| 169 | valueOf() { |
| 170 | assertUnreachable(); |
| 171 | } |
| 172 | }; |
| 173 | |
| 174 | var methodNames = [ |
| 175 | 'anchor', |
| 176 | 'big', |
| 177 | 'blink', |
| 178 | 'bold', |
| 179 | 'fixed', |
| 180 | 'fontcolor', |
| 181 | 'fontsize', |
| 182 | 'italics', |
| 183 | 'link', |
| 184 | 'small', |
| 185 | 'strike', |
| 186 | 'sub', |
| 187 | 'sup', |
| 188 | ]; |
| 189 | for (var name of methodNames) { |
| 190 | calls = 0; |
| 191 | String.prototype[name].call(obj); |
| 192 | assertEquals(1, calls); |
| 193 | } |
| 194 | })(); |
| 195 | |
| 196 | |
| 197 | (function TestDeleteStringRelace() { |
| 198 | assertEquals('<a name="n">s</a>', 's'.anchor('n')); |
| 199 | assertTrue(delete String.prototype.replace); |
| 200 | assertEquals('<a name="n">s</a>', 's'.anchor('n')); |
| 201 | })(); |