Skip to content

Commit 1de4619

Browse files
committed
feat: notNull
1 parent 52b6690 commit 1de4619

File tree

6 files changed

+110
-23
lines changed

6 files changed

+110
-23
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pactum-matchers",
3-
"version": "1.1.1",
3+
"version": "1.1.2",
44
"description": "collection of json matchers for contract testing",
55
"main": "./src/index.js",
66
"types": "./src/index.d.ts",
@@ -14,7 +14,7 @@
1414
},
1515
"repository": {
1616
"type": "git",
17-
"url": "git+https://github.com/ASaiAnudeep/pactum-matchers.git"
17+
"url": "git+https://github.com/pactumjs/pactum-matchers.git"
1818
},
1919
"keywords": [
2020
"pactum",
@@ -25,9 +25,9 @@
2525
"author": "Anudeep <[email protected]>",
2626
"license": "MIT",
2727
"bugs": {
28-
"url": "https://github.com/ASaiAnudeep/pactum-matchers/issues"
28+
"url": "https://github.com/pactumjs/pactum-matchers/issues"
2929
},
30-
"homepage": "https://github.com/ASaiAnudeep/pactum-matchers#readme",
30+
"homepage": "https://github.com/pactumjs/pactum-matchers#readme",
3131
"devDependencies": {
3232
"c8": "^7.7.1",
3333
"uvu": "^0.5.1"

src/compare.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ function compareWithRule(actual, expected, rules, regex_rules, path, rule) {
121121
case 'not_includes':
122122
compareNotIncludes(actual, expected, rule, path);
123123
break;
124+
case 'not_null':
125+
compareWithNotNull(actual, path);
126+
break;
124127
}
125128
}
126129

@@ -284,6 +287,12 @@ function compareNotIncludes(actual, expected_values, rule, path) {
284287
}
285288
}
286289

290+
function compareWithNotNull(actual, path) {
291+
if (actual === null) {
292+
throw `Json has a "null" at "${path}"`;
293+
}
294+
}
295+
287296
function typeCompare(actual, expected, path) {
288297
const actualType = getType(actual);
289298
const expectedType = getType(expected);

src/index.d.ts

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export function expression(expr: string): object;
2929
/**
3030
* non empty string matching
3131
*/
32-
export function string(value?: string): object;
32+
export function string(value?: string): object;
3333

3434
/**
3535
* regex matching
@@ -65,40 +65,45 @@ export function dateTime(value?: string): object;
6565
/**
6666
* Any type matching
6767
*/
68-
export function any(value?: any): object;
68+
export function any(value?: any): object;
6969

7070
/**
7171
* Int matching
7272
*/
73-
export function int(value?: number): object;
73+
export function int(value?: number): object;
7474

7575
/**
7676
* Float matching
7777
*/
78-
export function float(value?: number): object;
78+
export function float(value?: number): object;
7979

8080
/**
8181
* Greater than given number matching
8282
*/
83-
export function gt(value?: number): object;
83+
export function gt(value?: number): object;
8484

85-
/**
86-
* Greater than or equal to given number matching
87-
*/
88-
export function gte(value?: number): object;
85+
/**
86+
* Greater than or equal to given number matching
87+
*/
88+
export function gte(value?: number): object;
8989

90-
/**
91-
* Lesser than given number matching
92-
*/
93-
export function lt(value?: number): object;
90+
/**
91+
* Lesser than given number matching
92+
*/
93+
export function lt(value?: number): object;
9494

95-
/**
96-
* Lesser than or equal to given number matching
97-
*/
98-
export function lte(value?: number): object;
95+
/**
96+
* Lesser than or equal to given number matching
97+
*/
98+
export function lte(value?: number): object;
9999

100100
/**
101101
* not have property in object
102102
*/
103103
export function notIncludes(value: string | number | boolean): object;
104-
export function notIncludes(values: string[] | number[] | boolean[]): object;
104+
export function notIncludes(values: string[] | number[] | boolean[]): object;
105+
106+
/**
107+
* not null
108+
*/
109+
export function notNull(value: string | number | boolean): object;

src/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,13 @@ function notIncludes(value) {
174174
};
175175
}
176176

177+
function notNull(value) {
178+
return {
179+
value: value,
180+
pactum_type: 'NOT_NULL'
181+
};
182+
}
183+
177184
module.exports = {
178185
like,
179186
eachLike,
@@ -196,5 +203,6 @@ module.exports = {
196203
lt,
197204
lte,
198205
notIncludes,
206+
notNull,
199207
utils
200208
};

test/rules.notNull.test.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
const suite = require('uvu').suite;
2+
const assert = require('assert');
3+
const { notNull, utils } = require('../src/index');
4+
const { setMatchingRules, getValue, compare } = utils;
5+
6+
const test = suite('SetMatchingRules - notNull');
7+
8+
test('notNull - default value', () => {
9+
const actual = 'some';
10+
const value = notNull();
11+
assert.deepStrictEqual(value, {
12+
pactum_type: 'NOT_NULL',
13+
value: undefined
14+
});
15+
const rules = setMatchingRules({}, value, '$.body');
16+
assert.deepStrictEqual(rules, {
17+
'$.body': {
18+
match: 'not_null'
19+
}
20+
});
21+
const expected = getValue(value);
22+
const { equal, message } = compare(actual, expected, rules, '$.body');
23+
assert.strictEqual(equal, true);
24+
assert.strictEqual(message, '');
25+
});
26+
27+
test('notNull - custom value', () => {
28+
const actual = 'some';
29+
const value = notNull('any');
30+
assert.deepStrictEqual(value, {
31+
pactum_type: 'NOT_NULL',
32+
value: 'any'
33+
});
34+
const rules = setMatchingRules({}, value, '$.body');
35+
assert.deepStrictEqual(rules, {
36+
'$.body': {
37+
match: 'not_null'
38+
}
39+
});
40+
const expected = getValue(value);
41+
const { equal, message } = compare(actual, expected, rules, '$.body');
42+
assert.strictEqual(equal, true);
43+
assert.strictEqual(message, '');
44+
});
45+
46+
test('notNull - comparison fails', () => {
47+
const actual = null;
48+
const value = notNull('any');
49+
assert.deepStrictEqual(value, {
50+
pactum_type: 'NOT_NULL',
51+
value: 'any'
52+
});
53+
const rules = setMatchingRules({}, value, '$.body');
54+
assert.deepStrictEqual(rules, {
55+
'$.body': {
56+
match: 'not_null'
57+
}
58+
});
59+
const expected = getValue(value);
60+
const { equal, message } = compare(actual, expected, rules, '$.body');
61+
assert.strictEqual(equal, false);
62+
assert.strictEqual(message, 'Json has a "null" at "$.body"');
63+
});
64+
65+
test.run();

0 commit comments

Comments
 (0)