Skip to content

Commit 361b8f8

Browse files
test: add guard functions
1 parent 8157866 commit 361b8f8

10 files changed

Lines changed: 390 additions & 0 deletions
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Function.
2+
import { guardInstance } from '../lib/guard-instance.func';
3+
// Variables.
4+
import { CLASS, Class } from './variables/class.const';
5+
import { FALSE_EXPECTATION, TRUE_EXPECTATION } from './variables/boolean.const';
6+
import { NUMBER } from './variables/number.const';
7+
import { NUMBER_INSTANCE, NUMBER_NEW_INSTANCE } from './variables/number.const';
8+
import { STRING } from './variables/string.const';
9+
import { STRING_INSTANCE, STRING_NEW_INSTANCE } from './variables/string.const';
10+
import { TRUE, TRUE_INSTANCE, FALSE_INSTANCE, FALSE } from './variables/boolean.const';
11+
12+
describe(guardInstance.name , () => {
13+
// Defined.
14+
it('is DEFINED', () => expect(guardInstance).toBeDefined());
15+
16+
// Checks ...
17+
describe(`checks`, () => {
18+
it('callback', () => {
19+
guardInstance(CLASS, Class, (result: boolean, value: Class) => {
20+
expect(result).toBe(TRUE);
21+
expect(value).toEqual(CLASS);
22+
return result;
23+
});
24+
});
25+
// ... instance.
26+
describe(`instance`, () => it(`CLASS`, () => expect(guardInstance(CLASS, Class)).toBe(TRUE)));
27+
28+
// ... primitives.
29+
describe(`primitive`, () => {
30+
// boolean
31+
describe(`boolean`, () => {
32+
it(`${TRUE_EXPECTATION}`, () => expect(guardInstance(TRUE_INSTANCE, Boolean)).toBe(FALSE));
33+
it(`${FALSE_EXPECTATION}`, () => expect(guardInstance(FALSE_INSTANCE, Boolean)).toBe(FALSE));
34+
});
35+
36+
// number
37+
describe(`number`, () => {
38+
it(`Number(${NUMBER})`, () => expect(guardInstance(NUMBER_INSTANCE, Number)).toBe(FALSE));
39+
it(`new Number(${NUMBER})`, () => expect(guardInstance(NUMBER_NEW_INSTANCE, Number)).toBe(FALSE));
40+
});
41+
42+
// string
43+
describe(`string`, () => {
44+
it(`String(${STRING})`, () => expect(guardInstance(STRING_INSTANCE, String)).toBe(FALSE));
45+
it(`new String(${STRING})`, () => expect(guardInstance(STRING_NEW_INSTANCE, String)).toBe(FALSE));
46+
});
47+
});
48+
});
49+
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Function.
2+
import { guardKey } from '../lib/guard-key.func';
3+
// Variables.
4+
import { NUMBER } from './variables/number.const';
5+
import { STRING} from './variables/string.const';
6+
import { SYMBOL_NUMBER, SYMBOL_STRING } from './variables/symbol.const';
7+
import { TRUE } from './variables/boolean.const';
8+
// Type.
9+
import { Key } from '../../type/key.type';
10+
11+
describe(guardKey.name, () => {
12+
// Defined.
13+
it('is DEFINED', () => expect(guardKey).toBeDefined());
14+
15+
// Checks ...
16+
describe(`checks`, () => {
17+
it('callback', () => {
18+
guardKey(STRING, (result: boolean, value: Key) => {
19+
expect(result).toBe(TRUE);
20+
expect(value).toEqual(STRING);
21+
return result;
22+
});
23+
});
24+
25+
// ... primitives.
26+
describe(`primitive`, () => {
27+
// number
28+
describe(`number`, () => it(`${NUMBER}`, () => expect(guardKey(NUMBER)).toBe(TRUE)));
29+
// string
30+
describe(`string`, () => it(`${STRING}`, () => expect(guardKey(STRING)).toBe(TRUE)));
31+
// symbol
32+
describe(`symbol`, () => {
33+
it(`Symbol(${NUMBER})`, () => expect(guardKey(SYMBOL_NUMBER)).toBe(TRUE));
34+
it(`Symbol(${STRING})`, () => expect(guardKey(SYMBOL_STRING)).toBe(TRUE));
35+
});
36+
});
37+
});
38+
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Function.
2+
import { guardNull } from '../lib/guard-null.func';
3+
// Variables.
4+
import { NULL } from './variables/null.const';
5+
import { TRUE } from './variables/boolean.const';
6+
7+
describe(guardNull.name, () => {
8+
// Defined.
9+
it('is DEFINED', () => expect(guardNull).toBeDefined());
10+
11+
// Checks ...
12+
describe(`checks`, () => {
13+
it('callback', () => {
14+
guardNull(NULL, (result: boolean, value: null) => {
15+
expect(result).toBe(TRUE);
16+
expect(value).toBeNull();
17+
return result;
18+
});
19+
});
20+
21+
// ... primitives.
22+
describe(`primitive`, () => it(`${NULL}`, () => expect(guardNull(NULL)).toBe(TRUE)));
23+
});
24+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Function.
2+
import { guardNumber } from '../lib/guard-number.func';
3+
// Variables.
4+
import { NUMBER, NUMBER_INSTANCE, NUMBER_NEW_INSTANCE } from './variables/number.const';
5+
import { TRUE } from './variables/boolean.const';
6+
7+
describe(guardNumber.name, () => {
8+
// Defined.
9+
it('is DEFINED', () => expect(guardNumber).toBeDefined());
10+
11+
// Checks ...
12+
describe(`checks`, () => {
13+
it('callback', () => {
14+
guardNumber(NUMBER, (result: boolean, value: number) => {
15+
expect(result).toBe(TRUE);
16+
expect(value).toEqual(NUMBER);
17+
return result;
18+
});
19+
});
20+
21+
// ... primitives.
22+
describe(`primitive`, () => {
23+
// number
24+
describe(`number`, () => {
25+
it(`${NUMBER}`, () => expect(guardNumber(NUMBER)).toBe(TRUE));
26+
// it(`Number(${NUMBER})`, () => expect(guardNumber(NUMBER_INSTANCE)).toBe(TRUE));
27+
// it(`new Number(${NUMBER})`, () => expect(guardNumber(NUMBER_NEW_INSTANCE)).toBe(TRUE));
28+
});
29+
});
30+
});
31+
});
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Function.
2+
import { guardObjectKey } from '../lib/guard-object-key.func';
3+
// Variables.
4+
import { CLASS } from './variables/class.const';
5+
import { NUMBER } from './variables/number.const';
6+
import { OBJECT_ONE } from './variables/object.const';
7+
import { SYMBOL_NUMBER, SYMBOL_STRING } from './variables/symbol.const';
8+
import { TRUE } from './variables/boolean.const';
9+
10+
describe(guardObjectKey.name , () => {
11+
// Defined.
12+
it('is DEFINED', () => expect(guardObjectKey).toBeDefined());
13+
14+
// Checks ...
15+
describe(`checks`, () => {
16+
// it('callback', () => {
17+
// guardObjectKey(CLASS, ['firstName', 'surname'], (result: boolean, value: Class) => {
18+
// expect(result).toBe(TRUE);
19+
// expect(value).toEqual(CLASS);
20+
// return result;
21+
// });
22+
// });
23+
24+
// ... instance.
25+
describe(`instance`, () => {
26+
describe(`CLASS`, () => {
27+
// number.
28+
it('has number key', () => {
29+
expect(guardObjectKey(CLASS, 1030405027)).toBe(TRUE);
30+
expect(guardObjectKey(CLASS, 5)).toBe(TRUE);
31+
});
32+
33+
it('finds getter number', () => expect(guardObjectKey(CLASS, NUMBER)).toBe(TRUE));
34+
35+
// string.
36+
it('has string key', () => expect(guardObjectKey(CLASS, 'surname')).toBe(TRUE));
37+
38+
// symbol.
39+
it('finds getter symbol key', () => {
40+
expect(guardObjectKey(CLASS, SYMBOL_NUMBER)).toBe(TRUE);
41+
expect(guardObjectKey(CLASS, SYMBOL_STRING)).toBe(TRUE);
42+
});
43+
});
44+
});
45+
46+
// ... objects.
47+
describe('object', () => {
48+
describe(`OBJECT_ONE`, () => {
49+
// number.
50+
it('has number key', () => {
51+
expect(guardObjectKey(OBJECT_ONE, 1030405027)).toBe(TRUE);
52+
expect(guardObjectKey(OBJECT_ONE, 5)).toBe(TRUE);
53+
expect(guardObjectKey(OBJECT_ONE, NUMBER)).toBe(TRUE);
54+
});
55+
56+
// string.
57+
it('has string key', () => {
58+
expect(guardObjectKey(OBJECT_ONE, 'key as string')).toBe(TRUE);
59+
expect(guardObjectKey(OBJECT_ONE, 'x')).toBe(TRUE);
60+
});
61+
62+
// symbol.
63+
it('has symbol key', () => {
64+
expect(guardObjectKey(OBJECT_ONE, SYMBOL_NUMBER)).toBe(TRUE);
65+
expect(guardObjectKey(OBJECT_ONE, SYMBOL_STRING)).toBe(TRUE);
66+
});
67+
});
68+
});
69+
});
70+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Function.
2+
import { guardObject } from '../lib/guard-object.func';
3+
// Variables.
4+
import { CLASS } from './variables/class.const';
5+
import { OBJECT_ONE, OBJECT_TWO, OBJECT_ONE_NEW, OBJECT_TWO_NEW } from './variables/object.const';
6+
import { TRUE } from './variables/boolean.const';
7+
8+
describe(guardObject.name, () => {
9+
// Defined.
10+
it('is DEFINED', () => expect(guardObject).toBeDefined());
11+
12+
// Checks ...
13+
describe(`checks`, () => {
14+
// it('callback', () => {
15+
// guardObject(OBJECT_ONE, (result: boolean, value: ObjectOne) => {
16+
// expect(result).toBe(TRUE);
17+
// expect(OBJECT_ONE).toEqual(value);
18+
// return result;
19+
// });
20+
// });
21+
22+
// ... objects.
23+
describe('object', () => {
24+
it(`CLASS`, () => expect(guardObject(CLASS)).toBe(TRUE));
25+
it(`OBJECT_ONE`, () => expect(guardObject(OBJECT_ONE)).toBe(TRUE));
26+
it(`OBJECT_TWO`, () => expect(guardObject(OBJECT_TWO)).toBe(TRUE));
27+
it(`new Object(OBJECT_ONE_NEW})`, () => expect(guardObject(OBJECT_ONE_NEW)).toBe(TRUE));
28+
it(`new Object(OBJECT_TWO_NEW})`, () => expect(guardObject(OBJECT_TWO_NEW)).toBe(TRUE));
29+
});
30+
});
31+
});
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Function.
2+
import { guardPrimitive } from '../lib/guard-primitive.func';
3+
// Variables.
4+
import { BIGINT, BIGINT_EXPECTATION, BIGINT_INSTANCE } from './variables/big-int.const';
5+
import { FALSE, TRUE, TRUE_INSTANCE, FALSE_INSTANCE, FALSE_EXPECTATION, TRUE_EXPECTATION } from './variables/boolean.const';
6+
import { NULL } from './variables/null.const';
7+
import { NUMBER, NUMBER_INSTANCE, NUMBER_NEW_INSTANCE } from './variables/number.const';
8+
import { STRING, STRING_INSTANCE, STRING_NEW_INSTANCE } from './variables/string.const';
9+
import { SYMBOL_NUMBER, SYMBOL_STRING } from './variables/symbol.const';
10+
import { UNDEFINED } from './variables/undefined.const';
11+
12+
describe(guardPrimitive.name, () => {
13+
// Defined.
14+
it('is DEFINED', () => expect(guardPrimitive).toBeDefined());
15+
16+
// Checks ...
17+
describe(`checks`, () => {
18+
it('callback', () => {
19+
guardPrimitive<string>(STRING, 'string' , (result: boolean, value: string) => {
20+
expect(result).toBe(TRUE);
21+
expect(value).toEqual(STRING);
22+
return result;
23+
});
24+
});
25+
26+
// ... primitives.
27+
describe(`primitive`, () => {
28+
// bigint
29+
describe(`bigint`, () => {
30+
it(`${BIGINT}`, () => expect(guardPrimitive(BIGINT, 'bigint')).toBe(TRUE));
31+
it(`${BIGINT_EXPECTATION}`, () => expect(guardPrimitive(BIGINT_INSTANCE, 'bigint')).toBe(TRUE));
32+
});
33+
34+
// boolean
35+
describe(`boolean`, () => {
36+
it(`${TRUE}`, () => expect(guardPrimitive(TRUE, 'boolean')).toBe(TRUE));
37+
it(`${FALSE}`, () => expect(guardPrimitive(FALSE, 'boolean')).toBe(TRUE));
38+
// it(`${TRUE_EXPECTATION}`, () => expect(guardPrimitive(TRUE_INSTANCE, 'boolean')).toBe(TRUE));
39+
// it(`${FALSE_EXPECTATION}`, () => expect(guardPrimitive(FALSE_INSTANCE, 'boolean')).toBe(TRUE));
40+
});
41+
42+
// null
43+
it(`${NULL}`, () => expect(guardPrimitive(NULL, 'null')).toBe(TRUE));
44+
45+
// number
46+
describe(`number`, () => {
47+
it(`${NUMBER}`, () => expect(guardPrimitive(NUMBER, 'number')).toBe(TRUE));
48+
// it(`Number(${NUMBER})`, () => expect(guardPrimitive(NUMBER_INSTANCE, 'number')).toBe(TRUE));
49+
// it(`new Number(${NUMBER})`, () => expect(guardPrimitive(NUMBER_NEW_INSTANCE, 'number')).toBe(TRUE));
50+
});
51+
52+
// string
53+
describe(`string`, () => {
54+
it(`${STRING}`, () => expect(guardPrimitive(STRING, 'string')).toBe(TRUE));
55+
// it(`String(${STRING})`, () => expect(guardPrimitive(STRING_INSTANCE, 'string')).toBe(TRUE));
56+
// it(`new String(${STRING})`, () => expect(guardPrimitive(STRING_NEW_INSTANCE, 'string')).toBe(TRUE));
57+
});
58+
59+
// symbol
60+
describe(`symbol`, () => {
61+
it(`Symbol(${NUMBER})`, () => expect(guardPrimitive(SYMBOL_NUMBER, 'symbol')).toBe(TRUE));
62+
it(`Symbol(${STRING})`, () => expect(guardPrimitive(SYMBOL_STRING, 'symbol')).toBe(TRUE));
63+
});
64+
65+
// undefined
66+
it(`${UNDEFINED}`, () => expect(guardPrimitive(UNDEFINED, 'undefined')).toBe(TRUE));
67+
});
68+
});
69+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Function.
2+
import { guardString } from '../lib/guard-string.func';
3+
// Variables.
4+
import { STRING } from './variables/string.const';
5+
import { TRUE } from './variables/boolean.const';
6+
7+
describe(guardString.name, () => {
8+
// Defined.
9+
it('is DEFINED', () => expect(guardString).toBeDefined());
10+
11+
// Checks ...
12+
describe(`checks`, () => {
13+
it('callback', () => {
14+
guardString(STRING, (result: boolean, value: string) => {
15+
expect(result).toBe(TRUE);
16+
expect(value).toEqual(STRING);
17+
return result;
18+
});
19+
});
20+
// ... primitives.
21+
describe(`primitive`, () => describe(`string`, () => it(`${STRING}`, () => expect(guardString(STRING)).toBe(TRUE))));
22+
});
23+
});
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Function.
2+
import { guardSymbol } from '../lib/guard-symbol.func';
3+
// Variables.
4+
import { NUMBER } from './variables/number.const';
5+
import { STRING } from './variables/string.const';
6+
import { SYMBOL_NUMBER, SYMBOL_STRING } from './variables/symbol.const';
7+
import { TRUE } from './variables/boolean.const';
8+
9+
describe(guardSymbol.name, () => {
10+
// Defined.
11+
it('is DEFINED', () => expect(guardSymbol).toBeDefined());
12+
13+
// Checks ...
14+
describe(`checks`, () => {
15+
it('callback', () => {
16+
guardSymbol(SYMBOL_STRING, (result: boolean, value: symbol) => {
17+
expect(result).toBe(TRUE);
18+
expect(value).toEqual(SYMBOL_STRING);
19+
return result;
20+
});
21+
});
22+
23+
// ... primitives.
24+
describe(`primitive`, () => {
25+
// symbol
26+
describe(`symbol`, () => {
27+
it(`Symbol(${NUMBER})`, () => expect(guardSymbol(SYMBOL_NUMBER)).toBe(TRUE));
28+
it(`Symbol(${STRING})`, () => expect(guardSymbol(SYMBOL_STRING)).toBe(TRUE));
29+
});
30+
});
31+
});
32+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Function.
2+
import { guardUndefined } from '../lib/guard-undefined.func';
3+
// Variables.
4+
import { TRUE } from './variables/boolean.const';
5+
import { UNDEFINED } from './variables/undefined.const';
6+
7+
describe(guardUndefined.name, () => {
8+
// Defined.
9+
it('is DEFINED', () => expect(guardUndefined).toBeDefined());
10+
11+
// Checks ...
12+
describe(`checks`, () => {
13+
it('callback', () => {
14+
guardUndefined(UNDEFINED, (result: boolean, value: undefined) => {
15+
expect(result).toBe(TRUE);
16+
expect(value).toBeUndefined();
17+
return result;
18+
});
19+
});
20+
// ... primitives.
21+
describe(`primitive`, () => it(`${UNDEFINED}`, () => expect(guardUndefined(UNDEFINED)).toBe(TRUE)));
22+
});
23+
});

0 commit comments

Comments
 (0)