JS: Update the collection/struct API to compute the type. (#1502)

When you create or modify a data structure we now compute the type
based on the type of the values in the data structure.

Towards #1491
This commit is contained in:
Erik Arvidsson
2016-05-16 10:58:50 -07:00
parent e4540aae9f
commit 5db7fe8285
24 changed files with 456 additions and 869 deletions
+25 -55
View File
@@ -1,32 +1,32 @@
// @flow
import {makeTestingBatchStore} from './batch-store-adaptor.js';
import {default as Struct, newStruct, StructMirror, createStructClass} from './struct.js';
import {
default as Struct,
newStruct,
newStructWithType,
StructMirror,
createStructClass,
} from './struct.js';
import {assert} from 'chai';
import {
boolType,
numberType,
makeStructType,
makeRefType,
makeListType,
makeStructType,
numberType,
stringType,
valueType,
} from './type.js';
import {suite, test} from 'mocha';
import Database from './database.js';
import {newList} from './list.js';
import {equals} from './compare.js';
import {newList} from './list.js';
suite('Struct', () => {
test('equals', () => {
const type = makeStructType('S1', {
'x': boolType,
'o': stringType,
});
const data1 = {x: true, o: 'hi'};
const s1 = newStruct(type, data1);
const s2 = newStruct(type, data1);
const s1 = newStruct('S1', data1);
const s2 = newStruct('S1', data1);
assert.isTrue(equals(s1, s2));
});
@@ -34,52 +34,28 @@ suite('Struct', () => {
test('chunks', () => {
const ds = new Database(makeTestingBatchStore());
const bt = boolType;
const refOfBoolType = makeRefType(bt);
const type = makeStructType('S1', {
'r': refOfBoolType,
});
const b = true;
const r = ds.writeValue(b);
const s1 = newStruct(type, {r: r});
const s1 = newStruct('S1', {r: r});
assert.strictEqual(1, s1.chunks.length);
assert.isTrue(equals(r, s1.chunks[0]));
});
test('new', () => {
const type = makeStructType('S2', {
'b': boolType,
'o': stringType,
});
const s1 = newStruct(type, {b: true, o: 'hi'});
const s1 = newStruct('S2', {b: true, o: 'hi'});
assert.strictEqual(s1.b, true);
assert.strictEqual(s1.o, 'hi');
const s2 = newStruct(type, {b: false, o: 'hi'});
const s2 = newStruct('S2', {b: false, o: 'hi'});
assert.strictEqual(s2.b, false);
assert.strictEqual(s2.o, 'hi');
assert.throws(() => {
newStruct(type, {o: 'hi'}); // missing required field
});
assert.throws(() => {
newStruct(type, {x: 'hi'}); // unknown field
});
const s3 = newStruct(type, {b: true, o: 'hi'});
const s3 = newStruct('S2', {b: true, o: 'hi'});
assert.isTrue(equals(s1, s3));
});
test('struct set', () => {
const type = makeStructType('S3', {
'b': boolType,
'o': stringType,
});
const s1 = newStruct(type, {b: true, o: 'hi'});
const s1 = newStruct('S3', {b: true, o: 'hi'});
const s2 = s1.setB(false);
// TODO: assert throws on set wrong type
@@ -99,12 +75,6 @@ suite('Struct', () => {
assert.isTrue(equals(s5, s6));
});
test('type assertion on construct', () => {
assert.throws(() => {
newStruct(boolType, {b: true});
});
});
test('createStructClass', () => {
const typeA = makeStructType('A', {
'b': numberType,
@@ -125,13 +95,13 @@ suite('Struct', () => {
});
assert.throws(() => {
newStruct(type, {x: 1, o: 'hi'});
newStructWithType(type, {x: 1, o: 'hi'});
});
assert.throws(() => {
newStruct(type, {o: 1});
newStructWithType(type, {o: 1});
});
newStruct(type, {x: true, o: 'hi'});
newStructWithType(type, {x: true, o: 'hi'});
});
test('type validation cyclic', async () => {
@@ -147,16 +117,16 @@ suite('Struct', () => {
type.desc.fields['l'] = listType;
const emptyList = await newList([], listType);
newStruct(type, {b: true, l: emptyList});
newStruct(type, {b: true, l: await newList([
newStruct(type, {b: false, l: emptyList}),
newStructWithType(type, {b: true, l: emptyList});
newStructWithType(type, {b: true, l: await newList([
newStructWithType(type, {b: false, l: emptyList}),
], listType)});
assert.throws(() => {
newStruct(type, {b: 1});
newStructWithType(type, {b: 1});
});
assert.throws(() => {
newStruct(type, {b: true, o: 1});
newStructWithType(type, {b: true, o: 1});
});
});
});