• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
8assertEquals('_'.anchor('b'), '<a name="b">_</a>');
9assertEquals('<'.anchor('<'), '<a name="<"><</a>');
10assertEquals('_'.anchor(0x2A), '<a name="42">_</a>');
11assertEquals('_'.anchor('\x22'), '<a name="&quot;">_</a>');
12assertEquals(String.prototype.anchor.call(0x2A, 0x2A), '<a name="42">42</a>');
13assertThrows(function() {
14  String.prototype.anchor.call(undefined);
15}, TypeError);
16assertThrows(function() {
17  String.prototype.anchor.call(null);
18}, TypeError);
19assertEquals(String.prototype.anchor.length, 1);
20
21assertEquals('_'.big(), '<big>_</big>');
22assertEquals('<'.big(), '<big><</big>');
23assertEquals(String.prototype.big.call(0x2A), '<big>42</big>');
24assertThrows(function() {
25  String.prototype.big.call(undefined);
26}, TypeError);
27assertThrows(function() {
28  String.prototype.big.call(null);
29}, TypeError);
30assertEquals(String.prototype.big.length, 0);
31
32assertEquals('_'.blink(), '<blink>_</blink>');
33assertEquals('<'.blink(), '<blink><</blink>');
34assertEquals(String.prototype.blink.call(0x2A), '<blink>42</blink>');
35assertThrows(function() {
36  String.prototype.blink.call(undefined);
37}, TypeError);
38assertThrows(function() {
39  String.prototype.blink.call(null);
40}, TypeError);
41assertEquals(String.prototype.blink.length, 0);
42
43assertEquals('_'.bold(), '<b>_</b>');
44assertEquals('<'.bold(), '<b><</b>');
45assertEquals(String.prototype.bold.call(0x2A), '<b>42</b>');
46assertThrows(function() {
47  String.prototype.bold.call(undefined);
48}, TypeError);
49assertThrows(function() {
50  String.prototype.bold.call(null);
51}, TypeError);
52assertEquals(String.prototype.bold.length, 0);
53
54assertEquals('_'.fixed(), '<tt>_</tt>');
55assertEquals('<'.fixed(), '<tt><</tt>');
56assertEquals(String.prototype.fixed.call(0x2A), '<tt>42</tt>');
57assertThrows(function() {
58  String.prototype.fixed.call(undefined);
59}, TypeError);
60assertThrows(function() {
61  String.prototype.fixed.call(null);
62}, TypeError);
63assertEquals(String.prototype.fixed.length, 0);
64
65assertEquals('_'.fontcolor('b'), '<font color="b">_</font>');
66assertEquals('<'.fontcolor('<'), '<font color="<"><</font>');
67assertEquals('_'.fontcolor(0x2A), '<font color="42">_</font>');
68assertEquals('_'.fontcolor('\x22'), '<font color="&quot;">_</font>');
69assertEquals(String.prototype.fontcolor.call(0x2A, 0x2A),
70  '<font color="42">42</font>');
71assertThrows(function() {
72  String.prototype.fontcolor.call(undefined);
73}, TypeError);
74assertThrows(function() {
75  String.prototype.fontcolor.call(null);
76}, TypeError);
77assertEquals(String.prototype.fontcolor.length, 1);
78
79assertEquals('_'.fontsize('b'), '<font size="b">_</font>');
80assertEquals('<'.fontsize('<'), '<font size="<"><</font>');
81assertEquals('_'.fontsize(0x2A), '<font size="42">_</font>');
82assertEquals('_'.fontsize('\x22'), '<font size="&quot;">_</font>');
83assertEquals(String.prototype.fontsize.call(0x2A, 0x2A),
84  '<font size="42">42</font>');
85assertThrows(function() {
86  String.prototype.fontsize.call(undefined);
87}, TypeError);
88assertThrows(function() {
89  String.prototype.fontsize.call(null);
90}, TypeError);
91assertEquals(String.prototype.fontsize.length, 1);
92
93assertEquals('_'.italics(), '<i>_</i>');
94assertEquals('<'.italics(), '<i><</i>');
95assertEquals(String.prototype.italics.call(0x2A), '<i>42</i>');
96assertThrows(function() {
97  String.prototype.italics.call(undefined);
98}, TypeError);
99assertThrows(function() {
100  String.prototype.italics.call(null);
101}, TypeError);
102assertEquals(String.prototype.italics.length, 0);
103
104assertEquals('_'.link('b'), '<a href="b">_</a>');
105assertEquals('<'.link('<'), '<a href="<"><</a>');
106assertEquals('_'.link(0x2A), '<a href="42">_</a>');
107assertEquals('_'.link('\x22'), '<a href="&quot;">_</a>');
108assertEquals(String.prototype.link.call(0x2A, 0x2A), '<a href="42">42</a>');
109assertThrows(function() {
110  String.prototype.link.call(undefined);
111}, TypeError);
112assertThrows(function() {
113  String.prototype.link.call(null);
114}, TypeError);
115assertEquals(String.prototype.link.length, 1);
116
117assertEquals('_'.small(), '<small>_</small>');
118assertEquals('<'.small(), '<small><</small>');
119assertEquals(String.prototype.small.call(0x2A), '<small>42</small>');
120assertThrows(function() {
121  String.prototype.small.call(undefined);
122}, TypeError);
123assertThrows(function() {
124  String.prototype.small.call(null);
125}, TypeError);
126assertEquals(String.prototype.small.length, 0);
127
128assertEquals('_'.strike(), '<strike>_</strike>');
129assertEquals('<'.strike(), '<strike><</strike>');
130assertEquals(String.prototype.strike.call(0x2A), '<strike>42</strike>');
131assertThrows(function() {
132  String.prototype.strike.call(undefined);
133}, TypeError);
134assertThrows(function() {
135  String.prototype.strike.call(null);
136}, TypeError);
137assertEquals(String.prototype.strike.length, 0);
138
139assertEquals('_'.sub(), '<sub>_</sub>');
140assertEquals('<'.sub(), '<sub><</sub>');
141assertEquals(String.prototype.sub.call(0x2A), '<sub>42</sub>');
142assertThrows(function() {
143  String.prototype.sub.call(undefined);
144}, TypeError);
145assertThrows(function() {
146  String.prototype.sub.call(null);
147}, TypeError);
148assertEquals(String.prototype.sub.length, 0);
149
150assertEquals('_'.sup(), '<sup>_</sup>');
151assertEquals('<'.sup(), '<sup><</sup>');
152assertEquals(String.prototype.sup.call(0x2A), '<sup>42</sup>');
153assertThrows(function() {
154  String.prototype.sup.call(undefined);
155}, TypeError);
156assertThrows(function() {
157  String.prototype.sup.call(null);
158}, TypeError);
159assertEquals(String.prototype.sup.length, 0);
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})();
202