muschema
Version:
Schemas for mudb
201 lines • 6.31 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var test = require("tape");
var __1 = require("../");
var helper_1 = require("./helper");
test('union - identity', function (t) {
var unionSchema = new __1.MuUnion({
number: new __1.MuInt8(123),
});
t.same(unionSchema.identity, {
type: '',
data: void 0,
});
unionSchema = new __1.MuUnion({
number: new __1.MuInt8(123),
}, 'number');
t.same(unionSchema.identity, {
type: 'number',
data: 123,
});
t.end();
});
test('union - alloc()', function (t) {
var unionSchema = new __1.MuUnion({
number: new __1.MuInt8(123),
}, 'number');
t.same(unionSchema.alloc(), {
type: '',
data: void 0,
});
t.end();
});
function randomUnion(type) {
return {
type: type,
data: helper_1.randomValue(type),
};
}
test('union - equal()', function (t) {
var schemaSpec = {
uint8: new __1.MuUint8(),
uint16: new __1.MuUint16(),
};
var unionSchema = new __1.MuUnion(schemaSpec);
var a = randomUnion('uint8');
var b = { type: a.type, data: a.data };
t.ok(unionSchema.equal(a, b));
var c = { type: a.type, data: a.data - 1 };
t.notOk(unionSchema.equal(a, c));
var d = randomUnion('uint16');
d.data = a.data;
t.notOk(unionSchema.equal(a, d));
var e = { type: a.type, data: a.data };
var f = { type: a.type, data: a.data };
t.ok(unionSchema.equal(e, f));
delete e.data;
t.notOk(unionSchema.equal(e, f));
delete f.data;
t.notOk(unionSchema.equal(e, f));
delete e.type;
delete f.type;
t.notOk(unionSchema.equal(e, f));
var g = unionSchema.alloc();
var h = unionSchema.alloc();
t.ok(unionSchema.equal(g, h));
t.end();
});
test('union (flat type-data pair) - clone()', function (t) {
var schemaSpec = {
boolean: new __1.MuBoolean(),
float32: new __1.MuFloat32(),
float64: new __1.MuFloat64(),
int8: new __1.MuInt8(),
int16: new __1.MuInt16(),
int32: new __1.MuInt32(),
string: new __1.MuString(),
uint8: new __1.MuUint8(),
uint16: new __1.MuUint16(),
uint32: new __1.MuUint32(),
};
var unionSchema = new __1.MuUnion(schemaSpec);
for (var _i = 0, _a = Object.keys(schemaSpec); _i < _a.length; _i++) {
var muType = _a[_i];
var pair = randomUnion(muType);
var copy = unionSchema.clone(pair);
t.notEquals(copy, pair);
t.same(copy, pair);
}
t.end();
});
test('union (nested type-data pair) - clone()', function (t) {
var schemaSpec = {
subType: new __1.MuUnion({
number: new __1.MuUint8(123),
text: new __1.MuString('foo'),
}, 'number'),
};
var unionSchema = new __1.MuUnion(schemaSpec, 'subType');
var pair = {
type: 'subType',
data: {
type: 'number',
data: 123,
},
};
var copy = unionSchema.clone(pair);
t.notEquals(copy, pair);
t.same(copy, pair);
t.end();
});
test('union (flat type-data pair) - diff() & patch()', function (t) {
var schemaSpec = {
boolean: new __1.MuBoolean(),
float32: new __1.MuFloat32(),
float64: new __1.MuFloat64(),
int8: new __1.MuInt8(),
int16: new __1.MuInt16(),
int32: new __1.MuInt32(),
string: new __1.MuString(),
uint8: new __1.MuUint8(),
uint16: new __1.MuUint16(),
uint32: new __1.MuUint32(),
};
var unionSchema = new __1.MuUnion(schemaSpec);
var testPatchingPair = helper_1.testPatchingPairFactory(t, unionSchema);
for (var _i = 0, _a = Object.keys(schemaSpec); _i < _a.length; _i++) {
var typeA = _a[_i];
testPatchingPair(randomUnion(typeA), randomUnion(typeA));
for (var _b = 0, _c = Object.keys(schemaSpec); _b < _c.length; _b++) {
var typeB = _c[_b];
if (typeB === typeA) {
continue;
}
testPatchingPair(randomUnion(typeA), randomUnion(typeB));
}
}
t.end();
});
test('union (nested type-data pair) - diff() & patch()', function (t) {
var schemaSpec = {
subType: new __1.MuUnion({
boolean: new __1.MuBoolean(),
float32: new __1.MuFloat32(),
float64: new __1.MuFloat64(),
int8: new __1.MuInt8(),
int16: new __1.MuInt16(),
int32: new __1.MuInt32(),
string: new __1.MuString(),
uint8: new __1.MuUint8(),
uint16: new __1.MuUint16(),
uint32: new __1.MuUint32(),
}),
};
var unionSchema = new __1.MuUnion(schemaSpec);
var testPatchingPair = helper_1.testPatchingPairFactory(t, unionSchema);
for (var _i = 0, _a = Object.keys(schemaSpec.subType.muData); _i < _a.length; _i++) {
var typeA = _a[_i];
for (var i = 0; i < 200; ++i) {
var pairA = {
type: 'subType',
data: {
type: typeA,
data: helper_1.randomValue(typeA),
},
};
var pairB = {
type: 'subType',
data: {
type: typeA,
data: helper_1.randomValue(typeA),
},
};
testPatchingPair(pairA, pairB);
}
for (var _b = 0, _c = Object.keys(schemaSpec.subType.muData); _b < _c.length; _b++) {
var typeB = _c[_b];
if (typeA === typeB) {
continue;
}
for (var i = 0; i < 200; ++i) {
var pairA = {
type: 'subType',
data: {
type: typeA,
data: helper_1.randomValue(typeA),
},
};
var pairB = {
type: 'subType',
data: {
type: typeB,
data: helper_1.randomValue(typeB),
},
};
testPatchingPair(pairA, pairB);
}
}
}
t.end();
});
//# sourceMappingURL=union.js.map