type-enforcer-test-helper 1.3.10 → 2.0.0-alpha.0

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.
Files changed (59) hide show
  1. package/README.md +2 -156
  2. package/dist/js/index.js +7 -0
  3. package/dist/js/src/data/TestClass.js +12 -0
  4. package/dist/js/src/data/coercible.js +81 -0
  5. package/dist/js/src/data/testData.js +168 -0
  6. package/dist/js/src/data/testValues.js +116 -0
  7. package/dist/js/src/multiTest.js +116 -0
  8. package/dist/js/src/testCheck.js +39 -0
  9. package/dist/js/src/testEnforce.js +121 -0
  10. package/dist/js/src/testMethod.js +337 -0
  11. package/dist/js/src/utility/difference.js +7 -0
  12. package/dist/types/index.d.ts +7 -0
  13. package/dist/types/src/data/TestClass.d.ts +6 -0
  14. package/dist/types/src/data/coercible.d.ts +6 -0
  15. package/dist/types/src/data/testData.d.ts +30 -0
  16. package/dist/types/src/data/testData.test.d.ts +1 -0
  17. package/dist/types/src/data/testValues.d.ts +19 -0
  18. package/dist/types/src/data/testValues.test.d.ts +1 -0
  19. package/dist/types/src/multiTest.d.ts +58 -0
  20. package/dist/types/src/multiTest.test.d.ts +1 -0
  21. package/dist/types/src/testCheck.d.ts +3 -0
  22. package/dist/types/src/testCheck.test.d.ts +1 -0
  23. package/dist/types/src/testEnforce.d.ts +2 -0
  24. package/dist/types/src/testEnforce.test.d.ts +1 -0
  25. package/dist/types/src/testMethod.d.ts +20 -0
  26. package/dist/types/src/testMethod.test.d.ts +1 -0
  27. package/dist/types/src/utility/difference.d.ts +2 -0
  28. package/dist/types/src/utility/difference.test.d.ts +1 -0
  29. package/package.json +24 -42
  30. package/index.js +0 -17
  31. package/src/data/TestClass.js +0 -11
  32. package/src/data/coercible.js +0 -85
  33. package/src/data/testData.js +0 -417
  34. package/src/data/testValues.js +0 -241
  35. package/src/multiTest.js +0 -125
  36. package/src/testCheck.js +0 -67
  37. package/src/testEnforce.js +0 -143
  38. package/src/testMethod.js +0 -439
  39. package/src/utility/difference.js +0 -4
  40. package/types/index.d.ts +0 -8
  41. package/types/index.d.ts.map +0 -1
  42. package/types/src/data/TestClass.d.ts +0 -11
  43. package/types/src/data/TestClass.d.ts.map +0 -1
  44. package/types/src/data/coercible.d.ts +0 -7
  45. package/types/src/data/coercible.d.ts.map +0 -1
  46. package/types/src/data/testData.d.ts +0 -289
  47. package/types/src/data/testData.d.ts.map +0 -1
  48. package/types/src/data/testValues.d.ts +0 -26
  49. package/types/src/data/testValues.d.ts.map +0 -1
  50. package/types/src/multiTest.d.ts +0 -14
  51. package/types/src/multiTest.d.ts.map +0 -1
  52. package/types/src/testCheck.d.ts +0 -11
  53. package/types/src/testCheck.d.ts.map +0 -1
  54. package/types/src/testEnforce.d.ts +0 -12
  55. package/types/src/testEnforce.d.ts.map +0 -1
  56. package/types/src/testMethod.d.ts +0 -27
  57. package/types/src/testMethod.d.ts.map +0 -1
  58. package/types/src/utility/difference.d.ts +0 -3
  59. package/types/src/utility/difference.d.ts.map +0 -1
@@ -1,241 +0,0 @@
1
- import TestClass from './TestClass.js';
2
-
3
- /**
4
- * A concatenated array of all the "valid" arrays.
5
- *
6
- * @constant {Array} testValues
7
- */
8
-
9
- /**
10
- * Arrays instantiated in different ways or with different values.
11
- *
12
- * @constant {Array} validArrays
13
- */
14
- const arrayReference = Array(3);
15
- export const validArrays = [
16
- [1],
17
- [2],
18
- [],
19
- new Array(),
20
- new Array(12),
21
- Array(),
22
- arrayReference
23
- ];
24
-
25
- /**
26
- * Booleans instantiated in different ways or with different values.
27
- *
28
- * @constant {Array} validBooleans
29
- */
30
- const booleanReference = Boolean();
31
- export const validBooleans = [
32
- true,
33
- false,
34
- new Boolean(true), // eslint-disable-line no-new-wrappers
35
- Boolean(1),
36
- booleanReference
37
- ];
38
-
39
- /**
40
- * Dates instantiated in different ways or with different values.
41
- *
42
- * @constant {Array} validDates
43
- */
44
- const dateReference = new Date();
45
- export const validDates = [
46
- dateReference,
47
- new Date('01/15/2010')
48
- ];
49
-
50
- /**
51
- * Functions instantiated in different ways.
52
- *
53
- * @constant {Array} validFunctions
54
- */
55
- const namedFunctionExpression = function() {
56
- };
57
- export const validFunctions = [
58
- namedFunctionExpression,
59
- function namedFunctionDeclaration() {
60
- },
61
- function() {
62
- },
63
- () => {
64
- }
65
- ];
66
-
67
- /**
68
- * Different instances of TestClass with different values.
69
- *
70
- * @constant {Array} validInstances
71
- */
72
- const classReference = new TestClass(1);
73
- export const validInstances = [
74
- classReference,
75
- new TestClass(1),
76
- new TestClass(2)
77
- ];
78
-
79
- /**
80
- * Integers instantiated in different ways or with different values.
81
- *
82
- * @constant {Array} validIntegers
83
- */
84
- const integerReference = Number(11);
85
- export const validIntegers = [
86
- 1,
87
- 5,
88
- new Number(42), // eslint-disable-line no-new-wrappers
89
- integerReference
90
- ];
91
-
92
- /**
93
- * Floats instantiated in different ways or with different values.
94
- *
95
- * @constant {Array} validFloats
96
- */
97
- const floatReference = Number(11.3);
98
- export const validFloats = [
99
- 1.3,
100
- 2.5,
101
- -10.00000001,
102
- 3.14159,
103
- new Number(42.2), // eslint-disable-line no-new-wrappers
104
- floatReference
105
- ];
106
-
107
- /**
108
- * Infinity and -Infinity
109
- *
110
- * @constant {Array} validInfinities
111
- */
112
- const infinityReference = Infinity;
113
- export const validInfinities = [
114
- infinityReference,
115
- -Infinity
116
- ];
117
-
118
- /**
119
- * An empty Map and a Map with data.
120
- *
121
- * @constant {Array} validMaps
122
- */
123
- const mapReference = new Map().set('test', 12);
124
- export const validMaps = [
125
- new Map(),
126
- mapReference
127
- ];
128
-
129
- /**
130
- * Plain objects instantiated in different ways or with different values.
131
- *
132
- * @constant {Array} validObjects
133
- */
134
- const objectReference = Object();
135
- export const validObjects = [
136
- {},
137
- { test1: 1 },
138
- new Object(), // eslint-disable-line no-new-object
139
- objectReference
140
- ];
141
-
142
- /**
143
- * A normal Promise and Promise.all.
144
- *
145
- * @constant {Array} validPromises
146
- */
147
- const promiseReference = new Promise((resolve) => resolve());
148
- export const validPromises = [
149
- promiseReference,
150
- new Promise((resolve) => resolve()),
151
- Promise.all(promiseReference)
152
- ];
153
-
154
- /**
155
- * RegExps instantiated in different ways or with different values.
156
- *
157
- * @constant {Array} validRegExps
158
- */
159
- const regExpReference = RegExp(); // eslint-disable-line require-unicode-regexp
160
- export const validRegExps = [
161
- /asdf/g, // eslint-disable-line require-unicode-regexp
162
- new RegExp('test 2'), // eslint-disable-line require-unicode-regexp
163
- regExpReference
164
- ];
165
-
166
- /**
167
- * An empty Set and a Set with data.
168
- *
169
- * @constant {Array} validSets
170
- */
171
- const setReference = new Set([1, 2]);
172
- export const validSets = [
173
- new Set(),
174
- setReference
175
- ];
176
-
177
- /**
178
- * Strings instantiated in different ways or with different values.
179
- *
180
- * @constant {Array} validStrings
181
- */
182
- const stringReference = String('test3');
183
- export const validStrings = [
184
- 'test',
185
- '',
186
- new String('test2'), // eslint-disable-line no-new-wrappers
187
- stringReference
188
- ];
189
-
190
- /**
191
- * A Symbol with a label and one without.
192
- *
193
- * @constant {Array} validSymbols
194
- */
195
- const symbolReference = Symbol('test');
196
- export const validSymbols = [Symbol(), symbolReference];
197
-
198
- /**
199
- * An empty WeakMap and a WeakMap with data.
200
- *
201
- * @constant {Array} validWeakMaps
202
- */
203
- const weakMapReference = new WeakMap().set({}, 12);
204
- export const validWeakMaps = [
205
- new WeakMap(),
206
- weakMapReference
207
- ];
208
-
209
- /**
210
- * An empty WeakSet and a WeakSet with data.
211
- *
212
- * @constant {Array} validWeakSets
213
- */
214
- const weakSetReference = new WeakSet([new TestClass()]);
215
- export const validWeakSets = [new WeakSet(), weakSetReference];
216
-
217
- /**
218
- * Undefined, null, and NaN
219
- *
220
- * @constant {Array} validNots
221
- */
222
- export const validNots = [undefined, null, NaN];
223
-
224
- export const testValues = validNots.concat(
225
- validArrays,
226
- validBooleans,
227
- validDates,
228
- validFunctions,
229
- validInstances,
230
- validIntegers,
231
- validFloats,
232
- validInfinities,
233
- validMaps,
234
- validObjects,
235
- validPromises,
236
- validRegExps,
237
- validSets,
238
- validStrings,
239
- validWeakMaps,
240
- validWeakSets
241
- );
package/src/multiTest.js DELETED
@@ -1,125 +0,0 @@
1
- import displayValue from 'display-value';
2
- import { forOwn, mix } from 'object-agent';
3
- import { assert } from 'type-enforcer';
4
-
5
- const getMessage = (settings, assertion) => {
6
- if (settings.message) {
7
- return settings.message;
8
- }
9
- if (assertion === 'true') {
10
- return (input) => `should return true for ${ displayValue(input) }`;
11
- }
12
- if (assertion === 'false') {
13
- return (input) => `should return false for ${ displayValue(input) }`;
14
- }
15
-
16
- return (input, output) => `should return ${ displayValue(output) } when set to ${ input }`;
17
- };
18
-
19
- /**
20
- * Run multiple identical tests over a set of data.
21
- *
22
- * @function multiTest
23
- *
24
- * @param {object} settings - Settings object.
25
- * @param {object | Array} settings.values - The values to run tests against.
26
- * @param {object | Array} [settings.values2] - Only for eachPair. If not provided, pairs are made within the values array. If provided, pairs are only made with one from each array.
27
- * @param {Function} settings.test - The test to run against each value. Provided one or two args (two args if eachPair is true). Each arg is a value from values or values2. Should not call assert, but return a value to be asserted against configured output.
28
- * @param {Function} [settings.filter] - Provided one or two args (two args if eachPair is true). Each arg is a value from values or values2. Return a truthy value to run the test, falsey to skip the test.
29
- * @param {Function} [settings.message] - Provides two or three params, the input value(s) and the expected output value. Must return a string. It is recommended to use the display-value library on values for readability.
30
- * @param {string} [settings.inputKey] - If values is an array of objects, this specifies which key to get the input value from.
31
- * @param {string} [settings.outputKey] - If values is an array of objects, this specifies which key to get the expected output value from.
32
- * @param {unknown} [settings.output] - The expected output value of all tests.
33
- * @param {boolean} [settings.eachPair=false] - Values must be an array, runs tests on every combination of two items from values.
34
- * @param {string} [settings.assertion='equal'] - The type-enforcer assert function to run against all tests.
35
- */
36
- export default (settings) => {
37
- let assertion = settings.assertion || 'equal';
38
- const buildSingleMessage = getMessage(settings, assertion);
39
-
40
- if (assertion === 'true') {
41
- assertion = 'equal';
42
- settings.output = true;
43
- }
44
- else if (assertion === 'false') {
45
- assertion = 'equal';
46
- settings.output = false;
47
- }
48
-
49
- const buildDoubleMessage = settings.message || ((input1, input2, output) => {
50
- return `should return ${ displayValue(output) } when ${ displayValue(input1) } and ${ displayValue(input2) } are provided`;
51
- });
52
-
53
- const testSingleValue = (input, output, value) => {
54
- if ((!settings.filter) || settings.filter(value)) {
55
- it(buildSingleMessage(input, output), () => {
56
- assert[assertion](settings.test(input), output);
57
- });
58
- }
59
- };
60
-
61
- const testDoubleValue = (input1, input2, output, value1, value2) => {
62
- if ((!settings.filter) || settings.filter(value1, value2)) {
63
- it(buildDoubleMessage(input1, input2, output), () => {
64
- assert[assertion](settings.test(input1, input2), output);
65
- });
66
- }
67
- };
68
-
69
- const testSingleArrayValue = (value) => {
70
- if ('output' in settings) {
71
- if ('inputKey' in settings) {
72
- testSingleValue(value[settings.inputKey], settings.output, value);
73
- }
74
- else {
75
- testSingleValue(value, settings.output, value);
76
- }
77
- }
78
- else if ('outputKey' in settings) {
79
- if ('inputKey' in settings) {
80
- testSingleValue(value[settings.inputKey], value[settings.outputKey], value);
81
- }
82
- else {
83
- testSingleValue(value, value[settings.outputKey], value);
84
- }
85
- }
86
- else if ('inputKey' in settings) {
87
- testSingleValue(value[settings.inputKey], undefined, value);
88
- }
89
- else {
90
- testSingleValue(value, undefined, value);
91
- }
92
- };
93
-
94
- const testDoubleArrayValue = (value1, value2) => {
95
- if ('output' in settings) {
96
- if ('inputKey' in settings) {
97
- testDoubleValue(value1[settings.inputKey], value2[settings.inputKey], settings.output, value1, value2);
98
- }
99
- else {
100
- testDoubleValue(value1, value2, settings.output, value1, value2);
101
- }
102
- }
103
- else if ('inputKey' in settings) {
104
- testDoubleValue(value1[settings.inputKey], value2[settings.inputKey], undefined, value1, value2);
105
- }
106
- else {
107
- testDoubleValue(value1, value2, undefined, value1, value2);
108
- }
109
- };
110
-
111
- if (settings.values && settings.values.constructor === Object) {
112
- forOwn(settings.values, (value, key) => {
113
- testSingleValue(key, value, value);
114
- });
115
- }
116
- else if (settings.eachPair) {
117
- mix(settings.values, settings.values2 || settings.values)
118
- .forEach((values) => {
119
- testDoubleArrayValue(values[0], values[1]);
120
- });
121
- }
122
- else {
123
- settings.values.forEach(testSingleArrayValue);
124
- }
125
- };
package/src/testCheck.js DELETED
@@ -1,67 +0,0 @@
1
- import { assert } from 'type-enforcer';
2
- import multiTest from './multiTest.js';
3
-
4
- /**
5
- * Test a type-enforcer check function (isArray, isBoolean, etc.).
6
- *
7
- * @function testCheck
8
- *
9
- * @param {object} data - A data object (arrayData, booleanData, etc.).
10
- * @param {Function} check - The function to test.
11
- * @param {object} is - An object that includes this function.
12
- */
13
- export default function(data, check, is) {
14
- it('should exist in the exported "is" object', () => {
15
- assert.equal(check, is[data.name]);
16
- });
17
-
18
- multiTest({
19
- values: data.true,
20
- test(value) {
21
- return check(value);
22
- },
23
- assertion: 'true'
24
- });
25
-
26
- multiTest({
27
- values: data.false,
28
- test(value) {
29
- return check(value);
30
- },
31
- assertion: 'false'
32
- });
33
-
34
- multiTest({
35
- values: data.coerceTrue,
36
- test(value) {
37
- return check(value);
38
- },
39
- assertion: 'false'
40
- });
41
-
42
- describe('coerce', () => {
43
- multiTest({
44
- values: data.true,
45
- test(value) {
46
- return check(value, true);
47
- },
48
- assertion: 'true'
49
- });
50
-
51
- multiTest({
52
- values: data.coerceTrue,
53
- test(value) {
54
- return check(value, true);
55
- },
56
- assertion: 'true'
57
- });
58
-
59
- multiTest({
60
- values: data.coerceFalse,
61
- test(value) {
62
- return check(value, true);
63
- },
64
- assertion: 'false'
65
- });
66
- });
67
- }
@@ -1,143 +0,0 @@
1
- import displayValue from 'display-value';
2
- import { assert } from 'type-enforcer';
3
- import TestClass from './data/TestClass.js';
4
- import { testTypes } from './data/testData.js';
5
- import multiTest from './multiTest.js';
6
-
7
- /**
8
- * Test an enforce function (enforceArray, enforceBoolean, etc.).
9
- *
10
- * @function testEnforce
11
- *
12
- * @param {object} data - A data object (arrayData, booleanData, etc.).
13
- * @param {Function} enforcer - The function to test.
14
- * @param {object} enforce - An object that includes this function.
15
- * @param {Function} coercer - A function that does a coercion just like this function.
16
- */
17
- export default function(data, enforcer, enforce, coercer) {
18
- it('should exist in the exported "enforce" object', () => {
19
- assert.equal(enforcer, enforce[data.name]);
20
- });
21
-
22
- it(`should return the setter value when a ${data.name} is provided`, () => {
23
- let result = null;
24
- const value1 = data.true[0];
25
- const value2 = data.true[1];
26
-
27
- if (data.extraArg) {
28
- result = enforce[data.name](value1, data.extraArg, value2);
29
- }
30
- else {
31
- result = enforce[data.name](value1, value2);
32
- }
33
-
34
- assert.is(result, value1);
35
- assert.notIs(result, value2);
36
- });
37
-
38
- if (coercer === Number) {
39
- it('should return the min value when a integer less than the min value is provided', () => {
40
- assert.equal(enforce[data.name](-12, 11, false, 0, 5), 0);
41
- });
42
- it('should return the max value when a integer greater than the max value is provided', () => {
43
- assert.equal(enforce[data.name](12, 11, false, 0, 5), 5);
44
- });
45
- }
46
-
47
- multiTest({
48
- values: data.coerceTrue.map((item) => {
49
- return {
50
- input: item,
51
- output: item
52
- };
53
- }),
54
- message(input) {
55
- return `should NOT return a coerced ${displayValue(input)} when coerce is false`;
56
- },
57
- test(value) {
58
- return enforce[data.name](value, value, false);
59
- },
60
- inputKey: 'input',
61
- outputKey: 'output',
62
- assertion: 'equal'
63
- });
64
-
65
- describe('coerce', () => {
66
- if (coercer) {
67
- multiTest({
68
- values: data.coerceTrue.map((item) => {
69
- return {
70
- input: item,
71
- output: coercer(item)
72
- };
73
- }),
74
- message(input) {
75
- return `should return a coerced ${displayValue(input)} when coerce is true`;
76
- },
77
- test(value) {
78
- return enforce[data.name](value, value, true);
79
- },
80
- inputKey: 'input',
81
- outputKey: 'output',
82
- assertion: 'equal'
83
- });
84
- }
85
-
86
- multiTest({
87
- values: data.coerceFalse,
88
- message(input) {
89
- return `should return the alt value when ${displayValue(input)} is provided and coerce is true`;
90
- },
91
- test(value) {
92
- return enforce[data.name](value, 'testAlt', true);
93
- },
94
- output: 'testAlt',
95
- assertion: 'equal'
96
- });
97
- });
98
-
99
- testTypes.forEach((typesData) => {
100
- if (typesData.name && !([typesData.name].concat(typesData.skip)
101
- .includes(data.name))) {
102
- typesData.true.forEach((testItem) => {
103
- it(`should return the default value when ${displayValue(testItem)} is provided`, () => {
104
- if (data.extraArg) {
105
- assert.equal(enforce[data.name](testItem, data.extraArg, data.true[0]), data.true[0]);
106
- }
107
- else {
108
- assert.equal(enforce[data.name](testItem, data.true[0]), data.true[0]);
109
- }
110
- });
111
- });
112
- }
113
- });
114
-
115
- if (data.name !== 'instanceOf') {
116
- it('should return the default value when an instance is provided', () => {
117
- if (data.extraArg) {
118
- assert.equal(enforce[data.name](new TestClass(), data.extraArg, data.true[0]), data.true[0]);
119
- }
120
- else {
121
- assert.equal(enforce[data.name](new TestClass(), data.true[0]), data.true[0]);
122
- }
123
- });
124
- }
125
-
126
- it('should return the default value when undefined is provided', () => {
127
- if (data.extraArg) {
128
- assert.equal(enforce[data.name](undefined, data.extraArg, data.true[0]), data.true[0]);
129
- }
130
- else {
131
- assert.equal(enforce[data.name](undefined, data.true[0]), data.true[0]);
132
- }
133
- });
134
-
135
- it('should return the default value when null is provided', () => {
136
- if (data.extraArg) {
137
- assert.equal(enforce[data.name](null, data.extraArg, data.true[0]), data.true[0]);
138
- }
139
- else {
140
- assert.equal(enforce[data.name](null, data.true[0]), data.true[0]);
141
- }
142
- });
143
- }