Skip to content

Commit 145278a

Browse files
author
Rheilly Aguilar
committed
add a unit test to Type Tree and Schema Tree
1 parent aaf4ce0 commit 145278a

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

packages/idea-parser/tests/Schema.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { expect, use } from 'chai';
44
import deepEqualInAnyOrder from 'deep-equal-in-any-order';
55
import SchemaTree from '../src/trees/SchemaTree';
66
import Compiler from '../src/Compiler';
7+
import Exception from '../src/Exception';
78

89
use(deepEqualInAnyOrder);
910
const cleanAST = (node: any) => {
@@ -36,4 +37,12 @@ describe('Schema Tree', () => {
3637
const final = JSON.parse(fs.readFileSync(`${__dirname}/fixtures/final.json`, 'utf8'));
3738
expect(Compiler.final(actual)).to.deep.equalInAnyOrder(final);
3839
});
40+
41+
42+
// Line 81 - 86
43+
it('Should throw an exception when there is an unexpected token at the end of the code', () => {
44+
const codeWithUnexpectedToken = 'enum TestEnum { VALUE1, VALUE2, } unexpectedToken';
45+
46+
expect(() => SchemaTree.parse(codeWithUnexpectedToken)).to.throw(Exception, /Unexpected token ,/);
47+
});
3948
});

packages/idea-parser/tests/TypeTree.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,37 @@ describe('Type Tree', () => {
3737
//console.log(JSON.stringify(actual, null, 2));
3838
expect(actual).to.deep.equalInAnyOrder(expected);
3939
});
40+
41+
42+
// Line 39 - 40
43+
it('Should correctly identify "typeword" token at the start of the input string', () => {
44+
const lexerMock = {
45+
define: (name: string, callback: Function) => {
46+
if (name === 'TypeWord') {
47+
const result = callback('type Example', 0);
48+
expect(result).to.deep.equal({
49+
type: '_TypeWord',
50+
start: 0,
51+
end: 4,
52+
value: 'type',
53+
raw: 'type'
54+
});
55+
}
56+
}
57+
};
58+
TypeTree.definitions(lexerMock as any);
59+
});
60+
61+
// Line 55
62+
it('Should throw an error when the input code is an empty string', () => {
63+
expect(() => {
64+
const lexerMock = {
65+
expect: (tokenType: string) => { throw new Error('Unexpected end of input'); },
66+
load: () => { }
67+
};
68+
const typeTree = new TypeTree();
69+
(typeTree as any)._lexer = lexerMock;
70+
typeTree.parse('');
71+
}).to.throw(Error, 'Unexpected end of input');
72+
});
4073
});

0 commit comments

Comments
 (0)