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

138 lines
6.9 KiB
TypeScript
Raw Permalink Normal View History

2021-05-24 09:27:46 +00:00
import { Reader } from '@preserves/core';
import { Meta, readSchema } from '../src/index';
import './test-utils';
describe('checker', () => {
describe('simplest invertibility tests', () => {
it('passes simple invertibility test', () => {
2021-05-24 09:27:46 +00:00
expect(readSchema('version 1 . A = <a @x string @y symbol> .')).not.toBeNull();
});
it('passes invertibility check for literal field', () => {
2021-05-24 09:27:46 +00:00
expect(readSchema('version 1 . A = <a "string" @y symbol> .')).not.toBeNull();
});
it('detects non-invertibility for string field', () => {
2021-05-24 09:27:46 +00:00
expect(() => readSchema('version 1 . A = <a string @y symbol> .')).toThrow(/item 0 of fields of A/);
});
it('detects non-invertibility for symbol field', () => {
2021-05-24 09:27:46 +00:00
expect(() => readSchema('version 1 . A = <a @x string symbol> .')).toThrow(/item 1 of fields of A/);
});
2021-05-24 09:32:29 +00:00
it('is OK with no names in simple seqof', () => {
expect(readSchema('version 1 . A = [string ...].')).not.toBeNull();
});
2021-05-24 09:27:46 +00:00
});
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>
2021-12-08 13:06:16 +00:00
<tuplePrefix [<named a <atom String>>, <named b <atom String>>]
<named extra <seqof any>>>>}}>`).next());
2021-05-24 09:27:46 +00:00
});
it('non-invertibility tail', () => {
2021-05-24 09:27:46 +00:00
expect(() => readSchema(
'version 1 . ExtensibleRecord = <foo @a string @b string any ...>.'))
.toThrow(/tail of fields of ExtensibleRecord/);
});
});
2021-05-24 09:32:29 +00:00
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/);
});
2021-12-08 13:06:16 +00:00
it('complains about duplicates in tuplePrefixes', () => {
2021-05-24 09:32:29 +00:00
expect(() => readSchema('version 1 . A = [@a string @b string @a int @rest any ...].'))
.toThrow(/duplicate binding named "a" in item 2 of A/);
});
2021-12-08 13:06:16 +00:00
it('complains about duplicates in tuplePrefix tails', () => {
2021-05-24 09:32:29 +00:00
expect(() => readSchema('version 1 . A = [@a string @b string @c int @a any ...].'))
.toThrow(/duplicate binding named "a" in tail of A/);
});
describe('in records', () => {
it('complains about duplicates in recs (1)', () => {
expect(() => readSchema('version 1 . A = <a @a string @a int>.'))
.toThrow(/duplicate binding named "a" in item 1 of fields of A/);
});
it('complains about duplicates in recs (2)', () => {
expect(() => readSchema('version 1 . A = <a @a string <x @y int @a int>>.'))
.toThrow(/duplicate binding named "a" in item 1 of fields of item 1 of fields of A/);
});
it('complains about duplicates in recs (3)', () => {
expect(() => readSchema('version 1 . A = <a @a string <<rec> =x [@y int @a int]>>.'))
.toThrow(/duplicate binding named "a" in item 1 of fields of item 1 of fields of A/);
});
it('complains about duplicates in recs (4)', () => {
expect(() => readSchema('version 1 . A = <a @a string <<rec> @a =x [@y int @z int]>>.'))
.toThrow(/duplicate binding named "a" in label of item 1 of fields of A/);
});
it('complains about duplicates in recs (5)', () => {
expect(() => readSchema('version 1 . A = <a @a string <<rec> @a any [@y int @z int]>>.'))
.toThrow(/duplicate binding named "a" in label of item 1 of fields of A/);
});
});
describe('in unions', () => {
it('is OK with non-duplicate but duplicate-seeming bindings across branches', () => {
expect(readSchema('version 1 . A = <a @a string> / <b @a string>.')).not.toBeNull();
});
it('complains about duplicates within branches', () => {
expect(() => readSchema('version 1 . A = <a @a string @a int> / <b @a string>.'))
.toThrow(/in item 1 of fields of variant a of A/);
});
it('complains about duplicate branch names', () => {
expect(() => readSchema('version 1 . A = @x <a @a string> / @x <b @a string>.'))
.toThrow(/duplicate variant label in variant x of A/);
});
});
describe('in intersections', () => {
it('is OK with non-duplicate bindings across branches', () => {
expect(readSchema('version 1 . A = <a @a string> & <a @b string>.')).not.toBeNull();
});
it('complains about duplicates within branches', () => {
expect(() => readSchema(
`version 1 . A = <a @a string @a int> & <a @b string @c int>.`))
.toThrow(/in item 1 of fields of A/);
});
it('complains about duplicates across branches', () => {
expect(() => readSchema(
`version 1 . A = <a @a string @b int> & <a @a string @c int>.`))
.toThrow(/in item 0 of fields of A/);
});
it('complains about duplicates within named branches', () => {
expect(() => readSchema(
`version 1 .
AAA = <a @a string @a int>.
ABC = <a @b string @c int>.
2021-12-08 13:06:16 +00:00
A = @x AAA & @y ABC .`))
.toThrow(/in item 1 of fields of AAA/);
});
it('is OK with seeming- but non-duplicates across named branches', () => {
expect(readSchema(
`version 1 .
AAA = <a @a string @b int>.
ABC = <a @a string @c int>.
2021-12-08 13:06:16 +00:00
A = @x AAA & @y ABC .`))
.not.toBeNull();
});
it('complains about duplicate branch names', () => {
expect(() => readSchema(
`version 1 .
AAB = <a @a string @b int>.
ACD = <a @c string @d int>.
2021-12-08 13:06:16 +00:00
A = @x AAB & @x ACD .`))
.toThrow(/in A/);
});
});
2021-05-24 09:32:29 +00:00
});
2021-05-24 09:27:46 +00:00
});