This repository was archived by the owner on Nov 19, 2024. It is now read-only.
forked from rsdoiel/mkpage
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathblogit_test.go
More file actions
135 lines (124 loc) · 4.46 KB
/
Copy pathblogit_test.go
File metadata and controls
135 lines (124 loc) · 4.46 KB
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
//
// Package mkpage is an experiment in a light weight template and markdown processor.
//
// @author R. S. Doiel, <rsdoiel@caltech.edu>
//
// Copyright (c) 2021, Caltech
// All rights not granted herein are expressly reserved by Caltech
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
package mkpage
import (
"fmt"
"io/ioutil"
"os"
"path"
"strings"
"testing"
)
func TestPrivateFuncs(t *testing.T) {
rsdoiel := "R. S. Doiel"
obj := map[string]string{}
obj["name"] = rsdoiel
objs := append([]interface{}{}, obj)
creators := unpackCreators(objs)
if len(creators) != 1 {
t.Errorf("expected one creator got %d", len(creators))
} else {
if fmt.Sprintf("%T", creators[0].Name) != "string" {
t.Errorf("Expected crestors[0].Name to ba a string, got %T", creators[0].Name)
}
if strings.Compare(creators[0].Name, rsdoiel) != 0 {
t.Errorf("expected creators[0].Name = %q, got %q", rsdoiel, creators[0].Name)
}
}
ymd, err := calcYMD("2003-01-02")
if err != nil {
t.Errorf("Unexpected err %s, calcYMD()", err)
t.FailNow()
}
if len(ymd) != 3 {
t.Errorf("expected len(ymd) = 3, got %+v", ymd)
} else {
for i, val := range []string{"2003", "01", "02"} {
if ymd[i] != val {
t.Errorf("expected %q, got %q", val, ymd[i])
}
}
expectedS := path.Join("blog", "2003", "01", "02")
resultS, err := calcPath("blog", ymd)
if err != nil {
t.Errorf("Unexpected err %s, calcPath()", err)
}
if expectedS != resultS {
t.Errorf("expected %q, got %q", expectedS, resultS)
}
}
}
func TestExportedFuncs(t *testing.T) {
var (
pName string
prefix string
blogPrefix string
blogJSON string
src []byte
blogMeta *BlogMeta
)
prefix = "test"
blogPrefix = path.Join(prefix, "blog")
blogJSON = path.Join(blogPrefix, "blog.json")
// Start with an empty blog ...
os.RemoveAll(path.Join("test", "blog"))
os.MkdirAll("test", 0777)
blogMeta = new(BlogMeta)
// Generate and write test data for BlogIt()
for i := 1; i <= 10; i = i + 1 {
src = []byte(fmt.Sprintf(`{
"title": "Hello No. %d",
"subtitle": "This is the %d(th) test blog post",
"date": "2021-05-%02d",
"keywords": [ "test" ],
"creators": [ "R. S. Doiel" ],
"byline": "By R. S. Doiel"
}
# Hello World!
Test Blog post.
`, i, i, i))
pName = path.Join(prefix, fmt.Sprintf("hello_%d.md", i))
if err := ioutil.WriteFile(pName, src, 0666); err != nil {
t.Errorf("Can't created %q, %s", pName, err)
t.FailNow()
}
dateString := fmt.Sprintf("2021-05-%02d", i)
if err := blogMeta.BlogIt(blogPrefix, pName, dateString); err != nil {
t.Errorf("BlogIt(%q, %q, %q) failed, %s", blogPrefix, pName, dateString, err)
t.FailNow()
}
if err := blogMeta.Save(blogJSON); err != nil {
t.Errorf("Failed to write %q, %s", blogJSON, err)
t.FailNow()
}
}
}
func TestRefreshFromPath(t *testing.T) {
meta := new(BlogMeta)
year := "2021"
prefix := "test"
blogPrefix := path.Join(prefix, "blog")
blogJSON := path.Join(blogPrefix, "blog.json")
os.Remove(blogJSON)
if err := meta.RefreshFromPath(blogPrefix, year); err != nil {
t.Errorf("expected nil, got %s", err)
t.FailNow()
}
meta.Save(blogJSON)
}