mirror of
https://github.com/alecthomas/chroma.git
synced 2025-03-17 20:58:08 +02:00
Move test assertion generation into testcase
This commit is contained in:
parent
6227d9bbc5
commit
8548c73b9f
19
lexers/README.md
Normal file
19
lexers/README.md
Normal file
@ -0,0 +1,19 @@
|
||||
# Lexer tests
|
||||
|
||||
The tests in this directory feed a known input `testdata/<name>.actual` into the parser for `<name>` and check
|
||||
that its output matches `<name>.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
|
||||
```
|
@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
33
lexers/testdata/README.md
vendored
33
lexers/testdata/README.md
vendored
@ -1,33 +0,0 @@
|
||||
# Lexer tests
|
||||
|
||||
This directory contains input source and expected output lexer tokens.
|
||||
|
||||
Input filenames for lexers are in the form `<name>.actual`. Expected output filenames are in the form `<name>.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
|
||||
```
|
||||
|
Loading…
x
Reference in New Issue
Block a user