mirror of
https://github.com/rwinkhart/go-highlite.git
synced 2026-09-05 16:37:20 -04:00
Beginning of unit tests
This commit is contained in:
@@ -1,9 +1,9 @@
|
|||||||
# Highlight
|
# Highlight
|
||||||
[](https://goreportcard.com/report/github.com/jessp01/highlight)
|
[](https://goreportcard.com/report/github.com/jessp01/gohighlight)
|
||||||
[](http://godoc.org/github.com/jessp01/highlight)
|
[](http://godoc.org/github.com/jessp01/gohighlight)
|
||||||
[](https://github.com/jessp01/highlight/blob/master/LICENSE)
|
[](https://github.com/jessp01/gohighlight/blob/master/LICENSE)
|
||||||
|
|
||||||
`highlight` is a syntax highlighter of programming languages, config formats and UNIX commands.
|
`gohighlight` is a syntax highlighter of programming languages, config formats and UNIX commands.
|
||||||
It allows you to pass in a string and get back all the information you need to properly highlight it..
|
It allows you to pass in a string and get back all the information you need to properly highlight it..
|
||||||
This repo includes over a 100 different lexers and contributions are most welcome:)
|
This repo includes over a 100 different lexers and contributions are most welcome:)
|
||||||
|
|
||||||
@@ -24,7 +24,7 @@ Below is a recap of the main changes made since:
|
|||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
```
|
```
|
||||||
go get github.com/jessp01/highlight
|
$ go get github.com/jessp01/highlight
|
||||||
```
|
```
|
||||||
|
|
||||||
Be sure to point your code to the correct path of `syntax_files`.
|
Be sure to point your code to the correct path of `syntax_files`.
|
||||||
@@ -127,6 +127,7 @@ func helloWorld() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
color.Unset()
|
||||||
fmt.Print("\n")
|
fmt.Print("\n")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -9,7 +9,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
"github.com/jessp01/highlight"
|
"github.com/jessp01/gohighlight"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|||||||
@@ -0,0 +1,43 @@
|
|||||||
|
package highlight
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"io/ioutil"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/jessp01/gohighlight"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
func getDefs(t *testing.T, filename string, data []byte, highlight_lexer string) *highlight.Def {
|
||||||
|
var def *highlight.Def
|
||||||
|
syn_dir := "./syntax_files"
|
||||||
|
|
||||||
|
var defs []*highlight.Def
|
||||||
|
lerr := highlight.ParseSyntaxFiles(syn_dir, &defs)
|
||||||
|
if lerr != nil {
|
||||||
|
t.Errorf("Couldn't get defs from '%s', error: %v\n", syn_dir, lerr)
|
||||||
|
}
|
||||||
|
highlight.ResolveIncludes(defs)
|
||||||
|
|
||||||
|
// Always try to auto detect the best lexer:was
|
||||||
|
if def == nil {
|
||||||
|
def = 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 highlight_lexer != "" {
|
||||||
|
syntaxFile, lerr := ioutil.ReadFile(syn_dir + "/" + highlight_lexer + ".yaml")
|
||||||
|
if lerr == nil {
|
||||||
|
// Parse it into a `*highlight.Def`
|
||||||
|
def, _ = highlight.ParseDef(syntaxFile)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if def == nil {
|
||||||
|
t.Errorf("Found no defs for '%s'\n", filename)
|
||||||
|
}
|
||||||
|
|
||||||
|
//fmt.Println(def)
|
||||||
|
return def
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package highlight
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"testing"
|
||||||
|
"fmt"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestInputs(t *testing.T){
|
||||||
|
|
||||||
|
test_inputs_dir := "./test_inputs/*"
|
||||||
|
files, err := filepath.Glob(test_inputs_dir)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Couldn't open '%s', error: %v\n", test_inputs_dir, err)
|
||||||
|
}
|
||||||
|
for _, filename := range(files){
|
||||||
|
ext := strings.Split(filename, ".")
|
||||||
|
data, _ := ioutil.ReadFile(filename)
|
||||||
|
fmt.Println("Testing def detection for " + filename)
|
||||||
|
def := getDefs(t, 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user