mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-09-26 02:39:41 +02:00
test: Add unit tests
This commit is contained in:
@@ -67,7 +67,9 @@ export const populate = ({ blocks }: { blocks: Block[] }) => {
|
|||||||
}
|
}
|
||||||
if (start != lastByte + 1) {
|
if (start != lastByte + 1) {
|
||||||
throw new Error(
|
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;
|
lastByte = end ?? start;
|
||||||
|
@@ -1,31 +1,183 @@
|
|||||||
import { parser } from './parser.js';
|
import { parser } from './parser.js';
|
||||||
|
import { clear, getPacket } from './db.js';
|
||||||
|
describe('packet diagrams', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
clear();
|
||||||
|
});
|
||||||
|
|
||||||
describe('info', () => {
|
it('should handle a packet-beta definition', () => {
|
||||||
it('should handle an info definition', () => {
|
const str = `packet-beta`;
|
||||||
const str = `info`;
|
|
||||||
expect(() => {
|
expect(() => {
|
||||||
parser.parse(str);
|
parser.parse(str);
|
||||||
}).not.toThrow();
|
}).not.toThrow();
|
||||||
|
expect(getPacket()).toMatchInlineSnapshot('[]');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should handle an info definition with showInfo', () => {
|
it('should handle diagram with data', () => {
|
||||||
const str = `info showInfo`;
|
const str = `packet-beta
|
||||||
|
0-10: "test"
|
||||||
|
`;
|
||||||
expect(() => {
|
expect(() => {
|
||||||
parser.parse(str);
|
parser.parse(str);
|
||||||
}).not.toThrow();
|
}).not.toThrow();
|
||||||
|
expect(getPacket()).toMatchInlineSnapshot(`
|
||||||
|
[
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"end": 10,
|
||||||
|
"label": "test",
|
||||||
|
"start": 0,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
]
|
||||||
|
`);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should throw because of unsupported info grammar', () => {
|
it('should handle single bits', () => {
|
||||||
const str = `info unsupported`;
|
const str = `packet-beta
|
||||||
|
0-10: "test"
|
||||||
|
11: "single"
|
||||||
|
`;
|
||||||
expect(() => {
|
expect(() => {
|
||||||
parser.parse(str);
|
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', () => {
|
it('should split into multiple rows', () => {
|
||||||
const str = `info unsupported`;
|
const str = `packet-beta
|
||||||
|
0-10: "test"
|
||||||
|
11-90: "multiple"
|
||||||
|
`;
|
||||||
expect(() => {
|
expect(() => {
|
||||||
parser.parse(str);
|
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."'
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user