caddy/middleware/markdown/metadata_test.go

166 lines
3.1 KiB
Go
Raw Normal View History

2015-05-09 06:45:31 +08:00
package markdown
import (
"bytes"
"fmt"
"reflect"
"strings"
2015-05-09 06:45:31 +08:00
"testing"
)
var TOML = [4]string{`
title = "A title"
template = "default"
[variables]
name = "value"
`,
`+++
title = "A title"
template = "default"
[variables]
name = "value"
+++
Page content
2015-05-09 06:45:31 +08:00
`,
`+++
title = "A title"
template = "default"
[variables]
name = "value"
`,
`title = "A title" template = "default" [variables] name = "value"`,
}
var YAML = [4]string{`
title : A title
template : default
variables :
name : value
2015-05-09 06:45:31 +08:00
`,
`---
title : A title
template : default
variables :
name : value
2015-05-09 06:45:31 +08:00
---
Page content
2015-05-09 06:45:31 +08:00
`,
`---
title : A title
template : default
variables :
name : value
2015-05-09 06:45:31 +08:00
`,
`title : A title template : default variables : name : value`,
}
var JSON = [4]string{`
"title" : "A title",
"template" : "default",
"variables" : {
"name" : "value"
}
`,
`{
2015-05-09 06:45:31 +08:00
"title" : "A title",
"template" : "default",
"variables" : {
"name" : "value"
}
}
Page content
`,
`
2015-05-09 06:45:31 +08:00
{
"title" : "A title",
"template" : "default",
"variables" : {
"name" : "value"
}
`,
`
{{
"title" : "A title",
"template" : "default",
"variables" : {
"name" : "value"
}
}
`,
}
func check(t *testing.T, err error) {
if err != nil {
t.Fatal(err)
}
}
func TestParsers(t *testing.T) {
expected := Metadata{
Title: "A title",
Template: "default",
Variables: map[string]interface{}{"name": "value"},
}
compare := func(m Metadata) bool {
if m.Title != expected.Title {
return false
}
if m.Template != expected.Template {
return false
}
for k, v := range m.Variables {
if v != expected.Variables[k] {
return false
}
}
return len(m.Variables) == 1
2015-05-09 06:45:31 +08:00
}
data := []struct {
parser MetadataParser
testData [4]string
name string
}{
{&JSONMetadataParser{}, JSON, "json"},
{&YAMLMetadataParser{}, YAML, "yaml"},
{&TOMLMetadataParser{}, TOML, "toml"},
}
for _, v := range data {
// metadata without identifiers
if _, err := v.parser.Parse([]byte(v.testData[0])); err == nil {
t.Fatalf("Expected error for invalid metadata for %v", v.name)
2015-05-09 06:45:31 +08:00
}
// metadata with identifiers
md, err := v.parser.Parse([]byte(v.testData[1]))
2015-05-09 06:45:31 +08:00
check(t, err)
if !compare(v.parser.Metadata()) {
t.Fatalf("Expected %v, found %v for %v", expected, v.parser.Metadata(), v.name)
}
if "Page content" != strings.TrimSpace(string(md)) {
t.Fatalf("Expected %v, found %v for %v", "Page content", string(md), v.name)
2015-05-09 06:45:31 +08:00
}
var line []byte
fmt.Fscanln(bytes.NewReader([]byte(v.testData[1])), &line)
if parser := findParser(line); parser == nil {
t.Fatalf("Parser must be found for %v", v.name)
} else {
if reflect.TypeOf(parser) != reflect.TypeOf(v.parser) {
t.Fatalf("parsers not equal. %v != %v", reflect.TypeOf(parser), reflect.TypeOf(v.parser))
}
}
// metadata without closing identifier
if _, err := v.parser.Parse([]byte(v.testData[2])); err == nil {
2015-05-09 06:45:31 +08:00
t.Fatalf("Expected error for missing closing identifier for %v", v.name)
}
// invalid metadata
if md, err = v.parser.Parse([]byte(v.testData[3])); err == nil {
2015-05-09 06:45:31 +08:00
t.Fatalf("Expected error for invalid metadata for %v", v.name)
}
}
}