11
11
'use strict' ;
12
12
13
13
var fs = require ( 'fs' ) ;
14
- var _ = require ( 'lodash' ) ;
15
- var pathExists = require ( 'path-exists' ) ;
14
+ var toArray = require ( 'lodash.toarray' ) ;
15
+ var pathExists = fs . existsSync ;
16
+
17
+ function isFunction ( obj ) {
18
+ return typeof obj === 'function' ;
19
+ }
16
20
17
21
function extractMethods ( methods ) {
18
- return _ . isArray ( methods ) ? methods : Object . keys ( methods ) . filter ( function ( method ) {
19
- return _ . isFunction ( methods [ method ] ) ;
20
- } ) ;
22
+ return Array . isArray ( methods ) ?
23
+ methods : Object . keys ( methods ) . filter ( function ( method ) {
24
+ return isFunction ( methods [ method ] ) ;
25
+ } ) ;
26
+ }
27
+
28
+ function convertArgs ( args ) {
29
+ if ( args . length > 1 ) {
30
+ return [ toArray ( args ) ] ;
31
+ }
32
+ var arg = args [ 0 ] ;
33
+ return Array . isArray ( arg ) ? arg : [ arg ] ;
34
+ }
35
+
36
+ function readFile ( filename , json ) {
37
+ var file = fs . readFileSync ( filename , 'utf8' ) ;
38
+ return json ? JSON . parse ( file ) : file ;
21
39
}
22
40
23
41
// Extend the native assert module
24
42
var assert = module . exports = require ( 'assert' ) ;
25
43
26
44
/**
27
45
* Assert that a file exists
28
- * @param {String } path - path to a file
46
+ * @param {String } path - path to a file
29
47
* @example
30
48
* assert.file('templates/user.hbs');
31
49
*
@@ -38,18 +56,15 @@ var assert = module.exports = require('assert');
38
56
*/
39
57
40
58
assert . file = function ( ) {
41
- var args = _ . toArray ( arguments ) ;
42
- args = _ . isString ( args [ 0 ] ) ? args : args [ 0 ] ;
43
-
44
- args . forEach ( function ( file ) {
45
- var here = pathExists . sync ( file ) ;
59
+ convertArgs ( arguments ) . forEach ( function ( file ) {
60
+ var here = pathExists ( file ) ;
46
61
assert . ok ( here , file + ', no such file or directory' ) ;
47
62
} ) ;
48
63
} ;
49
64
50
65
/**
51
66
* Assert that a file doesn't exist
52
- * @param {String } file - path to a file
67
+ * @param {String } file - path to a file
53
68
* @example
54
69
* assert.noFile('templates/user.hbs');
55
70
*
@@ -62,19 +77,16 @@ assert.file = function () {
62
77
*/
63
78
64
79
assert . noFile = function ( ) {
65
- var args = _ . toArray ( arguments ) ;
66
- args = _ . isString ( args [ 0 ] ) ? args : args [ 0 ] ;
67
-
68
- args . forEach ( function ( file ) {
69
- var here = pathExists . sync ( file ) ;
80
+ convertArgs ( arguments ) . forEach ( function ( file ) {
81
+ var here = pathExists ( file ) ;
70
82
assert . ok ( ! here , file + ' exists' ) ;
71
83
} ) ;
72
84
} ;
73
85
74
86
/**
75
87
* Assert that a file's content matches a regex or string
76
- * @param {String } file - path to a file
77
- * @param {Regex|String } reg - regex / string that will be used to search the file
88
+ * @param {String } file - path to a file
89
+ * @param {Regex|String } reg - regex / string that will be used to search the file
78
90
* @example
79
91
* assert.fileContent('models/user.js', /App\.User = DS\.Model\.extend/);
80
92
* assert.fileContent('models/user.js', 'App.User = DS.Model.extend');
@@ -92,14 +104,11 @@ assert.noFile = function () {
92
104
*/
93
105
94
106
assert . fileContent = function ( ) {
95
- var args = _ . toArray ( arguments ) ;
96
- var pairs = _ . isString ( args [ 0 ] ) ? [ args ] : args [ 0 ] ;
97
-
98
- pairs . forEach ( function ( pair ) {
107
+ convertArgs ( arguments ) . forEach ( function ( pair ) {
99
108
var file = pair [ 0 ] ;
100
109
var regex = pair [ 1 ] ;
101
110
assert . file ( file ) ;
102
- var body = fs . readFileSync ( file , 'utf8' ) ;
111
+ var body = readFile ( file ) ;
103
112
104
113
var match = false ;
105
114
if ( typeof regex === 'string' ) {
@@ -114,8 +123,8 @@ assert.fileContent = function () {
114
123
115
124
/**
116
125
* Assert that a file's content does not match a regex / string
117
- * @param {String } file - path to a file
118
- * @param {Regex|String } reg - regex / string that will be used to search the file
126
+ * @param {String } file - path to a file
127
+ * @param {Regex|String } reg - regex / string that will be used to search the file
119
128
* @example
120
129
* assert.noFileContent('models/user.js', /App\.User = DS\.Model\.extend/);
121
130
* assert.noFileContent('models/user.js', 'App.User = DS.Model.extend');
@@ -132,14 +141,11 @@ assert.fileContent = function () {
132
141
*/
133
142
134
143
assert . noFileContent = function ( ) {
135
- var args = _ . toArray ( arguments ) ;
136
- var pairs = _ . isString ( args [ 0 ] ) ? [ args ] : args [ 0 ] ;
137
-
138
- pairs . forEach ( function ( pair ) {
144
+ convertArgs ( arguments ) . forEach ( function ( pair ) {
139
145
var file = pair [ 0 ] ;
140
146
var regex = pair [ 1 ] ;
141
147
assert . file ( file ) ;
142
- var body = fs . readFileSync ( file , 'utf8' ) ;
148
+ var body = readFile ( file ) ;
143
149
144
150
if ( typeof regex === 'string' ) {
145
151
assert . ok ( body . indexOf ( regex ) === - 1 , file + ' matched \'' + regex + '\'.' ) ;
@@ -152,7 +158,7 @@ assert.noFileContent = function () {
152
158
153
159
/**
154
160
* Assert that two strings are equal after standardization of newlines
155
- * @param {String } value - a string
161
+ * @param {String } value - a string
156
162
* @param {String } expected - the expected value of the string
157
163
* @example
158
164
* assert.textEqual('I have a yellow cat', 'I have a yellow cat');
@@ -168,40 +174,36 @@ assert.textEqual = function (value, expected) {
168
174
169
175
/**
170
176
* Assert an Object implements an interface
171
- * @param {Object } subject - subject implementing the façade
172
- * @param {Object|Array } methods - a façace, hash or array of keys to be implemented
177
+ * @param {Object } subject - subject implementing the façade
178
+ * @param {Object|Array } methods - a façace, hash or array of keys to be implemented
173
179
*/
174
180
175
181
assert . implement = function ( subject , methods ) {
176
- methods = extractMethods ( methods ) ;
177
-
178
- var pass = methods . filter ( function ( method ) {
179
- return ! _ . isFunction ( subject [ method ] ) ;
182
+ var pass = extractMethods ( methods ) . filter ( function ( method ) {
183
+ return ! isFunction ( subject [ method ] ) ;
180
184
} ) ;
181
185
182
186
assert . ok ( pass . length === 0 , 'expected object to implement methods named: ' + pass . join ( ', ' ) ) ;
183
187
} ;
184
188
185
189
/**
186
190
* Assert an Object doesn't implements any method of an interface
187
- * @param {Object } subject - subject not implementing the methods
188
- * @param {Object|Array } methods - hash or array of method names to be implemented
191
+ * @param {Object } subject - subject not implementing the methods
192
+ * @param {Object|Array } methods - hash or array of method names to be implemented
189
193
*/
190
194
191
195
assert . notImplement = function ( subject , methods ) {
192
- methods = extractMethods ( methods ) ;
193
-
194
- var pass = methods . filter ( function ( method ) {
195
- return _ . isFunction ( subject [ method ] ) ;
196
+ var pass = extractMethods ( methods ) . filter ( function ( method ) {
197
+ return isFunction ( subject [ method ] ) ;
196
198
} ) ;
197
199
198
200
assert . ok ( pass . length === 0 , 'expected object to not implement any methods named: ' + pass . join ( ', ' ) ) ;
199
201
} ;
200
202
201
203
/**
202
204
* Assert an object contains the provided keys
203
- * @param {Object } obj Object that should match the given pattern
204
- * @param {Object } content An object of key/values the object should contains
205
+ * @param {Object } obj Object that should match the given pattern
206
+ * @param {Object } content An object of key/values the object should contains
205
207
*/
206
208
207
209
assert . objectContent = function ( obj , content ) {
@@ -239,8 +241,7 @@ assert.noObjectContent = function (obj, content) {
239
241
*/
240
242
241
243
assert . JSONFileContent = assert . jsonFileContent = function ( filename , content ) {
242
- var obj = JSON . parse ( fs . readFileSync ( filename , 'utf8' ) ) ;
243
- assert . objectContent ( obj , content ) ;
244
+ assert . objectContent ( readFile ( filename , true ) , content ) ;
244
245
} ;
245
246
246
247
/**
@@ -250,6 +251,5 @@ assert.JSONFileContent = assert.jsonFileContent = function (filename, content) {
250
251
*/
251
252
252
253
assert . noJSONFileContent = assert . noJsonFileContent = function ( filename , content ) {
253
- var obj = JSON . parse ( fs . readFileSync ( filename , 'utf8' ) ) ;
254
- assert . noObjectContent ( obj , content ) ;
254
+ assert . noObjectContent ( readFile ( filename , true ) , content ) ;
255
255
} ;
0 commit comments