From f35df555231b4d0b1a4504cbacba0bbf79a64599 Mon Sep 17 00:00:00 2001 From: Jesse Portnoy Date: Mon, 17 Jul 2023 22:55:24 +0100 Subject: [PATCH] Beginning of unit tests --- README.md | 11 ++++++----- examples/syncat.go | 2 +- helpers_test.go | 43 +++++++++++++++++++++++++++++++++++++++++++ highlighter_test.go | 30 ++++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+), 6 deletions(-) create mode 100644 helpers_test.go create mode 100644 highlighter_test.go diff --git a/README.md b/README.md index 36c3324..86007a6 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ # Highlight -[![Go Report Card](https://goreportcard.com/badge/github.com/jessp01/highlight)](https://goreportcard.com/report/github.com/jessp01/highlight) -[![GoDoc](https://godoc.org/github.com/jessp01/highlight?status.svg)](http://godoc.org/github.com/jessp01/highlight) -[![MIT License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/jessp01/highlight/blob/master/LICENSE) +[![Go Report Card](https://goreportcard.com/badge/github.com/jessp01/gohighlight)](https://goreportcard.com/report/github.com/jessp01/gohighlight) +[![GoDoc](https://godoc.org/github.com/jessp01/gohighlight?status.svg)](http://godoc.org/github.com/jessp01/gohighlight) +[![MIT License](https://img.shields.io/badge/license-MIT-blue.svg)](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.. 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 ``` -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`. @@ -127,6 +127,7 @@ func helloWorld() { } } + color.Unset() fmt.Print("\n") } } diff --git a/examples/syncat.go b/examples/syncat.go index 3b2804e..df37828 100644 --- a/examples/syncat.go +++ b/examples/syncat.go @@ -9,7 +9,7 @@ import ( "strings" "github.com/fatih/color" - "github.com/jessp01/highlight" + "github.com/jessp01/gohighlight" ) func main() { diff --git a/helpers_test.go b/helpers_test.go new file mode 100644 index 0000000..e3f9b7e --- /dev/null +++ b/helpers_test.go @@ -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 +} diff --git a/highlighter_test.go b/highlighter_test.go new file mode 100644 index 0000000..143749e --- /dev/null +++ b/highlighter_test.go @@ -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) + } + } +}