/
test.js
199 lines (161 loc) · 4.84 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import {Realm} from "../..";
import {Struct} from "./";
import {$Backing, $Address} from "../../symbols";
describeRealm('UnionType', function (options) {
let realm;
let StructType;
let UnionType;
let T;
let Point;
let StringOrPoint;
let instance;
before(() => {
realm = options.realm;
StructType = realm.StructType;
UnionType = realm.UnionType;
T = realm.T;
});
it('should create some struct types', function () {
Point = new StructType({
x: T.Float64,
y: T.Float64
}, {
name: 'Point'
});
});
describe('Boxed', function () {
it('should create a StringOrPoint union type', function () {
StringOrPoint = new UnionType(
T.String,
Point
);
});
it('should have the right byte length', function () {
StringOrPoint.byteLength.should.equal(24);
});
it('should create a boxed StringOrPoint instance', function () {
instance = new StringOrPoint({x: 123, y: 456});
});
it('should have the right type', function () {
instance.type.should.equal(Point);
});
it('instance.value should be an instance of Point', function () {
instance.value.should.be.an.instanceOf(Point);
});
it('instance.value should have the right properties', function () {
instance.value.x.should.equal(123);
instance.value.y.should.equal(456);
});
it('should overwrite the value with a different type', function () {
instance.value = "hello world";
});
it('should have the right type', function () {
instance.type.should.equal(T.String);
});
it('should have overwritten the value', function () {
instance.value.should.equal("hello world");
});
it('should overwrite the value with the same type', function () {
instance.value = "foo bar";
});
it('should have updated the value', function () {
instance.value.should.equal("foo bar");
});
it('should write a null value', function () {
instance.value = null;
});
it('should have the right type', function () {
(instance.type === null).should.equal(true);
});
it('should read a null value', function () {
(instance.value == null).should.equal(true);
});
});
describe('UnionArray', function () {
let arr;
it('should create a StringOrPoint.Array instance', function () {
arr = new StringOrPoint.Array(3);
});
it('should have a length of 3', function () {
arr.length.should.equal(3);
});
it('should be full of nulls', function () {
arr.forEach(item => (item === null).should.equal(true));
});
it('should write some values', function () {
arr[0] = "foo";
arr[1] = {x: 1, y: 2};
arr[2] = "bar";
});
it('should have the right values', function () {
arr[0].should.equal("foo");
arr[1].toJSON().should.eql({x: 1, y: 2});
arr[2].should.equal("bar");
});
it('should overwrite a value', function () {
arr[1] = "baz";
});
it('should have overwritten the value', function () {
arr.toJSON().should.eql(["foo", "baz", "bar"]);
});
});
describe('Union in a struct', function () {
let SimpleStruct;
let struct;
it('should create a struct type', function () {
SimpleStruct = new StructType({
name: T.String,
age: T.Uint8,
pointOrAddress: StringOrPoint
});
});
it('should create a new instance', function () {
struct = new SimpleStruct({
name: 'Bob',
age: 99,
pointOrAddress: '123 Fake Street'
});
});
it('should have the right values', function () {
struct.toJSON().should.eql({
name: 'Bob',
age: 99,
pointOrAddress: '123 Fake Street'
});
});
it('should overwrite a value', function () {
struct.pointOrAddress = {
x: 2,
y: 4
};
});
it('should have the right type', function () {
struct.pointOrAddress.should.be.an.instanceOf(Point);
});
it('should have the right values', function () {
struct.pointOrAddress.x.should.equal(2);
struct.pointOrAddress.y.should.equal(4);
});
it('should overwite a point value', function () {
struct.pointOrAddress.y = 123;
});
it('should have the right values', function () {
struct.pointOrAddress.toJSON().should.eql({
x: 2,
y: 123
});
});
it('should overwrite again, this time with null', function () {
struct.pointOrAddress = null;
});
it('should have the right value', function () {
(struct.pointOrAddress === null).should.equal(true);
});
});
describe('.randomValue()', function () {
it('should create a random value', function () {
const value = StringOrPoint.randomValue();
(typeof value === 'string' || value instanceof Point).should.equal(true);
});
});
});