Fix chroma lexer mapping (#36629)
Fix some edge cases for ".hcl" and ".v" files, and add more tests
This commit is contained in:
@@ -45,7 +45,7 @@ func BenchmarkRenderCodeByLexer(b *testing.B) {
|
||||
lexer := DetectChromaLexerByFileName("a.sql", "")
|
||||
b.StartTimer()
|
||||
for b.Loop() {
|
||||
// Really slow .......
|
||||
// Really slow ....... the regexp2 used by Chroma takes most of the time
|
||||
// BenchmarkRenderCodeByLexer-12 22 47159038 ns/op
|
||||
RenderCodeByLexer(lexer, code)
|
||||
}
|
||||
@@ -55,13 +55,14 @@ func TestDetectChromaLexer(t *testing.T) {
|
||||
globalVars().highlightMapping[".my-html"] = "HTML"
|
||||
t.Cleanup(func() { delete(globalVars().highlightMapping, ".my-html") })
|
||||
|
||||
cases := []struct {
|
||||
casesWithContent := []struct {
|
||||
fileName string
|
||||
language string
|
||||
content string
|
||||
expected string
|
||||
}{
|
||||
{"test.py", "", "", "Python"},
|
||||
{"test.v", "", "", "V"},
|
||||
{"test.v", "any-lang-name", "", "V"},
|
||||
|
||||
{"any-file", "javascript", "", "JavaScript"},
|
||||
{"any-file", "", "/* vim: set filetype=python */", "Python"},
|
||||
@@ -80,11 +81,36 @@ func TestDetectChromaLexer(t *testing.T) {
|
||||
{"a.sql", "", "", "SQL"},
|
||||
{"dhcpd.conf", "", "", "ISCdhcpd"},
|
||||
{".env.my-production", "", "", "Bash"},
|
||||
|
||||
{"a.hcl", "", "", "HCL"}, // not the same as Chroma, enry detects "*.hcl" as "HCL"
|
||||
{"a.hcl", "HCL", "", "HCL"},
|
||||
{"a.hcl", "Terraform", "", "Terraform"},
|
||||
}
|
||||
for _, c := range cases {
|
||||
for _, c := range casesWithContent {
|
||||
lexer := detectChromaLexerWithAnalyze(c.fileName, c.language, []byte(c.content))
|
||||
if assert.NotNil(t, lexer, "case: %+v", c) {
|
||||
assert.Equal(t, c.expected, lexer.Config().Name, "case: %+v", c)
|
||||
}
|
||||
}
|
||||
|
||||
casesNameLang := []struct {
|
||||
fileName string
|
||||
language string
|
||||
expected string
|
||||
byLang bool
|
||||
}{
|
||||
{"a.v", "", "V", false},
|
||||
{"a.v", "V", "V", true},
|
||||
{"a.v", "verilog", "verilog", true},
|
||||
{"a.v", "any-lang-name", "V", false},
|
||||
|
||||
{"a.hcl", "", "Terraform", false}, // not the same as enry
|
||||
{"a.hcl", "HCL", "HCL", true},
|
||||
{"a.hcl", "Terraform", "Terraform", true},
|
||||
}
|
||||
for _, c := range casesNameLang {
|
||||
lexer, byLang := detectChromaLexerByFileName(c.fileName, c.language)
|
||||
assert.Equal(t, c.expected, lexer.Config().Name, "case: %+v", c)
|
||||
assert.Equal(t, c.byLang, byLang, "case: %+v", c)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user