Skip to content

Commit b72ab14

Browse files
authored
Merge pull request #193 from mohd-akram/fix-assert-method-def
Fix assert as method definition
2 parents 524017b + d31536b commit b72ab14

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

lib/binary_parser.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1011,7 +1011,9 @@ export class Parser {
10111011
);
10121012
}
10131013
ctx.generateError(
1014-
`"Assertion error: ${varName} is " + ${this.options.assert}`
1014+
`"Assertion error: ${varName} is " + ${JSON.stringify(
1015+
this.options.assert.toString()
1016+
)}`
10151017
);
10161018
ctx.pushCode("}");
10171019
}

test/composite_parser.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,6 +1156,25 @@ function compositeParserTests(
11561156
throws(() => {
11571157
parser2.parse(buffer4);
11581158
});
1159+
1160+
const parser3 = new Parser()
1161+
.int16le("a")
1162+
.int16le("b")
1163+
.int16le("c", {
1164+
assert(this: { a: number; b: number }, x: number | string) {
1165+
return this.a + this.b === x;
1166+
},
1167+
});
1168+
1169+
const buffer5 = hexToBuf("d2042e16001b");
1170+
doesNotThrow(() => {
1171+
parser3.parse(buffer5);
1172+
});
1173+
1174+
const buffer6 = hexToBuf("2e16001bd204");
1175+
throws(() => {
1176+
parser3.parse(buffer6);
1177+
});
11591178
});
11601179
});
11611180

0 commit comments

Comments
 (0)