diff --git a/lexers/README.md b/lexers/README.md new file mode 100644 index 0000000..43bdbbd --- /dev/null +++ b/lexers/README.md @@ -0,0 +1,19 @@ +# Lexer tests + +The tests in this directory feed a known input `testdata/.actual` into the parser for `` and check +that its output matches `.exported`. + +## Running the tests + +Run the tests as normal: +```go +go run ./lexers +``` + +## Updating the existing tests + +You can regenerate all the test outputs + +```go +RECORD=true go test ./lexers +``` diff --git a/lexers/lexers_test.go b/lexers/lexers_test.go index 69f6cf9..5cdbdb2 100644 --- a/lexers/lexers_test.go +++ b/lexers/lexers_test.go @@ -9,7 +9,6 @@ import ( "testing" "github.com/alecthomas/assert" - "github.com/alecthomas/chroma" "github.com/alecthomas/chroma/formatters" "github.com/alecthomas/chroma/lexers" @@ -64,15 +63,23 @@ func TestLexers(t *testing.T) { actual, err := chroma.Tokenise(lexer, nil, string(actualText)) assert.NoError(t, err) - // Read expected JSON into token slice. - var expected []chroma.Token - r, err := os.Open(expectedFilename) - assert.NoError(t, err) - err = json.NewDecoder(r).Decode(&expected) - assert.NoError(t, err) + if os.Getenv("RECORD") == "true" { + // Update the expected file with the generated output of this lexer + f, err := os.Create(expectedFilename) + defer f.Close() + assert.NoError(t, err) + assert.NoError(t, formatters.JSON.Format(f, nil, chroma.Literator(actual...))) + } else { + // Read expected JSON into token slice. + var expected []chroma.Token + r, err := os.Open(expectedFilename) + assert.NoError(t, err) + err = json.NewDecoder(r).Decode(&expected) + assert.NoError(t, err) - // Equal? - assert.Equal(t, expected, actual) + // Equal? + assert.Equal(t, expected, actual) + } }) } } diff --git a/lexers/testdata/README.md b/lexers/testdata/README.md deleted file mode 100644 index ec46906..0000000 --- a/lexers/testdata/README.md +++ /dev/null @@ -1,33 +0,0 @@ -# Lexer tests - -This directory contains input source and expected output lexer tokens. - -Input filenames for lexers are in the form `.actual`. Expected output filenames are in the form `.expected`. - -Each input filename is parsed by the corresponding lexer and checked against the expected JSON-encoded token list. - - -To add/update tests do the following: - -1. `export LEXER=csharp` -1. Create/edit a file `lexers/testdata/${LEXER}.actual` (eg. `csharp.actual`). -2. Run `go run ./cmd/chroma/main.go --lexer ${LEXER} --json lexers/testdata/${LEXER}.actual > lexers/testdata/${LEXER}.expected`. -3. Run `go test -v -run TestLexers ./lexers`. - - -eg. - -```bash -$ export LEXER=csharp -$ go run ./cmd/chroma/main.go --lexer ${LEXER} --json lexers/testdata/${LEXER}.actual > lexers/testdata/${LEXER}.expected -$ go test -v -run TestLexers ./lexers -=== RUN TestLexers -=== RUN TestLexers/C# -=== RUN TestLexers/CSS ---- PASS: TestLexers (0.01s) - --- PASS: TestLexers/C# (0.00s) - --- PASS: TestLexers/CSS (0.00s) -PASS -ok github.com/alecthomas/chroma/lexers 0.032s -``` -