Skip to content

Commit

Permalink
test: Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sidharthv96 committed Sep 14, 2023
1 parent afd7cf5 commit 38d9c6d
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 12 deletions.
4 changes: 3 additions & 1 deletion packages/mermaid/src/diagrams/packet/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
174 changes: 163 additions & 11 deletions packages/mermaid/src/diagrams/packet/packet.spec.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,183 @@
import { parser } from './parser.js';
import { clear, getPacket } from './db.js';
describe('packet diagrams', () => {
beforeEach(() => {
clear();
});

it('should handle a packet-beta definition', () => {
const str = `packet-beta`;
expect(() => {
parser.parse(str);
}).not.toThrow();
expect(getPacket()).toMatchInlineSnapshot('[]');
});

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 handle single bits', () => {
const str = `packet-beta
0-10: "test"
11: "single"
`;
expect(() => {
parser.parse(str);
}).not.toThrow();
expect(getPacket()).toMatchInlineSnapshot(`
[
[
{
"end": 10,
"label": "test",
"start": 0,
},
{
"end": 11,
"label": "single",
"start": 11,
},
],
]
`);
});

describe('info', () => {
it('should handle an info definition', () => {
const str = `info`;
it('should split into multiple rows', () => {
const str = `packet-beta
0-10: "test"
11-90: "multiple"
`;
expect(() => {
parser.parse(str);
}).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 handle an info definition with showInfo', () => {
const str = `info showInfo`;
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 because of unsupported info grammar', () => {
const str = `info unsupported`;
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);
}).toThrow('Parsing failed: unexpected character: ->u<- at offset: 5, skipped 11 characters.');
}).toThrowErrorMatchingInlineSnapshot(
'"Packet block 19 - 19 is not contiguous. It should start from 18."'
);
});

it('should throw because of unsupported info grammar', () => {
const str = `info unsupported`;
it('should throw error if end is less than start', () => {
const str = `packet-beta
0-16: "test"
25-20: "error"
`;
expect(() => {
parser.parse(str);
}).toThrow('Parsing failed: unexpected character: ->u<- at offset: 5, skipped 11 characters.');
}).toThrowErrorMatchingInlineSnapshot(
'"Packet block 25 - 20 is invalid. End must be greater than start."'
);
});
});

0 comments on commit 38d9c6d

Please sign in to comment.