-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzaje_test.go
71 lines (60 loc) · 1.79 KB
/
zaje_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package zaje
import (
"bytes"
"fmt"
"io/ioutil"
"path/filepath"
"strings"
"testing"
highlight "github.com/jessp01/gohighlight"
)
func parseDefs(t *testing.T, filename string, data []byte, highlightLexer string) *highlight.Def {
var currDef *highlight.Def
synDir := "./highlight/syntax_files"
var defs []*highlight.Def
warnings, lerr := highlight.ParseSyntaxFiles(synDir, &defs)
if lerr != nil {
t.Errorf("Couldn't get defs from '%s', error: %v\n", synDir, lerr)
}
if len(warnings) > 0 {
t.Errorf("Parsing ended with warnings: '%s'\n", strings.Join(warnings, ";"))
}
highlight.ResolveIncludes(defs)
// Always try to auto detect the best lexer:was
if currDef == nil {
currDef = highlight.DetectFiletype(defs, filename, bytes.Split(data, []byte("\n"))[0])
}
// if a specific lexer was requested by setting the ENV var, try to load it
if highlightLexer != "" {
syntaxFile, lerr := ioutil.ReadFile(synDir + "/" + highlightLexer + ".yaml")
if lerr == nil {
// Parse it into a `*highlight.Def`
def, _ = highlight.ParseDef(syntaxFile)
}
}
if currDef == nil {
t.Errorf("Found no defs for '%s'\n", filename)
}
return currDef
}
func TestInputs(t *testing.T) {
testInputsDir := "./highlight/test_inputs/*"
files, err := filepath.Glob(testInputsDir)
if err != nil {
t.Errorf("Couldn't open '%s', error: %v\n", testInputsDir, err)
}
for _, filename := range files {
ext := strings.Split(filename, ".")
data, _ := ioutil.ReadFile(filename)
NullifyDef()
fmt.Println("Testing def detection for " + filename)
def := parseDefs(t, filename, data, "")
HandleData(filename, data)
exp := ext[len(ext)-1]
if def.FileType != exp {
t.Errorf("\nInput [%#v]\nExpected[%#v]\nGot [%#v]\n",
// string(data), exp, def.FileType)
filename, exp, def.FileType)
}
}
}