mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-23 08:16:17 -04:00
+ Adding unit tests and fix for empty value
This commit is contained in:
parent
5f40ee8076
commit
1b9f0cda98
2 changed files with 81 additions and 6 deletions
|
@ -4,6 +4,7 @@
|
||||||
* @license Apache-2.0
|
* @license Apache-2.0
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import BigNumber from "bignumber.js";
|
||||||
import Operation from "../Operation.mjs";
|
import Operation from "../Operation.mjs";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -31,13 +32,19 @@ class BasicArithmetic extends Operation {
|
||||||
* @returns {number}
|
* @returns {number}
|
||||||
*/
|
*/
|
||||||
run(input, args) {
|
run(input, args) {
|
||||||
if (parseInt(input, 10).toString().length === input.length) {
|
if (input.length >= 1) {
|
||||||
return parseInt(input, 10);
|
if (parseInt(input, 10).toString().length === input.length) {
|
||||||
|
const val = parseInt(input, 10);
|
||||||
|
return BigNumber.isBigNumber(val) ? val : new BigNumber(NaN);
|
||||||
|
} else {
|
||||||
|
return (input.replace(/\s/g, "").match(/[+-]?([0-9.]+)/g) || [])
|
||||||
|
.reduce(function (sum, value) {
|
||||||
|
const val = parseFloat(sum) + parseFloat(value);
|
||||||
|
return BigNumber.isBigNumber(val) ? val : new BigNumber(NaN);
|
||||||
|
});
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
return (input.replace(/\s/g, "").match(/[+-]?([0-9.]+)/g) || [])
|
return new BigNumber(NaN);
|
||||||
.reduce(function (sum, value) {
|
|
||||||
return parseFloat(sum) + parseFloat(value);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
68
tests/operations/tests/BasicArithmetic.mjs
Normal file
68
tests/operations/tests/BasicArithmetic.mjs
Normal file
|
@ -0,0 +1,68 @@
|
||||||
|
/**
|
||||||
|
* @author scottdermott [scottdermott@outlook.com]
|
||||||
|
* @copyright Crown Copyright 2021
|
||||||
|
* @license Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Basic Arithmetic Tests
|
||||||
|
*/
|
||||||
|
import TestRegister from "../../lib/TestRegister.mjs";
|
||||||
|
|
||||||
|
TestRegister.addTests([
|
||||||
|
{
|
||||||
|
name: "BasicArithmetic: nothing",
|
||||||
|
input: "",
|
||||||
|
expectedOutput: "",
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "BasicArithmetic",
|
||||||
|
args: [],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "BasicArithmetic: Addition",
|
||||||
|
input: "1+2+3+4+5+6+7+8+9+0",
|
||||||
|
expectedOutput: 45,
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "BasicArithmetic",
|
||||||
|
args: [],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "BasicArithmetic: Subtraction",
|
||||||
|
input: "100-9-8-7-6-5-4-3-2-1-0",
|
||||||
|
expectedOutput: 55,
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "BasicArithmetic",
|
||||||
|
args: [],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "BasicArithmetic: Add + Sub",
|
||||||
|
input: "1+2+3+4+5+6+7+8+9-9-8-7-6-5-4-3-2-1",
|
||||||
|
expectedOutput: 0,
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "BasicArithmetic",
|
||||||
|
args: [],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "BasicArithmetic: Large number",
|
||||||
|
input: "999+999+999+999+999+999+999+999+999+999+999+999+999+999+999+999+999+999+999+999+999+999+999",
|
||||||
|
expectedOutput: 22977,
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "BasicArithmetic",
|
||||||
|
args: [],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
]);
|
Loading…
Add table
Add a link
Reference in a new issue