feat(parser/diff)!: use compare space
All checks were successful
build / build (push) Successful in 2m1s
build / trigger-build-image (push) Successful in 7s

This commit is contained in:
张泊明518370910136 2024-10-11 00:16:39 -04:00
parent 3882a311e7
commit 8c5c8bf35b
GPG Key ID: D47306D7062CDA9D

View File

@ -24,11 +24,11 @@ type Conf struct {
Cases []struct { Cases []struct {
IgnoreResultStatus bool IgnoreResultStatus bool
Outputs []struct { Outputs []struct {
Score int Score int
FileName string FileName string
AnswerPath string AnswerPath string
IgnoreWhitespace bool `default:"true"` CompareSpace bool
AlwaysHide bool AlwaysHide bool
} }
} }
} }
@ -70,7 +70,7 @@ func (*Diff) Run(results []stage.ExecutorResult, confAny any) (
"answer", string(answer)) "answer", string(answer))
// If no difference, assign score // If no difference, assign score
if compareChars(string(answer), result.Files[output.FileName], if compareChars(string(answer), result.Files[output.FileName],
output.IgnoreWhitespace) { output.CompareSpace) {
score += output.Score score += output.Score
} else { } else {
comment += fmt.Sprintf("Difference found in `%s`.\n", comment += fmt.Sprintf("Difference found in `%s`.\n",
@ -106,16 +106,16 @@ func (*Diff) Run(results []stage.ExecutorResult, confAny any) (
} }
// compareChars compares two strings character by character, optionally ignoring whitespace. // compareChars compares two strings character by character, optionally ignoring whitespace.
func compareChars(stdout, result string, ignoreWhitespace bool) bool { func compareChars(stdout, result string, compareSpace bool) bool {
if ignoreWhitespace { if !compareSpace {
stdout = removeWhitespace(stdout) stdout = removeSpace(stdout)
result = removeWhitespace(result) result = removeSpace(result)
} }
return stdout == result return stdout == result
} }
// removeWhitespace removes all whitespace characters from the string. // removeSpace removes all whitespace characters from the string.
func removeWhitespace(s string) string { func removeSpace(s string) string {
var b strings.Builder var b strings.Builder
for _, r := range s { for _, r := range s {
if !unicode.IsSpace(r) { if !unicode.IsSpace(r) {