test: Add unit tests

This commit is contained in:
Sidharth Vinod 2023-09-14 23:00:59 +05:30
parent afd7cf51cf
commit 38d9c6d26b
No known key found for this signature in database
GPG Key ID: FB5CCD378D3907CD
2 changed files with 166 additions and 12 deletions

View File

@ -67,7 +67,9 @@ export const populate = ({ blocks }: { blocks: Block[] }) => {
}
if (start != lastByte + 1) {
throw new Error(
`Packet block ${start} - ${end} is not contiguous. It should start from ${lastByte + 1}.`
`Packet block ${start} - ${end ?? start} is not contiguous. It should start from ${
lastByte + 1
}.`
);
}
lastByte = end ?? start;

View File

@ -1,31 +1,183 @@
import { parser } from './parser.js';
import { clear, getPacket } from './db.js';
describe('packet diagrams', () => {
beforeEach(() => {
clear();
});
describe('info', () => {
it('should handle an info definition', () => {
const str = `info`;
it('should handle a packet-beta definition', () => {
const str = `packet-beta`;
expect(() => {
parser.parse(str);
}).not.toThrow();
expect(getPacket()).toMatchInlineSnapshot('[]');
});
it('should handle an info definition with showInfo', () => {
const str = `info showInfo`;
it('should handle diagram with data', () => {
const str = `packet-beta
0-10: "test"
`;
expect(() => {
parser.parse(str);
}).not.toThrow();
expect(getPacket()).toMatchInlineSnapshot(`
[
[
{
"end": 10,
"label": "test",
"start": 0,
},
],
]
`);
});
it('should throw because of unsupported info grammar', () => {
const str = `info unsupported`;
it('should handle single bits', () => {
const str = `packet-beta
0-10: "test"
11: "single"
`;
expect(() => {
parser.parse(str);
}).toThrow('Parsing failed: unexpected character: ->u<- at offset: 5, skipped 11 characters.');
}).not.toThrow();
expect(getPacket()).toMatchInlineSnapshot(`
[
[
{
"end": 10,
"label": "test",
"start": 0,
},
{
"end": 11,
"label": "single",
"start": 11,
},
],
]
`);
});
it('should throw because of unsupported info grammar', () => {
const str = `info unsupported`;
it('should split into multiple rows', () => {
const str = `packet-beta
0-10: "test"
11-90: "multiple"
`;
expect(() => {
parser.parse(str);
}).toThrow('Parsing failed: unexpected character: ->u<- at offset: 5, skipped 11 characters.');
}).not.toThrow();
expect(getPacket()).toMatchInlineSnapshot(`
[
[
{
"end": 10,
"label": "test",
"start": 0,
},
{
"end": 31,
"label": "multiple",
"start": 11,
},
],
[
{
"end": 63,
"label": "multiple",
"start": 32,
},
],
[
{
"end": 90,
"label": "multiple",
"start": 64,
},
],
]
`);
});
it('should split into multiple rows when cut at exact length', () => {
const str = `packet-beta
0-16: "test"
17-63: "multiple"
`;
expect(() => {
parser.parse(str);
}).not.toThrow();
expect(getPacket()).toMatchInlineSnapshot(`
[
[
{
"end": 16,
"label": "test",
"start": 0,
},
{
"end": 31,
"label": "multiple",
"start": 17,
},
],
[
{
"end": 63,
"label": "multiple",
"start": 32,
},
],
]
`);
});
it('should throw error if numbers are not continuous', () => {
const str = `packet-beta
0-16: "test"
18-20: "error"
`;
expect(() => {
parser.parse(str);
}).toThrowErrorMatchingInlineSnapshot(
'"Packet block 18 - 20 is not contiguous. It should start from 17."'
);
});
it('should throw error if numbers are not continuous for single packets', () => {
const str = `packet-beta
0-16: "test"
18: "error"
`;
expect(() => {
parser.parse(str);
}).toThrowErrorMatchingInlineSnapshot(
'"Packet block 18 - 18 is not contiguous. It should start from 17."'
);
});
it('should throw error if numbers are not continuous for single packets - 2', () => {
const str = `packet-beta
0-16: "test"
17: "good"
19: "error"
`;
expect(() => {
parser.parse(str);
}).toThrowErrorMatchingInlineSnapshot(
'"Packet block 19 - 19 is not contiguous. It should start from 18."'
);
});
it('should throw error if end is less than start', () => {
const str = `packet-beta
0-16: "test"
25-20: "error"
`;
expect(() => {
parser.parse(str);
}).toThrowErrorMatchingInlineSnapshot(
'"Packet block 25 - 20 is invalid. End must be greater than start."'
);
});
});