Add tests for ANSIRenderer

This commit is contained in:
Christian Muehlhaeuser
2019-12-15 04:24:58 +01:00
parent 850c3affd3
commit 0c6cb858cf
14 changed files with 128 additions and 0 deletions
+92
View File
@@ -0,0 +1,92 @@
package ansi
import (
"bytes"
"encoding/json"
"io/ioutil"
"path/filepath"
"strings"
"testing"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer"
"github.com/yuin/goldmark/util"
)
const (
examplesDir = "../styles/examples/"
generate = false
)
func TestRenderer(t *testing.T) {
files, err := filepath.Glob(examplesDir + "*.md")
if err != nil {
t.Fatal(err)
}
for _, f := range files {
bn := strings.TrimSuffix(filepath.Base(f), ".md")
sn := filepath.Join(examplesDir, bn+".style")
tn := filepath.Join(examplesDir, bn+".test")
in, err := ioutil.ReadFile(f)
if err != nil {
t.Fatal(err)
}
b, err := ioutil.ReadFile(sn)
if err != nil {
t.Fatal(err)
}
options := Options{
WordWrap: 80,
}
err = json.Unmarshal(b, &options.Styles)
if err != nil {
t.Fatal(err)
}
md := goldmark.New(
goldmark.WithExtensions(
extension.GFM,
extension.DefinitionList,
),
goldmark.WithParserOptions(
parser.WithAutoHeadingID(),
),
)
ar := NewRenderer(options)
md.SetRenderer(
renderer.NewRenderer(
renderer.WithNodeRenderers(util.Prioritized(ar, 1000))))
var buf bytes.Buffer
err = md.Convert(in, &buf)
if err != nil {
t.Error(err)
}
// generate
if generate {
err = ioutil.WriteFile(tn, buf.Bytes(), 0644)
if err != nil {
t.Fatal(err)
}
continue
}
// verify
td, err := ioutil.ReadFile(tn)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(td, buf.Bytes()) {
t.Errorf("Rendered output for %s doesn't match!\nExpected: `\n%s`\nGot: `\n%s`\n",
bn, string(td), buf.String())
}
}
}
+3
View File
@@ -0,0 +1,3 @@
First line of quote                                                             
Second line                                                                     
+2
View File
@@ -0,0 +1,2 @@
This is a code. 
+2
View File
@@ -0,0 +1,2 @@
This is a code block.
+2
View File
@@ -0,0 +1,2 @@
This text is emphasized. 
+3
View File
@@ -0,0 +1,3 @@
1. First Item                                                                   
2. Second Item                                                                  
+3
View File
@@ -0,0 +1,3 @@
 => h1 <= 
 ## h2 
 ### h3
+1
View File
@@ -0,0 +1 @@
---
+2
View File
@@ -0,0 +1,2 @@
Image [Image: https://charm.sh/logo.png]. 
+2
View File
@@ -0,0 +1,2 @@
This is a link (https://charm.sh). 
+4
View File
@@ -0,0 +1,4 @@
• First Item                                                                    
    • Nested List Item                                                          
• Second Item                                                                   
+2
View File
@@ -0,0 +1,2 @@
This text is strong. 
+7
View File
@@ -0,0 +1,7 @@
+--------+-------+
| LABEL | VALUE |
+--------+-------+
| First | foo |
| Second | bar |
+--------+-------+
+3
View File
@@ -0,0 +1,3 @@
✓ Finished Task                                                                 
✗ Outstanding Task