Skip to content

Commit 2f6f8a6

Browse files
authored
Merge pull request #82 from codecrafters-io/ryan-gang-patch-1
fix: update regex replacer to keep Newline
2 parents a18bd54 + f795d64 commit 2f6f8a6

File tree

5 files changed

+44
-12
lines changed

5 files changed

+44
-12
lines changed

internal/stage10.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ func testWhitespace(stageHarness *test_case_harness.TestCaseHarness) error {
2222
ws4 := "{" + strings.Join(random.RandomElementsFromArray(WHITESPACES, 5), "") + "}" + LF + "((" + strings.Join(random.RandomElementsFromArray(slices.Concat(SINGLE_CHAR_OPERATORS, RELATIONALS, WHITESPACES), 5), "") + "))"
2323
tokenizeTestCases := testcases.MultiTestCase{
2424
TestCases: []testcases.TestCase{
25-
&testcases.TokenizeTestCase{FileContents: ws1, ExpectsError: false},
26-
&testcases.TokenizeTestCase{FileContents: ws2, ExpectsError: false},
27-
&testcases.TokenizeTestCase{FileContents: ws3, ExpectsError: false},
28-
&testcases.TokenizeTestCase{FileContents: ws4, ExpectsError: false},
25+
&testcases.TokenizeTestCase{FileContents: ws1, ExpectsError: false, HighlightWhitespaceWhileLogging: true},
26+
&testcases.TokenizeTestCase{FileContents: ws2, ExpectsError: false, HighlightWhitespaceWhileLogging: true},
27+
&testcases.TokenizeTestCase{FileContents: ws3, ExpectsError: false, HighlightWhitespaceWhileLogging: true},
28+
&testcases.TokenizeTestCase{FileContents: ws4, ExpectsError: false, HighlightWhitespaceWhileLogging: true},
2929
},
3030
}
3131
return tokenizeTestCases.RunAll(b, logger)

internal/stage11.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func testErrorsMulti(stageHarness *test_case_harness.TestCaseHarness) error {
1616

1717
logger := stageHarness.Logger
1818

19-
multiLineErrors1 := `()
19+
multiLineErrors1 := `()
2020
@`
2121
// A test string of length 4 is longer than the length of WHITESPACES (3), making sure the test case raises error.
2222
multiLineErrors2 := strings.Join(random.RandomElementsFromArray(slices.Concat(LEXICAL_ERRORS, WHITESPACES), 4), "")

internal/test_cases/tokenize_test_case.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@ import (
1616
// It is sent to the "tokenize" command of the executable,
1717
// the expected outputs are generated using the lox.ScanTokens function,
1818
// With that the output of the executable is matched.
19+
// Default value for HighlightWhitespaceWhileLogging is false.
20+
// We explicitly set it to true in the tests involving whitespace.
1921
type TokenizeTestCase struct {
20-
FileContents string
21-
ExpectsError bool
22+
FileContents string
23+
ExpectsError bool
24+
HighlightWhitespaceWhileLogging bool
2225
}
2326

2427
func (t *TokenizeTestCase) Run(executable *interpreter_executable.InterpreterExecutable, logger *logger.Logger) error {
@@ -28,7 +31,11 @@ func (t *TokenizeTestCase) Run(executable *interpreter_executable.InterpreterExe
2831
}
2932
defer os.Remove(tmpFileName)
3033

31-
logReadableFileContents(logger, t.FileContents)
34+
if t.HighlightWhitespaceWhileLogging {
35+
logReadableFileContentsPreservingWhitespace(logger, t.FileContents)
36+
} else {
37+
logReadableFileContents(logger, t.FileContents)
38+
}
3239

3340
result, err := executable.Run("tokenize", tmpFileName)
3441
if err != nil {

internal/test_cases/utils.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func logReadableFileContents(logger *logger.Logger, fileContents string) {
4545

4646
regex1 := regexp.MustCompile("[ ]+\n")
4747
regex2 := regexp.MustCompile("[ ]+$")
48-
printableFileContents = regex1.ReplaceAllString(printableFileContents, "<|SPACE|>")
48+
printableFileContents = regex1.ReplaceAllString(printableFileContents, "\n")
4949
printableFileContents = regex2.ReplaceAllString(printableFileContents, "<|SPACE|>")
5050

5151
// This is of the form "test-N"
@@ -70,6 +70,30 @@ func logReadableFileContents(logger *logger.Logger, fileContents string) {
7070
}
7171
}
7272

73+
func logReadableFileContentsPreservingWhitespace(logger *logger.Logger, fileContents string) {
74+
logger.Infof("Writing contents to ./test.lox:")
75+
76+
// If the file contents contain a single %, it will be decoded as a format specifier
77+
// And it will add a `(MISSING)` to the log line
78+
printableFileContents := strings.ReplaceAll(fileContents, "%", "%%")
79+
printableFileContents = strings.ReplaceAll(printableFileContents, "\t", "<|TAB|>")
80+
printableFileContents = strings.ReplaceAll(printableFileContents, " ", "<|SPACE|>")
81+
82+
// This is of the form "test-N"
83+
oldPrefix := logger.GetSecondaryPrefix()
84+
testNumber := strings.TrimPrefix(oldPrefix, "test-")
85+
logger.UpdateSecondaryPrefix(fmt.Sprintf("test-%s.lox", testNumber))
86+
defer logger.UpdateSecondaryPrefix(oldPrefix)
87+
88+
if len(printableFileContents) == 0 {
89+
logger.Plainf("<|EMPTY FILE|>")
90+
} else {
91+
for _, line := range strings.Split(printableFileContents, "\n") {
92+
logger.Plainf(line)
93+
}
94+
}
95+
}
96+
7397
var exitCodeToErrorTypeMapping = map[int]string{
7498
0: "no error",
7599
65: "compile error",

internal/test_helpers/fixtures/pass_scanning

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,8 @@ Debug = true
321321
[stage-11] Running tests for Stage #11: tz7
322322
[stage-11] [test-1] Running test case: 1
323323
[stage-11] [test-1] Writing contents to ./test.lox:
324-
[stage-11] [test-1.lox] ()<|SPACE|><|TAB|>@
324+
[stage-11] [test-1.lox] ()
325+
[stage-11] [test-1.lox] <|TAB|>@
325326
[stage-11] [test-1] $ ./your_program.sh tokenize test.lox
326327
[your_program] [line 2] Error: Unexpected character: @
327328
[your_program] LEFT_PAREN ( null
@@ -394,7 +395,7 @@ Debug = true
394395
[stage-10] [test-1] ✓ Received exit code 0.
395396
[stage-10] [test-2] Running test case: 2
396397
[stage-10] [test-2] Writing contents to ./test.lox:
397-
[stage-10] [test-2.lox]  <|TAB|>
398+
[stage-10] [test-2.lox] <|SPACE|><|TAB|>
398399
[stage-10] [test-2.lox] <|SPACE|>
399400
[stage-10] [test-2] $ ./your_program.sh tokenize test.lox
400401
[your_program] EOF null
@@ -423,7 +424,7 @@ Debug = true
423424
[stage-10] [test-4] Running test case: 4
424425
[stage-10] [test-4] Writing contents to ./test.lox:
425426
[stage-10] [test-4.lox] {<|TAB|>
426-
[stage-10] [test-4.lox]  <|TAB|>}
427+
[stage-10] [test-4.lox] <|SPACE|><|SPACE|><|TAB|>}
427428
[stage-10] [test-4.lox] ((
428429
[stage-10] [test-4.lox] <=<>-))
429430
[stage-10] [test-4] $ ./your_program.sh tokenize test.lox

0 commit comments

Comments
 (0)