@@ -2,8 +2,12 @@ package md_test
22
33import (
44 "bytes"
5+ "fmt"
56 "io/ioutil"
7+ "os"
68 "path"
9+ "path/filepath"
10+ "strings"
711 "testing"
812
913 md "github.com/JohannesKaufmann/html-to-markdown"
@@ -13,13 +17,56 @@ import (
1317)
1418
1519type GoldenTest struct {
16- Name string
20+ Name string
21+ Domain string
1722
1823 Options map [string ]* md.Options
1924 Plugins []md.Plugin
2025}
2126
2227func RunGoldenTest (t * testing.T , tests []GoldenTest ) {
28+ // loop through all test cases that were added manually
29+ dirs := make (map [string ]struct {})
30+ for _ , test := range tests {
31+ name := test .Name
32+ name = strings .Replace (name , " " , "_" , - 1 )
33+ dirs [name ] = struct {}{}
34+ }
35+
36+ // now add all tests that were found on disk to the tests slice
37+ err := filepath .Walk (path .Join ("testdata" , t .Name ()),
38+ func (p string , info os.FileInfo , err error ) error {
39+ if err != nil {
40+ return err
41+ }
42+ if ! info .IsDir () {
43+ return nil
44+ }
45+
46+ // skip folders that don't contain an input.html file
47+ if _ , err := os .Stat (path .Join (p , "input.html" )); os .IsNotExist (err ) {
48+ return nil
49+ }
50+
51+ parts := strings .SplitN (p , string (os .PathSeparator ), 3 )
52+ p = parts [2 ] // remove "testdata/TestCommonmark/" from "testdata/TestCommonmark/..."
53+
54+ _ , ok := dirs [p ]
55+ if ok {
56+ return nil
57+ }
58+
59+ // add the folder from disk to the tests slice, since its not it there yet
60+ tests = append (tests , GoldenTest {
61+ Name : p ,
62+ })
63+ return nil
64+ })
65+ if err != nil {
66+ t .Error (err )
67+ return
68+ }
69+
2370 for _ , test := range tests {
2471 if len (test .Options ) == 0 {
2572 test .Options = map [string ]* md.Options {
@@ -28,6 +75,11 @@ func RunGoldenTest(t *testing.T, tests []GoldenTest) {
2875 }
2976
3077 t .Run (test .Name , func (t * testing.T ) {
78+ if strings .Contains (t .Name (), "#" ) {
79+ fmt .Println ("the name" , test .Name , t .Name (), "seems too be used for multiple tests" )
80+ return
81+ }
82+
3183 g := goldie .New (t )
3284
3385 for key , options := range test .Options {
@@ -38,9 +90,11 @@ func RunGoldenTest(t *testing.T, tests []GoldenTest) {
3890 input , err := ioutil .ReadFile (path .Join ("testdata" , p ))
3991 if err != nil {
4092 t .Error (err )
93+ return
4194 }
4295
43- conv := md .NewConverter ("" , true , options )
96+ conv := md .NewConverter (test .Domain , true , options )
97+ conv .Keep ("keep-tag" ).Remove ("remove-tag" )
4498 for _ , plugin := range test .Plugins {
4599 conv .Use (plugin )
46100 }
@@ -70,73 +124,88 @@ func RunGoldenTest(t *testing.T, tests []GoldenTest) {
70124func TestCommonmark (t * testing.T ) {
71125 var tests = []GoldenTest {
72126 {
73- Name : "h1 " ,
127+ Name : "link " ,
74128 Options : map [string ]* md.Options {
75- "setext" : {HeadingStyle : "setext" },
76- "atx" : {HeadingStyle : "atx" },
129+ "inlined" : & md.Options {LinkStyle : "inlined" },
130+ "referenced_full" : & md.Options {LinkStyle : "referenced" , LinkReferenceStyle : "full" },
131+ "referenced_collapsed" : & md.Options {LinkStyle : "referenced" , LinkReferenceStyle : "collapsed" },
132+ "referenced_shortcut" : & md.Options {LinkStyle : "referenced" , LinkReferenceStyle : "shortcut" },
77133 },
78134 },
79135 {
80- Name : "h2 " ,
136+ Name : "heading " ,
81137 Options : map [string ]* md.Options {
82- "setext " : {HeadingStyle : "setext " },
83- "atx " : {HeadingStyle : "atx " },
138+ "atx " : & md. Options {HeadingStyle : "atx " },
139+ "setext " : & md. Options {HeadingStyle : "setext " },
84140 },
85141 },
86142 {
87- Name : "h3 " ,
143+ Name : "italic " ,
88144 Options : map [string ]* md.Options {
89- "setext " : { HeadingStyle : "setext " },
90- "atx " : { HeadingStyle : "atx " },
145+ "asterisks " : & md. Options { EmDelimiter : "* " },
146+ "underscores " : & md. Options { EmDelimiter : "_ " },
91147 },
92148 },
93149 {
94- Name : "p with content" ,
95- },
96- {
97- Name : "p inside div" ,
150+ Name : "bold" ,
151+ Options : map [string ]* md.Options {
152+ "asterisks" : & md.Options {StrongDelimiter : "**" },
153+ "underscores" : & md.Options {StrongDelimiter : "__" },
154+ },
98155 },
99156 {
100- Name : "p with span" ,
157+ Name : "pre_code" ,
158+ Options : map [string ]* md.Options {
159+ "indented" : & md.Options {CodeBlockStyle : "indented" },
160+ "fenced_backtick" : & md.Options {CodeBlockStyle : "fenced" , Fence : "```" },
161+ "fenced_tilde" : & md.Options {CodeBlockStyle : "fenced" , Fence : "~~~" },
162+ },
101163 },
102164 {
103- Name : "p with strong " ,
165+ Name : "list " ,
104166 Options : map [string ]* md.Options {
105- "default" : {StrongDelimiter : "" },
106- "underscore" : {StrongDelimiter : "__" },
167+ "asterisks" : & md.Options {BulletListMarker : "*" },
168+ "dash" : & md.Options {BulletListMarker : "-" },
169+ "plus" : & md.Options {BulletListMarker : "+" },
107170 },
108171 },
109172 {
110- Name : "p with b" ,
173+ Name : "list_nested" ,
174+ Options : map [string ]* md.Options {
175+ "asterisks" : & md.Options {BulletListMarker : "*" },
176+ "dash" : & md.Options {BulletListMarker : "-" },
177+ "plus" : & md.Options {BulletListMarker : "+" },
178+ },
111179 },
180+ // + all the test on disk that are added automatically
112181 }
113182
114183 RunGoldenTest (t , tests )
184+ }
115185
116- /*
117-
118- TestCommonmark
119- TestPlugins
120- TestRules/Keep/Remove
121-
122- ---- always start with the main tag
123-
124-
125- p with content
126-
127- strong nested
128-
129- h3
130-
131- escape
132-
133-
134- ---- files
135- h1.input
136-
137- h1.setext.golden
138- h1.atx.golden
139-
140-
141- */
186+ func TestRealWorld (t * testing.T ) {
187+ var tests = []GoldenTest {
188+ {
189+ Name : "blog.golang.org" ,
190+ Domain : "blog.golang.org" ,
191+ Options : map [string ]* md.Options {
192+ "inlined" : & md.Options {LinkStyle : "inlined" },
193+ "referenced_full" : & md.Options {LinkStyle : "referenced" , LinkReferenceStyle : "full" },
194+ "referenced_collapsed" : & md.Options {LinkStyle : "referenced" , LinkReferenceStyle : "collapsed" },
195+ "referenced_shortcut" : & md.Options {LinkStyle : "referenced" , LinkReferenceStyle : "shortcut" },
196+ "emphasis_asterisks" : & md.Options {EmDelimiter : "*" , StrongDelimiter : "**" },
197+ "emphasis_underscores" : & md.Options {EmDelimiter : "_" , StrongDelimiter : "__" },
198+ },
199+ },
200+ {
201+ Name : "golang.org" ,
202+ Domain : "golang.org" ,
203+ },
204+ {
205+ Name : "bonnerruderverein.de" ,
206+ Domain : "bonnerruderverein.de" ,
207+ },
208+ // + all the test on disk that are added automatically
209+ }
210+ RunGoldenTest (t , tests )
142211}
0 commit comments