meta_test.go (2801B)
1 package meta 2 3 import ( 4 "bytes" 5 "strings" 6 "testing" 7 8 "github.com/yuin/goldmark" 9 "github.com/yuin/goldmark/parser" 10 ) 11 12 var testMetaFormats = []string{"yaml", "json", "toml"} 13 var validSource = map[string]string{ 14 "yaml": `<!--: 15 Title: mmd 16 Summary: Add YAML metadata to the document 17 Tags: 18 - markdown 19 - goldmark 20 :--> 21 22 Markdown with metadata 23 `, 24 "json": `<!--{ "Title": "mmd", "Summary": "Add JSON metadata to the document", "Tags": [ "markdown", "goldmark" ] }--> 25 Markdown with metadata`, 26 "toml": `<!--# Title = "mmd" 27 Summary = "Add TOML metadata to the document" 28 Tags = [ "markdown", "goldmark" ] #--> 29 Markdown with metadata 30 `, 31 } 32 var invalidSource = map[string]string{ 33 "yaml": `<!--: 34 Title: mmd 35 Summary: Add YAML metadata to the document 36 Tags: 37 - : { 38 } 39 - markdown 40 - goldmark 41 :--> 42 43 Markdown with metadata`, 44 "json": `<!--{ "Title:" "mmd", "Summary": "Add JSON metadata to the document", "Tags": [ "markdown", "goldmark" ] }--> 45 Markdown with metadata`, 46 "toml": `<!--# Title = "mmd" 47 Summary = "Add TOML metadata to the document 48 Tags == [ markdown", "goldmark ] #--> 49 Markdown with metadata 50 `, 51 } 52 53 func TestMeta(t *testing.T) { 54 markdown := goldmark.New(goldmark.WithExtensions(MetaMarkdown)) 55 context := parser.NewContext() 56 57 for _, format := range testMetaFormats { 58 var buf bytes.Buffer 59 if err := markdown.Convert([]byte(validSource[format]), &buf, parser.WithContext(context)); err != nil { 60 t.Fatal(err) 61 } 62 63 metaData := Get(context) 64 65 title := metaData["Title"] 66 if s, ok := title.(string); !ok { 67 t.Errorf("%s: Title not found in meta data or is not a string", format) 68 } else if s != "mmd" { 69 t.Errorf("%s: Title must be 'mmd', but got %v", format, s) 70 } 71 72 if buf.String() != "<p>Markdown with metadata</p>\n" { 73 t.Errorf("%s: should render '<p>Markdown with metadata</p>', but '%s'", format, buf.String()) 74 } 75 76 if tags, ok := metaData["Tags"].([]interface{}); !ok { 77 t.Errorf("%s: Tags not found in meta data or is not a slice", format) 78 } else if len(tags) != 2 { 79 t.Errorf("%s: Tags must be a slice that has 2 elements", format) 80 } else if tags[0] != "markdown" { 81 t.Errorf("%s: Tag#1 must be 'markdown', but got %s", format, tags[0]) 82 } else if tags[1] != "goldmark" { 83 t.Errorf("%s: Tag#2 must be 'goldmark', but got %s", format, tags[1]) 84 } 85 } 86 } 87 88 func TestMeta_Error(t *testing.T) { 89 markdown := goldmark.New(goldmark.WithExtensions(MetaMarkdown)) 90 context := parser.NewContext() 91 92 var buf bytes.Buffer 93 var str string 94 for _, format := range testMetaFormats { 95 if err := markdown.Convert([]byte(invalidSource[format]), &buf, parser.WithContext(context)); err != nil { 96 t.Fatal(err) 97 } 98 str = buf.String() 99 if !strings.Contains(str, `<!-- meta error, `) { 100 t.Errorf("%s: invalid error output '%s'", format, str) 101 } 102 buf.Reset() 103 } 104 }