@lumjs/core 1.0.0-beta.1

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/test/types.js ADDED
@@ -0,0 +1,185 @@
1
+ // Current test count.
2
+ const plan = 91;
3
+ // A new test instance.
4
+ const t = require('@lumjs/tests').new({module, plan});
5
+ // The types core module
6
+ const types = require('../index').types;
7
+
8
+ // And a quick reference to the type names.
9
+ const TYP = types.TYPES;
10
+
11
+ // Helper function.
12
+ function stringify (what)
13
+ {
14
+ if (typeof what === TYP.F) return what.toString();
15
+ if (typeof what === TYP.SY) return what.constructor.toString();
16
+ return JSON.stringify(what);
17
+ }
18
+
19
+ // Now for some further basics.
20
+ t.ok(types.isObj({}), 'isObj({})');
21
+ t.ok(types.isComplex({}), 'isComplex(object)');
22
+ t.ok(types.isComplex(function(){}), 'isComplex(function)');
23
+ t.ok(types.isNil(undefined), 'isNil(undefined)');
24
+ t.ok(types.isNil(null), 'isNil(null)');
25
+ t.ok(types.notNil(true), 'notNil(true)');
26
+ t.ok(types.notNil(false), 'notNil(false)');
27
+ t.ok(types.notNil(''), "notNil('')");
28
+ t.ok(types.isScalar(true), 'isScalar(true)');
29
+ t.ok(types.isScalar(0), 'isScalar(0)');
30
+ t.ok(!types.isScalar({}), '!isScalar({})');
31
+ t.ok(!types.isScalar(null), '!isScalar(null)');
32
+ t.ok(types.isArray([]), 'isArray([])');
33
+ t.ok(types.isTypedArray(new Int8Array(8)), 'isTypedArray(Int8Array)');
34
+ t.ok(types.nonEmptyArray([1,2,3]), 'nonEmptyArray([1,2,3])');
35
+ t.ok(!types.nonEmptyArray([]), '!nonEmptyArray([])');
36
+ t.ok(types.isProperty('hi'), 'isProperty(string)');
37
+ t.ok(types.isProperty(Symbol('hi')), 'isProperty(Symbol)');
38
+ t.ok(!types.isProperty(false), '!isProperty(false)')
39
+
40
+ function getArguments() { return arguments; }
41
+
42
+ t.ok(types.isArguments(getArguments()), 'isArguments(arguments)');
43
+ t.ok(!types.isArguments({}), '!isArguments({})');
44
+
45
+ class TypeClass
46
+ {
47
+ notUnbound()
48
+ {
49
+ t.ok(!types.unbound(this), '!unbound(instanceThis)');
50
+ }
51
+ }
52
+
53
+ class SubtypeClass extends TypeClass {}
54
+
55
+ const typesInstance = new TypeClass();
56
+ const subtypeInstance = new SubtypeClass();
57
+
58
+ t.ok(types.isInstance(typesInstance, TypeClass), 'isInstance(typeInstance,TypeClass)');
59
+ t.ok(types.isInstance(subtypeInstance, SubtypeClass), 'isInstance(subtypeInstance, SubtypeClass)');
60
+ t.ok(types.isInstance(subtypeInstance, TypeClass), 'isInstance(subtypeInstance, TypeClass)');
61
+ t.ok(!types.isInstance(typesInstance, SubtypeClass), '!isInstance(typeInstance, SubtypeClass');
62
+
63
+ function doesDesc (tests, not=false)
64
+ {
65
+ for (const it of tests)
66
+ {
67
+ let result = types.doesDescriptor(it);
68
+ if (not) result = !result;
69
+ const desc = (not?'!':'')+'doesDescriptor'+stringify(it)+')';
70
+ t.ok(result, desc);
71
+ }
72
+ }
73
+
74
+ doesDesc(
75
+ [
76
+ {value: true},
77
+ {value: {}, writable: true},
78
+ {get: function(){}},
79
+ {set: function(){}},
80
+ {get: function(){}, set: function(){}},
81
+ {get: function(){}, configurable: true},
82
+ ]);
83
+
84
+ doesDesc(
85
+ [
86
+ {},
87
+ {value: true, get: function(){}},
88
+ {value: true, set: function(){}},
89
+ {get: function(){}, writable: true},
90
+ {set: function(){}, writable: true},
91
+ ], true);
92
+
93
+ function testIsType (tests, not=false)
94
+ {
95
+ for (const it of tests)
96
+ {
97
+ let result = types.isType(it[0], it[1]);
98
+ if (not) result = !result;
99
+ const desc = (it.length > 2)
100
+ ? it[2]
101
+ : (it[0]+','+stringify(it[1]));
102
+ t.ok(result, (not?'!':'')+'isType('+desc+')');
103
+ }
104
+ }
105
+
106
+ testIsType(
107
+ [
108
+ [TYP.O, {}],
109
+ [TYP.F, function(){}],
110
+ [TYP.S, 'hello'],
111
+ [TYP.B, true],
112
+ [TYP.B, false],
113
+ [TYP.N, 100],
114
+ [TYP.U, undefined],
115
+ [TYP.SY, Symbol('foo'), TYP.SY+',Symbol'],
116
+ [TYP.BI, BigInt(12345), TYP.BI+',BigInt(12345)'],
117
+ [TYP.BI, 54321n, TYP.BI+',54321n'],
118
+ [TYP.ARGS, getArguments()],
119
+ [TYP.ARRAY, []],
120
+ [TYP.NULL, null],
121
+ [TYP.TYPEDARRAY, new Int16Array(16), TYP.TYPEDARRAY+',Int16Array(16)'],
122
+ [TYP.DESCRIPTOR, {value: true}],
123
+ [TYP.DESCRIPTOR, {get: function(){}}],
124
+ [TYP.COMPLEX, {}],
125
+ [TYP.COMPLEX, function(){}],
126
+ [TYP.SCALAR, true],
127
+ [TYP.SCALAR, 'hi'],
128
+ [TYP.PROP, 'woah'],
129
+ [TYP.PROP, Symbol('woah'), TYP.PROP+',Symbol'],
130
+ ]);
131
+
132
+ testIsType(
133
+ [
134
+ [TYP.O, 'foo'],
135
+ [TYP.O, null],
136
+ [TYP.F, {}],
137
+ [TYP.B, 0],
138
+ [TYP.B, 1],
139
+ [TYP.N, '0'],
140
+ [TYP.U, null],
141
+ [TYP.SY, 'foo'],
142
+ [TYP.BI, 12345],
143
+ [TYP.ARGS, {}],
144
+ [TYP.ARRAY, {}],
145
+ [TYP.NULL, false],
146
+ [TYP.TYPEDARRAY, []],
147
+ [TYP.DESCRIPTOR, {value: true, get: function(){}}],
148
+ [TYP.DESCRIPTOR, {}],
149
+ [TYP.COMPLEX, 'a string'],
150
+ [TYP.SCALAR, {}],
151
+ [TYP.PROP, null],
152
+ ], true);
153
+
154
+ t.ok(types.containsAny(['hello','world'], 'world'), 'containsAny([a], a)');
155
+ t.ok(!types.containsAny(['hello','world'], 'universe'), '!containsAny([a], b)');
156
+ t.ok(types.containsAll(['hello','darkness','my','old','friend'], 'hello', 'friend'), 'containsAll([a,b,c], a, c)');
157
+ t.ok(!types.containsAll(['nothing','to','see'], 'nothing', 'here'), '!containsAll([a,b,c], a, d)');
158
+
159
+ const arr = ['hello', 'darkness', 'my', 'old', 'friend'];
160
+ types.removeFromArray(arr, 'darkness');
161
+ t.isJSON(arr, ['hello','my','old','friend'], 'removeFromArray(...)');
162
+
163
+ (function(){ t.ok(types.unbound(this), 'unbound(unboundThis)'); })();
164
+ (function(){ t.ok(!types.unbound(this), '!unbound(boundThis)') }).bind({})();
165
+ typesInstance.notUnbound();
166
+
167
+ t.ok((function(){types.needObj({}); return true})(), 'needObj({})');
168
+ t.dies(function(){types.needObj(null); return true}, '!needObj(null)');
169
+
170
+ t.ok((function(){types.needType(TYP.S, 'hi'); return true})(), "needType('string','hi')");
171
+ t.dies(function(){types.needType(TYP.O, null); return true}, "!needType('object',null)");
172
+
173
+ { // Try a few versions of 'def'
174
+ const obj = {};
175
+ types.def(obj, 'test1', 'Test 1');
176
+ t.is(obj.test1, 'Test 1', 'def(obj, name, value)');
177
+ types.def(obj)('test2', 'Test 2');
178
+ t.is(obj.test2, 'Test 2', 'def(obj)(name, value)');
179
+ }
180
+
181
+ t.dies(()=>types.NYI(), 'NYI()');
182
+
183
+ // All done.
184
+ t.output();
185
+