-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathciigo_test.go
138 lines (114 loc) · 2.38 KB
/
ciigo_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
// SPDX-FileCopyrightText: 2022 M. Shulhan <[email protected]>
// SPDX-License-Identifier: GPL-3.0-or-later
package ciigo
import (
"html/template"
"os"
"path/filepath"
"regexp"
"sort"
"testing"
"git.sr.ht/~shulhan/pakakeh.go/lib/memfs"
"git.sr.ht/~shulhan/pakakeh.go/lib/test"
)
func TestMain(m *testing.M) {
var templateCSS = template.CSS(`body{}`)
_embeddedCSS = &templateCSS
os.Exit(m.Run())
}
func TestListFileMarkups(t *testing.T) {
type testCase struct {
excRegex string
exp []string
}
var cases = []testCase{{
excRegex: `(ex)/.*`,
exp: []string{
`testdata/in/clu/de/file.adoc`,
`testdata/in/clu/de/markdown.md`,
},
}, {
excRegex: `(in|ex)/.*`,
}, {
excRegex: `de`,
}, {
excRegex: `file$`,
exp: []string{
`testdata/ex/clu/de/file.adoc`,
`testdata/ex/clu/de/markdown.md`,
`testdata/in/clu/de/file.adoc`,
`testdata/in/clu/de/markdown.md`,
},
}}
var (
dir = `testdata`
c testCase
excre *regexp.Regexp
list map[string]*FileMarkup
got []string
k string
err error
)
for _, c = range cases {
excre = regexp.MustCompile(c.excRegex)
list, err = listFileMarkups(dir, []*regexp.Regexp{excre})
if err != nil {
t.Fatal(err)
}
got = make([]string, 0, len(list))
for k = range list {
got = append(got, k)
}
sort.Strings(got)
test.Assert(t, `list`, c.exp, got)
}
}
func TestGoEmbed(t *testing.T) {
type testCase struct {
tag string
opts EmbedOptions
}
var (
outDir = `testdata/goembed/out`
tdata *test.Data
err error
)
tdata, err = test.LoadData(`testdata/goembed/GoEmbed_test.txt`)
if err != nil {
t.Fatal(err)
}
var listCase = []testCase{{
opts: EmbedOptions{
ConvertOptions: ConvertOptions{
Root: `testdata/in/`,
},
EmbedOptions: memfs.EmbedOptions{
PackageName: `mypackage`,
VarName: `memfsIn`,
WithoutModTime: true,
},
},
tag: `default`,
}}
var (
tcase testCase
fname string
fpath string
got []byte
)
for _, tcase = range listCase {
// Set the output file name based on tag.
fname = tcase.tag + `.go`
fpath = filepath.Join(outDir, fname)
tcase.opts.EmbedOptions.GoFileName = filepath.Join(outDir, fname)
err = GoEmbed(tcase.opts)
if err != nil {
t.Fatal(err)
}
got, err = os.ReadFile(fpath)
if err != nil {
t.Fatal(err)
}
test.Assert(t, tcase.tag, string(tdata.Output[fname]), string(got))
}
}