Skip to content

Commit

Permalink
Move test assertion generation into testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Scarr authored and alecthomas committed Jan 29, 2019
1 parent 6227d9b commit 8548c73
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 42 deletions.
19 changes: 19 additions & 0 deletions lexers/README.md
Original file line number Diff line number Diff line change
@@ -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
```
25 changes: 16 additions & 9 deletions lexers/lexers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"testing"

"github.com/alecthomas/assert"

"github.com/alecthomas/chroma"
"github.com/alecthomas/chroma/formatters"
"github.com/alecthomas/chroma/lexers"
Expand Down Expand Up @@ -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 changes: 0 additions & 33 deletions lexers/testdata/README.md

This file was deleted.

0 comments on commit 8548c73

Please sign in to comment.