1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2024-07-18 02:20:12 +02:00
programming-challenges/cli/services/SolutionTestsResult.ts

97 lines
3.0 KiB
TypeScript
Raw Normal View History

import logSymbols from "log-symbols"
import chalk from "chalk"
import { table } from "table"
2022-09-22 16:16:21 +02:00
import type { Solution } from "./Solution.js"
import type { Test } from "./Test.js"
2022-09-22 16:16:21 +02:00
export interface SolutionTestsResultOptions {
tests: Test[]
solution: Solution
elapsedTimeMilliseconds: number
}
export interface SolutionTestsResultPrintOptions {
shouldPrintBenchmark?: boolean
shouldPrintTableResult?: boolean
}
export class SolutionTestsResult implements SolutionTestsResultOptions {
public tests: Test[] = []
public solution: Solution
public isSuccess: boolean
public elapsedTimeMilliseconds: number
public static readonly SUCCESS_MESSAGE = `${chalk.bold.green(
"Success:",
2022-09-22 16:16:21 +02:00
)} Tests passed! 🎉`
constructor(options: SolutionTestsResultOptions) {
this.tests = options.tests.sort((a, b) => {
return a.testNumber - b.testNumber
})
this.solution = options.solution
this.isSuccess = this.tests.every((test) => {
return test.isSuccess
})
this.elapsedTimeMilliseconds = options.elapsedTimeMilliseconds
}
public print(options: SolutionTestsResultPrintOptions = {}): void {
2023-01-10 23:15:36 +01:00
const { shouldPrintBenchmark = false, shouldPrintTableResult = false } =
options
2022-09-22 16:16:21 +02:00
const name = `${this.solution.challenge.name}/${this.solution.programmingLanguageName}/${this.solution.name}`
console.log(`${chalk.bold("Name:")} ${name}\n`)
2022-09-22 16:16:21 +02:00
const tableResult = [
[
chalk.bold("N°"),
chalk.bold("Input"),
chalk.bold("Expected"),
chalk.bold("Received"),
],
2022-09-22 16:16:21 +02:00
]
let totalFailedTests = 0
let totalCorrectTests = 0
for (const test of this.tests) {
const testLabel = `Test n°${test.testNumber}`
if (test.isSuccess) {
console.log(logSymbols.success, testLabel)
totalCorrectTests += 1
} else {
console.log(logSymbols.error, testLabel)
const expected = test.output.split("\n").join("\n")
const received = test.stdout.split("\n").join("\n")
2022-09-22 16:16:21 +02:00
tableResult.push([
test.testNumber.toString(),
`"${test.input}"`,
`"${expected}"`,
`"${received}"`,
2022-09-22 16:16:21 +02:00
])
totalFailedTests += 1
}
}
const isSuccess = totalCorrectTests === this.tests.length
console.log()
if (!isSuccess && shouldPrintTableResult) {
console.log(table(tableResult))
}
const testsResult = isSuccess
? chalk.bold.green(`${totalCorrectTests} passed`)
: chalk.bold.red(`${totalFailedTests} failed`)
console.log(
`${chalk.bold("Tests:")} ${testsResult}, ${this.tests.length} total`,
2022-09-22 16:16:21 +02:00
)
if (shouldPrintBenchmark) {
SolutionTestsResult.printBenchmark(this.elapsedTimeMilliseconds)
}
if (!isSuccess) {
throw new Error("Tests failed, try again!")
2022-09-22 16:16:21 +02:00
}
console.log("\n------------------------------\n")
2022-09-22 16:16:21 +02:00
}
public static printBenchmark(elapsedTimeMilliseconds: number): void {
const elapsedTime = elapsedTimeMilliseconds / 1000
console.log(`${chalk.bold("Benchmark:")} ${elapsedTime} seconds`)
2022-09-22 16:16:21 +02:00
}
}