@@ -49,6 +49,41 @@ beforeEach(function() {
49
49
return hidden ;
50
50
}
51
51
52
+ function MinErrMatcher ( isNot , namespace , code , content , wording ) {
53
+ var codeRegex = new RegExp ( '^' + escapeRegexp ( '[' + namespace + ':' + code + ']' ) ) ;
54
+ var contentRegex = angular . isUndefined ( content ) || jasmine . isA_ ( 'RegExp' , content ) ?
55
+ content : new RegExp ( escapeRegexp ( content ) ) ;
56
+
57
+ this . test = test ;
58
+
59
+ function escapeRegexp ( str ) {
60
+ // This function escapes all special regex characters.
61
+ // We use it to create matching regex from arbitrary strings.
62
+ // http://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex
63
+ return str . replace ( / [ \- \[ \] \/ \{ \} \( \) \* \+ \? \. \\ \^ \$ \| ] / g, '\\$&' ) ;
64
+ }
65
+
66
+ function test ( exception ) {
67
+ var exceptionMessage = ( exception && exception . message ) || exception || '' ;
68
+
69
+ var codeMatches = codeRegex . test ( exceptionMessage ) ;
70
+ var contentMatches = angular . isUndefined ( contentRegex ) || contentRegex . test ( exceptionMessage ) ;
71
+ var matches = codeMatches && contentMatches ;
72
+
73
+ return {
74
+ pass : isNot ? ! matches : matches ,
75
+ message : message
76
+ } ;
77
+
78
+ function message ( ) {
79
+ return 'Expected ' + wording . inputType + ( isNot ? ' not' : '' ) + ' to ' +
80
+ wording . expectedAction + ' ' + namespace + 'MinErr(\'' + code + '\')' +
81
+ ( contentRegex ? ' matching ' + contentRegex . toString ( ) : '' ) +
82
+ ( ! exception ? '.' : ', but it ' + wording . actualAction + ': ' + exceptionMessage ) ;
83
+ }
84
+ }
85
+ }
86
+
52
87
jasmine . addMatchers ( {
53
88
toBeEmpty : cssMatcher ( 'ng-empty' , 'ng-not-empty' ) ,
54
89
toBeNotEmpty : cssMatcher ( 'ng-not-empty' , 'ng-empty' ) ,
@@ -58,6 +93,7 @@ beforeEach(function() {
58
93
toBePristine : cssMatcher ( 'ng-pristine' , 'ng-dirty' ) ,
59
94
toBeUntouched : cssMatcher ( 'ng-untouched' , 'ng-touched' ) ,
60
95
toBeTouched : cssMatcher ( 'ng-touched' , 'ng-untouched' ) ,
96
+
61
97
toBeAPromise : function ( ) {
62
98
return {
63
99
compare : generateCompare ( false ) ,
@@ -71,6 +107,7 @@ beforeEach(function() {
71
107
} ;
72
108
}
73
109
} ,
110
+
74
111
toBeShown : function ( ) {
75
112
return {
76
113
compare : generateCompare ( false ) ,
@@ -87,6 +124,7 @@ beforeEach(function() {
87
124
} ;
88
125
}
89
126
} ,
127
+
90
128
toBeHidden : function ( ) {
91
129
return {
92
130
compare : generateCompare ( false ) ,
@@ -267,26 +305,34 @@ beforeEach(function() {
267
305
}
268
306
} ,
269
307
308
+ toEqualMinErr : function ( ) {
309
+ return {
310
+ compare : generateCompare ( false ) ,
311
+ negativeCompare : generateCompare ( true )
312
+ } ;
313
+
314
+ function generateCompare ( isNot ) {
315
+ return function ( actual , namespace , code , content ) {
316
+ var matcher = new MinErrMatcher ( isNot , namespace , code , content , {
317
+ inputType : 'error' ,
318
+ expectedAction : 'equal' ,
319
+ actualAction : 'was'
320
+ } ) ;
321
+
322
+ return matcher . test ( actual ) ;
323
+ } ;
324
+ }
325
+ } ,
326
+
270
327
toThrowMinErr : function ( ) {
271
328
return {
272
329
compare : generateCompare ( false ) ,
273
330
negativeCompare : generateCompare ( true )
274
331
} ;
332
+
275
333
function generateCompare ( isNot ) {
276
334
return function ( actual , namespace , code , content ) {
277
- var result ,
278
- exception ,
279
- exceptionMessage = '' ,
280
- escapeRegexp = function ( str ) {
281
- // This function escapes all special regex characters.
282
- // We use it to create matching regex from arbitrary strings.
283
- // http://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex
284
- return str . replace ( / [ \- \[ \] \/ \{ \} \( \) \* \+ \? \. \\ \^ \$ \| ] / g, '\\$&' ) ;
285
- } ,
286
- codeRegex = new RegExp ( '^\\[' + escapeRegexp ( namespace ) + ':' + escapeRegexp ( code ) + '\\]' ) ,
287
- not = isNot ? 'not ' : '' ,
288
- regex = jasmine . isA_ ( 'RegExp' , content ) ? content :
289
- angular . isDefined ( content ) ? new RegExp ( escapeRegexp ( content ) ) : undefined ;
335
+ var exception ;
290
336
291
337
if ( ! angular . isFunction ( actual ) ) {
292
338
throw new Error ( 'Actual is not a function' ) ;
@@ -298,40 +344,16 @@ beforeEach(function() {
298
344
exception = e ;
299
345
}
300
346
301
- if ( exception ) {
302
- exceptionMessage = exception . message || exception ;
303
- }
304
-
305
- var message = function ( ) {
306
- return 'Expected function ' + not + 'to throw ' +
307
- namespace + 'MinErr(\'' + code + '\')' +
308
- ( regex ? ' matching ' + regex . toString ( ) : '' ) +
309
- ( exception ? ', but it threw ' + exceptionMessage : '.' ) ;
310
- } ;
311
-
312
- result = codeRegex . test ( exceptionMessage ) ;
313
- if ( ! result ) {
314
- if ( isNot ) {
315
- return { pass : ! result , message : message } ;
316
- } else {
317
- return { pass : result , message : message } ;
318
- }
319
- }
347
+ var matcher = new MinErrMatcher ( isNot , namespace , code , content , {
348
+ inputType : 'function' ,
349
+ expectedAction : 'throw' ,
350
+ actualAction : 'threw'
351
+ } ) ;
320
352
321
- if ( angular . isDefined ( regex ) ) {
322
- if ( isNot ) {
323
- return { pass : ! regex . test ( exceptionMessage ) , message : message } ;
324
- } else {
325
- return { pass : regex . test ( exceptionMessage ) , message : message } ;
326
- }
327
- }
328
- if ( isNot ) {
329
- return { pass : ! result , message : message } ;
330
- } else {
331
- return { pass : result , message : message } ;
332
- }
353
+ return matcher . test ( exception ) ;
333
354
} ;
334
355
}
356
+
335
357
}
336
358
} ) ;
337
359
} ) ;
0 commit comments