Skip to content

Commit

Permalink
# living colors
Browse files Browse the repository at this point in the history
Signed-off-by: Theo Truong <[email protected]>
  • Loading branch information
nhtruong committed May 21, 2024
1 parent 4b0667f commit 7f6aa8b
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 4 deletions.
1 change: 0 additions & 1 deletion tools/src/tester/ChapterEvaluator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { type Chapter, type FullResponse } from './types/story.types'
import { type ChapterEvaluation, type Evaluation, Result } from './types/eval.types'
import { type ParsedOperation } from './types/spec.types'
import YAML from 'yaml'

export default class ChapterEvaluator {
chapter: Chapter
Expand Down
73 changes: 72 additions & 1 deletion tools/src/tester/ResultsDisplayer.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,84 @@
import { type StoryEvaluation } from './types/eval.types'
import { type Evaluation, Result, type StoryEvaluation } from './types/eval.types'

function b (text: string): string { return `\x1b[1m${text}\x1b[0m` }
function i (text: string): string { return `\x1b[3m${text}\x1b[0m` }

function padding (text: string, length: number, prefix: number = 0): string {
const spaces = length - text.length > 0 ? ' '.repeat(length - text.length) : ''
return `${' '.repeat(prefix)}${text}${spaces}`
}

function green (text: string): string { return `\x1b[32m${text}\x1b[0m` }
function red (text: string): string { return `\x1b[31m${text}\x1b[0m` }
function yellow (text: string): string { return `\x1b[33m${text}\x1b[0m` }
function cyan (text: string): string { return `\x1b[36m${text}\x1b[0m` }
function gray (text: string): string { return `\x1b[90m${text}\x1b[0m` }
function magenta (text: string): string { return `\x1b[35m${text}\x1b[0m` }

export default class ResultsDisplayer {
evaluation: StoryEvaluation
skip_components: boolean

constructor (evaluation: StoryEvaluation) {
this.evaluation = evaluation
this.skip_components = [Result.PASSED, Result.SKIPPED].includes(evaluation.result)
}

display (): void {
this.#display_story()
this.#display_prologues()
this.#display_epilogues()
this.#display_chapters()
}

#display_story (): void {
const result = this.#result(this.evaluation.result)
const title = cyan(b(this.evaluation.display_path))
console.log(`${result} ${title}`)
}

#display_chapters (): void {
const evaluations = this.evaluation.chapters
if (this.skip_components || evaluations == null) return

let result = Result.PASSED
if (evaluations.some(e => e.result === Result.ERROR)) result = Result.ERROR
else if (evaluations.some(e => e.result === Result.FAILED)) result = Result.FAILED
else if (evaluations.every(e => e.result === Result.SKIPPED)) result = Result.SKIPPED
this.#display_evaluation({ result }, 'CHAPTERS', 4)

for (const evaluation of evaluations) {
this.#display_evaluation(evaluation, i(evaluation.synopsis), 7)
}
}

#display_prologues (): void {
const evaluation = this.evaluation.prologues
if (this.skip_components || evaluation == null) return
this.#display_evaluation(evaluation, 'PROLOGUES', 4)
}

#display_epilogues (): void {
const evaluation = this.evaluation.epilogues
if (this.skip_components || evaluation == null) return
this.#display_evaluation(evaluation, 'EPILOGUES', 4)
}

#display_evaluation (evaluation: Evaluation, title: string, prefix: number = 0): void {
const result = padding(this.#result(evaluation.result), 0, prefix)
const message = evaluation.message != null ? `${gray('(' + evaluation.message + ')')}` : ''
console.log(`${result} ${title} ${message}`)
}

#result (r: Result): string {
const text = padding(r, 7)
switch (r) {
case Result.PASSED: return green(text)
case Result.FAILED: return magenta(text)
case Result.SKIPPED: return yellow(text)
case Result.ERROR: return red(text)
case Result.OK: return green(text)
default: return gray(text)
}
}
}
2 changes: 1 addition & 1 deletion tools/src/tester/StoryEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default class StoryEvaluator {
for (const chapter of chapters) {
const evaluator = new ChapterEvaluator(chapter)
const evaluation = await evaluator.evaluate(skipped)
skipped = skipped || evaluation.result !== Result.PASSED
skipped = skipped || evaluation.result === Result.ERROR
if (evaluation.result !== Result.PASSED) this.result = Result.FAILED
evaluations.push(evaluation)
}
Expand Down
3 changes: 2 additions & 1 deletion tools/src/tester/types/eval.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface StoryEvaluation {
display_path: string
full_path: string
description: string
message?: string
chapters?: ChapterEvaluation[]
epilogues?: Evaluation
prologues?: Evaluation
Expand Down Expand Up @@ -34,5 +35,5 @@ export enum Result {
FAILED = 'FAILED',
SKIPPED = 'SKIPPED',
ERROR = 'ERROR',
OK = 'OK'
OK = 'OK',
}

0 comments on commit 7f6aa8b

Please sign in to comment.