-
Notifications
You must be signed in to change notification settings - Fork 0
/
blognow_test.go
88 lines (78 loc) · 1.88 KB
/
blognow_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
package main
import (
"os"
"path/filepath"
"testing"
"time"
)
func TestMakeBlogDir(t *testing.T) {
basepath := "test-blog"
paths := []string{
filepath.Join(basepath, "posts"),
filepath.Join(basepath, "posts/sample.md"),
filepath.Join(basepath, "config.toml"),
}
makeBlogDir(basepath)
// All sample files should exist.
for _, path := range paths {
_, err := os.Stat(path)
if os.IsNotExist(err) {
t.Errorf(path + " should exist; it does not.")
}
}
// Check the default files for the correct contents.
readFileAndCompare(paths[1], samplePost, t)
readFileAndCompare(paths[2], sampleConfig, t)
os.RemoveAll(basepath)
}
func TestParse(t *testing.T) {
fileContents := `---
title = "My First Post"
date = 2019-07-20T00:00:00Z
---
# Hello
This is my first blog post.
`
expected := Post{
Title: "My First Post",
Date: time.Date(2019, time.July, 20, 0, 0, 0, 0, time.UTC),
Content: "<h3>Hello</h3>\n<p>This is my first blog post.</p>\n",
}
actual := parse(fileContents)
if actual != expected {
t.Errorf("parse(%q) returned %q, want %q", fileContents, actual, expected)
}
}
func TestSlug(t *testing.T) {
cases := []struct {
in, want string
}{
{"My First Post", "my-first-post"},
{"Hello 世界", "hello-世界"},
{"", ""},
}
for _, c := range cases {
got := slug(c.in)
if got != c.want {
t.Errorf("slug(%q) == %q, want %q", c.in, got, c.want)
}
}
}
func TestCreateFile(t *testing.T) {
name := "xidf9.test"
contents := "This is the contents of the file."
createFile(name, contents)
readFileAndCompare(name, contents, t)
os.Remove(name)
}
func readFileAndCompare(path, expected string, t *testing.T) {
file, err := os.Open(path)
check(err)
data := make([]byte, 1000)
bytesRead, err := file.Read(data)
check(err)
actual := string(data[:bytesRead])
if actual != expected {
t.Errorf(path + " does not contain the expected default contents.")
}
}