From 38d9c6d26b71cb82ad364f3a5d30b8477487ba61 Mon Sep 17 00:00:00 2001 From: Sidharth Vinod Date: Thu, 14 Sep 2023 23:00:59 +0530 Subject: [PATCH] test: Add unit tests --- packages/mermaid/src/diagrams/packet/db.ts | 4 +- .../src/diagrams/packet/packet.spec.ts | 174 ++++++++++++++++-- 2 files changed, 166 insertions(+), 12 deletions(-) diff --git a/packages/mermaid/src/diagrams/packet/db.ts b/packages/mermaid/src/diagrams/packet/db.ts index 0773a2c0e..b964b88dc 100644 --- a/packages/mermaid/src/diagrams/packet/db.ts +++ b/packages/mermaid/src/diagrams/packet/db.ts @@ -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; diff --git a/packages/mermaid/src/diagrams/packet/packet.spec.ts b/packages/mermaid/src/diagrams/packet/packet.spec.ts index ea115e02e..c4ccdba74 100644 --- a/packages/mermaid/src/diagrams/packet/packet.spec.ts +++ b/packages/mermaid/src/diagrams/packet/packet.spec.ts @@ -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."' + ); }); });