1
1
"use strict" ;
2
2
3
- var arrayProto = require ( "@sinonjs/commons" ) . prototypes . array ;
4
- var calledInOrder = require ( "@sinonjs/commons" ) . calledInOrder ;
5
- var createMatcher = require ( "@sinonjs/samsam" ) . createMatcher ;
6
- var orderByFirstCall = require ( "@sinonjs/commons" ) . orderByFirstCall ;
7
- var timesInWords = require ( "./util/core/times-in-words" ) ;
8
- var inspect = require ( "util" ) . inspect ;
9
- var stringSlice = require ( "@sinonjs/commons" ) . prototypes . string . slice ;
10
- var globalObject = require ( "@sinonjs/commons" ) . global ;
11
-
12
- var arraySlice = arrayProto . slice ;
13
- var concat = arrayProto . concat ;
14
- var forEach = arrayProto . forEach ;
15
- var join = arrayProto . join ;
16
- var splice = arrayProto . splice ;
3
+ const arrayProto = require ( "@sinonjs/commons" ) . prototypes . array ;
4
+ const calledInOrder = require ( "@sinonjs/commons" ) . calledInOrder ;
5
+ const createMatcher = require ( "@sinonjs/samsam" ) . createMatcher ;
6
+ const orderByFirstCall = require ( "@sinonjs/commons" ) . orderByFirstCall ;
7
+ const timesInWords = require ( "./util/core/times-in-words" ) ;
8
+ const inspect = require ( "util" ) . inspect ;
9
+ const stringSlice = require ( "@sinonjs/commons" ) . prototypes . string . slice ;
10
+ const globalObject = require ( "@sinonjs/commons" ) . global ;
11
+
12
+ const arraySlice = arrayProto . slice ;
13
+ const concat = arrayProto . concat ;
14
+ const forEach = arrayProto . forEach ;
15
+ const join = arrayProto . join ;
16
+ const splice = arrayProto . splice ;
17
17
18
18
function createAssertObject ( ) {
19
- var assert ;
20
-
21
- function verifyIsStub ( ) {
22
- var args = arraySlice ( arguments ) ;
23
-
24
- forEach ( args , function ( method ) {
25
- if ( ! method ) {
26
- assert . fail ( "fake is not a spy" ) ;
27
- }
28
-
29
- if ( method . proxy && method . proxy . isSinonProxy ) {
30
- verifyIsStub ( method . proxy ) ;
31
- } else {
32
- if ( typeof method !== "function" ) {
33
- assert . fail ( `${ method } is not a function` ) ;
34
- }
35
-
36
- if ( typeof method . getCall !== "function" ) {
37
- assert . fail ( `${ method } is not stubbed` ) ;
38
- }
39
- }
40
- } ) ;
41
- }
42
-
43
- function verifyIsValidAssertion ( assertionMethod , assertionArgs ) {
44
- switch ( assertionMethod ) {
45
- case "notCalled" :
46
- case "called" :
47
- case "calledOnce" :
48
- case "calledTwice" :
49
- case "calledThrice" :
50
- if ( assertionArgs . length !== 0 ) {
51
- assert . fail (
52
- `${ assertionMethod } takes 1 argument but was called with ${
53
- assertionArgs . length + 1
54
- } arguments`
55
- ) ;
56
- }
57
- break ;
58
- default :
59
- break ;
60
- }
61
- }
62
-
63
- function failAssertion ( object , msg ) {
64
- var obj = object || globalObject ;
65
- var failMethod = obj . fail || assert . fail ;
66
- failMethod . call ( obj , msg ) ;
67
- }
68
-
69
- function mirrorPropAsAssertion ( name , method , message ) {
70
- var msg = message ;
71
- var meth = method ;
72
- if ( arguments . length === 2 ) {
73
- msg = method ;
74
- meth = name ;
75
- }
76
-
77
- assert [ name ] = function ( fake ) {
78
- verifyIsStub ( fake ) ;
79
-
80
- var args = arraySlice ( arguments , 1 ) ;
81
- var failed = false ;
82
-
83
- verifyIsValidAssertion ( name , args ) ;
84
-
85
- if ( typeof meth === "function" ) {
86
- failed = ! meth ( fake ) ;
87
- } else {
88
- failed =
89
- typeof fake [ meth ] === "function"
90
- ? ! fake [ meth ] . apply ( fake , args )
91
- : ! fake [ meth ] ;
92
- }
93
-
94
- if ( failed ) {
95
- failAssertion (
96
- this ,
97
- ( fake . printf || fake . proxy . printf ) . apply (
98
- fake ,
99
- concat ( [ msg ] , args )
100
- )
101
- ) ;
102
- } else {
103
- assert . pass ( name ) ;
104
- }
105
- } ;
106
- }
107
-
108
- function exposedName ( prefix , prop ) {
109
- return ! prefix || / ^ f a i l / . test ( prop )
110
- ? prop
111
- : prefix +
112
- stringSlice ( prop , 0 , 1 ) . toUpperCase ( ) +
113
- stringSlice ( prop , 1 ) ;
114
- }
115
-
116
- assert = {
19
+ const assert = {
117
20
failException : "AssertError" ,
118
21
119
22
fail : function fail ( message ) {
120
- var error = new Error ( message ) ;
23
+ const error = new Error ( message ) ;
121
24
error . name = this . failException || assert . failException ;
122
25
123
26
throw error ;
@@ -129,14 +32,14 @@ function createAssertObject() {
129
32
130
33
callOrder : function assertCallOrder ( ) {
131
34
verifyIsStub . apply ( null , arguments ) ;
132
- var expected = "" ;
133
- var actual = "" ;
35
+ let expected = "" ;
36
+ let actual = "" ;
134
37
135
38
if ( ! calledInOrder ( arguments ) ) {
136
39
try {
137
40
expected = join ( arguments , ", " ) ;
138
- var calls = arraySlice ( arguments ) ;
139
- var i = calls . length ;
41
+ const calls = arraySlice ( arguments ) ;
42
+ let i = calls . length ;
140
43
while ( i ) {
141
44
if ( ! calls [ -- i ] . called ) {
142
45
splice ( calls , i , 1 ) ;
@@ -159,7 +62,7 @@ function createAssertObject() {
159
62
callCount : function assertCallCount ( method , count ) {
160
63
verifyIsStub ( method ) ;
161
64
162
- var msg ;
65
+ let msg ;
163
66
if ( typeof count !== "number" ) {
164
67
msg =
165
68
`expected ${ inspect ( count ) } to be a number ` +
@@ -180,12 +83,12 @@ function createAssertObject() {
180
83
throw new TypeError ( "target is null or undefined" ) ;
181
84
}
182
85
183
- var o = options || { } ;
184
- var prefix =
86
+ const o = options || { } ;
87
+ const prefix =
185
88
( typeof o . prefix === "undefined" && "assert" ) || o . prefix ;
186
- var includeFail =
89
+ const includeFail =
187
90
typeof o . includeFail === "undefined" || Boolean ( o . includeFail ) ;
188
- var instance = this ;
91
+ const instance = this ;
189
92
190
93
forEach ( Object . keys ( instance ) , function ( method ) {
191
94
if (
@@ -200,11 +103,11 @@ function createAssertObject() {
200
103
} ,
201
104
202
105
match : function match ( actual , expectation ) {
203
- var matcher = createMatcher ( expectation ) ;
106
+ const matcher = createMatcher ( expectation ) ;
204
107
if ( matcher . test ( actual ) ) {
205
108
assert . pass ( "match" ) ;
206
109
} else {
207
- var formatted = [
110
+ const formatted = [
208
111
"expected value to match" ,
209
112
` expected = ${ inspect ( expectation ) } ` ,
210
113
` actual = ${ inspect ( actual ) } ` ,
@@ -215,6 +118,101 @@ function createAssertObject() {
215
118
} ,
216
119
} ;
217
120
121
+ function verifyIsStub ( ) {
122
+ const args = arraySlice ( arguments ) ;
123
+
124
+ forEach ( args , function ( method ) {
125
+ if ( ! method ) {
126
+ assert . fail ( "fake is not a spy" ) ;
127
+ }
128
+
129
+ if ( method . proxy && method . proxy . isSinonProxy ) {
130
+ verifyIsStub ( method . proxy ) ;
131
+ } else {
132
+ if ( typeof method !== "function" ) {
133
+ assert . fail ( `${ method } is not a function` ) ;
134
+ }
135
+
136
+ if ( typeof method . getCall !== "function" ) {
137
+ assert . fail ( `${ method } is not stubbed` ) ;
138
+ }
139
+ }
140
+ } ) ;
141
+ }
142
+
143
+ function verifyIsValidAssertion ( assertionMethod , assertionArgs ) {
144
+ switch ( assertionMethod ) {
145
+ case "notCalled" :
146
+ case "called" :
147
+ case "calledOnce" :
148
+ case "calledTwice" :
149
+ case "calledThrice" :
150
+ if ( assertionArgs . length !== 0 ) {
151
+ assert . fail (
152
+ `${ assertionMethod } takes 1 argument but was called with ${
153
+ assertionArgs . length + 1
154
+ } arguments`
155
+ ) ;
156
+ }
157
+ break ;
158
+ default :
159
+ break ;
160
+ }
161
+ }
162
+
163
+ function failAssertion ( object , msg ) {
164
+ const obj = object || globalObject ;
165
+ const failMethod = obj . fail || assert . fail ;
166
+ failMethod . call ( obj , msg ) ;
167
+ }
168
+
169
+ function mirrorPropAsAssertion ( name , method , message ) {
170
+ let msg = message ;
171
+ let meth = method ;
172
+ if ( arguments . length === 2 ) {
173
+ msg = method ;
174
+ meth = name ;
175
+ }
176
+
177
+ assert [ name ] = function ( fake ) {
178
+ verifyIsStub ( fake ) ;
179
+
180
+ const args = arraySlice ( arguments , 1 ) ;
181
+ let failed = false ;
182
+
183
+ verifyIsValidAssertion ( name , args ) ;
184
+
185
+ if ( typeof meth === "function" ) {
186
+ failed = ! meth ( fake ) ;
187
+ } else {
188
+ failed =
189
+ typeof fake [ meth ] === "function"
190
+ ? ! fake [ meth ] . apply ( fake , args )
191
+ : ! fake [ meth ] ;
192
+ }
193
+
194
+ if ( failed ) {
195
+ failAssertion (
196
+ this ,
197
+ ( fake . printf || fake . proxy . printf ) . apply (
198
+ fake ,
199
+ concat ( [ msg ] , args )
200
+ )
201
+ ) ;
202
+ } else {
203
+ assert . pass ( name ) ;
204
+ }
205
+ } ;
206
+ }
207
+
208
+ function exposedName ( prefix , prop ) {
209
+ return ! prefix || / ^ f a i l / . test ( prop )
210
+ ? prop
211
+ : prefix +
212
+ stringSlice ( prop , 0 , 1 ) . toUpperCase ( ) +
213
+ stringSlice ( prop , 1 ) ;
214
+ }
215
+
218
216
mirrorPropAsAssertion (
219
217
"called" ,
220
218
"expected %n to have been called at least once but was never called"
0 commit comments