Skip to content

Commit 275b594

Browse files
Fix BigNumber deserialisation in Dish, and add tests (#2607)
With ideas from: cyphercodes <cyphercodes@users.noreply.github.com> With ideas from: Sivachandran Paramasivam <sivachandran.p@gmail.com>
1 parent 2329a0a commit 275b594

4 files changed

Lines changed: 55 additions & 7 deletions

File tree

src/core/Dish.mjs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -292,11 +292,7 @@ class Dish {
292292
and reinitialise it as a BigNumber object.
293293
*/
294294
if (Object.keys(this.value).sort().equals(["c", "e", "s"])) {
295-
const temp = new BigNumber();
296-
temp.c = this.value.c;
297-
temp.e = this.value.e;
298-
temp.s = this.value.s;
299-
this.value = temp;
295+
this.value = new BigNumber({ s: this.value.s, e: this.value.e, c: this.value.c, _isBigNumber: true});
300296
return true;
301297
}
302298
return false;

tests/browser/02_ops.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,8 @@ module.exports = {
346346
// testOp(browser, "Strip HTTP headers", "test input", "test_output");
347347
// testOp(browser, "Subsection", "test input", "test_output");
348348
// testOp(browser, "Substitute", "test input", "test_output");
349-
// testOp(browser, "Subtract", "test input", "test_output");
350-
// testOp(browser, "Sum", "test input", "test_output");
349+
testOp(browser, "Subtract", "321,123,test", "198", ["Comma"]);
350+
testOp(browser, "Sum", "321,123,test", "444", ["Comma"]);
351351
// testOp(browser, "Swap endianness", "test input", "test_output");
352352
// testOp(browser, "Symmetric Difference", "test input", "test_output");
353353
testOpHtml(browser, "Syntax highlighter", "var a = [4,5,6]", ".hljs-selector-attr", "[4,5,6]");

tests/node/tests/Dish.mjs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,23 @@ TestRegister.addApiTests([
99
assert(dish.presentAs);
1010
}),
1111

12+
it("Disk - should not error on serialized BigNumber (0)", () => {
13+
const dish = new Dish({ s: 1, e: 0, c: [0] }, Dish.BIG_NUMBER);
14+
assert.strictEqual(dish.value.toString(), "0");
15+
}),
16+
17+
it("Dish - should not error on serialized BigNumber (1)", () => {
18+
const dish = new Dish({ c: [1], e: 0, s: 1 }, Dish.BIG_NUMBER);
19+
assert.strictEqual(dish.value.toString(), "1");
20+
}),
21+
22+
it("Dish - should not error on serialized BigNumber (-100)", () => {
23+
const dish = new Dish({ s: -1, e: 2, c: [100] }, Dish.BIG_NUMBER);
24+
assert.strictEqual(dish.value.toString(), "-100");
25+
}),
26+
27+
it("Dish - should not error on serialized BigNumber (NaN)", () => {
28+
const dish = new Dish({ s: null, e: null, c: null }, Dish.BIG_NUMBER);
29+
assert.strictEqual(dish.value.toString(), "NaN");
30+
}),
1231
]);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Tests for arithmetical operations
3+
*
4+
* @copyright Crown Copyright 2026
5+
* @license Apache-2.0
6+
*/
7+
8+
import TestRegister from "../../lib/TestRegister.mjs";
9+
10+
TestRegister.addTests([
11+
{
12+
name: "Subtract",
13+
input: "321,123,test",
14+
expectedOutput: "198",
15+
recipeConfig: [
16+
{
17+
"op": "Subtract",
18+
"args": ["Comma"]
19+
},
20+
],
21+
},
22+
{
23+
name: "Subtract - no valid input",
24+
input: "test",
25+
expectedOutput: "NaN",
26+
recipeConfig: [
27+
{
28+
"op": "Subtract",
29+
"args": ["Comma"]
30+
},
31+
],
32+
},
33+
]);

0 commit comments

Comments
 (0)