Skip to content

Commit

Permalink
move all goldenfile tests from go into own files
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesKaufmann committed Jul 28, 2020
1 parent 34fa6f3 commit d7c29f3
Show file tree
Hide file tree
Showing 202 changed files with 3,364 additions and 1,106 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

.DS_Store
161 changes: 115 additions & 46 deletions commonmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ package md_test

import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"testing"

md "github.com/JohannesKaufmann/html-to-markdown"
Expand All @@ -13,13 +17,56 @@ import (
)

type GoldenTest struct {
Name string
Name string
Domain string

Options map[string]*md.Options
Plugins []md.Plugin
}

func RunGoldenTest(t *testing.T, tests []GoldenTest) {
// loop through all test cases that were added manually
dirs := make(map[string]struct{})
for _, test := range tests {
name := test.Name
name = strings.Replace(name, " ", "_", -1)
dirs[name] = struct{}{}
}

// now add all tests that were found on disk to the tests slice
err := filepath.Walk(path.Join("testdata", t.Name()),
func(p string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
return nil
}

// skip folders that don't contain an input.html file
if _, err := os.Stat(path.Join(p, "input.html")); os.IsNotExist(err) {
return nil
}

parts := strings.SplitN(p, string(os.PathSeparator), 3)
p = parts[2] // remove "testdata/TestCommonmark/" from "testdata/TestCommonmark/..."

_, ok := dirs[p]
if ok {
return nil
}

// add the folder from disk to the tests slice, since its not it there yet
tests = append(tests, GoldenTest{
Name: p,
})
return nil
})
if err != nil {
t.Error(err)
return
}

for _, test := range tests {
if len(test.Options) == 0 {
test.Options = map[string]*md.Options{
Expand All @@ -28,6 +75,11 @@ func RunGoldenTest(t *testing.T, tests []GoldenTest) {
}

t.Run(test.Name, func(t *testing.T) {
if strings.Contains(t.Name(), "#") {
fmt.Println("the name", test.Name, t.Name(), "seems too be used for multiple tests")
return
}

g := goldie.New(t)

for key, options := range test.Options {
Expand All @@ -38,9 +90,11 @@ func RunGoldenTest(t *testing.T, tests []GoldenTest) {
input, err := ioutil.ReadFile(path.Join("testdata", p))
if err != nil {
t.Error(err)
return
}

conv := md.NewConverter("", true, options)
conv := md.NewConverter(test.Domain, true, options)
conv.Keep("keep-tag").Remove("remove-tag")
for _, plugin := range test.Plugins {
conv.Use(plugin)
}
Expand Down Expand Up @@ -70,73 +124,88 @@ func RunGoldenTest(t *testing.T, tests []GoldenTest) {
func TestCommonmark(t *testing.T) {
var tests = []GoldenTest{
{
Name: "h1",
Name: "link",
Options: map[string]*md.Options{
"setext": {HeadingStyle: "setext"},
"atx": {HeadingStyle: "atx"},
"inlined": &md.Options{LinkStyle: "inlined"},
"referenced_full": &md.Options{LinkStyle: "referenced", LinkReferenceStyle: "full"},
"referenced_collapsed": &md.Options{LinkStyle: "referenced", LinkReferenceStyle: "collapsed"},
"referenced_shortcut": &md.Options{LinkStyle: "referenced", LinkReferenceStyle: "shortcut"},
},
},
{
Name: "h2",
Name: "heading",
Options: map[string]*md.Options{
"setext": {HeadingStyle: "setext"},
"atx": {HeadingStyle: "atx"},
"atx": &md.Options{HeadingStyle: "atx"},
"setext": &md.Options{HeadingStyle: "setext"},
},
},
{
Name: "h3",
Name: "italic",
Options: map[string]*md.Options{
"setext": {HeadingStyle: "setext"},
"atx": {HeadingStyle: "atx"},
"asterisks": &md.Options{EmDelimiter: "*"},
"underscores": &md.Options{EmDelimiter: "_"},
},
},
{
Name: "p with content",
},
{
Name: "p inside div",
Name: "bold",
Options: map[string]*md.Options{
"asterisks": &md.Options{StrongDelimiter: "**"},
"underscores": &md.Options{StrongDelimiter: "__"},
},
},
{
Name: "p with span",
Name: "pre_code",
Options: map[string]*md.Options{
"indented": &md.Options{CodeBlockStyle: "indented"},
"fenced_backtick": &md.Options{CodeBlockStyle: "fenced", Fence: "```"},
"fenced_tilde": &md.Options{CodeBlockStyle: "fenced", Fence: "~~~"},
},
},
{
Name: "p with strong",
Name: "list",
Options: map[string]*md.Options{
"default": {StrongDelimiter: ""},
"underscore": {StrongDelimiter: "__"},
"asterisks": &md.Options{BulletListMarker: "*"},
"dash": &md.Options{BulletListMarker: "-"},
"plus": &md.Options{BulletListMarker: "+"},
},
},
{
Name: "p with b",
Name: "list_nested",
Options: map[string]*md.Options{
"asterisks": &md.Options{BulletListMarker: "*"},
"dash": &md.Options{BulletListMarker: "-"},
"plus": &md.Options{BulletListMarker: "+"},
},
},
// + all the test on disk that are added automatically
}

RunGoldenTest(t, tests)
}

/*
TestCommonmark
TestPlugins
TestRules/Keep/Remove
---- always start with the main tag
p with content
strong nested
h3
escape
---- files
h1.input
h1.setext.golden
h1.atx.golden
*/
func TestRealWorld(t *testing.T) {
var tests = []GoldenTest{
{
Name: "blog.golang.org",
Domain: "blog.golang.org",
Options: map[string]*md.Options{
"inlined": &md.Options{LinkStyle: "inlined"},
"referenced_full": &md.Options{LinkStyle: "referenced", LinkReferenceStyle: "full"},
"referenced_collapsed": &md.Options{LinkStyle: "referenced", LinkReferenceStyle: "collapsed"},
"referenced_shortcut": &md.Options{LinkStyle: "referenced", LinkReferenceStyle: "shortcut"},
"emphasis_asterisks": &md.Options{EmDelimiter: "*", StrongDelimiter: "**"},
"emphasis_underscores": &md.Options{EmDelimiter: "_", StrongDelimiter: "__"},
},
},
{
Name: "golang.org",
Domain: "golang.org",
},
{
Name: "bonnerruderverein.de",
Domain: "bonnerruderverein.de",
},
// + all the test on disk that are added automatically
}
RunGoldenTest(t, tests)
}
2 changes: 1 addition & 1 deletion from.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ func (c *Converter) ConvertURL(url string) (string, error) {
}
domain := DomainFromURL(url)
if c.domain != domain {
return "", errors.New("expected " + c.domain + " as the domain but got " + domain)
return "", fmt.Errorf("expected '%s' as the domain but got '%s'", c.domain, domain)
}
return c.Convert(doc.Selection), nil
}
Loading

0 comments on commit d7c29f3

Please sign in to comment.