@nestia/e2e 0.1.6 → 0.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/TestValidator.d.ts +19 -7
- package/lib/TestValidator.js +81 -29
- package/lib/TestValidator.js.map +1 -1
- package/package.json +9 -2
- package/src/TestValidator.ts +83 -17
package/lib/TestValidator.d.ts
CHANGED
|
@@ -7,22 +7,34 @@
|
|
|
7
7
|
*/
|
|
8
8
|
export declare namespace TestValidator {
|
|
9
9
|
/**
|
|
10
|
-
* Test whether
|
|
11
|
-
*
|
|
12
|
-
* If error occurs, nothing would be happened.
|
|
13
|
-
*
|
|
14
|
-
* However, no error exists, then exception would be thrown.
|
|
10
|
+
* Test whether condition is satisfied.
|
|
15
11
|
*
|
|
16
|
-
* @param title Title of
|
|
12
|
+
* @param title Title of error message when condition is not satisfied
|
|
13
|
+
* @return Currying function
|
|
17
14
|
*/
|
|
18
|
-
const
|
|
15
|
+
const predicate: (title: string) => <T extends boolean | (() => boolean) | (() => Promise<boolean>)>(condition: T) => T extends () => Promise<boolean> ? Promise<void> : void;
|
|
19
16
|
/**
|
|
20
17
|
* Test whether two values are equal.
|
|
21
18
|
*
|
|
19
|
+
* If you want to validate `covers` relationship,
|
|
20
|
+
* call smaller first and then larger.
|
|
21
|
+
*
|
|
22
|
+
* Otherwise you wanna non equals validator, combine with {@link error}.
|
|
23
|
+
*
|
|
22
24
|
* @param title Title of error message when different
|
|
23
25
|
* @returns Currying function
|
|
24
26
|
*/
|
|
25
27
|
const equals: (title: string) => <T>(x: T) => (y: T) => void;
|
|
28
|
+
/**
|
|
29
|
+
* Test whether error occurs.
|
|
30
|
+
*
|
|
31
|
+
* If error occurs, nothing would be happened.
|
|
32
|
+
*
|
|
33
|
+
* However, no error exists, then exception would be thrown.
|
|
34
|
+
*
|
|
35
|
+
* @param title Title of exception because of no error exists
|
|
36
|
+
*/
|
|
37
|
+
const error: (title: string) => <T>(task: () => T) => T extends Promise<any> ? Promise<void> : void;
|
|
26
38
|
/**
|
|
27
39
|
* Validate index API.
|
|
28
40
|
*
|
package/lib/TestValidator.js
CHANGED
|
@@ -49,8 +49,8 @@ var __values = (this && this.__values) || function(o) {
|
|
|
49
49
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
50
50
|
exports.TestValidator = void 0;
|
|
51
51
|
var ranges_1 = require("tstl/ranges");
|
|
52
|
-
var json_equal_to_1 = require("./internal/json_equal_to");
|
|
53
52
|
var RandomGenerator_1 = require("./RandomGenerator");
|
|
53
|
+
var json_equal_to_1 = require("./internal/json_equal_to");
|
|
54
54
|
/**
|
|
55
55
|
* Test validator.
|
|
56
56
|
*
|
|
@@ -62,44 +62,90 @@ var TestValidator;
|
|
|
62
62
|
(function (TestValidator) {
|
|
63
63
|
var _this = this;
|
|
64
64
|
/**
|
|
65
|
-
* Test whether
|
|
66
|
-
*
|
|
67
|
-
* If error occurs, nothing would be happened.
|
|
65
|
+
* Test whether condition is satisfied.
|
|
68
66
|
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
* @param title Title of exception because of no error exists
|
|
67
|
+
* @param title Title of error message when condition is not satisfied
|
|
68
|
+
* @return Currying function
|
|
72
69
|
*/
|
|
73
|
-
TestValidator.
|
|
74
|
-
return function (
|
|
75
|
-
var
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
70
|
+
TestValidator.predicate = function (title) {
|
|
71
|
+
return function (condition) {
|
|
72
|
+
var message = function () {
|
|
73
|
+
return "Bug on ".concat(title, ": expected condition is not satisfied.");
|
|
74
|
+
};
|
|
75
|
+
// SCALAR
|
|
76
|
+
if (typeof condition === "boolean") {
|
|
77
|
+
if (condition !== true)
|
|
78
|
+
throw new Error(message());
|
|
79
|
+
return undefined;
|
|
80
|
+
}
|
|
81
|
+
// CLOSURE
|
|
82
|
+
var output = condition();
|
|
83
|
+
if (typeof output === "boolean") {
|
|
84
|
+
if (output !== true)
|
|
85
|
+
throw new Error(message());
|
|
86
|
+
return undefined;
|
|
87
|
+
}
|
|
88
|
+
// ASYNCHRONOUS
|
|
89
|
+
return new Promise(function (resolve, reject) {
|
|
90
|
+
output
|
|
91
|
+
.then(function (flag) {
|
|
92
|
+
if (flag === true)
|
|
93
|
+
resolve();
|
|
94
|
+
else
|
|
95
|
+
reject(message());
|
|
96
|
+
})
|
|
97
|
+
.catch(reject);
|
|
89
98
|
});
|
|
90
|
-
}
|
|
99
|
+
};
|
|
91
100
|
};
|
|
92
101
|
/**
|
|
93
102
|
* Test whether two values are equal.
|
|
94
103
|
*
|
|
104
|
+
* If you want to validate `covers` relationship,
|
|
105
|
+
* call smaller first and then larger.
|
|
106
|
+
*
|
|
107
|
+
* Otherwise you wanna non equals validator, combine with {@link error}.
|
|
108
|
+
*
|
|
95
109
|
* @param title Title of error message when different
|
|
96
110
|
* @returns Currying function
|
|
97
111
|
*/
|
|
98
|
-
TestValidator.equals = function (title) {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
112
|
+
TestValidator.equals = function (title) {
|
|
113
|
+
return function (x) {
|
|
114
|
+
return function (y) {
|
|
115
|
+
var diff = (0, json_equal_to_1.json_equal_to)(x, y);
|
|
116
|
+
if (diff.length)
|
|
117
|
+
throw new Error("Bug on ".concat(title, ": found different values - [").concat(diff.join(", "), "]"));
|
|
118
|
+
};
|
|
119
|
+
};
|
|
120
|
+
};
|
|
121
|
+
/**
|
|
122
|
+
* Test whether error occurs.
|
|
123
|
+
*
|
|
124
|
+
* If error occurs, nothing would be happened.
|
|
125
|
+
*
|
|
126
|
+
* However, no error exists, then exception would be thrown.
|
|
127
|
+
*
|
|
128
|
+
* @param title Title of exception because of no error exists
|
|
129
|
+
*/
|
|
130
|
+
TestValidator.error = function (title) {
|
|
131
|
+
return function (task) {
|
|
132
|
+
var message = function () { return "Bug on ".concat(title, ": exception must be thrown."); };
|
|
133
|
+
try {
|
|
134
|
+
var output_1 = task();
|
|
135
|
+
if (is_promise(output_1))
|
|
136
|
+
return new Promise(function (resolve, reject) {
|
|
137
|
+
return output_1
|
|
138
|
+
.catch(function () { return resolve(); })
|
|
139
|
+
.then(function () { return reject(message()); });
|
|
140
|
+
});
|
|
141
|
+
else
|
|
142
|
+
throw new Error(message());
|
|
143
|
+
}
|
|
144
|
+
catch (_a) {
|
|
145
|
+
return undefined;
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
};
|
|
103
149
|
/**
|
|
104
150
|
* Validate index API.
|
|
105
151
|
*
|
|
@@ -282,4 +328,10 @@ var TestValidator;
|
|
|
282
328
|
function get_ids(entities) {
|
|
283
329
|
return entities.map(function (entity) { return entity.id; }).sort(function (x, y) { return (x < y ? -1 : 1); });
|
|
284
330
|
}
|
|
331
|
+
function is_promise(input) {
|
|
332
|
+
return (typeof input === "object" &&
|
|
333
|
+
input !== null &&
|
|
334
|
+
typeof input.then === "function" &&
|
|
335
|
+
typeof input.catch === "function");
|
|
336
|
+
}
|
|
285
337
|
//# sourceMappingURL=TestValidator.js.map
|
package/lib/TestValidator.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TestValidator.js","sourceRoot":"","sources":["../src/TestValidator.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,sCAAwC;
|
|
1
|
+
{"version":3,"file":"TestValidator.js","sourceRoot":"","sources":["../src/TestValidator.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,sCAAwC;AAExC,qDAAoD;AACpD,0DAAyD;AAEzD;;;;;;GAMG;AACH,IAAiB,aAAa,CA4P7B;AA5PD,WAAiB,aAAa;;IAC1B;;;;;OAKG;IACU,uBAAS,GA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O,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,UAAC,IAAI,IAAK,OAAC,IAAY,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,EAAxB,CAAwB,CAAC,CAAC,CAAC;4CAC9D,MAAM,IAAI,KAAK,CACX,iBAAU,KAAK,gCAAsB,SAAS,cAAI,MAAM,CAAC,IAAI,CACzD,IAAI,CACP,OAAI,CACR,CAAC;yCACL;;;;;qBACJ;gBAxBD,CAwBC;YA5BD,CA4BC;QAjCD,CAiCC;IA3CD,CA2CC,CAAC;AAKV,CAAC,EA5PgB,aAAa,GAAb,qBAAa,KAAb,qBAAa,QA4P7B;AAMD,SAAS,OAAO,CAA8B,QAAkB;IAC5D,OAAO,QAAQ,CAAC,GAAG,CAAC,UAAC,MAAM,IAAK,OAAA,MAAM,CAAC,EAAE,EAAT,CAAS,CAAC,CAAC,IAAI,CAAC,UAAC,CAAC,EAAE,CAAC,IAAK,OAAA,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAhB,CAAgB,CAAC,CAAC;AAChF,CAAC;AAED,SAAS,UAAU,CAAC,KAAU;IAC1B,OAAO,CACH,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACd,OAAQ,KAAa,CAAC,IAAI,KAAK,UAAU;QACzC,OAAQ,KAAa,CAAC,KAAK,KAAK,UAAU,CAC7C,CAAC;AACN,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nestia/e2e",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.8",
|
|
4
4
|
"description": "E2E test utilify functions",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"build": "rimraf lib && tsc",
|
|
8
8
|
"dev": "npm run build -- --watch",
|
|
9
|
+
"eslint": "eslint src",
|
|
9
10
|
"prettier": "prettier --write ./src/**/*.ts",
|
|
11
|
+
"prepare": "ts-patch install",
|
|
10
12
|
"test": "node lib/test"
|
|
11
13
|
},
|
|
12
14
|
"repository": {
|
|
@@ -31,9 +33,14 @@
|
|
|
31
33
|
"@trivago/prettier-plugin-sort-imports": "^4.0.0",
|
|
32
34
|
"@types/cli": "^0.11.21",
|
|
33
35
|
"@types/node": "^18.11.18",
|
|
36
|
+
"@typescript-eslint/eslint-plugin": "^5.57.0",
|
|
37
|
+
"@typescript-eslint/parser": "^5.57.0",
|
|
34
38
|
"prettier": "^2.8.3",
|
|
35
39
|
"rimraf": "^4.1.2",
|
|
36
|
-
"
|
|
40
|
+
"ts-node": "^10.9.1",
|
|
41
|
+
"ts-patch": "^2.1.0",
|
|
42
|
+
"typescript": "^4.9.5",
|
|
43
|
+
"typia": "^3.6.9"
|
|
37
44
|
},
|
|
38
45
|
"dependencies": {
|
|
39
46
|
"chalk": "^4.1.2",
|
package/src/TestValidator.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { is_sorted } from "tstl/ranges";
|
|
2
|
-
import { json_equal_to } from "./internal/json_equal_to";
|
|
3
2
|
|
|
4
3
|
import { RandomGenerator } from "./RandomGenerator";
|
|
4
|
+
import { json_equal_to } from "./internal/json_equal_to";
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Test validator.
|
|
@@ -11,6 +11,68 @@ import { RandomGenerator } from "./RandomGenerator";
|
|
|
11
11
|
* @author Jeongho Nam - https://github.com/samchon
|
|
12
12
|
*/
|
|
13
13
|
export namespace TestValidator {
|
|
14
|
+
/**
|
|
15
|
+
* Test whether condition is satisfied.
|
|
16
|
+
*
|
|
17
|
+
* @param title Title of error message when condition is not satisfied
|
|
18
|
+
* @return Currying function
|
|
19
|
+
*/
|
|
20
|
+
export const predicate =
|
|
21
|
+
(title: string) =>
|
|
22
|
+
<T extends boolean | (() => boolean) | (() => Promise<boolean>)>(
|
|
23
|
+
condition: T,
|
|
24
|
+
): T extends () => Promise<boolean> ? Promise<void> : void => {
|
|
25
|
+
const message = () =>
|
|
26
|
+
`Bug on ${title}: expected condition is not satisfied.`;
|
|
27
|
+
|
|
28
|
+
// SCALAR
|
|
29
|
+
if (typeof condition === "boolean") {
|
|
30
|
+
if (condition !== true) throw new Error(message());
|
|
31
|
+
return undefined as any;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// CLOSURE
|
|
35
|
+
const output: boolean | Promise<boolean> = condition();
|
|
36
|
+
if (typeof output === "boolean") {
|
|
37
|
+
if (output !== true) throw new Error(message());
|
|
38
|
+
return undefined as any;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// ASYNCHRONOUS
|
|
42
|
+
return new Promise<void>((resolve, reject) => {
|
|
43
|
+
output
|
|
44
|
+
.then((flag) => {
|
|
45
|
+
if (flag === true) resolve();
|
|
46
|
+
else reject(message());
|
|
47
|
+
})
|
|
48
|
+
.catch(reject);
|
|
49
|
+
}) as any;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Test whether two values are equal.
|
|
54
|
+
*
|
|
55
|
+
* If you want to validate `covers` relationship,
|
|
56
|
+
* call smaller first and then larger.
|
|
57
|
+
*
|
|
58
|
+
* Otherwise you wanna non equals validator, combine with {@link error}.
|
|
59
|
+
*
|
|
60
|
+
* @param title Title of error message when different
|
|
61
|
+
* @returns Currying function
|
|
62
|
+
*/
|
|
63
|
+
export const equals =
|
|
64
|
+
(title: string) =>
|
|
65
|
+
<T>(x: T) =>
|
|
66
|
+
(y: T) => {
|
|
67
|
+
const diff: string[] = json_equal_to(x, y);
|
|
68
|
+
if (diff.length)
|
|
69
|
+
throw new Error(
|
|
70
|
+
`Bug on ${title}: found different values - [${diff.join(
|
|
71
|
+
", ",
|
|
72
|
+
)}]`,
|
|
73
|
+
);
|
|
74
|
+
};
|
|
75
|
+
|
|
14
76
|
/**
|
|
15
77
|
* Test whether error occurs.
|
|
16
78
|
*
|
|
@@ -22,27 +84,22 @@ export namespace TestValidator {
|
|
|
22
84
|
*/
|
|
23
85
|
export const error =
|
|
24
86
|
(title: string) =>
|
|
25
|
-
|
|
87
|
+
<T>(task: () => T): T extends Promise<any> ? Promise<void> : void => {
|
|
88
|
+
const message = () => `Bug on ${title}: exception must be thrown.`;
|
|
26
89
|
try {
|
|
27
|
-
|
|
90
|
+
const output: T = task();
|
|
91
|
+
if (is_promise(output))
|
|
92
|
+
return new Promise<void>((resolve, reject) =>
|
|
93
|
+
output
|
|
94
|
+
.catch(() => resolve())
|
|
95
|
+
.then(() => reject(message())),
|
|
96
|
+
) as any;
|
|
97
|
+
else throw new Error(message());
|
|
28
98
|
} catch {
|
|
29
|
-
return;
|
|
99
|
+
return undefined as any;
|
|
30
100
|
}
|
|
31
|
-
throw new Error(`Bug on ${title}: exception must be thrown.`);
|
|
32
101
|
};
|
|
33
102
|
|
|
34
|
-
/**
|
|
35
|
-
* Test whether two values are equal.
|
|
36
|
-
*
|
|
37
|
-
* @param title Title of error message when different
|
|
38
|
-
* @returns Currying function
|
|
39
|
-
*/
|
|
40
|
-
export const equals = (title: string) => <T>(x: T) => (y: T) => {
|
|
41
|
-
const diff: string[] = json_equal_to(x, y);
|
|
42
|
-
if (diff.length)
|
|
43
|
-
throw new Error(`Bug on ${title}: found different values - [${diff.join(", ")}]`);
|
|
44
|
-
};
|
|
45
|
-
|
|
46
103
|
/**
|
|
47
104
|
* Validate index API.
|
|
48
105
|
*
|
|
@@ -214,3 +271,12 @@ interface IEntity<Type extends string | number | bigint> {
|
|
|
214
271
|
function get_ids<Entity extends IEntity<any>>(entities: Entity[]): string[] {
|
|
215
272
|
return entities.map((entity) => entity.id).sort((x, y) => (x < y ? -1 : 1));
|
|
216
273
|
}
|
|
274
|
+
|
|
275
|
+
function is_promise(input: any): input is Promise<any> {
|
|
276
|
+
return (
|
|
277
|
+
typeof input === "object" &&
|
|
278
|
+
input !== null &&
|
|
279
|
+
typeof (input as any).then === "function" &&
|
|
280
|
+
typeof (input as any).catch === "function"
|
|
281
|
+
);
|
|
282
|
+
}
|