2015-05-08 17:45:31 -05:00
|
|
|
package markdown
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
2015-05-09 05:49:54 -05:00
|
|
|
"strings"
|
2015-05-08 17:45:31 -05:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var TOML = [4]string{`
|
|
|
|
title = "A title"
|
|
|
|
template = "default"
|
|
|
|
[variables]
|
|
|
|
name = "value"
|
|
|
|
`,
|
|
|
|
`+++
|
|
|
|
title = "A title"
|
|
|
|
template = "default"
|
|
|
|
[variables]
|
|
|
|
name = "value"
|
|
|
|
+++
|
2015-05-09 05:49:54 -05:00
|
|
|
Page content
|
2015-05-08 17:45:31 -05: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 :
|
2015-05-12 18:44:35 -05:00
|
|
|
name : value
|
2015-05-08 17:45:31 -05:00
|
|
|
`,
|
|
|
|
`---
|
|
|
|
title : A title
|
|
|
|
template : default
|
|
|
|
variables :
|
2015-05-12 18:44:35 -05:00
|
|
|
name : value
|
2015-05-08 17:45:31 -05:00
|
|
|
---
|
2015-05-09 05:49:54 -05:00
|
|
|
Page content
|
2015-05-08 17:45:31 -05:00
|
|
|
`,
|
|
|
|
`---
|
|
|
|
title : A title
|
|
|
|
template : default
|
|
|
|
variables :
|
2015-05-12 18:44:35 -05:00
|
|
|
name : value
|
2015-05-08 17:45:31 -05:00
|
|
|
`,
|
|
|
|
`title : A title template : default variables : name : value`,
|
|
|
|
}
|
|
|
|
var JSON = [4]string{`
|
|
|
|
"title" : "A title",
|
|
|
|
"template" : "default",
|
|
|
|
"variables" : {
|
|
|
|
"name" : "value"
|
|
|
|
}
|
|
|
|
`,
|
2015-05-09 05:49:54 -05:00
|
|
|
`{
|
2015-05-08 17:45:31 -05:00
|
|
|
"title" : "A title",
|
|
|
|
"template" : "default",
|
|
|
|
"variables" : {
|
|
|
|
"name" : "value"
|
|
|
|
}
|
|
|
|
}
|
2015-05-09 05:49:54 -05:00
|
|
|
Page content
|
|
|
|
`,
|
|
|
|
`
|
2015-05-08 17:45:31 -05: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
|
|
|
|
}
|
|
|
|
}
|
2015-05-12 18:44:35 -05:00
|
|
|
return len(m.Variables) == 1
|
2015-05-08 17:45:31 -05: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
|
2015-05-09 05:49:54 -05:00
|
|
|
if _, err := v.parser.Parse([]byte(v.testData[0])); err == nil {
|
|
|
|
t.Fatalf("Expected error for invalid metadata for %v", v.name)
|
2015-05-08 17:45:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// metadata with identifiers
|
2015-05-09 05:49:54 -05:00
|
|
|
md, err := v.parser.Parse([]byte(v.testData[1]))
|
2015-05-08 17:45:31 -05:00
|
|
|
check(t, err)
|
2015-05-09 05:49:54 -05:00
|
|
|
if !compare(v.parser.Metadata()) {
|
2015-05-12 18:44:35 -05:00
|
|
|
t.Fatalf("Expected %v, found %v for %v", expected, v.parser.Metadata(), v.name)
|
2015-05-09 05:49:54 -05:00
|
|
|
}
|
|
|
|
if "Page content" != strings.TrimSpace(string(md)) {
|
|
|
|
t.Fatalf("Expected %v, found %v for %v", "Page content", string(md), v.name)
|
2015-05-08 17:45:31 -05: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
|
2015-05-09 05:49:54 -05:00
|
|
|
if _, err := v.parser.Parse([]byte(v.testData[2])); err == nil {
|
2015-05-08 17:45:31 -05:00
|
|
|
t.Fatalf("Expected error for missing closing identifier for %v", v.name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// invalid metadata
|
2015-05-09 05:49:54 -05:00
|
|
|
if md, err = v.parser.Parse([]byte(v.testData[3])); err == nil {
|
2015-05-08 17:45:31 -05:00
|
|
|
t.Fatalf("Expected error for invalid metadata for %v", v.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|