Skip to content

Commit 4db399e

Browse files
fernandopasikSBoudrias
authored andcommitted
Add assert.noObjectContent() and assert.noJsonFileContent() (#16)
* Add assert.noObjectContent() and assert.noJsonFileContent() Fix #15 * Add assert.noObjectContent() and assert.noJsonFileContent() to README.md Fix #15
1 parent aa6a727 commit 4db399e

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,16 @@ var anObject = {a: 1};
155155
assert.objectContent(anObject, {a: 2});
156156
```
157157

158+
### `assert.noObjectContent()`
159+
160+
Assert an object does not contain at least a set of keys
161+
162+
```js
163+
var anObject = {a: 1};
164+
165+
assert.noObjectContent(anObject, {a: 1});
166+
```
167+
158168
### `assert.jsonFileContent()`
159169

160170
Assert a JSON file contains at least a set of keys (rely of `assert.objectContent()`)
@@ -163,6 +173,14 @@ Assert a JSON file contains at least a set of keys (rely of `assert.objectConten
163173
assert.jsonFileContent('path/to/file.json', {a: 2});
164174
```
165175

176+
### `assert.noJsonFileContent()`
177+
178+
Assert a JSON file does not contain at least a set of keys (rely of `assert.noObjectContent()`)
179+
180+
```js
181+
assert.noJsonFileContent('path/to/file.json', {a: 1});
182+
```
183+
166184
## Contribute
167185

168186
See the [contributing docs](http://yeoman.io/contributing/).

index.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,23 @@ assert.objectContent = function (obj, content) {
215215
});
216216
};
217217

218+
/**
219+
* Assert an object does not contain the provided keys
220+
* @param {Object} obj Object that should not match the given pattern
221+
* @param {Object} content An object of key/values the object should not contain
222+
*/
223+
224+
assert.noObjectContent = function (obj, content) {
225+
Object.keys(content).forEach(function (key) {
226+
if (typeof content[key] === 'object') {
227+
assert.noObjectContent(obj[key], content[key]);
228+
return;
229+
}
230+
231+
assert.notEqual(obj[key], content[key]);
232+
});
233+
};
234+
218235
/**
219236
* Assert a JSON file contains the provided keys
220237
* @param {String} filename
@@ -225,3 +242,14 @@ assert.JSONFileContent = assert.jsonFileContent = function (filename, content) {
225242
var obj = JSON.parse(fs.readFileSync(filename, 'utf8'));
226243
assert.objectContent(obj, content);
227244
};
245+
246+
/**
247+
* Assert a JSON file does not contain the provided keys
248+
* @param {String} filename
249+
* @param {Object} content An object of key/values the file should not contain
250+
*/
251+
252+
assert.JSONFileContent = assert.noJsonFileContent = function (filename, content) {
253+
var obj = JSON.parse(fs.readFileSync(filename, 'utf8'));
254+
assert.noObjectContent(obj, content);
255+
};

test.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,49 @@ describe('yeoman-assert', function () {
208208
});
209209
});
210210

211+
describe('.noObjectContent()', function () {
212+
it('fails if object contains the keys', function () {
213+
assert.throws(yoAssert.noObjectContent.bind(yoAssert, {
214+
a: 'foo'
215+
}, {
216+
a: 'foo'
217+
}));
218+
});
219+
220+
it('pass if object contains nested objects and arrays', function () {
221+
assert.throws(yoAssert.noObjectContent.bind(yoAssert, {
222+
a: {b: 'foo'},
223+
b: [0, 'a'],
224+
c: 'a'
225+
}, {
226+
a: {b: 'foo'},
227+
b: [0, 'a']
228+
}));
229+
});
230+
231+
it('pass if array is incomplete', function () {
232+
assert.throws(yoAssert.noObjectContent.bind(yoAssert, {
233+
b: [0, 'a']
234+
}, {
235+
b: [0]
236+
}));
237+
});
238+
239+
it('pass if object does not contain a key', function () {
240+
assert.doesNotThrow(yoAssert.noObjectContent.bind(yoAssert, {}, {
241+
a: 'foo'
242+
}));
243+
});
244+
245+
it('pass if nested object does not contain a key', function () {
246+
assert.doesNotThrow(yoAssert.noObjectContent.bind(yoAssert, {
247+
a: {}
248+
}, {
249+
a: {b: 'foo'}
250+
}));
251+
});
252+
});
253+
211254
describe('.jsonFileContent()', function () {
212255
var file = path.join(__dirname, 'fixtures/dummy.json');
213256

@@ -234,4 +277,31 @@ describe('yeoman-assert', function () {
234277
assert.throws(yoAssert.jsonFileContent.bind(yoAssert, 'does-not-exist', {}));
235278
});
236279
});
280+
281+
describe('.noJsonFileContent()', function () {
282+
var file = path.join(__dirname, 'fixtures/dummy.json');
283+
284+
it('fails if file contains the keys', function () {
285+
assert.throws(yoAssert.noJsonFileContent.bind(yoAssert, file, {
286+
a: {b: 1},
287+
b: [1, 2]
288+
}));
289+
});
290+
291+
it('pass if file does not contain the keys', function () {
292+
assert.doesNotThrow(yoAssert.noJsonFileContent.bind(yoAssert, file, {
293+
c: {b: 1},
294+
b: 'a'
295+
}));
296+
297+
assert.doesNotThrow(yoAssert.noJsonFileContent.bind(yoAssert, file, {
298+
a: {b: 3},
299+
b: [2]
300+
}));
301+
});
302+
303+
it('fails if file does not exists', function () {
304+
assert.throws(yoAssert.noJsonFileContent.bind(yoAssert, 'does-not-exist', {}));
305+
});
306+
});
237307
});

0 commit comments

Comments
 (0)