forked from Codecademy/jquery-expect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.expect.js
1068 lines (929 loc) · 28 KB
/
jquery.expect.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
MIT License.
Copyright (c) 2012 Amjad Masad <amjad@codecademy.com> Ryzac, Inc.
*/
(function (global, undefined) {
var jQuery = global.jQuery
, $ = jQuery;
function $expect (obj) {
return new Assertion(obj);
}
if (window.require && !jQuery) {
jQuery = $ = require("jquery");
module.exports = $expect;
}
/**
* Utilities
*/
/**
* Object.keys
*/
function keys (obj) {
if (Object.keys) {
return Object.keys(obj);
}
var ret = [];
for (var i in obj) {
if (Object.prototype.hasOwnProperty.call(obj, i)) {
ret.push(i);
}
}
return ret;
}
/**
* Inspect
*/
function inspect (obj) {
if (obj.selector) {
return obj.selector;
} else if (obj[0] && obj[0].tagName) {
var selector = obj[0].tagName.toLowerCase();
var id = obj.attr('id');
if (id) {
selector += '#' + id;
}
var classes = obj.attr('class');
if (classes) {
classes = classes.split(' ');
for (var i = 0; i < classes.length; i++) {
selector += '.' + classes[i];
}
}
return selector;
} else {
return obj;
}
}
/**
* Extended css color names hash.
* name -> HEX
* source: http://www.w3.org/TR/css3-color/#svg-color
*/
var COLORS = {
'aliceblue': '#F0F8FF'
, 'antiquewhite': '#FAEBD7'
, 'aqua': '#00FFFF'
, 'aquamarine': '#7FFFD4'
, 'azure': '#F0FFFF'
, 'beige': '#F5F5DC'
, 'bisque': '#FFE4C4'
, 'black': '#000000'
, 'blanchedalmond': '#FFEBCD'
, 'blue': '#0000FF'
, 'blueviolet': '#8A2BE2'
, 'brown': '#A52A2A'
, 'burlywood': '#DEB887'
, 'cadetblue': '#5F9EA0'
, 'chartreuse': '#7FFF00'
, 'chocolate': '#D2691E'
, 'coral': '#FF7F50'
, 'cornflowerblue': '#6495ED'
, 'cornsilk': '#FFF8DC'
, 'crimson': '#DC143C'
, 'cyan': '#00FFFF'
, 'darkblue': '#00008B'
, 'darkcyan': '#008B8B'
, 'darkgoldenrod': '#B8860B'
, 'darkgray': '#A9A9A9'
, 'darkgreen': '#006400'
, 'darkgrey': '#A9A9A9'
, 'darkkhaki': '#BDB76B'
, 'darkmagenta': '#8B008B'
, 'darkolivegreen': '#556B2F'
, 'darkorange': '#FF8C00'
, 'darkorchid': '#9932CC'
, 'darkred': '#8B0000'
, 'darksalmon': '#E9967A'
, 'darkseagreen': '#8FBC8F'
, 'darkslateblue': '#483D8B'
, 'darkslategray': '#2F4F4F'
, 'darkslategrey': '#2F4F4F'
, 'darkturquoise': '#00CED1'
, 'darkviolet': '#9400D3'
, 'deeppink': '#FF1493'
, 'deepskyblue': '#00BFFF'
, 'dimgray': '#696969'
, 'dimgrey': '#696969'
, 'dodgerblue': '#1E90FF'
, 'firebrick': '#B22222'
, 'floralwhite': '#FFFAF0'
, 'forestgreen': '#228B22'
, 'fuchsia': '#FF00FF'
, 'gainsboro': '#DCDCDC'
, 'ghostwhite': '#F8F8FF'
, 'gold': '#FFD700'
, 'goldenrod': '#DAA520'
, 'gray': '#808080'
, 'green': '#008000'
, 'greenyellow': '#ADFF2F'
, 'grey': '#808080'
, 'honeydew': '#F0FFF0'
, 'hotpink': '#FF69B4'
, 'indianred': '#CD5C5C'
, 'indigo': '#4B0082'
, 'ivory': '#FFFFF0'
, 'khaki': '#F0E68C'
, 'lavender': '#E6E6FA'
, 'lavenderblush': '#FFF0F5'
, 'lawngreen': '#7CFC00'
, 'lemonchiffon': '#FFFACD'
, 'lightblue': '#ADD8E6'
, 'lightcoral': '#F08080'
, 'lightcyan': '#E0FFFF'
, 'lightgoldenrodyellow': '#FAFAD2'
, 'lightgray': '#D3D3D3'
, 'lightgreen': '#90EE90'
, 'lightgrey': '#D3D3D3'
, 'lightpink': '#FFB6C1'
, 'lightsalmon': '#FFA07A'
, 'lightseagreen': '#20B2AA'
, 'lightskyblue': '#87CEFA'
, 'lightslategray': '#778899'
, 'lightslategrey': '#778899'
, 'lightsteelblue': '#B0C4DE'
, 'lightyellow': '#FFFFE0'
, 'lime': '#00FF00'
, 'limegreen': '#32CD32'
, 'linen': '#FAF0E6'
, 'magenta': '#FF00FF'
, 'maroon': '#800000'
, 'mediumaquamarine': '#66CDAA'
, 'mediumblue': '#0000CD'
, 'mediumorchid': '#BA55D3'
, 'mediumpurple': '#9370DB'
, 'mediumseagreen': '#3CB371'
, 'mediumslateblue': '#7B68EE'
, 'mediumspringgreen': '#00FA9A'
, 'mediumturquoise': '#48D1CC'
, 'mediumvioletred': '#C71585'
, 'midnightblue': '#191970'
, 'mintcream': '#F5FFFA'
, 'mistyrose': '#FFE4E1'
, 'moccasin': '#FFE4B5'
, 'navajowhite': '#FFDEAD'
, 'navy': '#000080'
, 'oldlace': '#FDF5E6'
, 'olive': '#808000'
, 'olivedrab': '#6B8E23'
, 'orange': '#FFA500'
, 'orangered': '#FF4500'
, 'orchid': '#DA70D6'
, 'palegoldenrod': '#EEE8AA'
, 'palegreen': '#98FB98'
, 'paleturquoise': '#AFEEEE'
, 'palevioletred': '#DB7093'
, 'papayawhip': '#FFEFD5'
, 'peachpuff': '#FFDAB9'
, 'peru': '#CD853F'
, 'pink': '#FFC0CB'
, 'plum': '#DDA0DD'
, 'powderblue': '#B0E0E6'
, 'purple': '#800080'
, 'red': '#FF0000'
, 'rosybrown': '#BC8F8F'
, 'royalblue': '#4169E1'
, 'saddlebrown': '#8B4513'
, 'salmon': '#FA8072'
, 'sandybrown': '#F4A460'
, 'seagreen': '#2E8B57'
, 'seashell': '#FFF5EE'
, 'sienna': '#A0522D'
, 'silver': '#C0C0C0'
, 'skyblue': '#87CEEB'
, 'slateblue': '#6A5ACD'
, 'slategray': '#708090'
, 'slategrey': '#708090'
, 'snow': '#FFFAFA'
, 'springgreen': '#00FF7F'
, 'steelblue': '#4682B4'
, 'tan': '#D2B48C'
, 'teal': '#008080'
, 'thistle': '#D8BFD8'
, 'tomato': '#FF6347'
, 'turquoise': '#40E0D0'
, 'violet': '#EE82EE'
, 'wheat': '#F5DEB3'
, 'white': '#FFFFFF'
, 'whitesmoke': '#F5F5F5'
, 'yellow': '#FFFF00'
, 'yellowgreen': '#9ACD32'
};
/**
* Possible assertion flags.
*/
var flags = {
not: ['to', 'be', 'have', 'include', 'only']
, to: ['be', 'have', 'include', 'match', 'matchHtml', 'not', 'only']
, be: []
, is: []
, are: []
, have: []
, has: []
, any: []
};
/**
* Constructor
* from expect.js (copyright LearnBoost, MIT license)
*/
function Assertion (obj, flag, parent) {
this.obj = obj instanceof jQuery ? obj : $(obj);
this.flags = {};
if (undefined !== parent) {
this.flags[flag] = true;
for (var p in parent.flags) {
if (parent.flags.hasOwnProperty(p)) {
this.flags[p] = true;
}
}
}
var $flags = flag ? flags[flag] : keys(flags)
, self = this;
if ($flags) {
for (var i = 0, l = $flags.length; i < l; i++) {
if (this.flags[$flags[i]]) continue;
(function () {
// avoid recursion
var name = $flags[i]
, assertion = new Assertion(this.obj, name, this);
if ('function' === typeof Assertion.prototype[name]) {
// clone the function, make sure we dont touch the prot reference
var old = this[name];
this[name] = function () {
return old.apply(self, arguments);
};
for (var fn in Assertion.prototype) {
if (Assertion.prototype.hasOwnProperty(fn) && fn !== name) {
this[name][fn] = $.proxy(assertion[fn], assertion);
}
}
} else {
this[name] = assertion;
}
}).call(this);
}
}
}
/**
* Constructor
* @inherits Error
*/
function AssertionError (msg) {
Error.call(this);
/*jshint noarg:false*/
if (Error.captureStackTrace) Error.captureStackTrace(this, arguments.callee);
this.message = msg;
this.name = 'AssertionError';
}
AssertionError.prototype = new Error();
AssertionError.prototype.constructor = AssertionError;
global.$expect = $expect;
$expect.Assertion = Assertion;
$expect.AssertionError = AssertionError;
/**
* Performs an assertion
*
* @param {Boolean} truth
* @param {String|Function} msg
* @param {String} error
* @api private
*/
Assertion.prototype.assert = function (truth, msg, error) {
var ok = this.flags.not ? !truth : truth;
if ($.isFunction(msg)) {
error = msg = msg.call(this, !ok);
}
msg = this.flags.not ? error : msg;
if (!ok) {
throw new AssertionError(msg);
}
this.and = new Assertion(this.obj);
};
/**
* Check if the jQuery object has any elements.
*
* @api public
*/
Assertion.prototype.exist = function (msg) {
this.assert(
!!this.obj.length
, msg || 'expected ' + inspect(this.obj) + ' to exist'
, msg || 'expected ' + inspect(this.obj) + ' not to exist');
return this;
};
/**
* Assert having _n_ elements.
*
* @param {Number} n
* @api public
*/
Assertion.prototype.elements =
Assertion.prototype.items =
Assertion.prototype.length = function (n, msg) {
var len = this.obj.length;
this.assert(
n === len
, msg || 'expected ' + inspect(this.obj) + ' to have a length of ' + n + ' but got ' + len
, msg || 'expected ' + inspect(this.obj) + ' to not have a length of ' + len);
return this;
};
/**
* Assert having elements greater than _n_ elements.
*
* @param {Number} n
* @api public
*/
Assertion.prototype.greaterThan =
Assertion.prototype.above = function (n, msg) {
this.assert(
this.obj.length > n
, msg || 'expected ' + inspect(this.obj) + ' to have a length greater than ' + n
, msg || 'expected ' + inspect(this.obj) + ' to have a length less than ' + n);
return this;
};
/**
* Assert having elements less than _n_ elements.
*
* @param {Number} n
* @api public
*/
Assertion.prototype.lessThan =
Assertion.prototype.below = function (n, msg) {
this.assert(
this.obj.length < n
, msg || 'expected ' + inspect(this.obj) + ' to have a length less than ' + n
, msg || 'expected ' + inspect(this.obj) + ' to have a length greater than ' + n);
return this;
};
/**
* Checks if a jQuery collection has exactly the same and no more elements than another one.
*
* @api public
*/
Assertion.prototype.eql =
Assertion.prototype.equal = function ($el, msg) {
$el = $el instanceof jQuery ? $el : $($el);
// Returns true if every element in a appears exactly once in b.
var injSurj = function(a,b) {
if (a.length !== b.length) return false;
return a.map(function(i, el) {
return $.inArray(el, b) > -1 ? true : null;
}).length === a.length;
};
// Arrays are equal if every element in this.obj
// appears in $el, and vice-versa.
var eq = injSurj(this.obj, $el) && injSurj($el, this.obj);
this.assert(
eq
, msg || 'expected ' + inspect(this.obj) + ' to equal ' + inspect($el)
, msg || 'expected ' + inspect(this.obj) + ' to not equal ' + inspect($el));
return this;
};
/**
* Asserts the value of an attribute on an element.
*
* @api public
*/
Assertion.prototype.attr = function (prop, val, msg) {
var got = this.obj.attr(prop);
if (undefined === val) {
this.assert(
undefined !== got
, msg || 'expected ' + inspect(this.obj) + ' to have an attribute ' + prop
, msg || 'expected ' + inspect(this.obj) + ' not to have attribute ' + prop);
} else {
this.assert(
got === val
, msg || 'expected ' + inspect(this.obj) + ' to have an attribute ' + prop + ' equals to ' + val
, msg || 'expected ' + inspect(this.obj) + ' to not have an attribute ' + prop + ' equals to ' + val);
}
return this;
};
/**
* Convert rgb(x, y, z) -> Hex
* source: http://wowmotty.blogspot.com/2009/06/convert-jquery-rgb-output-to-hex-color.html
*/
function rgb2hex (rgb) {
rgb = rgb.match(/^rgba?[\s+]?\([\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?/i);
return (rgb && rgb.length === 4) ? "#" +
("0" + parseInt(rgb[1],10).toString(16)).slice(-2) +
("0" + parseInt(rgb[2],10).toString(16)).slice(-2) +
("0" + parseInt(rgb[3],10).toString(16)).slice(-2) : '';
}
/**
* Takes color in any format and returns it in a HEX (uppercase) format.
*/
function normalizeColor (color) {
if (!color) return '';
function r (x) { return x.toUpperCase() + x.toUpperCase(); }
if (color.match(/^#/)) {
if (color.length === 4) {
return '#' + r(color.charAt(1)) + r(color.charAt(2)) + r(color.charAt(3));
}
return color.toUpperCase();
} else if (color.match(/^rgb/)) {
return rgb2hex(color).toUpperCase();
} else {
return COLORS[$.trim(color)];
}
}
/**
* Breakdown shorthand css and compare each direction.
*/
function compareQuad ($el, prop, val) {
var isColor = prop.match(/color/);
function props (dir) {
if (prop.match(/^border/)) {
var parts = prop.split(/-/);
return parts[0] + '-' + dir + '-' + parts[1];
} else {
return prop + '-' + dir;
}
}
val = val.split(/\s/);
var passing = true
, got = $.map(['top', 'right', 'bottom', 'left'], function (dir, i) {
// 4 values or 2 values repeat or 1 value repeat.
var value = val[i] || val[i - 2] || val[0]
, got = $el.css(props(dir));
if (isColor) {
if (normalizeColor(value) !== (got = normalizeColor(got))) passing = false;
} else {
if (value !== got) passing = false;
}
return got;
});
return {passing: passing, got: got.join(' ')};
}
/**
* Breakdown border shorthand css and compare each style.
*/
function borderQuad ($el, prop, val) {
val = val.split(/\s/);
var passing = true
, got = $.map(['width', 'style', 'color'], function (style, i) {
var got = $el.css(prop + '-' + style );
if (style === 'color') {
if ((got = normalizeColor(got)) !== normalizeColor(val[i])) passing = false;
} else {
if (got !== val[i]) passing = false;
}
return got;
});
return {passing: passing, got: got.join(' ')};
}
/**
* Array unique and join.
*/
function stringifyGot (got) {
return $.map(got, function (g, i) {
return $.inArray(g, got) === i ? g : null;
}).join(' ');
}
/**
* Assert having a css value.
*
* @api public
* @param {String} prop
* @param {*} val
* @param {String|Function} msg
*/
Assertion.prototype.css = function (prop, val, msg) {
prop = $.trim(prop);
val = typeof val === 'string' ? $.trim(val) : val;
var obj = this.obj
, template = function (got) {
return msg || 'expected ' + inspect(obj) + ' to have its ' + prop
+ ' style equal to ' + val + ( got ? ' but got ' + got : '');
}
, that = this
, got, passing;
function check (obj) {
switch (prop) {
case 'backgroundColor':
case 'background-color':
case 'color':
passing = (got = normalizeColor(obj.css(prop))) === normalizeColor(val);
break;
case 'border-style':
case 'border-color':
case 'border-width':
case 'margin':
case 'padding':
got = compareQuad(obj, prop, val);
passing = got.passing;
got = got.got;
break;
case 'border-top':
case 'border-right':
case 'border-left':
case 'border-bottom':
got = borderQuad(obj, prop, val);
passing = got.passing;
got = got.got;
break;
case 'border':
passing = true;
got = $.map(['top', 'right', 'left', 'bottom'], function (dir) {
var ret = borderQuad(obj, prop + '-' + dir, val);
if (!ret.passing) passing = false;
return ret.got;
});
got = stringifyGot(got);
break;
case 'border-radius':
got = [];
passing = ((got[0] = obj.css('border-top-left-radius')) === val)
&& ((got[1] = obj.css('border-top-right-radius')) === val)
&& ((got[2] = obj.css('border-bottom-left-radius')) === val)
&& ((got[3] = obj.css('border-bottom-right-radius')) === val);
got = stringifyGot(got);
break;
default:
passing = (got = obj.css(prop)) === val;
}
that.assert(
passing
, template(got)
, template());
}
this.obj.each(function (_, el) {
check($(el));
});
return this;
};
/**
* Asserts that an element has certain text. The check is loose by default, meaning punctuation
* and spaces are stripped out and case is ignored. Pass in true as the second argument for
* strict equality.
*
* @param {String} val
* @param {String|Function} msg
* @api public
*/
Assertion.prototype.text = function (val, msg) {
var text = this.obj.text();
if ('number' === typeof val) {
this.assert(
text.length === val
, msg || 'expected ' + inspect(this.obj) + ' text to be of length ' + val + ' but got ' + text.length
, msg || 'expected ' + inspect(this.obj) + ' text to not be of length ' + val);
} else if (val instanceof RegExp) {
this.assert(
val.test(text)
, msg || 'expected ' + inspect(this.obj) + ' text to match ' + String(val)
, msg || 'expected ' + inspect(this.obj) + ' text not to match ' + String(val));
} else if (null == val) {
this.assert(
!!text.length
, msg || 'expected ' + inspect(this.obj) + ' to have text'
, msg || 'expected ' + inspect(this.obj) + ' to not have text');
} else {
val = String(val);
this.assert(
text === val
, msg || 'expected ' + inspect(this.obj) + ' text to be equal to ' + val + ' but got ' + text
, msg || 'expected ' + inspect(this.obj) + ' text to not be equal to ' + val);
}
return this;
};
/**
* Asserts that an element has certain text. The check is loose by default, meaning punctuation
* and spaces are stripped out and case is ignored. Pass in true as the second argument for
* strict equality.
*
* @param {String} val
* @param @optional {Boolean} strict
* @param {String|Function} msg
* @api public
*/
Assertion.prototype.contain = function (text, strict, msg) {
if ('boolean' !== typeof strict) {
msg = strict;
strict = false;
}
var re = /[\.,-\/#!$%\^&\*;:{}=\-_`~()\s'"]/g
, passing;
if (strict) {
passing = this.obj.is(':contains(\'' + text + '\')');
} else {
passing = this.obj.text().replace(re, '').toLowerCase().indexOf(text.replace(re, '').toLowerCase()) > -1;
}
this.assert(
passing
, msg || 'expected ' + inspect(this.obj) + ' to contain "' + text + '"'
, msg || 'expected ' + inspect(this.obj) + ' not to contain "' + text + '"');
return this;
};
/**
* Asserts the value of the following jquery accessor functions.
*
* @param {*} val
* @api public
*/
$.each([ 'width', 'innerWidth', 'outerWidth'
, 'height', 'innerHeight', 'outerHeight'
, 'scrollLeft', 'scrollTop'
], function (_, fn) {
Assertion.prototype[fn] = function (val, msg) {
var ops = {
'>': function (v1, v2) { return v1 > v2; }
, '>=': function (v1, v2) { return v1 >= v2; }
, '<': function (v1, v2) { return v1 < v2; }
, '<=': function (v1, v2) { return v1 <= v2; }
};
if ('string' === typeof val && (ops[$.trim(val).slice(0, 2)] || ops[$.trim(val).charAt(0)])) {
val = $.trim(val);
var op, opName;
if (op = ops[val.slice(0, 2)]) {
opName = val.slice(0, 2);
val = parseFloat(val.slice(2));
} else if (op = ops[val.charAt(0)] ) {
opName = val.charAt(0);
val = parseFloat(val.slice(1));
}
var v = this.obj[fn]();
this.assert(
op(v, val)
, msg || 'expected ' + inspect(this.obj) + ' to have a ' + fn + ' ' + opName + ' ' + val
, msg || 'expected ' + inspect(this.obj) + ' not to have a ' + fn + ' ' + opName + ' ' + val);
} else {
var got;
this.assert(
(got = this.obj[fn]()) === val
, msg || 'expected ' + inspect(this.obj) + ' to have a ' + fn + ' of ' + val + ' but got ' + got
, msg || 'expected ' + inspect(this.obj) + ' not to have a ' + fn + ' of ' + val);
}
return this;
};
});
/**
* Asserts that an input element has a value.
*
* @param {String|Number} val
* @param {String|Function} msg
* @api public
*/
Assertion.prototype.value =
Assertion.prototype.val = function (val, msg) {
var got;
this.assert(
(got = this.obj.val()) === val
, msg || 'expected ' + inspect(this.obj) + ' to have value ' + val + ' but got ' + got
, msg || 'expected ' + inspect(this.obj) + ' not to have value ' + val);
};
/**
* Asserts the equality of an element's HTML with a string.
*
* @param {String} html
* @param {String|Function} msg
* @api public
*/
Assertion.prototype.html = function (html, msg) {
var got;
this.assert(
(got = this.obj.html()) === html
, msg || 'expected ' + inspect(this.obj) + ' to have HTML ' + html + ' but got ' + got
, msg || 'expected ' + inspect(this.obj) + ' not to have HTML ' + html);
};
/**
* Asserts the existence of elements in different directions and traversal ways
* of the tree.
*
* @param {String|jQueryObject} val
* @api public
*/
$.each([ 'children', 'closest', 'find'
, 'next', 'nextAll', 'nextUntil'
, 'offsetParent', 'parent', 'parents'
, 'parentsUntil', 'prev', 'prevAll'
, 'prevUntil', 'siblings'
], function (_, fn) {
Assertion.prototype[fn] = function (val, msg) {
var got = this.obj[fn](val);
this.assert(
!!got.length
, msg || 'expected ' + inspect(this.obj) + ' to have ' + fn + ' ' + val
, msg || 'expected ' + inspect(this.obj) + ' not to have ' + fn + ' ' + val);
this.that = this.which = new Assertion(got);
return this;
};
});
// alias
Assertion.prototype.have = Assertion.prototype.find;
/**
* Asserts the element(s) using the jquery $().is method.
*
* @api public
*/
Assertion.prototype.be =
Assertion.prototype.an =
Assertion.prototype.a = function (obj, msg) {
this.assert(
this.obj.is(obj)
, msg || 'expected ' + inspect(this.obj) + ' to be ' + inspect(obj)
, msg || 'expected ' + inspect(this.obj) + ' not to be ' + inspect(obj));
return this;
};
/**
* Asserts that the html of an element matches the given regexp.
*
* @param {RegExp} regexp
* @param {String|Function} msg
* @api public
*/
Assertion.prototype.matchHtml = function (regexp, msg) {
this.assert(
regexp.exec(this.obj.html())
, msg || 'expected ' + inspect(this.obj) + ' to match ' + regexp
, msg || 'expected ' + inspect(this.obj) + ' not to match ' + regexp);
return this;
}
/**
* Asserts that the text of an element matches the given regexp.
*
* @param {RegExp} regexp
* @param {String|Function} msg
* @api public
*/
Assertion.prototype.match = function (regexp, msg) {
return this.text(regexp, msg);
}
/**
* Asserts that at least one currently selected element passes the given
* assertion function.
*
* @param {Function} assertionFn
* @param {String} msg
* @api public
*/
Assertion.prototype.any = function (assertionFn, msg) {
if (!(assertionFn instanceof Function)) {
throw TypeError('The any assertion must be passed an assertion ' +
'function as its first parameter.');
}
var numFailedChildren = 0;
this.obj.each(function(_, element) {
try {
assertionFn(element)
} catch (e) {
if (e instanceof AssertionError) {
numFailedChildren++;
} else {
throw e;
}
}
});
this.assert(
numFailedChildren < this.obj.length
, msg || 'expected ' + inspect(this.obj) + ' to have at least one ' +
'element that passed the any assertion'
, msg || 'expected ' + inspect(this.obj) + ' to have no elements that ' +
'passed the any assertion'
);
return this;
}
/**
* Returns a new assertion object after calling the jquery end method.
*
* @api public
*/
Assertion.prototype.end = function () {
return new Assertion(this.obj.end());
};
/**
* Async testing signals.
*
* @param {String} event
* @param {Deferred} deferred
*/
function DeferredSignal (deferred, event, args) {
this.deferred = deferred;
this.event = event;
this.args = args;
}
DeferredSignal.prototype = new Error();
$expect.DeferredSignal = DeferredSignal;
function signal (deferred, event /* args... */) {
throw new DeferredSignal(deferred, event, [].slice.call(arguments, 2));
}
/**
* Attaches a *once* callback to an event.
* Calls asyncWait with the eventType and a deferred object.
* When the event is fired the deferred is resolved and passed to asynDone alont with the eventType.
*
* @api public
* @param {String} evt
* @param {Function} cb
*/
Assertion.prototype.on = function (evt, cb) {
var obj = this.obj
, dfd = $.Deferred()
, callback = function () {
obj.off(evt, callback);
var ret = null;
try {
ret = cb.apply(this, arguments);
dfd.resolveWith(obj, [null, ret]);
} catch (e) {
dfd.rejectWith(obj, [e, ret]);
}
return ret;
};
this.obj.on(evt, callback);
return signal(dfd, evt, String(inspect(this.obj)));
};
/**
* Wait before executing a callback
* @api public
* @param {String} evt
* @param {Function} cb
*/
$expect.wait = function (delay, cb) {
var dfd = $.Deferred();
setTimeout(function () {
try {
cb();
dfd.resolve();
} catch (e) {
dfd.reject(e);
}
}, delay);
return signal(dfd, 'wait', delay);
};
/**
* Wait for a callback to call our done callback to reject or resolve a
* deferred with a string that would be made an assertion error.
* @api public
* @param {Function} cb
*/
$expect.async = function (cb) {
var dfd = $.Deferred();
var done = function (res) {
if (res) {
var err = new AssertionError(String(res));
dfd.reject(err);
} else {
dfd.resolve();