preserves/implementations/javascript/packages/schema/test/checker.test.ts

64 lines
2.9 KiB
TypeScript

import { Reader } from '@preserves/core';
import { Meta, readSchema } from '../src/index';
import './test-utils';
describe('checker', () => {
describe('simplest non-bijection tests', () => {
it('passes simple non-bijection test', () => {
expect(readSchema('version 1 . A = <a @x string @y symbol> .')).not.toBeNull();
});
it('passes non-bijection check for literal field', () => {
expect(readSchema('version 1 . A = <a "string" @y symbol> .')).not.toBeNull();
});
it('detects non-bijection for string field', () => {
expect(() => readSchema('version 1 . A = <a string @y symbol> .')).toThrow(/item 0 of fields of A/);
});
it('detects non-bijection for symbol field', () => {
expect(() => readSchema('version 1 . A = <a @x string symbol> .')).toThrow(/item 1 of fields of A/);
});
it('is OK with no names in simple seqof', () => {
expect(readSchema('version 1 . A = [string ...].')).not.toBeNull();
});
});
describe('extensible record', () => {
it('is happy with extensible record', () => {
expect(Meta.fromSchema(readSchema(
'version 1 . ExtensibleRecord = <foo @a string @b string @extra any ...>.')))
.is(new Reader(
`<schema {
version: 1,
embeddedType: #f,
definitions: {
ExtensibleRecord:
<rec <lit foo>
<tuple* [<named a <atom String>>, <named b <atom String>>]
<named extra <seqof any>>>>}}>`).next());
});
it('non-bijection tail', () => {
expect(() => readSchema(
'version 1 . ExtensibleRecord = <foo @a string @b string any ...>.'))
.toThrow(/tail of fields of ExtensibleRecord/);
});
});
describe('duplicate bindings', () => {
it('complains about duplicates in tuples', () => {
expect(() => readSchema('version 1 . A = [@a string @a string].'))
.toThrow(/duplicate binding named "a" in item 1 of A/);
});
it('complains about duplicates in dicts', () => {
expect(() => readSchema('version 1 . A = { x: @a string , y: @a string }.'))
.toThrow(/duplicate binding named "a" in entry y in dictionary in A/);
});
it('complains about duplicates in tuple*s', () => {
expect(() => readSchema('version 1 . A = [@a string @b string @a int @rest any ...].'))
.toThrow(/duplicate binding named "a" in item 2 of A/);
});
it('complains about duplicates in tuple* tails', () => {
expect(() => readSchema('version 1 . A = [@a string @b string @c int @a any ...].'))
.toThrow(/duplicate binding named "a" in tail of A/);
});
});
});