-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(report): add line number reporter for assertion
- Loading branch information
1 parent
2a5029c
commit eb9f47b
Showing
6 changed files
with
193 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# report | ||
|
||
assert result reporter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright 2021-Present the Unitest authors. All rights reserved. MIT license. | ||
import { brightBlack, red } from "../deps.ts"; | ||
|
||
type CodeInfo = { | ||
code: string; | ||
lineNumber: number; | ||
column: number; | ||
}; | ||
|
||
// /** insert something to beginning of line */ | ||
// function BOL(text: string, symbol: string): string { | ||
// return `${symbol}${text.replaceAll("\n", `\n${symbol}`)}`; | ||
// } | ||
|
||
function prettyCode({ code, lineNumber }: CodeInfo): string { | ||
const codes = code.split("\n"); | ||
|
||
const sliced = codes.slice( | ||
lineNumber - 2, | ||
lineNumber + 2, | ||
); | ||
|
||
const numberedCodes = sliced.map((fragment, i) => { | ||
const number = i + lineNumber - 1; | ||
const isWrongLine = number === lineNumber; | ||
|
||
return `${isWrongLine ? red(">") : " "} ${brightBlack(String(number))} ${ | ||
brightBlack("|") | ||
} ${isWrongLine ? fragment : brightBlack(fragment)}`; | ||
}); | ||
|
||
return numberedCodes.join("\n"); | ||
} | ||
|
||
export { prettyCode }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2021-Present the Unitest authors. All rights reserved. MIT license. | ||
type StackTrace = { | ||
file: string; | ||
methodName: string; | ||
lineNumber: number; | ||
column: number; | ||
}; | ||
|
||
function parseStackTraceLine(line: string): StackTrace | null { | ||
const re = | ||
/^\s*at (?<methodName>\S+) \(?(?<file>\S+):(?<lineNumber>\d+):(?<column>\d+)\)?.*$/i; | ||
|
||
const result = re.exec(line); | ||
if (!result || !result.groups) return null; | ||
const { file, methodName, lineNumber, column } = result.groups; | ||
|
||
return { | ||
file, | ||
methodName, | ||
lineNumber: Number(lineNumber), | ||
column: Number(column), | ||
}; | ||
} | ||
|
||
function parseStackTrace(stack: string): StackTrace[] { | ||
const lines = stack.split("\n"); | ||
|
||
const traces = lines.map((line) => parseStackTraceLine(line)); | ||
|
||
return traces.filter(Boolean) as StackTrace[]; | ||
} | ||
|
||
export { parseStackTrace, parseStackTraceLine }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright 2021-Present the Unitest authors. All rights reserved. MIT license. | ||
import { assertEquals } from "../dev_deps.ts"; | ||
import { parseStackTrace, parseStackTraceLine } from "./stack_trace.ts"; | ||
|
||
Deno.test({ | ||
name: "parseStackTraceLine", | ||
fn: () => { | ||
const table: [ | ||
...Parameters<typeof parseStackTraceLine>, | ||
ReturnType<typeof parseStackTraceLine>, | ||
][] = [ | ||
[ | ||
` at main (file:///path/to/file.ts:23:11)`, | ||
{ | ||
file: "file:///path/to/file.ts", | ||
methodName: "main", | ||
lineNumber: 23, | ||
column: 11, | ||
}, | ||
], | ||
[ | ||
` at assertEquals (https://deno.land/std@0.115.1/testing/asserts.ts:266:9)`, | ||
{ | ||
file: "https://deno.land/std@0.115.1/testing/asserts.ts", | ||
methodName: "assertEquals", | ||
lineNumber: 266, | ||
column: 9, | ||
}, | ||
], | ||
|
||
[ | ||
` at assertEquals https://deno.land/std@0.115.1/testing/asserts.ts:266:9`, | ||
{ | ||
file: "https://deno.land/std@0.115.1/testing/asserts.ts", | ||
methodName: "assertEquals", | ||
lineNumber: 266, | ||
column: 9, | ||
}, | ||
], | ||
|
||
[ | ||
` at Array.forEach (<anonymous>)`, | ||
null, | ||
], | ||
]; | ||
|
||
table.forEach(([value, result]) => { | ||
assertEquals(parseStackTraceLine(value), result); | ||
}); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "trace", | ||
fn: () => { | ||
const table: [ | ||
...Parameters<typeof parseStackTrace>, | ||
ReturnType<typeof parseStackTrace>, | ||
][] = [ | ||
[ | ||
`ReferenceError: FAIL is not defined | ||
at Constraint.execute (deltablue.js:525:2) | ||
at Constraint.execute anonymous | ||
at Constraint.recalculate (deltablue.js:424:21)`, | ||
[ | ||
{ | ||
file: "deltablue.js", | ||
methodName: "Constraint.execute", | ||
lineNumber: 525, | ||
column: 2, | ||
}, | ||
{ | ||
file: "deltablue.js", | ||
methodName: "Constraint.recalculate", | ||
lineNumber: 424, | ||
column: 21, | ||
}, | ||
], | ||
], | ||
]; | ||
|
||
table.forEach(([value, result]) => { | ||
assertEquals(parseStackTrace(value), result); | ||
}); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters