diff --git a/server/src/score-import/framework/core/string-asserts.test.ts b/server/src/score-import/framework/core/string-asserts.test.ts new file mode 100644 index 000000000..db75d5fc4 --- /dev/null +++ b/server/src/score-import/framework/core/string-asserts.test.ts @@ -0,0 +1,179 @@ +import t from "tap"; +import { InvalidScoreFailure } from "./converter-failures"; +import { AssertStrAsPositiveInt, AssertStrAsPositiveNonZeroInt } from "./string-asserts"; + +function astr(v: string) { + try { + let r = AssertStrAsPositiveInt(v, "err"); + + return r; + } catch (e) { + return e; + } +} + +function astrp(v: string) { + try { + let r = AssertStrAsPositiveNonZeroInt(v, "err"); + + return r; + } catch (e) { + return e; + } +} + +t.test("#AssertStrAsPositiveInt", (t) => { + t.strictSame( + astr("---"), + new InvalidScoreFailure(`err (Not an integer.)`), + "Should reject non-int input" + ); + + t.strictSame( + astr("1.4"), + new InvalidScoreFailure(`err (Not an integer.)`), + "Should reject float input" + ); + + t.strictSame( + astr("NaN"), + new InvalidScoreFailure(`err (Not an integer.)`), + "Should reject NaN" + ); + t.strictSame( + astr("-0.3"), + new InvalidScoreFailure(`err (Not an integer.)`), + "Should reject negative float input" + ); + + t.strictSame( + astr("0xFF"), + new InvalidScoreFailure(`err (Not an integer.)`), + "Should reject hex input" + ); + + t.strictSame( + astr("0b11"), + new InvalidScoreFailure(`err (Not an integer.)`), + "Should reject binary input" + ); + + t.strictSame( + astr("0o77"), + new InvalidScoreFailure(`err (Not an integer.)`), + "Should reject oct input" + ); + + t.strictSame( + astr("--1"), + new InvalidScoreFailure(`err (Not an integer.)`), + "Should reject double neg input" + ); + + t.strictSame( + astr("12f"), + new InvalidScoreFailure(`err (Not an integer.)`), + "Should reject valid parseInt but invalid Number() input" + ); + + t.strictSame( + astr(`${Number.MAX_SAFE_INTEGER.toString()}000`), + new InvalidScoreFailure(`err (Not an integer.)`), + "Should reject numbers > max_safe_integer." + ); + + t.strictSame( + astr("-1"), + new InvalidScoreFailure(`err (Was negative.)`), + "Should reject negative integer input" + ); + + t.strictSame(astr("-0"), 0, "Should allow negative 0 input"); + t.strictSame(astr("0"), 0, "Should allow 0 input"); + t.strictSame(astr("13"), 13, "Should allow valid int input"); + + t.end(); +}); + +t.test("#AssertStrAsPositiveNonZeroInt", (t) => { + t.strictSame( + astrp("---"), + new InvalidScoreFailure(`err (Not an integer.)`), + "Should reject non-int input" + ); + + t.strictSame( + astrp("1.4"), + new InvalidScoreFailure(`err (Not an integer.)`), + "Should reject float input" + ); + + t.strictSame( + astrp("NaN"), + new InvalidScoreFailure(`err (Not an integer.)`), + "Should reject NaN" + ); + t.strictSame( + astrp("-0.3"), + new InvalidScoreFailure(`err (Not an integer.)`), + "Should reject negative float input" + ); + + t.strictSame( + astrp("0xFF"), + new InvalidScoreFailure(`err (Not an integer.)`), + "Should reject hex input" + ); + + t.strictSame( + astrp("0b11"), + new InvalidScoreFailure(`err (Not an integer.)`), + "Should reject binary input" + ); + + t.strictSame( + astrp("0o77"), + new InvalidScoreFailure(`err (Not an integer.)`), + "Should reject oct input" + ); + + t.strictSame( + astrp("--1"), + new InvalidScoreFailure(`err (Not an integer.)`), + "Should reject double neg input" + ); + + t.strictSame( + astrp("12f"), + new InvalidScoreFailure(`err (Not an integer.)`), + "Should reject valid parseInt but invalid Number() input" + ); + + t.strictSame( + astrp(`${Number.MAX_SAFE_INTEGER.toString()}000`), + new InvalidScoreFailure(`err (Not an integer.)`), + "Should reject numbers > max_safe_integer." + ); + + t.strictSame( + astrp("-1"), + new InvalidScoreFailure(`err (Was negative or zero.)`), + "Should reject negative integer input" + ); + + t.strictSame( + astrp("-0"), + new InvalidScoreFailure(`err (Was negative or zero.)`), + "Should reject negative 0 input" + ); + + t.strictSame( + astrp("0"), + new InvalidScoreFailure(`err (Was negative or zero.)`), + "Should reject 0 input" + ); + + t.strictSame(astrp("13"), 13, "Should allow valid int input"); + + t.end(); +}); diff --git a/server/src/score-import/framework/core/string-asserts.ts b/server/src/score-import/framework/core/string-asserts.ts index 6bff85fcc..fa9f3a8a1 100644 --- a/server/src/score-import/framework/core/string-asserts.ts +++ b/server/src/score-import/framework/core/string-asserts.ts @@ -11,12 +11,10 @@ export function AssertStrAsPositiveInt(strVal: string, errorMessage: string) { let val = Number(strVal); - if (Number.isNaN(val)) { - throw new InvalidScoreFailure(`${errorMessage} (Not a number.)`); - } else if (!Number.isSafeInteger(val)) { + if (!Number.isSafeInteger(val)) { throw new InvalidScoreFailure(`${errorMessage} (Not an integer.)`); } else if (val < 0) { - throw new InvalidScoreFailure(`${errorMessage} (Was Negative).`); + throw new InvalidScoreFailure(`${errorMessage} (Was negative.)`); } return val; @@ -31,12 +29,10 @@ export function AssertStrAsPositiveNonZeroInt(strVal: string, errorMessage: stri let val = Number(strVal); - if (Number.isNaN(val)) { - throw new InvalidScoreFailure(`${errorMessage} (Not a number.)`); - } else if (!Number.isSafeInteger(val)) { + if (!Number.isSafeInteger(val)) { throw new InvalidScoreFailure(`${errorMessage} (Not an integer.)`); } else if (val <= 0) { - throw new InvalidScoreFailure(`${errorMessage} (Was Negative).`); + throw new InvalidScoreFailure(`${errorMessage} (Was negative or zero.)`); } return val;