2018-02-15 21:01:44 +11:00
|
|
|
package lexers_test
|
2018-01-02 14:53:25 +11:00
|
|
|
|
|
|
|
import (
|
2018-03-03 10:16:21 +11:00
|
|
|
"encoding/json"
|
2018-01-02 14:53:25 +11:00
|
|
|
"io/ioutil"
|
2018-03-03 10:16:21 +11:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2020-12-14 14:35:47 -03:00
|
|
|
"strconv"
|
2018-03-03 10:16:21 +11:00
|
|
|
"strings"
|
2018-01-02 14:53:25 +11:00
|
|
|
"testing"
|
|
|
|
|
2018-03-03 10:16:21 +11:00
|
|
|
"github.com/alecthomas/chroma"
|
2018-02-15 21:01:44 +11:00
|
|
|
"github.com/alecthomas/chroma/formatters"
|
|
|
|
"github.com/alecthomas/chroma/lexers"
|
|
|
|
"github.com/alecthomas/chroma/lexers/a"
|
|
|
|
"github.com/alecthomas/chroma/lexers/x"
|
|
|
|
"github.com/alecthomas/chroma/styles"
|
2021-06-18 16:55:49 +10:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-01-02 14:53:25 +11:00
|
|
|
)
|
|
|
|
|
2018-02-15 21:01:44 +11:00
|
|
|
func TestCompileAllRegexes(t *testing.T) {
|
|
|
|
for _, lexer := range lexers.Registry.Lexers {
|
|
|
|
it, err := lexer.Tokenise(nil, "")
|
|
|
|
assert.NoError(t, err, "%s failed", lexer.Config().Name)
|
|
|
|
err = formatters.NoOp.Format(ioutil.Discard, styles.SwapOff, it)
|
|
|
|
assert.NoError(t, err, "%s failed", lexer.Config().Name)
|
2018-01-02 14:53:25 +11:00
|
|
|
}
|
|
|
|
}
|
2018-02-15 21:01:44 +11:00
|
|
|
|
|
|
|
func TestGet(t *testing.T) {
|
|
|
|
t.Run("ByName", func(t *testing.T) {
|
|
|
|
assert.Equal(t, lexers.Get("xml"), x.XML)
|
|
|
|
})
|
|
|
|
t.Run("ByAlias", func(t *testing.T) {
|
|
|
|
assert.Equal(t, lexers.Get("as"), a.Actionscript)
|
|
|
|
})
|
|
|
|
t.Run("ViaFilename", func(t *testing.T) {
|
|
|
|
assert.Equal(t, lexers.Get("svg"), x.XML)
|
|
|
|
})
|
|
|
|
}
|
2018-03-03 10:16:21 +11:00
|
|
|
|
2020-04-26 00:55:10 +03:00
|
|
|
func BenchmarkGet(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
lexers.Get("go")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-06 16:57:22 +02:00
|
|
|
func FileTest(t *testing.T, lexer chroma.Lexer, actualFilename, expectedFilename string) {
|
|
|
|
t.Helper()
|
|
|
|
t.Run(lexer.Config().Name+"/"+actualFilename, func(t *testing.T) {
|
|
|
|
// Read and tokenise source text.
|
|
|
|
actualText, err := ioutil.ReadFile(actualFilename)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
actual, err := chroma.Tokenise(lexer, nil, string(actualText))
|
|
|
|
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() // nolint: gosec
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-03-03 10:16:21 +11:00
|
|
|
// Test source files are in the form <key>.<key> and validation data is in the form <key>.<key>.expected.
|
|
|
|
func TestLexers(t *testing.T) {
|
|
|
|
files, err := ioutil.ReadDir("testdata")
|
2018-03-03 10:24:05 +11:00
|
|
|
assert.NoError(t, err)
|
2018-03-03 10:16:21 +11:00
|
|
|
|
|
|
|
for _, file := range files {
|
2020-12-14 14:35:47 -03:00
|
|
|
// skip text analysis test files
|
|
|
|
if file.Name() == "analysis" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-05-06 16:57:22 +02:00
|
|
|
if file.IsDir() {
|
|
|
|
dirname := filepath.Join("testdata", file.Name())
|
|
|
|
lexer := lexers.Get(file.Name())
|
|
|
|
assert.NotNil(t, lexer)
|
2018-03-03 10:16:21 +11:00
|
|
|
|
2021-05-06 16:57:22 +02:00
|
|
|
subFiles, err := ioutil.ReadDir(dirname)
|
|
|
|
assert.NoError(t, err)
|
2018-03-03 10:16:21 +11:00
|
|
|
|
2021-05-06 16:57:22 +02:00
|
|
|
for _, subFile := range subFiles {
|
|
|
|
ext := filepath.Ext(subFile.Name())[1:]
|
|
|
|
if ext != "actual" {
|
|
|
|
continue
|
|
|
|
}
|
2018-03-03 10:16:21 +11:00
|
|
|
|
2021-05-06 16:57:22 +02:00
|
|
|
filename := filepath.Join(dirname, subFile.Name())
|
|
|
|
expectedFilename := strings.TrimSuffix(filename, filepath.Ext(filename)) + ".expected"
|
2018-03-03 10:16:21 +11:00
|
|
|
|
2021-05-06 16:57:22 +02:00
|
|
|
lexer = chroma.Coalesce(lexer)
|
|
|
|
FileTest(t, lexer, filename, expectedFilename)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ext := filepath.Ext(file.Name())[1:]
|
|
|
|
if ext != "actual" {
|
|
|
|
continue
|
2019-01-29 15:15:33 +11:00
|
|
|
}
|
2021-05-06 16:57:22 +02:00
|
|
|
|
|
|
|
base := strings.Split(strings.TrimSuffix(file.Name(), filepath.Ext(file.Name())), ".")[0]
|
|
|
|
lexer := lexers.Get(base)
|
|
|
|
assert.NotNil(t, lexer)
|
|
|
|
|
|
|
|
filename := filepath.Join("testdata", file.Name())
|
|
|
|
expectedFilename := strings.TrimSuffix(filename, filepath.Ext(filename)) + ".expected"
|
|
|
|
|
|
|
|
lexer = chroma.Coalesce(lexer)
|
|
|
|
FileTest(t, lexer, filename, expectedFilename)
|
|
|
|
}
|
2018-03-03 10:16:21 +11:00
|
|
|
}
|
|
|
|
}
|
2020-12-14 14:35:47 -03:00
|
|
|
|
|
|
|
func FileTestAnalysis(t *testing.T, lexer chroma.Lexer, actualFilepath, expectedFilepath string) {
|
|
|
|
t.Helper()
|
|
|
|
t.Run(lexer.Config().Name+"/"+actualFilepath, func(t *testing.T) {
|
|
|
|
expectedData, err := ioutil.ReadFile(expectedFilepath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
analyser, ok := lexer.(chroma.Analyser)
|
|
|
|
assert.True(t, ok, "lexer %q does not set analyser", lexer.Config().Name)
|
|
|
|
|
|
|
|
data, err := ioutil.ReadFile(actualFilepath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
actual := analyser.AnalyseText(string(data))
|
|
|
|
|
|
|
|
if os.Getenv("RECORD") == "true" {
|
|
|
|
// Update the expected file with the generated output of this lexer
|
|
|
|
f, err := os.Create(expectedFilepath)
|
|
|
|
defer f.Close() // nolint: gosec
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = f.WriteString(strconv.FormatFloat(float64(actual), 'f', -1, 32))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
} else {
|
|
|
|
expected, err := strconv.ParseFloat(strings.TrimSpace(string(expectedData)), 32)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, float32(expected), actual)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLexersTextAnalyser(t *testing.T) {
|
|
|
|
files, err := filepath.Glob("testdata/analysis/*.actual")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
for _, actualFilepath := range files {
|
|
|
|
filename := filepath.Base(actualFilepath)
|
|
|
|
baseFilename := strings.TrimSuffix(filename, filepath.Ext(filename))
|
|
|
|
lexerName := strings.Split(baseFilename, ".")[0]
|
|
|
|
|
|
|
|
lexer := lexers.Get(lexerName)
|
|
|
|
assert.NotNil(t, lexer, "no lexer found for name %q", lexerName)
|
|
|
|
|
|
|
|
expectedFilepath := "testdata/analysis/" + baseFilename + ".expected"
|
|
|
|
|
|
|
|
FileTestAnalysis(t, lexer, actualFilepath, expectedFilepath)
|
|
|
|
}
|
|
|
|
}
|