From 52491478a442724c2083809c1a959a3d7f2d4f5c Mon Sep 17 00:00:00 2001 From: Boming Zhang Date: Wed, 11 Sep 2024 07:06:11 -0400 Subject: [PATCH] feat: diff parser with multiple outputs in one case --- examples/compile/error | 2 +- examples/compile/success | 2 +- examples/diff/basic | 2 +- examples/diff/complex | 2 +- internal/parsers/diff/parser.go | 57 ++++++++++++++++----------------- 5 files changed, 32 insertions(+), 33 deletions(-) diff --git a/examples/compile/error b/examples/compile/error index 2593e79..4e5fab9 160000 --- a/examples/compile/error +++ b/examples/compile/error @@ -1 +1 @@ -Subproject commit 2593e79505a93042d308c5fc355dba671dd4fdba +Subproject commit 4e5fab93e5a0ce67c8f40fef1e8f4cab7018fc5d diff --git a/examples/compile/success b/examples/compile/success index 638e9f6..1512cb5 160000 --- a/examples/compile/success +++ b/examples/compile/success @@ -1 +1 @@ -Subproject commit 638e9f661092d39daaf6e1ffc8ba5998fc56c96a +Subproject commit 1512cb5f20473a598d7504a08dacff3d6406b983 diff --git a/examples/diff/basic b/examples/diff/basic index d8d66fb..af99032 160000 --- a/examples/diff/basic +++ b/examples/diff/basic @@ -1 +1 @@ -Subproject commit d8d66fb5b47b5e79e08532da31d397a5d461f087 +Subproject commit af990327ab095c22a383448ad70d915f8d10490b diff --git a/examples/diff/complex b/examples/diff/complex index fc77411..ac7a2fc 160000 --- a/examples/diff/complex +++ b/examples/diff/complex @@ -1 +1 @@ -Subproject commit fc774118794a5c5ec0b88863ba6c8492e5b13f89 +Subproject commit ac7a2fc912fb51af156cd4babb7e72148ebe1c14 diff --git a/internal/parsers/diff/parser.go b/internal/parsers/diff/parser.go index 86fe863..ebcccb7 100644 --- a/internal/parsers/diff/parser.go +++ b/internal/parsers/diff/parser.go @@ -20,9 +20,12 @@ const ( type Conf struct { Cases []struct { - Score int - StdoutPath string - IgnoreWhitespace bool + Outputs []struct { + Score int + FileName string + AnswerPath string + IgnoreWhitespace bool + } } } @@ -43,35 +46,31 @@ func (*Diff) Run(results []stage.ExecutorResult, confAny any) ( for i, caseConf := range conf.Cases { result := results[i] score := 0 - var comment string + comment := "" + for _, output := range caseConf.Outputs { + answer, err := os.ReadFile(output.AnswerPath) + if err != nil { + return nil, true, err + } - stdout, err := os.ReadFile(caseConf.StdoutPath) - if err != nil { - return nil, true, err - } + // If no difference, assign score + if compareChars(string(answer), result.Files[output.FileName], output.IgnoreWhitespace) { + score += output.Score + } else { + // Convert answer to string and split by lines + stdoutLines := strings.Split(string(answer), "\n") + resultLines := strings.Split(result.Files[output.FileName], "\n") - comment = fmt.Sprintf( - "executor status: run time: %d ns, memory: %d bytes\n", - result.RunTime, result.Memory, - ) + // Generate Myers diff + diffOps := myersDiff(stdoutLines, resultLines) - // If no difference, assign score - if compareChars(string(stdout), result.Files["stdout"], caseConf.IgnoreWhitespace) { - score = caseConf.Score - } else { - // Convert stdout to string and split by lines - stdoutLines := strings.Split(string(stdout), "\n") - resultLines := strings.Split(result.Files["stdout"], "\n") - - // Generate Myers diff - diffOps := myersDiff(stdoutLines, resultLines) - - // Generate diff block with surrounding context - diffOutput := generateDiffWithContext(stdoutLines, resultLines, diffOps) - comment += fmt.Sprintf( - "difference found:\n```diff\n%s```", - diffOutput, - ) + // Generate diff block with surrounding context + diffOutput := generateDiffWithContext(stdoutLines, resultLines, diffOps) + comment += fmt.Sprintf( + "difference found in %s:\n```diff\n%s```\n", + output.FileName, diffOutput, + ) + } } res = append(res, stage.ParserResult{