-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawght_test.go
165 lines (131 loc) · 4.17 KB
/
drawght_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package drawght_test
import "github.com/drawght/drawght-parser-go"
import (
"io/ioutil"
"encoding/json"
"fmt"
"strings"
"testing"
)
var dataset = map[string]interface{} {
"title": "This is a title",
"author": "Nathan Never",
"tags": []string {
"Template",
"Draft",
},
}
var templatesWithKeysOnly = map[string]string {
"<h1>{title}</h1>": fmt.Sprintf("<h1>%s</h1>", dataset["title"]),
"# {title} written by {author}": fmt.Sprintf("# %s written by %s", dataset["title"], dataset["author"]),
"- {tags}": "",
}
func TestParseTemplate(t *testing.T) {
tagsSlice := make([]string, len(dataset["tags"].([]string)))
for i, tag := range (dataset["tags"].([]string)) {
tagsSlice[i] = fmt.Sprintf("- %s", tag)
}
templatesWithKeysOnly["- {tags}"] = strings.Join(tagsSlice[:], "\n")
for template, expected := range(templatesWithKeysOnly) {
result := drawght.ParseTemplate(template, dataset)
if (result != expected) {
t.Errorf("Template '%s' not parsed", template)
}
}
}
func TestParseKeys(t *testing.T) {
templateLines := make([]string, len(templatesWithKeysOnly))
expectedLines := make([]string, len(templatesWithKeysOnly))
i := 0
for template, expected := range(templatesWithKeysOnly) {
templateLines[i] = template
expectedLines[i] = expected
i++
}
template := strings.Join(templateLines[:], "\n\n")
expected := strings.Join(expectedLines[:], "\n\n")
result := drawght.ParseKeys(template, dataset)
if result != expected {
t.Errorf("Template not parsed")
t.Errorf("Template:\n%v\n", template)
t.Errorf("Expected:\n%v\n", expected)
t.Errorf("Result:\n%v\n", result)
}
}
func TestParseQueryKeys(t *testing.T) {
var templatesWithQueries = map[string]string {
"# Written by {author.name}": "# Written by Hallison Batista",
}
testParse(drawght.ParseKeys, t, templatesWithQueries, getDataset())
}
func TestParseQueryItems(t *testing.T) {
var templatesWithQueries = map[string]string {
"- {references#1.name}": "- Mustache",
"- {references#2.name}": "- Handlebars",
"- {references#3.name}": "- {references#3.name}",
"- {tags#1}": "- Template",
"- {tags}": "- Template\n- Draft",
}
testParse(drawght.ParseKeys, t, templatesWithQueries, getDataset())
}
func TestParseQueryLists(t *testing.T) {
var templatesWithQueries = map[string]string {
"- {references:name} {references:url}": "- Mustache //mustache.github.io\n- Handlebars //handlebarsjs.com",
"- [{author.networks:name}]({author.networks:url})": "- [Dev.to](//dev.to/hallison)\n- [Github](//github.com/hallison)\n- [Twitter](//twitter.com/hallison)",
}
testParse(drawght.ParseQueries, t, templatesWithQueries, getDataset())
}
func TestParse(t *testing.T) {
template := getTemplate()
expected := getExpected()
result := drawght.Parse(template, getDataset())
if result != expected {
t.Errorf("Template not parsed")
t.Errorf("Template:\n%v\n", template)
t.Errorf("Expected:\n%v\n", expected)
t.Errorf("Result:\n%v\n", result)
}
}
func getDataset() (dataset map[string]interface{}) {
jsonContent, fail := ioutil.ReadFile("example.json")
if fail != nil {
fmt.Println(fail)
}
fail = json.Unmarshal(jsonContent, &dataset)
if fail != nil {
fmt.Println(fail)
}
return dataset
}
func testParse(parse func(string, map[string]interface{})(string), t *testing.T, templatesWithQueries map[string]string, dataset map[string]interface{}) {
templateLines := make([]string, len(templatesWithQueries))
expectedLines := make([]string, len(templatesWithQueries))
i := 0
for template, expected := range(templatesWithQueries) {
templateLines[i] = template
expectedLines[i] = expected
i++
}
template := strings.Join(templateLines[:], "\n\n")
expected := strings.Join(expectedLines[:], "\n\n")
result := parse(template, dataset)
if result != expected {
t.Errorf("Template not parsed")
t.Errorf("Template:\n%v\n", template)
t.Errorf("Expected:\n%v\n", expected)
t.Errorf("Result:\n%v\n", result)
}
}
func getTemplate() (template string) {
return readFile("example.in")
}
func getExpected() (expected string) {
return readFile("example.out")
}
func readFile(filename string) (string) {
lines, fail := ioutil.ReadFile(filename)
if fail != nil {
fmt.Println(fail)
}
return string(lines)
}