Skip to content

Commit 27b9f84

Browse files
committed
Moved negative functions into the 'not' namespaces and added a function.
1 parent fb846fe commit 27b9f84

File tree

2 files changed

+137
-78
lines changed

2 files changed

+137
-78
lines changed

README.md

Lines changed: 51 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -334,10 +334,10 @@ Expects HTTP response to return a single JSON element.
334334
Expects HTTP response to return a JSON collection.
335335
* [`utils.expect.response.empty`](#utilsexpectresponseempty):
336336
Expects HTTP response to return an empty JSON collection.
337-
* [`utils.expect.response.nonempty`](#utilsexpectresponsenonempty):
338-
Expects HTTP response to return a non-empty JSON collection.
339337
* [`utils.expect.response.unique`](#utilsexpectresponseunique):
340338
Expects HTTP response to return a JSON collection with a single element.
339+
* [`utils.expect.response.not.empty`](#utilsexpectresponsenotempty):
340+
Expects HTTP response to return a non-empty JSON collection.
341341

342342
### `utils.expect.response.text`
343343
Expects HTTP response to return a simple (not the JSON) data type, such as string.
@@ -385,7 +385,7 @@ utils.expect.response.one(pm);
385385
Expects HTTP response to return a JSON collection (can be empty or contain the specified minimum and/or maximum number of items). The `utils.expect.response.many` function can be called via one of these shortcuts:
386386

387387
* [`utils.expect.response.empty`](#utilsexpectresponseempty) to check for an empty collection
388-
* [`utils.expect.response.nonempty`](#utilsexpectresponsenonempty) to check for a non-empty collection
388+
* [`utils.expect.response.not.empty`](#utilsexpectresponsenotempty) to check for a non-empty collection
389389
* [`utils.expect.response.unique`](#utilsexpectresponseunique) to check for a collection with a single (unique) item
390390

391391
#### Prototype
@@ -419,45 +419,45 @@ Check if the HTTP response data contains an empty JSON collection (but not `null
419419
utils.expect.response.empty(pm);
420420
```
421421

422-
### `utils.expect.response.nonempty`
423-
Expects HTTP response to return a non-empty JSON collection (one or more items).
422+
### `utils.expect.response.unique`
423+
Expects HTTP response to return a JSON collection with a single element (but not a single element matching the condition of the utils.expect.response.one function).
424424

425425
#### Prototype
426426
```JavaScript
427-
utils.expect.response.nonempty(pm)
427+
utils.expect.response.unique(pm)
428428
```
429429

430430
#### Example
431-
Check if the HTTP response data contains a JSON collection with at least one item.
431+
Check if the HTTP response contains a JSON collection with a single element.
432+
432433
```JavaScript
433-
utils.expect.response.nonempty(pm);
434+
utils.expect.response.unique(pm);
434435
```
435436

436-
### `utils.expect.response.unique`
437-
Expects HTTP response to return a JSON collection with a single element (but not a single element matching the condition of the utils.expect.response.one function).
437+
### `utils.expect.response.not.empty`
438+
Expects HTTP response to return a non-empty JSON collection (one or more items).
438439

439440
#### Prototype
440441
```JavaScript
441-
utils.expect.response.unique(pm)
442+
utils.expect.response.not.empty(pm)
442443
```
443444

444445
#### Example
445-
Check if the HTTP response contains a JSON collection with a single element.
446-
446+
Check if the HTTP response data contains a JSON collection with at least one item.
447447
```JavaScript
448-
utils.expect.response.unique(pm);
448+
utils.expect.response.not.empty(pm);
449449
```
450450

451451
## Property validation functions
452452
Property validation functions check named properties of the specified objects. The primary benefits of these functions (compared to the underlying [Chai assertions](https://www.chaijs.com/api/bdd/) they use) is that they (a) always check to make sure that the properties exist before additional validation (so you can skip one test step) and (b) generate more complete error messages on assertion failures (the default assertion errors do not mention named of the properties being checked, which makes them not that useful). Property validation functions are grouped under the `utils.expect.property` namespace and include:
453453

454454
* [`utils.expect.property.exist`](#utilsexpectpropertyexist):
455455
Expects the specified object to have a property with the given name.
456-
* [`utils.expect.property.notexist`](#utilsexpectpropertynotexist):
457-
Expects the specified object to not have a property with the given name.
458456
* [`utils.expect.property.equal`](#utilsexpectpropertyequal):
459457
Expects a named property of the specified object to be equal to the specific value.
460-
* [`utils.expect.property.notequal`](#utilsexpectpropertynotequal):
458+
* [`utils.expect.property.not.exist`](#utilsexpectpropertynotexist):
459+
Expects the specified object to not have a property with the given name.
460+
* [`utils.expect.property.not.equal`](#utilsexpectpropertynotequal):
461461
Expects a named property of the specified object to not be equal to the specific value.
462462

463463
### Parameters
@@ -484,43 +484,43 @@ var response = pm.response.json();
484484
utils.expect.property.exist(pm, response, "id");
485485
```
486486

487-
### `utils.expect.property.notexist`
488-
Expects the specified object to not have a property with the given name.
487+
### `utils.expect.property.equal`
488+
Expects a named property of the specified object to be equal to the specific value (can be any simple data type, such as boolean, integer, etc.).
489489

490490
#### Prototype
491491
```JavaScript
492-
utils.expect.property.notexist(pm, data, name)
492+
utils.expect.property.equal(pm, data, name, value)
493493
```
494494

495+
#### Parameters
496+
* `value`:
497+
Expected property value held by the specified object property (can be any simple data type including `null`).
498+
495499
#### Example
496-
Check if the JSON object returned in the HTTP response does not contain a property `id`.
500+
Check if the JSON object returned in the HTTP response contains a property `active` holding the boolean value of `true`.
497501

498502
```JavaScript
499503
var response = pm.response.json();
500-
utils.expect.property.notexist(pm, response, "id");
504+
utils.expect.property.equal(pm, response, "active", true);
501505
```
502506

503-
### `utils.expect.property.equal`
504-
Expects a named property of the specified object to be equal to the specific value (can be any simple data type, such as boolean, integer, etc.).
507+
### `utils.expect.property.not.exist`
508+
Expects the specified object to not have a property with the given name.
505509

506510
#### Prototype
507511
```JavaScript
508-
utils.expect.property.equal(pm, data, name, value)
512+
utils.expect.property.not.exist(pm, data, name)
509513
```
510514

511-
#### Parameters
512-
* `value`:
513-
Expected property value held by the specified object property (can be any simple data type including `null`).
514-
515515
#### Example
516-
Check if the JSON object returned in the HTTP response contains a property `active` holding the boolean value of `true`.
516+
Check if the JSON object returned in the HTTP response does not contain a property `id`.
517517

518518
```JavaScript
519519
var response = pm.response.json();
520-
utils.expect.property.equal(pm, response, "active", true);
520+
utils.expect.property.not.exist(pm, response, "id");
521521
```
522522

523-
### `utils.expect.property.notequal`
523+
### `utils.expect.property.not.equal`
524524
Expects a named property of the specified object to not be equal to the specific value (can be any simple data type, such as boolean, integer, etc.).
525525

526526
#### Parameters
@@ -532,7 +532,7 @@ Check if the JSON object returned in the HTTP response contains a property `acti
532532

533533
```JavaScript
534534
var response = pm.response.json();
535-
utils.expect.property.notequal(pm, response, "active", true);
535+
utils.expect.property.not.equal(pm, response, "active", true);
536536
```
537537

538538
## String validation functions
@@ -548,6 +548,8 @@ Expects the named object property to start with the specified string value.
548548
Expects the named object property to end with the specified string value.
549549
* [`utils.expect.property.string.match`](#utilsexpectpropertystringmatch):
550550
Expects the named object property to match the specified regular expression.
551+
* [`utils.expect.property.string.not.match`](#utilsexpectpropertystringnotmatch):
552+
Expects the named object property to not match the specified regular expression.
551553

552554
### Parameters
553555

@@ -650,6 +652,22 @@ var response = pm.response.json();
650652
utils.expect.property.string.match(pm, response, "name", /^John$/);
651653
```
652654

655+
### `utils.expect.property.string.not.match`
656+
Expects the named object property to not match the specified regular expression.
657+
658+
#### Prototype
659+
```JavaScript
660+
utils.expect.property.string.not.match(pm, data, name, value)
661+
```
662+
663+
#### Example
664+
Check if the JSON object returned in the HTTP response contains the string property `name` holding the value that doe not match a regular expression `/^John$/`.
665+
666+
```JavaScript
667+
var response = pm.response.json();
668+
utils.expect.property.string.not.match(pm, response, "name", /^John$/);
669+
```
670+
653671
## Trace functions
654672
Trace functions print trace messages that can indicate the start and end of pre-request and test script execution. You can also customize trace function to print your custom trace messages.
655673

utils.js

Lines changed: 86 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@
101101

102102
utils = {
103103
// Version of this library.
104-
version: "1.0.1",
104+
version: "1.0.2",
105105

106106
// Default name of the problem details object property returning
107107
// application specific error code.
@@ -608,18 +608,21 @@ utils = {
608608
utils.expect.response.many(pm, 0, 0);
609609
},
610610

611-
// DESCRITION
612-
// Expects response to contain an non-empty array.
613-
nonempty: function(pm) {
614-
utils.expect.response.many(pm, 1, -1);
615-
},
616-
617611
// DESCRITION
618612
// Expects response to contain an array holding a single item.
619613
unique: function(pm) {
620614
utils.expect.response.many(pm, 1, 1);
621-
}
615+
},
622616

617+
// DESCRIPTION
618+
// Negative functions checking response.
619+
not: {
620+
// DESCRITION
621+
// Expects response to contain an non-empty array.
622+
empty: function(pm) {
623+
utils.expect.response.many(pm, 1, -1);
624+
}
625+
}
623626
// End of 'utils.expect.response' functions.
624627
},
625628

@@ -643,20 +646,6 @@ utils = {
643646
pm.expect(data).to.have.property(name);
644647
},
645648

646-
// DESCRIPTION
647-
// Expects object to not have a named property holding any value
648-
// including null.
649-
//
650-
// PARAMETERS
651-
// - data (object)
652-
// Data object which property is being checked.
653-
//
654-
// - name (string)
655-
// Property name.
656-
notexist: function(pm, data, name) {
657-
pm.expect(data).to.not.have.property(name);
658-
},
659-
660649
// DESCRIPTION
661650
// Expects object property to exist and equal the specified value.
662651
//
@@ -687,30 +676,48 @@ utils = {
687676
},
688677

689678
// DESCRIPTION
690-
// Expects object property to exist and equal the specified value.
691-
//
692-
// PARAMETERS
693-
// - data
694-
// Same as in 'utils.expect.property.exist'.
695-
//
696-
// - name
697-
// Same as in 'utils.expect.property.exist'.
698-
//
699-
// - value (object)
700-
// Expected value (can be null).
701-
notequal: function(pm, data, name, value) {
702-
utils.expect.property.exist(pm, data, name);
679+
// Negative property check functions.
680+
not: {
681+
// DESCRIPTION
682+
// Expects object to not have a named property holding any value
683+
// including null.
684+
//
685+
// PARAMETERS
686+
// - data (object)
687+
// Data object which property is being checked.
688+
//
689+
// - name (string)
690+
// Property name.
691+
exist: function(pm, data, name) {
692+
pm.expect(data).to.not.have.property(name);
693+
},
703694

704-
if (value !== undefined) {
705-
if (value === null) {
706-
pm.expect(data[name]).to.not.be.null;
707-
} else {
708-
var msg = "Expected '" + name +
709-
"' property to not equal '" + value +
710-
"' but got '" + data[name] +
711-
"'";
695+
// DESCRIPTION
696+
// Expects object property to exist and equal the specified value.
697+
//
698+
// PARAMETERS
699+
// - data
700+
// Same as in 'utils.expect.property.exist'.
701+
//
702+
// - name
703+
// Same as in 'utils.expect.property.exist'.
704+
//
705+
// - value (object)
706+
// Expected value (can be null).
707+
equal: function(pm, data, name, value) {
708+
utils.expect.property.exist(pm, data, name);
712709

713-
pm.expect(data[name]).to.not.equal(value, msg);
710+
if (value !== undefined) {
711+
if (value === null) {
712+
pm.expect(data[name]).to.not.be.null;
713+
} else {
714+
var msg = "Expected '" + name +
715+
"' property to not equal '" + value +
716+
"' but got '" + data[name] +
717+
"'";
718+
719+
pm.expect(data[name]).to.not.equal(value, msg);
720+
}
714721
}
715722
}
716723
},
@@ -888,13 +895,47 @@ utils = {
888895
pm.expect(data[name]).to.be.null;
889896
} else {
890897
var msg = "Expected '" + name +
891-
"' property to match regex '" + value +
898+
"' property to match regular expression '" + value +
892899
"' but got '" + data[name] +
893900
"'";
894901

895902
pm.expect(data[name]).to.match(value, msg);
896903
}
897904
}
905+
},
906+
907+
// DESCRIPTION
908+
// Negative string property check functions.
909+
not: {
910+
// DESCRIPTION
911+
// Expects the string object property to exist and
912+
// not match the specified regular expression.
913+
//
914+
// PARAMETERS
915+
// - data
916+
// Same as in 'utils.expect.property.exist'.
917+
//
918+
// - name
919+
// Same as in 'utils.expect.property.exist'.
920+
//
921+
// - value (regular expression)
922+
// Regular expression (can be null).
923+
match: function(pm, data, name, value) {
924+
utils.expect.property.exist(pm, data, name);
925+
926+
if (value !== undefined) {
927+
if (value === null) {
928+
pm.expect(data[name]).to.not.be.null;
929+
} else {
930+
var msg = "Expected '" + name +
931+
"' property to not match regular expression '" + value +
932+
"' but got '" + data[name] +
933+
"'";
934+
935+
pm.expect(data[name]).to.not.match(value, msg);
936+
}
937+
}
938+
}
898939
}
899940

900941
// End of 'utils.expect.property.string' functions.

0 commit comments

Comments
 (0)