babilu 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,1411 @@
1
+ /**
2
+ * JSSpec
3
+ *
4
+ * Copyright 2007 Alan Kang
5
+ * - mailto:jania902@gmail.com
6
+ * - http://jania.pe.kr
7
+ *
8
+ * http://jania.pe.kr/aw/moin.cgi/JSSpec
9
+ *
10
+ * Dependencies:
11
+ * - diff_match_patch.js ( http://code.google.com/p/google-diff-match-patch )
12
+ *
13
+ * This library is free software; you can redistribute it and/or
14
+ * modify it under the terms of the GNU Lesser General Public
15
+ * License as published by the Free Software Foundation; either
16
+ * version 2.1 of the License, or (at your option) any later version.
17
+ *
18
+ * This library is distributed in the hope that it will be useful,
19
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21
+ * Lesser General Public License for more details.
22
+ *
23
+ * You should have received a copy of the GNU Lesser General Public
24
+ * License along with this library; if not, write to the Free Software
25
+ * Foundation, Inc, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
26
+ */
27
+
28
+ /**
29
+ * Namespace
30
+ */
31
+ JSSpec = {
32
+ specs: [],
33
+
34
+ EMPTY_FUNCTION: function() {},
35
+
36
+ Browser: {
37
+ Trident: navigator.appName == "Microsoft Internet Explorer",
38
+ Webkit: navigator.userAgent.indexOf('AppleWebKit/') > -1,
39
+ Gecko: navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1,
40
+ Presto: navigator.appName == "Opera"
41
+ }
42
+ }
43
+
44
+
45
+
46
+ /**
47
+ * Executor
48
+ */
49
+ JSSpec.Executor = function(target, onSuccess, onException) {
50
+ this.target = target;
51
+ this.onSuccess = typeof onSuccess == 'function' ? onSuccess : JSSpec.EMPTY_FUNCTION;
52
+ this.onException = typeof onException == 'function' ? onException : JSSpec.EMPTY_FUNCTION;
53
+
54
+ if(JSSpec.Browser.Trident) {
55
+ // Exception handler for Trident. It helps to collect exact line number where exception occured.
56
+ window.onerror = function(message, fileName, lineNumber) {
57
+ var self = window._curExecutor;
58
+ var ex = {message:message, fileName:fileName, lineNumber:lineNumber};
59
+
60
+ if(JSSpec._secondPass) {
61
+ ex = self.mergeExceptions(JSSpec._assertionFailure, ex);
62
+ delete JSSpec._secondPass;
63
+ delete JSSpec._assertionFailure;
64
+
65
+ ex.type = "failure";
66
+ self.onException(self, ex);
67
+ } else if(JSSpec._assertionFailure) {
68
+ JSSpec._secondPass = true;
69
+ self.run();
70
+ } else {
71
+ self.onException(self, ex);
72
+ }
73
+
74
+ return true;
75
+ }
76
+ }
77
+ }
78
+ JSSpec.Executor.prototype.mergeExceptions = function(assertionFailure, normalException) {
79
+ var merged = {
80
+ message:assertionFailure.message,
81
+ fileName:normalException.fileName,
82
+ lineNumber:normalException.lineNumber
83
+ };
84
+
85
+ return merged;
86
+ }
87
+ JSSpec.Executor.prototype.run = function() {
88
+ var self = this;
89
+ var target = this.target;
90
+ var onSuccess = this.onSuccess;
91
+ var onException = this.onException;
92
+
93
+ window.setTimeout(
94
+ function() {
95
+ if(JSSpec.Browser.Trident) {
96
+ window._curExecutor = self;
97
+
98
+ var result = self.target();
99
+ self.onSuccess(self, result);
100
+ } else {
101
+ try {
102
+ var result = self.target();
103
+ self.onSuccess(self, result);
104
+ } catch(ex) {
105
+ if(JSSpec.Browser.Webkit) ex = {message:ex.message, fileName:ex.sourceURL, lineNumber:ex.line}
106
+
107
+ if(JSSpec._secondPass) {
108
+ ex = self.mergeExceptions(JSSpec._assertionFailure, ex);
109
+ delete JSSpec._secondPass;
110
+ delete JSSpec._assertionFailure;
111
+
112
+ ex.type = "failure";
113
+ self.onException(self, ex);
114
+ } else if(JSSpec._assertionFailure) {
115
+ JSSpec._secondPass = true;
116
+ self.run();
117
+ } else {
118
+ self.onException(self, ex);
119
+ }
120
+ }
121
+ }
122
+ },
123
+ 0
124
+ );
125
+ }
126
+
127
+
128
+
129
+ /**
130
+ * CompositeExecutor composites one or more executors and execute them sequencially.
131
+ */
132
+ JSSpec.CompositeExecutor = function(onSuccess, onException, continueOnException) {
133
+ this.queue = [];
134
+ this.onSuccess = typeof onSuccess == 'function' ? onSuccess : JSSpec.EMPTY_FUNCTION;
135
+ this.onException = typeof onException == 'function' ? onException : JSSpec.EMPTY_FUNCTION;
136
+ this.continueOnException = !!continueOnException;
137
+ }
138
+ JSSpec.CompositeExecutor.prototype.addFunction = function(func) {
139
+ this.addExecutor(new JSSpec.Executor(func));
140
+ }
141
+ JSSpec.CompositeExecutor.prototype.addExecutor = function(executor) {
142
+ var last = this.queue.length == 0 ? null : this.queue[this.queue.length - 1];
143
+ if(last) {
144
+ last.next = executor;
145
+ }
146
+
147
+ executor.parent = this;
148
+ executor.onSuccessBackup = executor.onSuccess;
149
+ executor.onSuccess = function(result) {
150
+ this.onSuccessBackup(result);
151
+ if(this.next) {
152
+ this.next.run()
153
+ } else {
154
+ this.parent.onSuccess();
155
+ }
156
+ }
157
+ executor.onExceptionBackup = executor.onException;
158
+ executor.onException = function(executor, ex) {
159
+ this.onExceptionBackup(executor, ex);
160
+
161
+ if(this.parent.continueOnException) {
162
+ if(this.next) {
163
+ this.next.run()
164
+ } else {
165
+ this.parent.onSuccess();
166
+ }
167
+ } else {
168
+ this.parent.onException(executor, ex);
169
+ }
170
+ }
171
+
172
+ this.queue.push(executor);
173
+ }
174
+ JSSpec.CompositeExecutor.prototype.run = function() {
175
+ if(this.queue.length > 0) {
176
+ this.queue[0].run();
177
+ }
178
+ }
179
+
180
+
181
+
182
+ /**
183
+ * Spec is a set of Examples in a specific context
184
+ */
185
+ JSSpec.Spec = function(context, entries) {
186
+ this.id = JSSpec.Spec.id++;
187
+ this.context = context;
188
+ this.url = location.href;
189
+
190
+ this.filterEntriesByEmbeddedExpressions(entries);
191
+ this.extractOutSpecialEntries(entries);
192
+ this.examples = this.makeExamplesFromEntries(entries);
193
+ this.examplesMap = this.makeMapFromExamples(this.examples);
194
+ }
195
+ JSSpec.Spec.id = 0;
196
+ JSSpec.Spec.prototype.getExamples = function() {
197
+ return this.examples;
198
+ }
199
+ JSSpec.Spec.prototype.hasException = function() {
200
+ return this.getTotalFailures() > 0 || this.getTotalErrors() > 0;
201
+ }
202
+ JSSpec.Spec.prototype.getTotalFailures = function() {
203
+ var examples = this.examples;
204
+ var failures = 0;
205
+ for(var i = 0; i < examples.length; i++) {
206
+ if(examples[i].isFailure()) failures++;
207
+ }
208
+ return failures;
209
+ }
210
+ JSSpec.Spec.prototype.getTotalErrors = function() {
211
+ var examples = this.examples;
212
+ var errors = 0;
213
+ for(var i = 0; i < examples.length; i++) {
214
+ if(examples[i].isError()) errors++;
215
+ }
216
+ return errors;
217
+ }
218
+ JSSpec.Spec.prototype.filterEntriesByEmbeddedExpressions = function(entries) {
219
+ var isTrue;
220
+ for(name in entries) {
221
+ var m = name.match(/\[\[(.+)\]\]/);
222
+ if(m && m[1]) {
223
+ eval("isTrue = (" + m[1] + ")");
224
+ if(!isTrue) delete entries[name];
225
+ }
226
+ }
227
+ }
228
+ JSSpec.Spec.prototype.extractOutSpecialEntries = function(entries) {
229
+ this.beforeEach = JSSpec.EMPTY_FUNCTION;
230
+ this.beforeAll = JSSpec.EMPTY_FUNCTION;
231
+ this.afterEach = JSSpec.EMPTY_FUNCTION;
232
+ this.afterAll = JSSpec.EMPTY_FUNCTION;
233
+
234
+ for(name in entries) {
235
+ if(name == 'before' || name == 'before each') {
236
+ this.beforeEach = entries[name];
237
+ } else if(name == 'before all') {
238
+ this.beforeAll = entries[name];
239
+ } else if(name == 'after' || name == 'after each') {
240
+ this.afterEach = entries[name];
241
+ } else if(name == 'after all') {
242
+ this.afterAll = entries[name];
243
+ }
244
+ }
245
+
246
+ delete entries['before'];
247
+ delete entries['before each'];
248
+ delete entries['before all'];
249
+ delete entries['after'];
250
+ delete entries['after each'];
251
+ delete entries['after all'];
252
+ }
253
+ JSSpec.Spec.prototype.makeExamplesFromEntries = function(entries) {
254
+ var examples = [];
255
+ for(name in entries) {
256
+ examples.push(new JSSpec.Example(name, entries[name], this.beforeEach, this.afterEach));
257
+ }
258
+ return examples;
259
+ }
260
+ JSSpec.Spec.prototype.makeMapFromExamples = function(examples) {
261
+ var map = {};
262
+ for(var i = 0; i < examples.length; i++) {
263
+ var example = examples[i];
264
+ map[example.id] = examples[i];
265
+ }
266
+ return map;
267
+ }
268
+ JSSpec.Spec.prototype.getExampleById = function(id) {
269
+ return this.examplesMap[id];
270
+ }
271
+ JSSpec.Spec.prototype.getExecutor = function() {
272
+ var self = this;
273
+ var onException = function(executor, ex) {self.exception = ex}
274
+
275
+ var composite = new JSSpec.CompositeExecutor();
276
+ composite.addFunction(function() {JSSpec.log.onSpecStart(self)});
277
+ composite.addExecutor(new JSSpec.Executor(this.beforeAll, null, function(exec, ex) {
278
+ self.exception = ex;
279
+ JSSpec.log.onSpecEnd(self);
280
+ }));
281
+
282
+ var exampleAndAfter = new JSSpec.CompositeExecutor(null,null,true);
283
+ for(var i = 0; i < this.examples.length; i++) {
284
+ exampleAndAfter.addExecutor(this.examples[i].getExecutor());
285
+ }
286
+ exampleAndAfter.addExecutor(new JSSpec.Executor(this.afterAll, null, onException));
287
+ exampleAndAfter.addFunction(function() {JSSpec.log.onSpecEnd(self)});
288
+ composite.addExecutor(exampleAndAfter);
289
+
290
+ return composite;
291
+ }
292
+
293
+
294
+
295
+ /**
296
+ * Example
297
+ */
298
+ JSSpec.Example = function(name, target, before, after) {
299
+ this.id = JSSpec.Example.id++;
300
+ this.name = name;
301
+ this.target = target;
302
+ this.before = before;
303
+ this.after = after;
304
+ }
305
+ JSSpec.Example.id = 0;
306
+ JSSpec.Example.prototype.isFailure = function() {
307
+ return this.exception && this.exception.type == "failure";
308
+ }
309
+ JSSpec.Example.prototype.isError = function() {
310
+ return this.exception && !this.exception.type;
311
+ }
312
+ JSSpec.Example.prototype.getExecutor = function() {
313
+ var self = this;
314
+ var onException = function(executor, ex) {
315
+ self.exception = ex
316
+ }
317
+
318
+ var composite = new JSSpec.CompositeExecutor();
319
+ composite.addFunction(function() {JSSpec.log.onExampleStart(self)});
320
+ composite.addExecutor(new JSSpec.Executor(this.before, null, function(exec, ex) {
321
+ self.exception = ex;
322
+ JSSpec.log.onExampleEnd(self);
323
+ }));
324
+
325
+ var targetAndAfter = new JSSpec.CompositeExecutor(null,null,true);
326
+
327
+ targetAndAfter.addExecutor(new JSSpec.Executor(this.target, null, onException));
328
+ targetAndAfter.addExecutor(new JSSpec.Executor(this.after, null, onException));
329
+ targetAndAfter.addFunction(function() {JSSpec.log.onExampleEnd(self)});
330
+
331
+ composite.addExecutor(targetAndAfter);
332
+
333
+ return composite;
334
+ }
335
+
336
+
337
+
338
+ /**
339
+ * Runner
340
+ */
341
+ JSSpec.Runner = function(specs, logger) {
342
+ JSSpec.log = logger;
343
+
344
+ this.totalExamples = 0;
345
+ this.specs = [];
346
+ this.specsMap = {};
347
+ this.addAllSpecs(specs);
348
+ }
349
+ JSSpec.Runner.prototype.addAllSpecs = function(specs) {
350
+ for(var i = 0; i < specs.length; i++) {
351
+ this.addSpec(specs[i]);
352
+ }
353
+ }
354
+ JSSpec.Runner.prototype.addSpec = function(spec) {
355
+ this.specs.push(spec);
356
+ this.specsMap[spec.id] = spec;
357
+ this.totalExamples += spec.getExamples().length;
358
+ }
359
+ JSSpec.Runner.prototype.getSpecById = function(id) {
360
+ return this.specsMap[id];
361
+ }
362
+ JSSpec.Runner.prototype.getSpecByContext = function(context) {
363
+ for(var i = 0; i < this.specs.length; i++) {
364
+ if(this.specs[i].context == context) return this.specs[i];
365
+ }
366
+ return null;
367
+ }
368
+ JSSpec.Runner.prototype.getSpecs = function() {
369
+ return this.specs;
370
+ }
371
+ JSSpec.Runner.prototype.hasException = function() {
372
+ return this.getTotalFailures() > 0 || this.getTotalErrors() > 0;
373
+ }
374
+ JSSpec.Runner.prototype.getTotalFailures = function() {
375
+ var specs = this.specs;
376
+ var failures = 0;
377
+ for(var i = 0; i < specs.length; i++) {
378
+ failures += specs[i].getTotalFailures();
379
+ }
380
+ return failures;
381
+ }
382
+ JSSpec.Runner.prototype.getTotalErrors = function() {
383
+ var specs = this.specs;
384
+ var errors = 0;
385
+ for(var i = 0; i < specs.length; i++) {
386
+ errors += specs[i].getTotalErrors();
387
+ }
388
+ return errors;
389
+ }
390
+
391
+ JSSpec.Runner.prototype.run = function() {
392
+ JSSpec.log.onRunnerStart();
393
+ var executor = new JSSpec.CompositeExecutor(function() {JSSpec.log.onRunnerEnd()},null,true);
394
+ for(var i = 0; i < this.specs.length; i++) {
395
+ executor.addExecutor(this.specs[i].getExecutor());
396
+ }
397
+ executor.run();
398
+ }
399
+
400
+ JSSpec.Runner.prototype.rerun = function(context) {
401
+ JSSpec.runner = new JSSpec.Runner([this.getSpecByContext(context)], JSSpec.log);
402
+ JSSpec.runner.run();
403
+ }
404
+
405
+ /**
406
+ * Logger
407
+ */
408
+ JSSpec.Logger = function() {
409
+ this.finishedExamples = 0;
410
+ this.startedAt = null;
411
+ }
412
+
413
+ JSSpec.Logger.prototype.onRunnerStart = function() {
414
+ this._title = document.title;
415
+
416
+ this.startedAt = new Date();
417
+ var container = document.getElementById('jsspec_container');
418
+ if(container) {
419
+ container.innerHTML = "";
420
+ } else {
421
+ container = document.createElement("DIV");
422
+ container.id = "jsspec_container";
423
+ document.body.appendChild(container);
424
+ }
425
+
426
+ var title = document.createElement("DIV");
427
+ title.id = "title";
428
+ title.innerHTML = [
429
+ '<h1>JSSpec runner</h1>',
430
+ '<ul>',
431
+ ' <li><span id="total_examples">' + JSSpec.runner.totalExamples + '</span> examples</li>',
432
+ ' <li><span id="total_failures">0</span> failures</li>',
433
+ ' <li><span id="total_errors">0</span> errors</li>',
434
+ ' <li><span id="progress">0</span>% done</li>',
435
+ ' <li><span id="total_elapsed">0</span> secs</li>',
436
+ '</ul>',
437
+ '<p><a href="http://jania.pe.kr/aw/moin.cgi/JSSpec">JSSpec homepage</a></p>',
438
+ ].join("");
439
+ container.appendChild(title);
440
+
441
+ var list = document.createElement("DIV");
442
+ list.id = "list";
443
+ list.innerHTML = [
444
+ '<h2>List</h2>',
445
+ '<ul class="specs">',
446
+ function() {
447
+ var specs = JSSpec.runner.getSpecs();
448
+ var sb = [];
449
+ for(var i = 0; i < specs.length; i++) {
450
+ var spec = specs[i];
451
+ sb.push('<li id="spec_' + specs[i].id + '_list"><h3><a href="#spec_' + specs[i].id + '">' + JSSpec.util.escapeTags(specs[i].context) + '</a> [<a href="?rerun=' + encodeURIComponent(specs[i].context) + '">rerun</a>]</h3></li>');
452
+ }
453
+ return sb.join("");
454
+ }(),
455
+ '</ul>'
456
+ ].join("");
457
+ container.appendChild(list);
458
+
459
+ var log = document.createElement("DIV");
460
+ log.id = "log";
461
+ log.innerHTML = [
462
+ '<h2>Log</h2>',
463
+ '<ul class="specs">',
464
+ function() {
465
+ var specs = JSSpec.runner.getSpecs();
466
+ var sb = [];
467
+ for(var i = 0; i < specs.length; i++) {
468
+ var spec = specs[i];
469
+ sb.push(' <li id="spec_' + specs[i].id + '">');
470
+ sb.push(' <h3>' + JSSpec.util.escapeTags(specs[i].context) + ' [<a href="?rerun=' + encodeURIComponent(specs[i].context) + '">rerun</a>]</h3>');
471
+ sb.push(' <ul id="spec_' + specs[i].id + '_examples" class="examples">');
472
+ for(var j = 0; j < spec.examples.length; j++) {
473
+ var example = spec.examples[j];
474
+ sb.push(' <li id="example_' + example.id + '">')
475
+ sb.push(' <h4>' + JSSpec.util.escapeTags(example.name) + '</h4>')
476
+ sb.push(' </li>')
477
+ }
478
+ sb.push(' </ul>');
479
+ sb.push(' </li>');
480
+ }
481
+ return sb.join("");
482
+ }(),
483
+ '</ul>'
484
+ ].join("");
485
+ container.appendChild(log);
486
+ }
487
+ JSSpec.Logger.prototype.onRunnerEnd = function() {
488
+ if(JSSpec.runner.hasException()) {
489
+ var times = 4;
490
+ var title1 = "*" + this._title;
491
+ var title2 = "*F" + JSSpec.runner.getTotalFailures() + " E" + JSSpec.runner.getTotalErrors() + "* " + this._title;
492
+ } else {
493
+ var times = 2;
494
+ var title1 = this._title;
495
+ var title2 = "Success";
496
+ }
497
+ this.blinkTitle(times,title1,title2);
498
+ }
499
+ JSSpec.Logger.prototype.blinkTitle = function(times, title1, title2) {
500
+ var times = times * 2;
501
+ var mode = true;
502
+
503
+ var f = function() {
504
+ if(times > 0) {
505
+ document.title = mode ? title1 : title2;
506
+ mode = !mode;
507
+ times--;
508
+ window.setTimeout(f, 500);
509
+ } else {
510
+ document.title = title1;
511
+ }
512
+ }
513
+
514
+ f();
515
+ }
516
+ JSSpec.Logger.prototype.onSpecStart = function(spec) {
517
+ var spec_list = document.getElementById("spec_" + spec.id + "_list");
518
+ var spec_log = document.getElementById("spec_" + spec.id);
519
+
520
+ spec_list.className = "ongoing";
521
+ spec_log.className = "ongoing";
522
+ }
523
+ JSSpec.Logger.prototype.onSpecEnd = function(spec) {
524
+ var spec_list = document.getElementById("spec_" + spec.id + "_list");
525
+ var spec_log = document.getElementById("spec_" + spec.id);
526
+ var examples = document.getElementById("spec_" + spec.id + "_examples");
527
+ var className = spec.hasException() ? "exception" : "success";
528
+
529
+ spec_list.className = className;
530
+ spec_log.className = className;
531
+
532
+ if(JSSpec.options.autocollapse && !spec.hasException()) examples.style.display = "none";
533
+
534
+ if(spec.exception) {
535
+ heading.appendChild(document.createTextNode(" - " + spec.exception.message));
536
+ }
537
+ }
538
+ JSSpec.Logger.prototype.onExampleStart = function(example) {
539
+ var li = document.getElementById("example_" + example.id);
540
+ li.className = "ongoing";
541
+ }
542
+ JSSpec.Logger.prototype.onExampleEnd = function(example) {
543
+ var li = document.getElementById("example_" + example.id);
544
+ li.className = example.exception ? "exception" : "success";
545
+
546
+ if(example.exception) {
547
+ var div = document.createElement("DIV");
548
+ div.innerHTML = example.exception.message + "<p><br />" + " at " + example.exception.fileName + ", line " + example.exception.lineNumber + "</p>";
549
+ li.appendChild(div);
550
+ }
551
+
552
+ var title = document.getElementById("title");
553
+ var runner = JSSpec.runner;
554
+
555
+ title.className = runner.hasException() ? "exception" : "success";
556
+
557
+ this.finishedExamples++;
558
+ document.getElementById("total_failures").innerHTML = runner.getTotalFailures();
559
+ document.getElementById("total_errors").innerHTML = runner.getTotalErrors();
560
+ var progress = parseInt(this.finishedExamples / runner.totalExamples * 100);
561
+ document.getElementById("progress").innerHTML = progress;
562
+ document.getElementById("total_elapsed").innerHTML = (new Date().getTime() - this.startedAt.getTime()) / 1000;
563
+
564
+ document.title = progress + "%: " + this._title;
565
+ }
566
+
567
+
568
+
569
+ /**
570
+ * IncludeMatcher
571
+ */
572
+ JSSpec.IncludeMatcher = function(actual, expected, condition) {
573
+ this.actual = actual;
574
+ this.expected = expected;
575
+ this.condition = condition;
576
+ this.match = false;
577
+ this.explaination = this.makeExplain();
578
+ }
579
+ JSSpec.IncludeMatcher.createInstance = function(actual, expected, condition) {
580
+ return new JSSpec.IncludeMatcher(actual, expected, condition);
581
+ }
582
+ JSSpec.IncludeMatcher.prototype.matches = function() {
583
+ return this.match;
584
+ }
585
+ JSSpec.IncludeMatcher.prototype.explain = function() {
586
+ return this.explaination;
587
+ }
588
+ JSSpec.IncludeMatcher.prototype.makeExplain = function() {
589
+ if(typeof this.actual.length == 'undefined') {
590
+ return this.makeExplainForNotArray();
591
+ } else {
592
+ return this.makeExplainForArray();
593
+ }
594
+ }
595
+ JSSpec.IncludeMatcher.prototype.makeExplainForNotArray = function() {
596
+ var sb = [];
597
+ sb.push('<p>actual value:</p>');
598
+ sb.push('<p style="margin-left:2em;">' + JSSpec.util.inspect(this.actual) + '</p>');
599
+ sb.push('<p>should ' + (this.condition ? '' : 'not') + ' include:</p>');
600
+ sb.push('<p style="margin-left:2em;">' + JSSpec.util.inspect(this.expected) + '</p>');
601
+ sb.push('<p>but since it\s not an array, include or not doesn\'t make any sense.</p>');
602
+ return sb.join("");
603
+ }
604
+ JSSpec.IncludeMatcher.prototype.makeExplainForArray = function() {
605
+ if(this.condition) {
606
+ for(var i = 0; i < this.actual.length; i++) {
607
+ var matches = JSSpec.EqualityMatcher.createInstance(this.expected, this.actual[i]).matches();
608
+ if(matches) {
609
+ this.match = true;
610
+ break;
611
+ }
612
+ }
613
+ } else {
614
+ for(var i = 0; i < this.actual.length; i++) {
615
+ var matches = JSSpec.EqualityMatcher.createInstance(this.expected, this.actual[i]).matches();
616
+ if(matches) {
617
+ this.match = false;
618
+ break;
619
+ }
620
+ }
621
+ }
622
+
623
+ if(this.match) return "";
624
+
625
+ var sb = [];
626
+ sb.push('<p>actual value:</p>');
627
+ sb.push('<p style="margin-left:2em;">' + JSSpec.util.inspect(this.actual, false, this.condition ? null : i) + '</p>');
628
+ sb.push('<p>should ' + (this.condition ? '' : 'not') + ' include:</p>');
629
+ sb.push('<p style="margin-left:2em;">' + JSSpec.util.inspect(this.expected) + '</p>');
630
+ return sb.join("");
631
+ }
632
+
633
+
634
+
635
+
636
+ /**
637
+ * PropertyLengthMatcher
638
+ */
639
+ JSSpec.PropertyLengthMatcher = function(num, property, o, condition) {
640
+ this.num = num;
641
+ this.o = o;
642
+ this.property = property;
643
+ if((property == 'characters' || property == 'items') && typeof o.length != 'undefined') {
644
+ this.property = 'length';
645
+ }
646
+
647
+ this.condition = condition;
648
+ this.conditionMet = function(x) {
649
+ if(condition == 'exactly') return x.length == num;
650
+ if(condition == 'at least') return x.length >= num;
651
+ if(condition == 'at most') return x.length <= num;
652
+
653
+ throw "Unknown condition '" + condition + "'";
654
+ };
655
+ this.match = false;
656
+ this.explaination = this.makeExplain();
657
+ }
658
+ JSSpec.PropertyLengthMatcher.prototype.makeExplain = function() {
659
+ if(this.o._type == 'String' && this.property == 'length') {
660
+ this.match = this.conditionMet(this.o);
661
+ return this.match ? '' : this.makeExplainForString();
662
+ } else if(typeof this.o.length != 'undefined' && this.property == "length") {
663
+ this.match = this.conditionMet(this.o);
664
+ return this.match ? '' : this.makeExplainForArray();
665
+ } else if(typeof this.o[this.property] != 'undefined' && this.o[this.property] != null) {
666
+ this.match = this.conditionMet(this.o[this.property]);
667
+ return this.match ? '' : this.makeExplainForObject();
668
+ } else if(typeof this.o[this.property] == 'undefined' || this.o[this.property] == null) {
669
+ this.match = false;
670
+ return this.makeExplainForNoProperty();
671
+ }
672
+
673
+ this.match = true;
674
+ }
675
+ JSSpec.PropertyLengthMatcher.prototype.makeExplainForString = function() {
676
+ var sb = [];
677
+
678
+ var exp = this.num == 0 ?
679
+ 'be an <strong>empty string</strong>' :
680
+ 'have <strong>' + this.condition + ' ' + this.num + ' characters</strong>';
681
+
682
+ sb.push('<p>actual value has <strong>' + this.o.length + ' characters</strong>:</p>');
683
+ sb.push('<p style="margin-left:2em;">' + JSSpec.util.inspect(this.o) + '</p>');
684
+ sb.push('<p>but it should ' + exp + '.</p>');
685
+
686
+ return sb.join("");
687
+ }
688
+ JSSpec.PropertyLengthMatcher.prototype.makeExplainForArray = function() {
689
+ var sb = [];
690
+
691
+ var exp = this.num == 0 ?
692
+ 'be an <strong>empty array</strong>' :
693
+ 'have <strong>' + this.condition + ' ' + this.num + ' items</strong>';
694
+
695
+ sb.push('<p>actual value has <strong>' + this.o.length + ' items</strong>:</p>');
696
+ sb.push('<p style="margin-left:2em;">' + JSSpec.util.inspect(this.o) + '</p>');
697
+ sb.push('<p>but it should ' + exp + '.</p>');
698
+
699
+ return sb.join("");
700
+ }
701
+ JSSpec.PropertyLengthMatcher.prototype.makeExplainForObject = function() {
702
+ var sb = [];
703
+
704
+ var exp = this.num == 0 ?
705
+ 'be <strong>empty</strong>' :
706
+ 'have <strong>' + this.condition + ' ' + this.num + ' ' + this.property + '.</strong>';
707
+
708
+ sb.push('<p>actual value has <strong>' + this.o[this.property].length + ' ' + this.property + '</strong>:</p>');
709
+ sb.push('<p style="margin-left:2em;">' + JSSpec.util.inspect(this.o, false, this.property) + '</p>');
710
+ sb.push('<p>but it should ' + exp + '.</p>');
711
+
712
+ return sb.join("");
713
+ }
714
+ JSSpec.PropertyLengthMatcher.prototype.makeExplainForNoProperty = function() {
715
+ var sb = [];
716
+
717
+ sb.push('<p>actual value:</p>');
718
+ sb.push('<p style="margin-left:2em;">' + JSSpec.util.inspect(this.o) + '</p>');
719
+ sb.push('<p>should have <strong>' + this.condition + ' ' + this.num + ' ' + this.property + '</strong> but there\'s no such property.</p>');
720
+
721
+ return sb.join("");
722
+ }
723
+ JSSpec.PropertyLengthMatcher.prototype.matches = function() {
724
+ return this.match;
725
+ }
726
+ JSSpec.PropertyLengthMatcher.prototype.explain = function() {
727
+ return this.explaination;
728
+ }
729
+ JSSpec.PropertyLengthMatcher.createInstance = function(num, property, o, condition) {
730
+ return new JSSpec.PropertyLengthMatcher(num, property, o, condition);
731
+ }
732
+
733
+
734
+
735
+ /**
736
+ * EqualityMatcher
737
+ */
738
+ JSSpec.EqualityMatcher = {}
739
+ JSSpec.EqualityMatcher.createInstance = function(expected, actual) {
740
+ if(expected == null || actual == null) {
741
+ return new JSSpec.NullEqualityMatcher(expected, actual);
742
+ } else if(expected._type == actual._type) {
743
+ if(expected._type == "String") {
744
+ return new JSSpec.StringEqualityMatcher(expected, actual);
745
+ } else if(expected._type == "Date") {
746
+ return new JSSpec.DateEqualityMatcher(expected, actual);
747
+ } else if(expected._type == "Number") {
748
+ return new JSSpec.NumberEqualityMatcher(expected, actual);
749
+ } else if(expected._type == "Array") {
750
+ return new JSSpec.ArrayEqualityMatcher(expected, actual);
751
+ } else if(expected._type == "Boolean") {
752
+ return new JSSpec.BooleanEqualityMatcher(expected, actual);
753
+ }
754
+ }
755
+
756
+ return new JSSpec.ObjectEqualityMatcher(expected, actual);
757
+ }
758
+ JSSpec.EqualityMatcher.basicExplain = function(expected, actual, expectedDesc, actualDesc) {
759
+ var sb = [];
760
+
761
+ sb.push(actualDesc || '<p>actual value:</p>');
762
+ sb.push('<p style="margin-left:2em;">' + JSSpec.util.inspect(actual) + '</p>');
763
+ sb.push(expectedDesc || '<p>should be:</p>');
764
+ sb.push('<p style="margin-left:2em;">' + JSSpec.util.inspect(expected) + '</p>');
765
+
766
+ return sb.join("");
767
+ }
768
+ JSSpec.EqualityMatcher.diffExplain = function(expected, actual) {
769
+ var sb = [];
770
+
771
+ sb.push('<p>diff:</p>');
772
+ sb.push('<p style="margin-left:2em;">');
773
+
774
+ var dmp = new diff_match_patch();
775
+ var diff = dmp.diff_main(expected, actual);
776
+ dmp.diff_cleanupEfficiency(diff);
777
+
778
+ sb.push(JSSpec.util.inspect(dmp.diff_prettyHtml(diff), true));
779
+
780
+ sb.push('</p>');
781
+
782
+ return sb.join("");
783
+ }
784
+
785
+
786
+
787
+ /**
788
+ * BooleanEqualityMatcher
789
+ */
790
+ JSSpec.BooleanEqualityMatcher = function(expected, actual) {
791
+ this.expected = expected;
792
+ this.actual = actual;
793
+ }
794
+ JSSpec.BooleanEqualityMatcher.prototype.explain = function() {
795
+ var sb = [];
796
+ sb.push('<p>actual value:</p>');
797
+ sb.push('<p style="margin-left:2em;">' + JSSpec.util.inspect(this.actual) + '</p>');
798
+ sb.push('<p>should be:</p>');
799
+ sb.push('<p style="margin-left:2em;">' + JSSpec.util.inspect(this.expected) + '</p>');
800
+
801
+ return sb.join("");
802
+ }
803
+ JSSpec.BooleanEqualityMatcher.prototype.matches = function() {
804
+ return this.expected == this.actual;
805
+ }
806
+
807
+
808
+
809
+ /**
810
+ * NullEqualityMatcher
811
+ */
812
+ JSSpec.NullEqualityMatcher = function(expected, actual) {
813
+ this.expected = expected;
814
+ this.actual = actual;
815
+ }
816
+ JSSpec.NullEqualityMatcher.prototype.matches = function() {
817
+ return this.expected == this.actual && typeof this.expected == typeof this.actual;
818
+ }
819
+ JSSpec.NullEqualityMatcher.prototype.explain = function() {
820
+ return JSSpec.EqualityMatcher.basicExplain(this.expected, this.actual);
821
+ }
822
+
823
+
824
+
825
+ JSSpec.DateEqualityMatcher = function(expected, actual) {
826
+ this.expected = expected;
827
+ this.actual = actual;
828
+ }
829
+ JSSpec.DateEqualityMatcher.prototype.matches = function() {
830
+ return this.expected.getTime() == this.actual.getTime();
831
+ }
832
+ JSSpec.DateEqualityMatcher.prototype.explain = function() {
833
+ var sb = [];
834
+
835
+ sb.push(JSSpec.EqualityMatcher.basicExplain(this.expected, this.actual));
836
+ sb.push(JSSpec.EqualityMatcher.diffExplain(this.expected.toString(), this.actual.toString()));
837
+
838
+ return sb.join("");
839
+ }
840
+
841
+
842
+
843
+ /**
844
+ * ObjectEqualityMatcher
845
+ */
846
+ JSSpec.ObjectEqualityMatcher = function(expected, actual) {
847
+ this.expected = expected;
848
+ this.actual = actual;
849
+ this.match = this.expected == this.actual;
850
+ this.explaination = this.makeExplain();
851
+ }
852
+ JSSpec.ObjectEqualityMatcher.prototype.matches = function() {return this.match}
853
+ JSSpec.ObjectEqualityMatcher.prototype.explain = function() {return this.explaination}
854
+ JSSpec.ObjectEqualityMatcher.prototype.makeExplain = function() {
855
+ if(this.expected == this.actual) {
856
+ this.match = true;
857
+ return "";
858
+ }
859
+
860
+ if(JSSpec.util.isDomNode(this.expected)) {
861
+ return this.makeExplainForDomNode();
862
+ }
863
+
864
+ for(var key in this.expected) {
865
+ var expectedHasItem = this.expected[key] != null && typeof this.expected[key] != 'undefined';
866
+ var actualHasItem = this.actual[key] != null && typeof this.actual[key] != 'undefined';
867
+ if(expectedHasItem && !actualHasItem) return this.makeExplainForMissingItem(key);
868
+ }
869
+ for(var key in this.actual) {
870
+ var expectedHasItem = this.expected[key] != null && typeof this.expected[key] != 'undefined';
871
+ var actualHasItem = this.actual[key] != null && typeof this.actual[key] != 'undefined';
872
+ if(actualHasItem && !expectedHasItem) return this.makeExplainForUnknownItem(key);
873
+ }
874
+
875
+ for(var key in this.expected) {
876
+ var matcher = JSSpec.EqualityMatcher.createInstance(this.expected[key], this.actual[key]);
877
+ if(!matcher.matches()) return this.makeExplainForItemMismatch(key);
878
+ }
879
+
880
+ this.match = true;
881
+ }
882
+ JSSpec.ObjectEqualityMatcher.prototype.makeExplainForDomNode = function(key) {
883
+ var sb = [];
884
+
885
+ sb.push(JSSpec.EqualityMatcher.basicExplain(this.expected, this.actual));
886
+
887
+ return sb.join("");
888
+ }
889
+ JSSpec.ObjectEqualityMatcher.prototype.makeExplainForMissingItem = function(key) {
890
+ var sb = [];
891
+
892
+ sb.push('<p>actual value has no item named <strong>' + JSSpec.util.inspect(key) + '</strong></p>');
893
+ sb.push('<p style="margin-left:2em;">' + JSSpec.util.inspect(this.actual, false, key) + '</p>');
894
+ sb.push('<p>but it should have the item whose value is <strong>' + JSSpec.util.inspect(this.expected[key]) + '</strong></p>');
895
+ sb.push('<p style="margin-left:2em;">' + JSSpec.util.inspect(this.expected, false, key) + '</p>');
896
+
897
+ return sb.join("");
898
+ }
899
+ JSSpec.ObjectEqualityMatcher.prototype.makeExplainForUnknownItem = function(key) {
900
+ var sb = [];
901
+
902
+ sb.push('<p>actual value has item named <strong>' + JSSpec.util.inspect(key) + '</strong></p>');
903
+ sb.push('<p style="margin-left:2em;">' + JSSpec.util.inspect(this.actual, false, key) + '</p>');
904
+ sb.push('<p>but there should be no such item</p>');
905
+ sb.push('<p style="margin-left:2em;">' + JSSpec.util.inspect(this.expected, false, key) + '</p>');
906
+
907
+ return sb.join("");
908
+ }
909
+ JSSpec.ObjectEqualityMatcher.prototype.makeExplainForItemMismatch = function(key) {
910
+ var sb = [];
911
+
912
+ sb.push('<p>actual value has an item named <strong>' + JSSpec.util.inspect(key) + '</strong> whose value is <strong>' + JSSpec.util.inspect(this.actual[key]) + '</strong></p>');
913
+ sb.push('<p style="margin-left:2em;">' + JSSpec.util.inspect(this.actual, false, key) + '</p>');
914
+ sb.push('<p>but it\'s value should be <strong>' + JSSpec.util.inspect(this.expected[key]) + '</strong></p>');
915
+ sb.push('<p style="margin-left:2em;">' + JSSpec.util.inspect(this.expected, false, key) + '</p>');
916
+
917
+ return sb.join("");
918
+ }
919
+
920
+
921
+
922
+ /**
923
+ * ArrayEqualityMatcher
924
+ */
925
+ JSSpec.ArrayEqualityMatcher = function(expected, actual) {
926
+ this.expected = expected;
927
+ this.actual = actual;
928
+ this.match = this.expected == this.actual;
929
+ this.explaination = this.makeExplain();
930
+ }
931
+ JSSpec.ArrayEqualityMatcher.prototype.matches = function() {return this.match}
932
+ JSSpec.ArrayEqualityMatcher.prototype.explain = function() {return this.explaination}
933
+ JSSpec.ArrayEqualityMatcher.prototype.makeExplain = function() {
934
+ if(this.expected.length != this.actual.length) return this.makeExplainForLengthMismatch();
935
+
936
+ for(var i = 0; i < this.expected.length; i++) {
937
+ var matcher = JSSpec.EqualityMatcher.createInstance(this.expected[i], this.actual[i]);
938
+ if(!matcher.matches()) return this.makeExplainForItemMismatch(i);
939
+ }
940
+
941
+ this.match = true;
942
+ }
943
+ JSSpec.ArrayEqualityMatcher.prototype.makeExplainForLengthMismatch = function() {
944
+ return JSSpec.EqualityMatcher.basicExplain(
945
+ this.expected,
946
+ this.actual,
947
+ '<p>but it should be <strong>' + this.expected.length + '</strong></p>',
948
+ '<p>actual value has <strong>' + this.actual.length + '</strong> items</p>'
949
+ );
950
+ }
951
+ JSSpec.ArrayEqualityMatcher.prototype.makeExplainForItemMismatch = function(index) {
952
+ var postfix = ["th", "st", "nd", "rd", "th"][Math.min((index + 1) % 10,4)];
953
+
954
+ var sb = [];
955
+
956
+ sb.push('<p>' + (index + 1) + postfix + ' item (index ' + index + ') of actual value is <strong>' + JSSpec.util.inspect(this.actual[index]) + '</strong>:</p>');
957
+ sb.push('<p style="margin-left:2em;">' + JSSpec.util.inspect(this.actual, false, index) + '</p>');
958
+ sb.push('<p>but it should be <strong>' + JSSpec.util.inspect(this.expected[index]) + '</strong>:</p>');
959
+ sb.push('<p style="margin-left:2em;">' + JSSpec.util.inspect(this.expected, false, index) + '</p>');
960
+
961
+ return sb.join("");
962
+ }
963
+
964
+
965
+
966
+ /**
967
+ * NumberEqualityMatcher
968
+ */
969
+ JSSpec.NumberEqualityMatcher = function(expected, actual) {
970
+ this.expected = expected;
971
+ this.actual = actual;
972
+ }
973
+ JSSpec.NumberEqualityMatcher.prototype.matches = function() {
974
+ if(this.expected == this.actual) return true;
975
+ }
976
+ JSSpec.NumberEqualityMatcher.prototype.explain = function() {
977
+ return JSSpec.EqualityMatcher.basicExplain(this.expected, this.actual);
978
+ }
979
+
980
+
981
+
982
+ /**
983
+ * StringEqualityMatcher
984
+ */
985
+ JSSpec.StringEqualityMatcher = function(expected, actual) {
986
+ this.expected = expected;
987
+ this.actual = actual;
988
+ }
989
+ JSSpec.StringEqualityMatcher.prototype.matches = function() {
990
+ if(this.expected == this.actual) return true;
991
+ }
992
+ JSSpec.StringEqualityMatcher.prototype.explain = function() {
993
+ var sb = [];
994
+
995
+ sb.push(JSSpec.EqualityMatcher.basicExplain(this.expected, this.actual));
996
+ sb.push(JSSpec.EqualityMatcher.diffExplain(this.expected, this.actual));
997
+ return sb.join("");
998
+ }
999
+
1000
+
1001
+
1002
+ /**
1003
+ * PatternMatcher
1004
+ */
1005
+ JSSpec.PatternMatcher = function(actual, pattern, condition) {
1006
+ this.actual = actual;
1007
+ this.pattern = pattern;
1008
+ this.condition = condition;
1009
+ this.match = false;
1010
+ this.explaination = this.makeExplain();
1011
+ }
1012
+ JSSpec.PatternMatcher.createInstance = function(actual, pattern, condition) {
1013
+ return new JSSpec.PatternMatcher(actual, pattern, condition);
1014
+ }
1015
+ JSSpec.PatternMatcher.prototype.makeExplain = function() {
1016
+ if(this.actual == null || this.actual._type != 'String') {
1017
+ var sb = [];
1018
+ sb.push('<p>actual value:</p>');
1019
+ sb.push('<p style="margin-left:2em;">' + JSSpec.util.inspect(this.actual) + '</p>');
1020
+ sb.push('<p>should ' + (this.condition ? '' : 'not') + ' match with pattern:</p>');
1021
+ sb.push('<p style="margin-left:2em;">' + JSSpec.util.inspect(this.pattern) + '</p>');
1022
+ sb.push('<p>but pattern matching cannot be performed.</p>');
1023
+ return sb.join("");
1024
+ } else {
1025
+ this.match = this.condition == !!this.actual.match(this.pattern);
1026
+ if(this.match) return "";
1027
+
1028
+ var sb = [];
1029
+ sb.push('<p>actual value:</p>');
1030
+ sb.push('<p style="margin-left:2em;">' + JSSpec.util.inspect(this.actual) + '</p>');
1031
+ sb.push('<p>should ' + (this.condition ? '' : 'not') + ' match with pattern:</p>');
1032
+ sb.push('<p style="margin-left:2em;">' + JSSpec.util.inspect(this.pattern) + '</p>');
1033
+ return sb.join("");
1034
+ }
1035
+
1036
+ }
1037
+ JSSpec.PatternMatcher.prototype.matches = function() {
1038
+ return this.match;
1039
+ }
1040
+ JSSpec.PatternMatcher.prototype.explain = function() {
1041
+ return this.explaination;
1042
+ }
1043
+
1044
+
1045
+
1046
+ /**
1047
+ * Domain Specific Languages
1048
+ */
1049
+ JSSpec.DSL = {};
1050
+
1051
+ JSSpec.DSL.forString = {
1052
+ normalizeHtml: function() {
1053
+ var html = this;
1054
+
1055
+ // Uniformize quotation, turn tag names and attribute names into lower case
1056
+ html = html.replace(/<(\/?)(\w+)([^>]*?)>/img, function(str, closingMark, tagName, attrs) {
1057
+ var sortedAttrs = JSSpec.util.sortHtmlAttrs(JSSpec.util.correctHtmlAttrQuotation(attrs).toLowerCase())
1058
+ return "<" + closingMark + tagName.toLowerCase() + sortedAttrs + ">"
1059
+ })
1060
+
1061
+ // validation self-closing tags
1062
+ html = html.replace(/<(br|hr|img)([^>]*?)>/mg, function(str, tag, attrs) {
1063
+ return "<" + tag + attrs + " />";
1064
+ });
1065
+
1066
+ // append semi-colon at the end of style value
1067
+ html = html.replace(/style="(.*?)"/mg, function(str, styleStr) {
1068
+ styleStr = JSSpec.util.sortStyleEntries(styleStr.strip()); // for Safari
1069
+ if(styleStr.charAt(styleStr.length - 1) != ';') styleStr += ";"
1070
+
1071
+ return 'style="' + styleStr + '"'
1072
+ })
1073
+
1074
+ // sort style entries
1075
+
1076
+ // remove empty style attributes
1077
+ html = html.replace(/ style=";"/mg, "")
1078
+
1079
+ // remove new-lines
1080
+ html = html.replace(/\r/mg, '')
1081
+ html = html.replace(/\n/mg, '')
1082
+
1083
+ // TODO remove this?
1084
+ //html = html.replace(/(>[^<>]*?)\s+([^<>]*?<)/mg, '$1$2')
1085
+
1086
+ return html;
1087
+ }
1088
+ }
1089
+
1090
+
1091
+
1092
+ JSSpec.DSL.describe = function(context, entries) {
1093
+ JSSpec.specs.push(new JSSpec.Spec(context, entries));
1094
+ }
1095
+ JSSpec.DSL.expect = function(target) {
1096
+ if(JSSpec._secondPass) return {}
1097
+
1098
+ var subject = new JSSpec.DSL.Subject(target);
1099
+ return subject;
1100
+ }
1101
+ JSSpec.DSL.Subject = function(target) {
1102
+ this.target = target;
1103
+ }
1104
+ JSSpec.DSL.Subject.prototype._type = 'Subject';
1105
+ JSSpec.DSL.Subject.prototype.should_fail = function(message) {
1106
+ JSSpec._assertionFailure = {message:message};
1107
+ throw JSSpec._assertionFailure;
1108
+ }
1109
+ JSSpec.DSL.Subject.prototype.should_be = function(expected) {
1110
+ var matcher = JSSpec.EqualityMatcher.createInstance(expected, this.target);
1111
+ if(!matcher.matches()) {
1112
+ JSSpec._assertionFailure = {message:matcher.explain()};
1113
+ throw JSSpec._assertionFailure;
1114
+ }
1115
+ }
1116
+ JSSpec.DSL.Subject.prototype.should_not_be = function(expected) {
1117
+ // TODO JSSpec.EqualityMatcher should support 'condition'
1118
+ var matcher = JSSpec.EqualityMatcher.createInstance(expected, this.target);
1119
+ if(matcher.matches()) {
1120
+ JSSpec._assertionFailure = {message:"'" + this.target + "' should not be '" + expected + "'"};
1121
+ throw JSSpec._assertionFailure;
1122
+ }
1123
+ }
1124
+ JSSpec.DSL.Subject.prototype.should_be_empty = function() {
1125
+ this.should_have(0, this.getType() == 'String' ? 'characters' : 'items');
1126
+ }
1127
+ JSSpec.DSL.Subject.prototype.should_not_be_empty = function() {
1128
+ this.should_have_at_least(1, this.getType() == 'String' ? 'characters' : 'items');
1129
+ }
1130
+ JSSpec.DSL.Subject.prototype.should_be_true = function() {
1131
+ this.should_be(true);
1132
+ }
1133
+ JSSpec.DSL.Subject.prototype.should_be_false = function() {
1134
+ this.should_be(false);
1135
+ }
1136
+ JSSpec.DSL.Subject.prototype.should_be_null = function() {
1137
+ this.should_be(null);
1138
+ }
1139
+ JSSpec.DSL.Subject.prototype.should_be_undefined = function() {
1140
+ this.should_be(undefined);
1141
+ }
1142
+ JSSpec.DSL.Subject.prototype.should_not_be_null = function() {
1143
+ this.should_not_be(null);
1144
+ }
1145
+ JSSpec.DSL.Subject.prototype.should_not_be_undefined = function() {
1146
+ this.should_not_be(undefined);
1147
+ }
1148
+ JSSpec.DSL.Subject.prototype._should_have = function(num, property, condition) {
1149
+ var matcher = JSSpec.PropertyLengthMatcher.createInstance(num, property, this.target, condition);
1150
+ if(!matcher.matches()) {
1151
+ JSSpec._assertionFailure = {message:matcher.explain()};
1152
+ throw JSSpec._assertionFailure;
1153
+ }
1154
+ }
1155
+ JSSpec.DSL.Subject.prototype.should_have = function(num, property) {
1156
+ this._should_have(num, property, "exactly");
1157
+ }
1158
+ JSSpec.DSL.Subject.prototype.should_have_exactly = function(num, property) {
1159
+ this._should_have(num, property, "exactly");
1160
+ }
1161
+ JSSpec.DSL.Subject.prototype.should_have_at_least = function(num, property) {
1162
+ this._should_have(num, property, "at least");
1163
+ }
1164
+ JSSpec.DSL.Subject.prototype.should_have_at_most = function(num, property) {
1165
+ this._should_have(num, property, "at most");
1166
+ }
1167
+ JSSpec.DSL.Subject.prototype.should_include = function(expected) {
1168
+ var matcher = JSSpec.IncludeMatcher.createInstance(this.target, expected, true);
1169
+ if(!matcher.matches()) {
1170
+ JSSpec._assertionFailure = {message:matcher.explain()};
1171
+ throw JSSpec._assertionFailure;
1172
+ }
1173
+ }
1174
+ JSSpec.DSL.Subject.prototype.should_not_include = function(expected) {
1175
+ var matcher = JSSpec.IncludeMatcher.createInstance(this.target, expected, false);
1176
+ if(!matcher.matches()) {
1177
+ JSSpec._assertionFailure = {message:matcher.explain()};
1178
+ throw JSSpec._assertionFailure;
1179
+ }
1180
+ }
1181
+ JSSpec.DSL.Subject.prototype.should_match = function(pattern) {
1182
+ var matcher = JSSpec.PatternMatcher.createInstance(this.target, pattern, true);
1183
+ if(!matcher.matches()) {
1184
+ JSSpec._assertionFailure = {message:matcher.explain()};
1185
+ throw JSSpec._assertionFailure;
1186
+ }
1187
+ }
1188
+ JSSpec.DSL.Subject.prototype.should_not_match = function(pattern) {
1189
+ var matcher = JSSpec.PatternMatcher.createInstance(this.target, pattern, false);
1190
+ if(!matcher.matches()) {
1191
+ JSSpec._assertionFailure = {message:matcher.explain()};
1192
+ throw JSSpec._assertionFailure;
1193
+ }
1194
+ }
1195
+
1196
+ JSSpec.DSL.Subject.prototype.getType = function() {
1197
+ if(typeof this.target == 'undefined') {
1198
+ return 'undefined';
1199
+ } else if(this.target == null) {
1200
+ return 'null';
1201
+ } else if(this.target._type) {
1202
+ return this.target._type;
1203
+ } else if(JSSpec.util.isDomNode(this.target)) {
1204
+ return 'DomNode';
1205
+ } else {
1206
+ return 'object';
1207
+ }
1208
+ }
1209
+
1210
+
1211
+
1212
+ /**
1213
+ * Utilities
1214
+ */
1215
+ JSSpec.util = {
1216
+ escapeTags: function(string) {
1217
+ return string.replace(/</img, '&lt;').replace(/>/img, '&gt;');
1218
+ },
1219
+ parseOptions: function(defaults) {
1220
+ var options = defaults;
1221
+
1222
+ var url = location.href;
1223
+ var queryIndex = url.indexOf('?');
1224
+ if(queryIndex == -1) return options;
1225
+
1226
+ var query = url.substring(queryIndex + 1);
1227
+ var pairs = query.split('&');
1228
+ for(var i = 0; i < pairs.length; i++) {
1229
+ var tokens = pairs[i].split('=');
1230
+ options[tokens[0]] = tokens[1];
1231
+ }
1232
+
1233
+ return options;
1234
+ },
1235
+ correctHtmlAttrQuotation: function(html) {
1236
+ html = html.replace(/(\w+)=['"]([^'"]+)['"]/mg,function (str, name, value) {return name + '=' + '"' + value + '"'});
1237
+ html = html.replace(/(\w+)=([^ '"]+)/mg,function (str, name, value) {return name + '=' + '"' + value + '"'});
1238
+ html = html.replace(/'/mg, '"');
1239
+
1240
+ return html;
1241
+ },
1242
+ sortHtmlAttrs: function(html) {
1243
+ var attrs = []
1244
+ html.replace(/((\w+)="[^"]+")/mg, function(str, matched) {
1245
+ attrs.push(matched);
1246
+ })
1247
+ return attrs.length == 0 ? "" : " " + attrs.sort().join(" ");
1248
+ },
1249
+ sortStyleEntries: function(styleText) {
1250
+ var entries = styleText.split(/; /);
1251
+ return entries.sort().join("; ");
1252
+ },
1253
+ escapeHtml: function(str) {
1254
+ if(!this._div) {
1255
+ this._div = document.createElement("DIV");
1256
+ this._text = document.createTextNode('');
1257
+ this._div.appendChild(this._text);
1258
+ }
1259
+ this._text.data = str;
1260
+ return this._div.innerHTML;
1261
+ },
1262
+ isDomNode: function(o) {
1263
+ // TODO: make it more stricter
1264
+ return (typeof o.nodeName == 'string') && (typeof o.nodeType == 'number');
1265
+ },
1266
+ inspectDomPath: function(o) {
1267
+ var sb = [];
1268
+ while(o && o.nodeName != '#document' && o.parent) {
1269
+ var siblings = o.parentNode.childNodes;
1270
+ for(var i = 0; i < siblings.length; i++) {
1271
+ if(siblings[i] == o) {
1272
+ sb.push(o.nodeName + (i == 0 ? '' : '[' + i + ']'));
1273
+ break;
1274
+ }
1275
+ }
1276
+ o = o.parentNode;
1277
+ }
1278
+ return sb.join(" &gt; ");
1279
+ },
1280
+ inspectDomNode: function(o) {
1281
+ if(o.nodeType == 1) {
1282
+ var nodeName = o.nodeName.toLowerCase();
1283
+ var sb = [];
1284
+ sb.push('<span class="dom_value">');
1285
+ sb.push("&lt;");
1286
+ sb.push(nodeName);
1287
+
1288
+ var attrs = o.attributes;
1289
+ for(var i = 0; i < attrs.length; i++) {
1290
+ if(
1291
+ attrs[i].nodeValue &&
1292
+ attrs[i].nodeName != 'contentEditable' &&
1293
+ attrs[i].nodeName != 'style' &&
1294
+ typeof attrs[i].nodeValue != 'function'
1295
+ ) sb.push(' <span class="dom_attr_name">' + attrs[i].nodeName.toLowerCase() + '</span>=<span class="dom_attr_value">"' + attrs[i].nodeValue + '"</span>');
1296
+ }
1297
+ if(o.style && o.style.cssText) {
1298
+ sb.push(' <span class="dom_attr_name">style</span>=<span class="dom_attr_value">"' + o.style.cssText + '"</span>');
1299
+ }
1300
+ sb.push('&gt;');
1301
+ sb.push(JSSpec.util.escapeHtml(o.innerHTML));
1302
+ sb.push('&lt;/' + nodeName + '&gt;');
1303
+ sb.push(' <span class="dom_path">(' + JSSpec.util.inspectDomPath(o) + ')</span>' );
1304
+ sb.push('</span>');
1305
+ return sb.join("");
1306
+ } else if(o.nodeType == 3) {
1307
+ return '<span class="dom_value">#text ' + o.nodeValue + '</span>';
1308
+ } else {
1309
+ return '<span class="dom_value">UnknownDomNode</span>';
1310
+ }
1311
+ },
1312
+ inspect: function(o, dontEscape, emphasisKey) {
1313
+ if(typeof o == 'undefined') return '<span class="undefined_value">undefined</span>';
1314
+ if(o == null) return '<span class="null_value">null</span>';
1315
+ if(o._type == 'String') return '<span class="string_value">"' + (dontEscape ? o : JSSpec.util.escapeHtml(o)) + '"</span>';
1316
+
1317
+ if(o._type == 'Date') {
1318
+ return '<span class="date_value">"' + o.toString() + '"</span>';
1319
+ }
1320
+
1321
+ if(o._type == 'Number') return '<span class="number_value">' + (dontEscape ? o : JSSpec.util.escapeHtml(o)) + '</span>';
1322
+
1323
+ if(o._type == 'Boolean') return '<span class="boolean_value">' + o + '</span>';
1324
+
1325
+ if(o._type == 'RegExp') return '<span class="regexp_value">' + JSSpec.util.escapeHtml(o.toString()) + '</span>';
1326
+
1327
+ if(JSSpec.util.isDomNode(o)) return JSSpec.util.inspectDomNode(o);
1328
+
1329
+ if(o._type == 'Array' || typeof o.length != 'undefined') {
1330
+ var sb = [];
1331
+ for(var i = 0; i < o.length; i++) {
1332
+ var inspected = JSSpec.util.inspect(o[i]);
1333
+ sb.push(i == emphasisKey ? ('<strong>' + inspected + '</strong>') : inspected);
1334
+ }
1335
+ return '<span class="array_value">[' + sb.join(', ') + ']</span>';
1336
+ }
1337
+
1338
+ // object
1339
+ var sb = [];
1340
+ for(var key in o) {
1341
+ if(key == 'should') continue;
1342
+
1343
+ var inspected = JSSpec.util.inspect(key) + ":" + JSSpec.util.inspect(o[key]);
1344
+ sb.push(key == emphasisKey ? ('<strong>' + inspected + '</strong>') : inspected);
1345
+ }
1346
+ return '<span class="object_value">{' + sb.join(', ') + '}</span>';
1347
+ }
1348
+ }
1349
+
1350
+ describe = JSSpec.DSL.describe;
1351
+ expect = JSSpec.DSL.expect;
1352
+
1353
+ String.prototype._type = "String";
1354
+ Number.prototype._type = "Number";
1355
+ Date.prototype._type = "Date";
1356
+ Array.prototype._type = "Array";
1357
+ Boolean.prototype._type = "Boolean";
1358
+ RegExp.prototype._type = "RegExp";
1359
+
1360
+ var targets = [Array.prototype, Date.prototype, Number.prototype, String.prototype, Boolean.prototype, RegExp.prototype];
1361
+
1362
+ String.prototype.normalizeHtml = JSSpec.DSL.forString.normalizeHtml;
1363
+ String.prototype.asHtml = String.prototype.normalizeHtml;
1364
+
1365
+
1366
+
1367
+ /**
1368
+ * Main
1369
+ */
1370
+ JSSpec.defaultOptions = {
1371
+ autorun: 1,
1372
+ specIdBeginsWith: 0,
1373
+ exampleIdBeginsWith: 0,
1374
+ autocollapse: 1
1375
+ };
1376
+ JSSpec.options = JSSpec.util.parseOptions(JSSpec.defaultOptions);
1377
+
1378
+ JSSpec.Spec.id = JSSpec.options.specIdBeginsWith;
1379
+ JSSpec.Example.id = JSSpec.options.exampleIdBeginsWith;
1380
+
1381
+
1382
+
1383
+ window.onload = function() {
1384
+ if(JSSpec.specs.length > 0) {
1385
+ if(!JSSpec.options.inSuite) {
1386
+ JSSpec.runner = new JSSpec.Runner(JSSpec.specs, new JSSpec.Logger());
1387
+ if(JSSpec.options.rerun) {
1388
+ JSSpec.runner.rerun(decodeURIComponent(JSSpec.options.rerun));
1389
+ } else {
1390
+ JSSpec.runner.run();
1391
+ }
1392
+ } else {
1393
+ // in suite, send all specs to parent
1394
+ var parentWindow = window.frames.parent.window;
1395
+ for(var i = 0; i < JSSpec.specs.length; i++) {
1396
+ parentWindow.JSSpec.specs.push(JSSpec.specs[i]);
1397
+ }
1398
+ }
1399
+ } else {
1400
+ var links = document.getElementById('list').getElementsByTagName('A');
1401
+ var frameContainer = document.createElement('DIV');
1402
+ frameContainer.style.display = 'none';
1403
+ document.body.appendChild(frameContainer);
1404
+
1405
+ for(var i = 0; i < links.length; i++) {
1406
+ var frame = document.createElement('IFRAME');
1407
+ frame.src = links[i].href + '?inSuite=0&specIdBeginsWith=' + (i * 10000) + '&exampleIdBeginsWith=' + (i * 10000);
1408
+ frameContainer.appendChild(frame);
1409
+ }
1410
+ }
1411
+ }