-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathhelpers_test.go
195 lines (172 loc) · 5.03 KB
/
helpers_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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"os"
"testing"
)
func TestDiffPath(t *testing.T) {
type tuple struct{ base, complete string }
for tu, expected := range map[tuple]string{
tuple{"/foo/bar", "/foo/bar"}: "",
tuple{"/U/f/src", "/U/f/src/bar.html"}: "bar.html",
tuple{"/foo/src/", "/foo/src/a/b.json"}: "a/b.json",
tuple{"//foo/src/", "/foo/src/a/b.json"}: "a/b.json",
tuple{"/foo//src/", "/foo/src/a/b.json"}: "a/b.json",
tuple{"/foo/src//", "/foo/src/a/b.json"}: "a/b.json",
tuple{"/foo/src///", "/foo/src/a/b.json"}: "a/b.json",
} {
if got := Relative(tu.base, tu.complete); expected != got {
t.Errorf("Relative(%s, %s): expected %s, got %s", tu.base, tu.complete, expected, got)
}
}
}
func TestMustCopy(t *testing.T) {
src, err := ioutil.TempFile(os.TempDir(), "grender-test-mustcopy")
if err != nil {
t.Fatal(err)
}
defer os.Remove(src.Name())
srcBuf := "the contents of\nthe file\n"
if n, err := src.Write([]byte(srcBuf)); err != nil {
t.Fatal(err)
} else if n < len(srcBuf) {
t.Fatalf("short write")
}
dst := src.Name() + ".copy"
Copy(dst, src.Name())
dstBuf, err := ioutil.ReadFile(dst)
if err != nil {
t.Fatal(err)
}
if len(dstBuf) != len(srcBuf) {
t.Fatalf("dst (%d) != src (%d)", len(dstBuf), len(srcBuf))
}
for i := 0; i < len(srcBuf); i++ {
if dstBuf[i] != srcBuf[i] {
t.Fatalf("dst[%d] (%d) != src[%d] (%d)", i, dstBuf[i], i, srcBuf[i])
}
}
}
func TestMustJSON(t *testing.T) {
tmpFile, err := ioutil.TempFile(os.TempDir(), "grender-test-mustjson")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpFile.Name())
buf := []byte(`{"a":"X","b":123}`)
if err := ioutil.WriteFile(tmpFile.Name(), buf, 0655); err != nil {
t.Fatal(err)
}
m := ParseJSON(Read(tmpFile.Name()))
a, ok := m["a"]
if !ok {
t.Fatal("'a' not present")
}
s, ok := a.(string)
if !ok {
t.Fatal("'a' not string")
}
if s != "X" {
t.Fatal("'a' not 'X'")
}
b, ok := m["b"]
if !ok {
t.Fatal("'b' not present")
}
i, ok := b.(float64)
if !ok {
t.Fatal("'b' not number")
}
if i != 123 {
t.Fatal("'b' not 123")
}
}
func TestTargetFileFor(t *testing.T) {
type tuple struct{ relativePath, ext string }
for src, expected := range map[tuple]string{
tuple{"/foo", ""}: *targetDir + "/foo",
tuple{"/foo", ".html"}: *targetDir + "/foo.html",
tuple{"/foo.blah", ".html"}: *targetDir + "/foo.html",
tuple{"/foo.html", ".blah"}: *targetDir + "/foo.blah",
tuple{"/a/b/c", ".php"}: *targetDir + "/a/b/c.php",
tuple{"/a/b/c.php", ".html"}: *targetDir + "/a/b/c.html",
} {
path, ext := *sourceDir+src.relativePath, src.ext
got := TargetFileFor(path, ext)
if expected != got {
t.Errorf("%s: expected '%s', got '%s'", path, expected, got)
}
}
}
func TestMustTemplate(t *testing.T) {
// TODO
// rather a lot of setup involved here
}
func TestSplitPath(t *testing.T) {
assert := func(a, b []string) {
if len(a) != len(b) {
t.Fatalf("%v != %v", a, b)
}
for i := 0; i < len(a); i++ {
if a[i] != b[i] {
t.Fatalf("%v != %v", a, b)
}
}
}
for path, expected := range map[string][]string{
"": []string{}, // special-case
"foo": []string{"foo"},
"/foo": []string{"foo"},
"foo/": []string{"foo"},
"a/b/c.d": []string{"a", "b", "c.d"},
"/foo/bar/baz.txt": []string{"foo", "bar", "baz.txt"},
} {
assert(SplitPath(path), expected)
}
}
func TestDefaultTitle(t *testing.T) {
for path, expected := range map[string]string{
"2013-01-01-foo-bar-baz.md": "Foo bar baz",
"/foo/2013-1-2-foo_bar-baz.md": "Foo bar baz",
} {
bt, _ := NewBlogTuple(path, ".html")
if got := bt.Title; expected != got {
t.Errorf("'%s': expected '%s', got '%s'", path, expected, got)
}
}
}
func TestDefaultDate(t *testing.T) {
for path, expected := range map[string]string{
"2013-01-01-foo-bar-baz.md": "2013 01 01",
"/foo/2013-1-2-foo_bar-baz.md": "2013 01 02",
} {
bt, _ := NewBlogTuple(path, ".html")
if got := bt.DateString(); expected != got {
t.Errorf("'%s': expected '%s', got '%s'", path, expected, got)
}
}
}
func TestSplatInto(t *testing.T) {
m := map[string]interface{}{}
assert := func(expected string) {
got, err := json.Marshal(m)
if err != nil {
t.Fatal(err)
}
if bytes.Compare([]byte(expected), got) != 0 {
t.Errorf("expected '%s', got '%s'", expected, string(got))
}
}
SplatInto(m, "foo", map[string]interface{}{"a": 1, "b": 2})
assert(`{"foo":{"a":1,"b":2}}`)
SplatInto(m, "bar/baz", map[string]interface{}{"x": map[string]string{"y": "z"}})
assert(`{"bar":{"baz":{"x":{"y":"z"}}},"foo":{"a":1,"b":2}}`)
SplatInto(m, "bar/baz", map[string]interface{}{"x": map[string]string{"y": "!"}})
assert(`{"bar":{"baz":{"x":{"y":"!"}}},"foo":{"a":1,"b":2}}`)
SplatInto(m, "bar/baz", map[string]interface{}{"x": map[string]string{"yy": "!!"}})
assert(`{"bar":{"baz":{"x":{"y":"!","yy":"!!"}}},"foo":{"a":1,"b":2}}`)
SplatInto(m, "foo", map[string]interface{}{"a": "x"})
assert(`{"bar":{"baz":{"x":{"y":"!","yy":"!!"}}},"foo":{"a":"x","b":2}}`)
}