1- import { ImportsArgument } from "../index.js" ;
2- import { AssertFailMessage , AssertMessage , IAssertResult } from "../interface.js" ;
1+ import { ImportsArgument , UnitTestFramework } from "../index.js" ;
2+ import { AssertFailMessage , AssertMessage , IAssertResult , FailedLogMessages } from "../interface.js" ;
33
4- export class ExecutionRecorder implements IAssertResult {
4+ class LogRecorder {
5+ #currentTestLogMessages: string [ ] = [ ] ;
6+ #isTestFailed: boolean = false ;
7+
8+ addLog ( msg : string ) : void {
9+ this . #currentTestLogMessages. push ( msg ) ;
10+ }
11+ markTestFailed ( ) : void {
12+ this . #isTestFailed = true ;
13+ }
14+
15+ onStartTest ( ) : void {
16+ this . #currentTestLogMessages = [ ] ;
17+ this . #isTestFailed = false ;
18+ }
19+ onFinishTest ( ) : string [ ] | null {
20+ if ( this . #currentTestLogMessages. length === 0 ) {
21+ return null ;
22+ }
23+ if ( this . #isTestFailed === false ) {
24+ return null ;
25+ }
26+ return this . #currentTestLogMessages;
27+ }
28+ }
29+
30+ // to do: split execution environment and recorder
31+ export class ExecutionRecorder implements IAssertResult , UnitTestFramework {
532 total : number = 0 ;
633 fail : number = 0 ;
734 failed_info : AssertFailMessage = { } ;
35+ failedLogMessages : FailedLogMessages = { } ;
36+
837 registerFunctions : [ string , number ] [ ] = [ ] ;
9- _currentTestDescriptions : string [ ] = [ ] ;
38+ #currentTestDescriptions: string [ ] = [ ] ;
39+ #logRecorder = new LogRecorder ( ) ;
40+
41+ get #currentTestDescription( ) : string {
42+ return this . #currentTestDescriptions. join ( " - " ) ;
43+ }
1044
1145 _addDescription ( description : string ) : void {
12- this . _currentTestDescriptions . push ( description ) ;
46+ this . #currentTestDescriptions . push ( description ) ;
1347 }
1448 _removeDescription ( ) : void {
15- this . _currentTestDescriptions . pop ( ) ;
49+ this . #currentTestDescriptions . pop ( ) ;
1650 }
1751 registerTestFunction ( fncIndex : number ) : void {
18- const testCaseFullName = this . _currentTestDescriptions . join ( " - " ) ;
19- this . registerFunctions . push ( [ testCaseFullName , fncIndex ] ) ;
52+ this . registerFunctions . push ( [ this . #currentTestDescription, fncIndex ] ) ;
53+ this . #logRecorder. onStartTest ( ) ;
54+ }
55+ _finishTestFunction ( ) : void {
56+ const logMessages : string [ ] | null = this . #logRecorder. onFinishTest ( ) ;
57+ if ( logMessages !== null ) {
58+ const testCaseFullName = this . #currentTestDescription;
59+ this . failedLogMessages [ testCaseFullName ] = ( this . failedLogMessages [ testCaseFullName ] || [ ] ) . concat ( logMessages ) ;
60+ }
2061 }
62+
2163 collectCheckResult ( result : boolean , codeInfoIndex : number , actualValue : string , expectValue : string ) : void {
2264 this . total ++ ;
2365 if ( ! result ) {
66+ this . #logRecorder. markTestFailed ( ) ;
2467 this . fail ++ ;
25- const testCaseFullName = this . _currentTestDescriptions . join ( " - " ) ;
68+ const testCaseFullName = this . #currentTestDescription ;
2669 const assertMessage : AssertMessage = [ codeInfoIndex . toString ( ) , actualValue , expectValue ] ;
2770 this . failed_info [ testCaseFullName ] = this . failed_info [ testCaseFullName ] || [ ] ;
2871 this . failed_info [ testCaseFullName ] . push ( assertMessage ) ;
2972 }
3073 }
3174
75+ log ( msg : string ) : void {
76+ this . #logRecorder. addLog ( msg ) ;
77+ }
78+
3279 getCollectionFuncSet ( arg : ImportsArgument ) : Record < string , Record < string , unknown > > {
3380 return {
3481 __unittest_framework_env : {
@@ -41,6 +88,9 @@ export class ExecutionRecorder implements IAssertResult {
4188 registerTestFunction : ( index : number ) : void => {
4289 this . registerTestFunction ( index ) ;
4390 } ,
91+ finishTestFunction : ( ) => {
92+ this . _finishTestFunction ( ) ;
93+ } ,
4494 collectCheckResult : ( result : number , codeInfoIndex : number , actualValue : number , expectValue : number ) : void => {
4595 this . collectCheckResult (
4696 result !== 0 ,
0 commit comments