baluchan / sudoku-github

0 stars 0 forks source link

tests #3

Open Puckoland opened 1 month ago

Puckoland commented 1 month ago

Add some simple tests for the createArray function. For this reason, it would be better to pass the input string to it as a parameter instead of getting it inside (createArray(input: string)). And return the sudoku: number[][] instead of just filling the global variable.

Then, just create a test that passes the string to the function and check that it returns the right value. Somthing like:

const input: string = '23512421....';
const expected: number[][] = [
[2, 3, 5, ...],
...
];
const actual: number[][] = createArray(input);
if (JSON.stringify(expected) == JSON.stringify(actual)) { // stringify used because array in JS are references == would not actually look into values inside the array
  throw new Error('Unexpected array returned' + JSON.stringify(actual));
}
Puckoland commented 1 month ago

Think about how you can see why global parameters are "bad". That just simple testing is not possible (or it would be very complicated) when they are used.