-
-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
afd7cf5
commit 38d9c6d
Showing
2 changed files
with
166 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."' | ||
); | ||
}); | ||
}); |