Skip to content

Commit

Permalink
Merge pull request #193 from mohd-akram/fix-assert-method-def
Browse files Browse the repository at this point in the history
Fix assert as method definition
  • Loading branch information
keichi authored Nov 23, 2021
2 parents 524017b + d31536b commit b72ab14
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/binary_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,9 @@ export class Parser {
);
}
ctx.generateError(
`"Assertion error: ${varName} is " + ${this.options.assert}`
`"Assertion error: ${varName} is " + ${JSON.stringify(
this.options.assert.toString()
)}`
);
ctx.pushCode("}");
}
Expand Down
19 changes: 19 additions & 0 deletions test/composite_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1156,6 +1156,25 @@ function compositeParserTests(
throws(() => {
parser2.parse(buffer4);
});

const parser3 = new Parser()
.int16le("a")
.int16le("b")
.int16le("c", {
assert(this: { a: number; b: number }, x: number | string) {
return this.a + this.b === x;
},
});

const buffer5 = hexToBuf("d2042e16001b");
doesNotThrow(() => {
parser3.parse(buffer5);
});

const buffer6 = hexToBuf("2e16001bd204");
throws(() => {
parser3.parse(buffer6);
});
});
});

Expand Down

0 comments on commit b72ab14

Please sign in to comment.