Comparing version 1.5.0 to 1.6.0
@@ -37,3 +37,3 @@ var __defProp = Object.defineProperty; | ||
"@types/node": "^16.11.6", | ||
aocrunner: "^1.5.0", | ||
aocrunner: "^1.6.0", | ||
prettier: "^2.4.1" | ||
@@ -40,0 +40,0 @@ }, |
import { stripIndents } from "common-tags"; | ||
import toFixed from "../utils/toFixed.js"; | ||
const renderDayBadges = (config) => { | ||
@@ -32,4 +33,4 @@ return config.days.map(({ part1, part2 }, index) => { | ||
Day ${day} | ||
Time part 1: ${part1.time !== null && part1.solved ? part1.time + "ms" : "-"} | ||
Time part 2: ${part2.time !== null && part2.solved ? part2.time + "ms" : "-"} | ||
Time part 1: ${part1.time !== null && part1.solved ? toFixed(part1.time) + "ms" : "-"} | ||
Time part 2: ${part2.time !== null && part2.solved ? toFixed(part2.time) + "ms" : "-"} | ||
Both parts: ${timeBoth !== 0 ? timeBoth + "ms" : "-"} | ||
@@ -42,3 +43,3 @@ \`\`\` | ||
Total stars: ${totalStars}/50 | ||
Total time: ${totalTime}ms | ||
Total time: ${toFixed(totalTime)}ms | ||
\`\`\` | ||
@@ -45,0 +46,0 @@ `; |
declare type Tests = { | ||
name?: string; | ||
input: string; | ||
@@ -3,0 +4,0 @@ expected: string | number | bigint | void; |
@@ -8,11 +8,13 @@ import fs from "fs"; | ||
import { saveConfig, readConfig } from "./io/config.js"; | ||
import toFixed from "./utils/toFixed.js"; | ||
const runTests = async (tests, solution, part, trimTestInputs = true) => { | ||
for (let i = 0; i < tests.length; i++) { | ||
const { input, expected } = tests[i]; | ||
const { name, input, expected } = tests[i]; | ||
const data = trimTestInputs ? stripIndent(input) : input; | ||
const result = await solution(data); | ||
const testName = `Part ${part}, test ${i + 1}${name ? `, ${name}` : ""}`; | ||
if (result === expected) { | ||
console.log(kleur.green(`Part ${part}, test ${i + 1} - passed`)); | ||
console.log(kleur.green(`${testName} - passed`)); | ||
} else { | ||
console.log(kleur.red(`Part ${part}, test ${i + 1} - failed`)); | ||
console.log(kleur.red(`${testName} - failed`)); | ||
console.log(` | ||
@@ -32,3 +34,3 @@ Result:`); | ||
const t1 = process.hrtime.bigint(); | ||
const time = (Number(t1 - t0) / 1e6).toFixed(2); | ||
const time = Number(t1 - t0) / 1e6; | ||
if (!["string", "number", "bigint", "undefined"].includes(typeof result)) { | ||
@@ -38,5 +40,5 @@ console.log(kleur.yellow(`Warning - the result type of part ${part} should be a string, a number or a bigint, got:`), kleur.red(typeof result)); | ||
console.log(` | ||
Part ${part} (in ${time}ms):`); | ||
Part ${part} (in ${toFixed(time)}ms):`); | ||
console.dir(result); | ||
return { result, time: Number(time) }; | ||
return { result, time }; | ||
}; | ||
@@ -68,3 +70,3 @@ const runAsync = async (solutions, inputFile, day) => { | ||
console.log(` | ||
Total time: ${totalTime.toFixed(2)}ms`); | ||
Total time: ${toFixed(totalTime)}ms`); | ||
config.days[day - 1].part1.result = (output1 == null ? void 0 : output1.result) === void 0 ? null : String(output1.result); | ||
@@ -71,0 +73,0 @@ config.days[day - 1].part1.time = (output1 == null ? void 0 : output1.result) === void 0 ? null : output1.time; |
{ | ||
"name": "aocrunner", | ||
"version": "1.5.0", | ||
"version": "1.6.0", | ||
"repository": "https://github.com/caderek/aocrunner", | ||
@@ -5,0 +5,0 @@ "description": "Advent of Code runner", |
@@ -124,5 +124,5 @@ # AoC Runner | ||
- `tests` accepts an array of tests (you can add as many as you want) | ||
- `tests` key accepts an array of tests (you can add as many as you want), each test takes `input` and `expected` result, you can also provide an optional `name` for each test that will be displayed when the tests are executed. | ||
- `solution` accepts a function that takes the raw input as an argument and returns the solution as `string` / `number` / `bigint` (return value will be converted to string before sending a solution) | ||
- `solution` key accepts a function that takes the raw input as an argument and returns the solution as `string` / `number` / `bigint` (return value will be converted to string before sending a solution) | ||
@@ -267,4 +267,31 @@ ### `trimTestInputs` | ||
Example: | ||
```js | ||
run({ | ||
part1: { | ||
tests: [ | ||
{ | ||
input: `some test input`, | ||
expected: "some result", | ||
}, | ||
], | ||
solution: part1, | ||
}, | ||
part2: { | ||
tests: [ | ||
{ | ||
input: `some test input`, | ||
expected: "some result", | ||
}, | ||
], | ||
solution: part2, | ||
}, | ||
trimTestInputs: true, | ||
onlyTests: true, // <- Yay! Comment this line to quickly switch mode. | ||
}) | ||
``` | ||
## License | ||
Project is under open, non-restrictive [ISC license](LICENSE.md). |
@@ -21,3 +21,3 @@ import type { Setup } from "../types/common" | ||
"@types/node": "^16.11.6", | ||
aocrunner: "^1.5.0", | ||
aocrunner: "^1.6.0", | ||
prettier: "^2.4.1", | ||
@@ -24,0 +24,0 @@ }, |
import type { Setup, Config } from "../types/common" | ||
import { stripIndents } from "common-tags" | ||
import toFixed from "../utils/toFixed.js" | ||
@@ -56,6 +57,6 @@ const renderDayBadges = (config: Config) => { | ||
Time part 1: ${ | ||
part1.time !== null && part1.solved ? part1.time + "ms" : "-" | ||
part1.time !== null && part1.solved ? toFixed(part1.time) + "ms" : "-" | ||
} | ||
Time part 2: ${ | ||
part2.time !== null && part2.solved ? part2.time + "ms" : "-" | ||
part2.time !== null && part2.solved ? toFixed(part2.time) + "ms" : "-" | ||
} | ||
@@ -71,3 +72,3 @@ Both parts: ${timeBoth !== 0 ? timeBoth + "ms" : "-"} | ||
Total stars: ${totalStars}/50 | ||
Total time: ${totalTime}ms | ||
Total time: ${toFixed(totalTime)}ms | ||
\`\`\` | ||
@@ -74,0 +75,0 @@ ` |
@@ -8,4 +8,10 @@ import fs from "fs" | ||
import { saveConfig, readConfig } from "./io/config.js" | ||
import toFixed from "./utils/toFixed.js" | ||
type Tests = { input: string; expected: string | number | bigint | void }[] | ||
type Tests = { | ||
name?: string | ||
input: string | ||
expected: string | number | bigint | void | ||
}[] | ||
type Solution = (input: string) => string | number | bigint | void | ||
@@ -33,3 +39,3 @@ | ||
for (let i = 0; i < tests.length; i++) { | ||
const { input, expected } = tests[i] | ||
const { name, input, expected } = tests[i] | ||
@@ -40,6 +46,8 @@ const data = trimTestInputs ? stripIndent(input) : input | ||
const testName = `Part ${part}, test ${i + 1}${name ? `, ${name}` : ""}` | ||
if (result === expected) { | ||
console.log(kleur.green(`Part ${part}, test ${i + 1} - passed`)) | ||
console.log(kleur.green(`${testName} - passed`)) | ||
} else { | ||
console.log(kleur.red(`Part ${part}, test ${i + 1} - failed`)) | ||
console.log(kleur.red(`${testName} - failed`)) | ||
console.log(`\nResult:`) | ||
@@ -58,3 +66,3 @@ console.dir(result) | ||
const t1 = process.hrtime.bigint() | ||
const time = (Number(t1 - t0) / 1e6).toFixed(2) | ||
const time = Number(t1 - t0) / 1e6 | ||
@@ -70,6 +78,6 @@ if (!["string", "number", "bigint", "undefined"].includes(typeof result)) { | ||
console.log(`\nPart ${part} (in ${time}ms):`) | ||
console.log(`\nPart ${part} (in ${toFixed(time)}ms):`) | ||
console.dir(result) | ||
return { result, time: Number(time) } | ||
return { result, time } | ||
} | ||
@@ -122,3 +130,3 @@ | ||
console.log(`\nTotal time: ${totalTime.toFixed(2)}ms`) | ||
console.log(`\nTotal time: ${toFixed(totalTime)}ms`) | ||
@@ -125,0 +133,0 @@ config.days[day - 1].part1.result = |
1280629
98
2607
296