alloy-js-test-san 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (91) hide show
  1. data/README.rdoc +83 -0
  2. data/Rakefile +27 -0
  3. data/VERSION.yml +4 -0
  4. data/bin/jstest +10 -0
  5. data/lib/js_test_san/runner.rb +70 -0
  6. data/lib/js_test_san/test.rb +17 -0
  7. data/lib/js_test_san/test_case.rb +103 -0
  8. data/lib/js_test_san.rb +2 -0
  9. data/test/fixtures/a_unit_test.html +27 -0
  10. data/test/fixtures/a_unit_test.js +22 -0
  11. data/test/regression/prototype/unit/ajax_test.html +29 -0
  12. data/test/regression/prototype/unit/array_test.html +28 -0
  13. data/test/regression/prototype/unit/assets/prototype.js +4900 -0
  14. data/test/regression/prototype/unit/assets/unittest.css +50 -0
  15. data/test/regression/prototype/unit/assets/unittest.js +615 -0
  16. data/test/regression/prototype/unit/assets/unittest_transport.js +1 -0
  17. data/test/regression/prototype/unit/base_test.html +27 -0
  18. data/test/regression/prototype/unit/class_test.html +27 -0
  19. data/test/regression/prototype/unit/date_test.html +27 -0
  20. data/test/regression/prototype/unit/dom_test.html +304 -0
  21. data/test/regression/prototype/unit/element_mixins_test.html +30 -0
  22. data/test/regression/prototype/unit/enumerable_test.html +35 -0
  23. data/test/regression/prototype/unit/event_test.html +31 -0
  24. data/test/regression/prototype/unit/fixtures/ajax.html +2 -0
  25. data/test/regression/prototype/unit/fixtures/ajax.js +42 -0
  26. data/test/regression/prototype/unit/fixtures/array.html +1 -0
  27. data/test/regression/prototype/unit/fixtures/class.js +83 -0
  28. data/test/regression/prototype/unit/fixtures/content.html +1 -0
  29. data/test/regression/prototype/unit/fixtures/data.json +1 -0
  30. data/test/regression/prototype/unit/fixtures/dom.css +84 -0
  31. data/test/regression/prototype/unit/fixtures/dom.html +278 -0
  32. data/test/regression/prototype/unit/fixtures/dom.js +17 -0
  33. data/test/regression/prototype/unit/fixtures/element_mixins.html +4 -0
  34. data/test/regression/prototype/unit/fixtures/element_mixins.js +2 -0
  35. data/test/regression/prototype/unit/fixtures/empty.html +0 -0
  36. data/test/regression/prototype/unit/fixtures/empty.js +1 -0
  37. data/test/regression/prototype/unit/fixtures/enumerable.html +8 -0
  38. data/test/regression/prototype/unit/fixtures/enumerable.js +23 -0
  39. data/test/regression/prototype/unit/fixtures/event.html +4 -0
  40. data/test/regression/prototype/unit/fixtures/form.html +108 -0
  41. data/test/regression/prototype/unit/fixtures/function.js +13 -0
  42. data/test/regression/prototype/unit/fixtures/hash.js +25 -0
  43. data/test/regression/prototype/unit/fixtures/hello.js +1 -0
  44. data/test/regression/prototype/unit/fixtures/logo.gif +0 -0
  45. data/test/regression/prototype/unit/fixtures/object.html +6 -0
  46. data/test/regression/prototype/unit/fixtures/object.js +7 -0
  47. data/test/regression/prototype/unit/fixtures/position.html +9 -0
  48. data/test/regression/prototype/unit/fixtures/selector.html +71 -0
  49. data/test/regression/prototype/unit/fixtures/string.js +8 -0
  50. data/test/regression/prototype/unit/fixtures/unittest.html +18 -0
  51. data/test/regression/prototype/unit/form_test.html +135 -0
  52. data/test/regression/prototype/unit/function_test.html +27 -0
  53. data/test/regression/prototype/unit/hash_test.html +27 -0
  54. data/test/regression/prototype/unit/number_test.html +27 -0
  55. data/test/regression/prototype/unit/object_test.html +32 -0
  56. data/test/regression/prototype/unit/periodical_executer_test.html +27 -0
  57. data/test/regression/prototype/unit/position_test.html +36 -0
  58. data/test/regression/prototype/unit/prototype_test.html +27 -0
  59. data/test/regression/prototype/unit/range_test.html +27 -0
  60. data/test/regression/prototype/unit/regexp_test.html +27 -0
  61. data/test/regression/prototype/unit/selector_test.html +98 -0
  62. data/test/regression/prototype/unit/string_test.html +27 -0
  63. data/test/regression/prototype/unit/tests/ajax_test.js +379 -0
  64. data/test/regression/prototype/unit/tests/array_test.js +186 -0
  65. data/test/regression/prototype/unit/tests/base_test.js +43 -0
  66. data/test/regression/prototype/unit/tests/class_test.js +136 -0
  67. data/test/regression/prototype/unit/tests/date_test.js +5 -0
  68. data/test/regression/prototype/unit/tests/dom_test.js +1493 -0
  69. data/test/regression/prototype/unit/tests/element_mixins_test.js +32 -0
  70. data/test/regression/prototype/unit/tests/enumerable_test.js +271 -0
  71. data/test/regression/prototype/unit/tests/event_test.js +235 -0
  72. data/test/regression/prototype/unit/tests/form_test.js +382 -0
  73. data/test/regression/prototype/unit/tests/function_test.js +133 -0
  74. data/test/regression/prototype/unit/tests/hash_test.js +178 -0
  75. data/test/regression/prototype/unit/tests/number_test.js +44 -0
  76. data/test/regression/prototype/unit/tests/object_test.js +180 -0
  77. data/test/regression/prototype/unit/tests/periodical_executer_test.js +15 -0
  78. data/test/regression/prototype/unit/tests/position_test.js +44 -0
  79. data/test/regression/prototype/unit/tests/prototype_test.js +43 -0
  80. data/test/regression/prototype/unit/tests/range_test.js +58 -0
  81. data/test/regression/prototype/unit/tests/regexp_test.js +42 -0
  82. data/test/regression/prototype/unit/tests/selector_test.js +408 -0
  83. data/test/regression/prototype/unit/tests/string_test.js +548 -0
  84. data/test/regression/prototype/unit/tests/unittest_test.js +148 -0
  85. data/test/regression/prototype/unit/unittest_test.html +45 -0
  86. data/test/regression/prototype/upstream +1 -0
  87. data/test/test_helper.rb +20 -0
  88. data/test/unit/runner_test.rb +165 -0
  89. data/test/unit/test_case_test.rb +167 -0
  90. data/test/unit/test_test.rb +24 -0
  91. metadata +145 -0
@@ -0,0 +1,548 @@
1
+ new Test.Unit.Runner({
2
+ testInterpret: function(){
3
+ this.assertIdentical('true', String.interpret(true));
4
+ this.assertIdentical('123', String.interpret(123));
5
+ this.assertIdentical('foo bar', String.interpret('foo bar'));
6
+ this.assertIdentical(
7
+ 'object string',
8
+ String.interpret({ toString: function(){ return 'object string' } }));
9
+
10
+ this.assertIdentical('0', String.interpret(0));
11
+ this.assertIdentical('false', String.interpret(false));
12
+ this.assertIdentical('', String.interpret(undefined));
13
+ this.assertIdentical('', String.interpret(null));
14
+ this.assertIdentical('', String.interpret(''));
15
+ },
16
+
17
+ testGsubWithReplacementFunction: function() {
18
+ var source = 'foo boo boz';
19
+
20
+ this.assertEqual('Foo Boo BoZ',
21
+ source.gsub(/[^o]+/, function(match) {
22
+ return match[0].toUpperCase()
23
+ }));
24
+ this.assertEqual('f2 b2 b1z',
25
+ source.gsub(/o+/, function(match) {
26
+ return match[0].length;
27
+ }));
28
+ this.assertEqual('f0 b0 b1z',
29
+ source.gsub(/o+/, function(match) {
30
+ return match[0].length % 2;
31
+ }));
32
+
33
+ },
34
+
35
+ testGsubWithReplacementString: function() {
36
+ var source = 'foo boo boz';
37
+
38
+ this.assertEqual('foobooboz',
39
+ source.gsub(/\s+/, ''));
40
+ this.assertEqual(' z',
41
+ source.gsub(/(.)(o+)/, ''));
42
+
43
+ this.assertEqual('ウィメンズ2007<br/>クルーズコレクション',
44
+ 'ウィメンズ2007\nクルーズコレクション'.gsub(/\n/,'<br/>'));
45
+ this.assertEqual('ウィメンズ2007<br/>クルーズコレクション',
46
+ 'ウィメンズ2007\nクルーズコレクション'.gsub('\n','<br/>'));
47
+
48
+ this.assertEqual('barfbarobarobar barbbarobarobar barbbarobarzbar',
49
+ source.gsub('', 'bar'));
50
+ this.assertEqual('barfbarobarobar barbbarobarobar barbbarobarzbar',
51
+ source.gsub(new RegExp(''), 'bar'));
52
+ },
53
+
54
+ testGsubWithReplacementTemplateString: function() {
55
+ var source = 'foo boo boz';
56
+
57
+ this.assertEqual('-oo-#{1}- -oo-#{1}- -o-#{1}-z',
58
+ source.gsub(/(.)(o+)/, '-#{2}-\\#{1}-'));
59
+ this.assertEqual('-foo-f- -boo-b- -bo-b-z',
60
+ source.gsub(/(.)(o+)/, '-#{0}-#{1}-'));
61
+ this.assertEqual('-oo-f- -oo-b- -o-b-z',
62
+ source.gsub(/(.)(o+)/, '-#{2}-#{1}-'));
63
+ this.assertEqual(' z',
64
+ source.gsub(/(.)(o+)/, '#{3}'));
65
+ },
66
+
67
+ testGsubWithTroublesomeCharacters: function() {
68
+ this.assertEqual('ab', 'a|b'.gsub('|', ''));
69
+ //'ab'.gsub('', ''); // freeze
70
+ this.assertEqual('ab', 'ab(?:)'.gsub('(?:)', ''));
71
+ this.assertEqual('ab', 'ab()'.gsub('()', ''));
72
+ this.assertEqual('ab', 'ab'.gsub('^', ''));
73
+ this.assertEqual('ab', 'a?b'.gsub('?', ''))
74
+ this.assertEqual('ab', 'a+b'.gsub('+', ''));
75
+ this.assertEqual('ab', 'a*b'.gsub('*', ''));
76
+ this.assertEqual('ab', 'a{1}b'.gsub('{1}', ''));
77
+ this.assertEqual('ab', 'a.b'.gsub('.', ''));
78
+ },
79
+
80
+ testSubWithReplacementFunction: function() {
81
+ var source = 'foo boo boz';
82
+
83
+ this.assertEqual('Foo boo boz',
84
+ source.sub(/[^o]+/, function(match) {
85
+ return match[0].toUpperCase()
86
+ }), 1);
87
+ this.assertEqual('Foo Boo boz',
88
+ source.sub(/[^o]+/, function(match) {
89
+ return match[0].toUpperCase()
90
+ }, 2), 2);
91
+ this.assertEqual(source,
92
+ source.sub(/[^o]+/, function(match) {
93
+ return match[0].toUpperCase()
94
+ }, 0), 0);
95
+ this.assertEqual(source,
96
+ source.sub(/[^o]+/, function(match) {
97
+ return match[0].toUpperCase()
98
+ }, -1), -1);
99
+ },
100
+
101
+ testSubWithReplacementString: function() {
102
+ var source = 'foo boo boz';
103
+
104
+ this.assertEqual('oo boo boz',
105
+ source.sub(/[^o]+/, ''));
106
+ this.assertEqual('oooo boz',
107
+ source.sub(/[^o]+/, '', 2));
108
+ this.assertEqual('-f-oo boo boz',
109
+ source.sub(/[^o]+/, '-#{0}-'));
110
+ this.assertEqual('-f-oo- b-oo boz',
111
+ source.sub(/[^o]+/, '-#{0}-', 2));
112
+ },
113
+
114
+ testScan: function() {
115
+ var source = 'foo boo boz', results = [];
116
+ var str = source.scan(/[o]+/, function(match) {
117
+ results.push(match[0].length);
118
+ });
119
+ this.assertEnumEqual([2, 2, 1], results);
120
+ this.assertEqual(source, source.scan(/x/, this.fail));
121
+ this.assert(typeof str == 'string');
122
+ },
123
+
124
+ testToArray: function() {
125
+ this.assertEnumEqual([],''.toArray());
126
+ this.assertEnumEqual(['a'],'a'.toArray());
127
+ this.assertEnumEqual(['a','b'],'ab'.toArray());
128
+ this.assertEnumEqual(['f','o','o'],'foo'.toArray());
129
+ },
130
+
131
+ /*
132
+ Note that camelize() differs from its Rails counterpart,
133
+ as it is optimized for dealing with JavaScript object
134
+ properties in conjunction with CSS property names:
135
+ - Looks for dashes, not underscores
136
+ - CamelCases first word if there is a front dash
137
+ */
138
+ testCamelize: function() {
139
+ this.assertEqual('', ''.camelize());
140
+ this.assertEqual('', '-'.camelize());
141
+ this.assertEqual('foo', 'foo'.camelize());
142
+ this.assertEqual('foo_bar', 'foo_bar'.camelize());
143
+ this.assertEqual('FooBar', '-foo-bar'.camelize());
144
+ this.assertEqual('FooBar', 'FooBar'.camelize());
145
+
146
+ this.assertEqual('fooBar', 'foo-bar'.camelize());
147
+ this.assertEqual('borderBottomWidth', 'border-bottom-width'.camelize());
148
+
149
+ this.assertEqual('classNameTest','class-name-test'.camelize());
150
+ this.assertEqual('classNameTest','className-test'.camelize());
151
+ this.assertEqual('classNameTest','class-nameTest'.camelize());
152
+
153
+ /* this.benchmark(function(){
154
+ 'class-name-test'.camelize();
155
+ },10000); */
156
+ },
157
+
158
+ testCapitalize: function() {
159
+ this.assertEqual('',''.capitalize());
160
+ this.assertEqual('Ä','ä'.capitalize());
161
+ this.assertEqual('A','A'.capitalize());
162
+ this.assertEqual('Hello','hello'.capitalize());
163
+ this.assertEqual('Hello','HELLO'.capitalize());
164
+ this.assertEqual('Hello','Hello'.capitalize());
165
+ this.assertEqual('Hello world','hello WORLD'.capitalize());
166
+ },
167
+
168
+ testUnderscore: function() {
169
+ this.assertEqual('', ''.underscore());
170
+ this.assertEqual('_', '-'.underscore());
171
+ this.assertEqual('foo', 'foo'.underscore());
172
+ this.assertEqual('foo', 'Foo'.underscore());
173
+ this.assertEqual('foo_bar', 'foo_bar'.underscore());
174
+ this.assertEqual('border_bottom', 'borderBottom'.underscore());
175
+ this.assertEqual('border_bottom_width', 'borderBottomWidth'.underscore());
176
+ this.assertEqual('border_bottom_width', 'border-Bottom-Width'.underscore());
177
+ },
178
+
179
+ testDasherize: function() {
180
+ this.assertEqual('', ''.dasherize());
181
+ this.assertEqual('foo', 'foo'.dasherize());
182
+ this.assertEqual('Foo', 'Foo'.dasherize());
183
+ this.assertEqual('foo-bar', 'foo-bar'.dasherize());
184
+ this.assertEqual('border-bottom-width', 'border_bottom_width'.dasherize());
185
+ },
186
+
187
+ testTruncate: function() {
188
+ var source = 'foo boo boz foo boo boz foo boo boz foo boo boz';
189
+ this.assertEqual(source, source.truncate(source.length));
190
+ this.assertEqual('foo boo boz foo boo boz foo...', source.truncate(0));
191
+ this.assertEqual('fo...', source.truncate(5));
192
+ this.assertEqual('foo b', source.truncate(5, ''));
193
+
194
+ this.assert(typeof 'foo'.truncate(5) == 'string');
195
+ this.assert(typeof 'foo bar baz'.truncate(5) == 'string');
196
+ },
197
+
198
+ testStrip: function() {
199
+ this.assertEqual('hello world', ' hello world '.strip());
200
+ this.assertEqual('hello world', 'hello world'.strip());
201
+ this.assertEqual('hello \n world', ' hello \n world '.strip());
202
+ this.assertEqual('', ' '.strip());
203
+ },
204
+
205
+ testStripTags: function() {
206
+ this.assertEqual('hello world', 'hello world'.stripTags());
207
+ this.assertEqual('hello world', 'hello <span>world</span>'.stripTags());
208
+ this.assertEqual('hello world', '<a href="#" onclick="moo!">hello</a> world'.stripTags());
209
+ this.assertEqual('hello world', 'h<b><em>e</em></b>l<i>l</i>o w<span class="moo" id="x"><b>o</b></span>rld'.stripTags());
210
+ this.assertEqual('1\n2', '1\n2'.stripTags());
211
+ },
212
+
213
+ testStripScripts: function() {
214
+ this.assertEqual('foo bar', 'foo bar'.stripScripts());
215
+ this.assertEqual('foo bar', ('foo <script>boo();<'+'/script>bar').stripScripts());
216
+ this.assertEqual('foo bar', ('foo <script type="text/javascript">boo();\nmoo();<'+'/script>bar').stripScripts());
217
+ },
218
+
219
+ testExtractScripts: function() {
220
+ this.assertEnumEqual([], 'foo bar'.extractScripts());
221
+ this.assertEnumEqual(['boo();'], ('foo <script>boo();<'+'/script>bar').extractScripts());
222
+ this.assertEnumEqual(['boo();','boo();\nmoo();'],
223
+ ('foo <script>boo();<'+'/script><script type="text/javascript">boo();\nmoo();<'+'/script>bar').extractScripts());
224
+ this.assertEnumEqual(['boo();','boo();\nmoo();'],
225
+ ('foo <script>boo();<'+'/script>blub\nblub<script type="text/javascript">boo();\nmoo();<'+'/script>bar').extractScripts());
226
+ },
227
+
228
+ testEvalScripts: function() {
229
+ this.assertEqual(0, evalScriptsCounter);
230
+
231
+ ('foo <script>evalScriptsCounter++<'+'/script>bar').evalScripts();
232
+ this.assertEqual(1, evalScriptsCounter);
233
+
234
+ var stringWithScripts = '';
235
+ (3).times(function(){ stringWithScripts += 'foo <script>evalScriptsCounter++<'+'/script>bar' });
236
+ stringWithScripts.evalScripts();
237
+ this.assertEqual(4, evalScriptsCounter);
238
+ },
239
+
240
+ testEscapeHTML: function() {
241
+ this.assertEqual('foo bar', 'foo bar'.escapeHTML());
242
+ this.assertEqual('foo &lt;span&gt;bar&lt;/span&gt;', 'foo <span>bar</span>'.escapeHTML());
243
+ this.assertEqual('foo ß bar', 'foo ß bar'.escapeHTML());
244
+
245
+ this.assertEqual('ウィメンズ2007\nクルーズコレクション',
246
+ 'ウィメンズ2007\nクルーズコレクション'.escapeHTML());
247
+
248
+ this.assertEqual('a&lt;a href="blah"&gt;blub&lt;/a&gt;b&lt;span&gt;&lt;div&gt;&lt;/div&gt;&lt;/span&gt;cdef&lt;strong&gt;!!!!&lt;/strong&gt;g',
249
+ 'a<a href="blah">blub</a>b<span><div></div></span>cdef<strong>!!!!</strong>g'.escapeHTML());
250
+
251
+ this.assertEqual(largeTextEscaped, largeTextUnescaped.escapeHTML());
252
+
253
+ this.assertEqual('1\n2', '1\n2'.escapeHTML());
254
+
255
+ this.benchmark(function() { largeTextUnescaped.escapeHTML() }, 1000);
256
+ },
257
+
258
+ testUnescapeHTML: function() {
259
+ this.assertEqual('foo bar', 'foo bar'.unescapeHTML());
260
+ this.assertEqual('foo <span>bar</span>', 'foo &lt;span&gt;bar&lt;/span&gt;'.unescapeHTML());
261
+ this.assertEqual('foo ß bar', 'foo ß bar'.unescapeHTML());
262
+
263
+ this.assertEqual('a<a href="blah">blub</a>b<span><div></div></span>cdef<strong>!!!!</strong>g',
264
+ 'a&lt;a href="blah"&gt;blub&lt;/a&gt;b&lt;span&gt;&lt;div&gt;&lt;/div&gt;&lt;/span&gt;cdef&lt;strong&gt;!!!!&lt;/strong&gt;g'.unescapeHTML());
265
+
266
+ this.assertEqual(largeTextUnescaped, largeTextEscaped.unescapeHTML());
267
+
268
+ this.assertEqual('1\n2', '1\n2'.unescapeHTML());
269
+ this.assertEqual('Pride & Prejudice', '<h1>Pride &amp; Prejudice</h1>'.unescapeHTML());
270
+
271
+ this.assertIdentical('&lt;', '&amp;lt;'.unescapeHTML());
272
+
273
+ this.benchmark(function() { largeTextEscaped.unescapeHTML() }, 1000);
274
+
275
+ },
276
+
277
+ testTemplateEvaluation: function() {
278
+ var source = '<tr><td>#{name}</td><td>#{age}</td></tr>';
279
+ var person = {name: 'Sam', age: 21};
280
+ var template = new Template(source);
281
+
282
+ this.assertEqual('<tr><td>Sam</td><td>21</td></tr>',
283
+ template.evaluate(person));
284
+ this.assertEqual('<tr><td></td><td></td></tr>',
285
+ template.evaluate({}));
286
+ },
287
+
288
+ testTemplateEvaluationWithEmptyReplacement: function() {
289
+ var template = new Template('##{}');
290
+ this.assertEqual('#', template.evaluate({}));
291
+ this.assertEqual('#', template.evaluate({foo: 'bar'}));
292
+
293
+ template = new Template('#{}');
294
+ this.assertEqual('', template.evaluate({}));
295
+ },
296
+
297
+ testTemplateEvaluationWithFalses: function() {
298
+ var source = '<tr><td>#{zero}</td><td>#{_false}</td><td>#{undef}</td><td>#{_null}</td><td>#{empty}</td></tr>';
299
+ var falses = {zero:0, _false:false, undef:undefined, _null:null, empty:""};
300
+ var template = new Template(source);
301
+
302
+ this.assertEqual('<tr><td>0</td><td>false</td><td></td><td></td><td></td></tr>',
303
+ template.evaluate(falses));
304
+ },
305
+
306
+ testTemplateEvaluationWithNested: function() {
307
+ var source = '#{name} #{manager.name} #{manager.age} #{manager.undef} #{manager.age.undef} #{colleagues.first.name}';
308
+ var subject = { manager: { name: 'John', age: 29 }, name: 'Stephan', age: 22, colleagues: { first: { name: 'Mark' }} };
309
+ this.assertEqual('Stephan', new Template('#{name}').evaluate(subject));
310
+ this.assertEqual('John', new Template('#{manager.name}').evaluate(subject));
311
+ this.assertEqual('29', new Template('#{manager.age}').evaluate(subject));
312
+ this.assertEqual('', new Template('#{manager.undef}').evaluate(subject));
313
+ this.assertEqual('', new Template('#{manager.age.undef}').evaluate(subject));
314
+ this.assertEqual('Mark', new Template('#{colleagues.first.name}').evaluate(subject));
315
+ this.assertEqual('Stephan John 29 Mark', new Template(source).evaluate(subject));
316
+ },
317
+
318
+ testTemplateEvaluationWithIndexing: function() {
319
+ var source = '#{0} = #{[0]} - #{1} = #{[1]} - #{[2][0]} - #{[2].name} - #{first[0]} - #{[first][0]} - #{[\]]} - #{first[\]]}';
320
+ var subject = [ 'zero', 'one', [ 'two-zero' ] ];
321
+ subject[2].name = 'two-zero-name';
322
+ subject.first = subject[2];
323
+ subject[']'] = '\\';
324
+ subject.first[']'] = 'first\\';
325
+ this.assertEqual('zero', new Template('#{[0]}').evaluate(subject), "#{[0]}");
326
+ this.assertEqual('one', new Template('#{[1]}').evaluate(subject), "#{[1]}");
327
+ this.assertEqual('two-zero', new Template('#{[2][0]}').evaluate(subject), '#{[2][0]}');
328
+ this.assertEqual('two-zero-name', new Template('#{[2].name}').evaluate(subject), '#{[2].name}');
329
+ this.assertEqual('two-zero', new Template('#{first[0]}').evaluate(subject), '#{first[0]}');
330
+ this.assertEqual('\\', new Template('#{[\]]}').evaluate(subject), '#{[\]]}');
331
+ this.assertEqual('first\\', new Template('#{first[\]]}').evaluate(subject), '#{first[\]]}');
332
+ this.assertEqual('empty - empty2', new Template('#{[]} - #{m[]}').evaluate({ '': 'empty', m: {'': 'empty2'}}), '#{[]} - #{m[]}');
333
+ this.assertEqual('zero = zero - one = one - two-zero - two-zero-name - two-zero - two-zero - \\ - first\\', new Template(source).evaluate(subject));
334
+ },
335
+
336
+ testTemplateToTemplateReplacements: function() {
337
+ var source = 'My name is #{name}, my job is #{job}';
338
+ var subject = {
339
+ name: 'Stephan',
340
+ getJob: function() { return 'Web developer'; },
341
+ toTemplateReplacements: function() { return { name: this.name, job: this.getJob() } }
342
+ };
343
+ this.assertEqual('My name is Stephan, my job is Web developer', new Template(source).evaluate(subject));
344
+ },
345
+
346
+ testTemplateEvaluationCombined: function() {
347
+ var source = '#{name} is #{age} years old, managed by #{manager.name}, #{manager.age}.\n' +
348
+ 'Colleagues include #{colleagues[0].name} and #{colleagues[1].name}.';
349
+ var subject = {
350
+ name: 'Stephan', age: 22,
351
+ manager: { name: 'John', age: 29 },
352
+ colleagues: [ { name: 'Mark' }, { name: 'Indy' } ]
353
+ };
354
+ this.assertEqual('Stephan is 22 years old, managed by John, 29.\n' +
355
+ 'Colleagues include Mark and Indy.',
356
+ new Template(source).evaluate(subject));
357
+ },
358
+
359
+ testInterpolate: function() {
360
+ var subject = { name: 'Stephan' };
361
+ var pattern = /(^|.|\r|\n)(#\((.*?)\))/;
362
+ this.assertEqual('#{name}: Stephan', '\\#{name}: #{name}'.interpolate(subject));
363
+ this.assertEqual('#(name): Stephan', '\\#(name): #(name)'.interpolate(subject, pattern));
364
+ },
365
+
366
+ testToQueryParams: function() {
367
+ // only the query part
368
+ var result = {a:undefined, b:'c'};
369
+ this.assertHashEqual({}, ''.toQueryParams(), 'empty query');
370
+ this.assertHashEqual({}, 'foo?'.toQueryParams(), 'empty query with URL');
371
+ this.assertHashEqual(result, 'foo?a&b=c'.toQueryParams(), 'query with URL');
372
+ this.assertHashEqual(result, 'foo?a&b=c#fragment'.toQueryParams(), 'query with URL and fragment');
373
+ this.assertHashEqual(result, 'a;b=c'.toQueryParams(';'), 'custom delimiter');
374
+
375
+ this.assertHashEqual({a:undefined}, 'a'.toQueryParams(), 'key without value');
376
+ this.assertHashEqual({a:'b'}, 'a=b&=c'.toQueryParams(), 'empty key');
377
+ this.assertHashEqual({a:'b', c:''}, 'a=b&c='.toQueryParams(), 'empty value');
378
+
379
+ this.assertHashEqual({'a b':'c', d:'e f', g:'h'},
380
+ 'a%20b=c&d=e%20f&g=h'.toQueryParams(), 'proper decoding');
381
+ this.assertHashEqual({a:'b=c=d'}, 'a=b=c=d'.toQueryParams(), 'multiple equal signs');
382
+ this.assertHashEqual({a:'b', c:'d'}, '&a=b&&&c=d'.toQueryParams(), 'proper splitting');
383
+
384
+ this.assertEnumEqual($w('r g b'), 'col=r&col=g&col=b'.toQueryParams()['col'],
385
+ 'collection without square brackets');
386
+ var msg = 'empty values inside collection';
387
+ this.assertEnumEqual(['r', '', 'b'], 'c=r&c=&c=b'.toQueryParams()['c'], msg);
388
+ this.assertEnumEqual(['', 'blue'], 'c=&c=blue'.toQueryParams()['c'], msg);
389
+ this.assertEnumEqual(['blue', ''], 'c=blue&c='.toQueryParams()['c'], msg);
390
+ },
391
+
392
+ testInspect: function() {
393
+ this.assertEqual('\'\'', ''.inspect());
394
+ this.assertEqual('\'test\'', 'test'.inspect());
395
+ this.assertEqual('\'test \\\'test\\\' "test"\'', 'test \'test\' "test"'.inspect());
396
+ this.assertEqual('\"test \'test\' \\"test\\"\"', 'test \'test\' "test"'.inspect(true));
397
+ this.assertEqual('\'\\b\\t\\n\\f\\r"\\\\\'', '\b\t\n\f\r"\\'.inspect());
398
+ this.assertEqual('\"\\b\\t\\n\\f\\r\\"\\\\\"', '\b\t\n\f\r"\\'.inspect(true));
399
+ this.assertEqual('\'\\b\\t\\n\\f\\r\'', '\x08\x09\x0a\x0c\x0d'.inspect());
400
+ this.assertEqual('\'\\u001a\'', '\x1a'.inspect());
401
+ },
402
+
403
+ testInclude: function() {
404
+ this.assert('hello world'.include('h'));
405
+ this.assert('hello world'.include('hello'));
406
+ this.assert('hello world'.include('llo w'));
407
+ this.assert('hello world'.include('world'));
408
+ this.assert(!'hello world'.include('bye'));
409
+ this.assert(!''.include('bye'));
410
+ },
411
+
412
+ testStartsWith: function() {
413
+ this.assert('hello world'.startsWith('h'));
414
+ this.assert('hello world'.startsWith('hello'));
415
+ this.assert(!'hello world'.startsWith('bye'));
416
+ this.assert(!''.startsWith('bye'));
417
+ this.assert(!'hell'.startsWith('hello'));
418
+ },
419
+
420
+ testEndsWith: function() {
421
+ this.assert('hello world'.endsWith('d'));
422
+ this.assert('hello world'.endsWith(' world'));
423
+ this.assert(!'hello world'.endsWith('planet'));
424
+ this.assert(!''.endsWith('planet'));
425
+ this.assert('hello world world'.endsWith(' world'));
426
+ this.assert(!'z'.endsWith('az'));
427
+ },
428
+
429
+ testBlank: function() {
430
+ this.assert(''.blank());
431
+ this.assert(' '.blank());
432
+ this.assert('\t\r\n '.blank());
433
+ this.assert(!'a'.blank());
434
+ this.assert(!'\t y \n'.blank());
435
+ },
436
+
437
+ testEmpty: function() {
438
+ this.assert(''.empty());
439
+ this.assert(!' '.empty());
440
+ this.assert(!'\t\r\n '.empty());
441
+ this.assert(!'a'.empty());
442
+ this.assert(!'\t y \n'.empty());
443
+ },
444
+
445
+ testSucc: function() {
446
+ this.assertEqual('b', 'a'.succ());
447
+ this.assertEqual('B', 'A'.succ());
448
+ this.assertEqual('1', '0'.succ());
449
+ this.assertEqual('abce', 'abcd'.succ());
450
+ this.assertEqual('{', 'z'.succ());
451
+ this.assertEqual(':', '9'.succ());
452
+ },
453
+
454
+ testTimes: function() {
455
+
456
+ this.assertEqual('', ''.times(0));
457
+ this.assertEqual('', ''.times(5));
458
+ this.assertEqual('', 'a'.times(-1));
459
+ this.assertEqual('', 'a'.times(0));
460
+ this.assertEqual('a', 'a'.times(1));
461
+ this.assertEqual('aa', 'a'.times(2));
462
+ this.assertEqual('aaaaa', 'a'.times(5));
463
+ this.assertEqual('foofoofoofoofoo', 'foo'.times(5));
464
+ this.assertEqual('', 'foo'.times(-5));
465
+
466
+ /*window.String.prototype.oldTimes = function(count) {
467
+ var result = '';
468
+ for (var i = 0; i < count; i++) result += this;
469
+ return result;
470
+ };
471
+
472
+ this.benchmark(function() {
473
+ 'foo'.times(15);
474
+ }, 1000, 'new: ');
475
+
476
+ this.benchmark(function() {
477
+ 'foo'.oldTimes(15);
478
+ }, 1000, 'previous: ');*/
479
+ },
480
+
481
+ testToJSON: function() {
482
+ this.assertEqual('\"\"', ''.toJSON());
483
+ this.assertEqual('\"test\"', 'test'.toJSON());
484
+ },
485
+
486
+ testIsJSON: function() {
487
+ this.assert(!''.isJSON());
488
+ this.assert(!' '.isJSON());
489
+ this.assert('""'.isJSON());
490
+ this.assert('"foo"'.isJSON());
491
+ this.assert('{}'.isJSON());
492
+ this.assert('[]'.isJSON());
493
+ this.assert('null'.isJSON());
494
+ this.assert('123'.isJSON());
495
+ this.assert('true'.isJSON());
496
+ this.assert('false'.isJSON());
497
+ this.assert('"\\""'.isJSON());
498
+ this.assert(!'\\"'.isJSON());
499
+ this.assert(!'new'.isJSON());
500
+ this.assert(!'\u0028\u0029'.isJSON());
501
+ // we use '@' as a placeholder for characters authorized only inside brackets,
502
+ // so this tests make sure it is not considered authorized elsewhere.
503
+ this.assert(!'@'.isJSON());
504
+ },
505
+
506
+ testEvalJSON: function() {
507
+ var valid = '{"test": \n\r"hello world!"}';
508
+ var invalid = '{"test": "hello world!"';
509
+ var dangerous = '{});attackTarget = "attack succeeded!";({}';
510
+
511
+ // use smaller huge string size for KHTML
512
+ var size = navigator.userAgent.include('KHTML') ? 20 : 100;
513
+ var longString = '"' + '123456789\\"'.times(size * 10) + '"';
514
+ var object = '{' + longString + ': ' + longString + '},';
515
+ var huge = '[' + object.times(size) + '{"test": 123}]';
516
+
517
+ this.assertEqual('hello world!', valid.evalJSON().test);
518
+ this.assertEqual('hello world!', valid.evalJSON(true).test);
519
+ this.assertRaise('SyntaxError', function() { invalid.evalJSON() });
520
+ this.assertRaise('SyntaxError', function() { invalid.evalJSON(true) });
521
+
522
+ attackTarget = "scared";
523
+ dangerous.evalJSON();
524
+ this.assertEqual("attack succeeded!", attackTarget);
525
+
526
+ attackTarget = "Not scared!";
527
+ this.assertRaise('SyntaxError', function(){dangerous.evalJSON(true)});
528
+ this.assertEqual("Not scared!", attackTarget);
529
+
530
+ this.assertEqual('hello world!', ('/*-secure- \r \n ' + valid + ' \n */').evalJSON().test);
531
+ var temp = Prototype.JSONFilter;
532
+ Prototype.JSONFilter = /^\/\*([\s\S]*)\*\/$/; // test custom delimiters.
533
+ this.assertEqual('hello world!', ('/*' + valid + '*/').evalJSON().test);
534
+ Prototype.JSONFilter = temp;
535
+
536
+ this.assertMatch(123, huge.evalJSON(true).last().test);
537
+
538
+ this.assertEqual('', '""'.evalJSON());
539
+ this.assertEqual('foo', '"foo"'.evalJSON());
540
+ this.assert('object', typeof '{}'.evalJSON());
541
+ this.assert(Object.isArray('[]'.evalJSON()));
542
+ this.assertNull('null'.evalJSON());
543
+ this.assert(123, '123'.evalJSON());
544
+ this.assertIdentical(true, 'true'.evalJSON());
545
+ this.assertIdentical(false, 'false'.evalJSON());
546
+ this.assertEqual('"', '"\\""'.evalJSON());
547
+ }
548
+ });