Simple tests; more exports

This commit is contained in:
Tony Garnock-Jones 2021-05-24 11:27:46 +02:00
parent e594d22d09
commit a1fdddcf7b
4 changed files with 96 additions and 0 deletions

View File

@ -1,2 +1,6 @@
export * from './checker';
export * from './error';
export * from './reader';
export * from './compiler';
export * as Meta from './meta';
export * as Type from './type';

View File

@ -0,0 +1,41 @@
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/);
});
});
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/);
});
});
});

View File

@ -0,0 +1,16 @@
import { readSchema } from '../src/index';
describe('reader schema', () => {
it('complains about bad version', () => {
expect(() => readSchema('version 999 .')).toThrow(/Invalid Version/);
});
it('complains about missing version', () => {
expect(() => readSchema('')).toThrow(/missing version/);
});
it('is OK with an empty schema correctly versioned', () => {
const s = readSchema('version 1 .');
expect(s.version).toBeNull();
expect(s.definitions.size).toBe(0);
expect(s.embeddedType._variant).toBe('false');
});
});

View File

@ -0,0 +1,35 @@
import { Value, is, preserves } from '@preserves/core';
declare global {
namespace jest {
interface Matchers<R> {
is<T>(expected: Value<T>): R;
toThrowFilter(f: (e: Error) => boolean): R;
}
}
}
expect.extend({
is(actual, expected) {
return is(actual, expected)
? { message: () => preserves`expected ${actual} not to be Preserves.is to ${expected}`,
pass: true }
: { message: () => preserves`expected ${actual} to be Preserves.is to ${expected}`,
pass: false };
},
toThrowFilter(thunk, f) {
try {
thunk();
return { message: () => preserves`expected an exception`, pass: false };
} catch (e) {
if (f(e)) {
return { message: () => preserves`expected an exception not matching the filter`,
pass: true };
} else {
return { message: () => preserves`expected an exception matching the filter: ${e.constructor.name}`,
pass: false };
}
}
}
});