-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhite600_test.go
92 lines (78 loc) · 1.8 KB
/
white600_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
/*
* white600_test
* available at https://github.com/TomSuzuki/white600/
*
* Copyright 2022 TomSuzuki
* LICENSE: MIT
*
* # How to use
* > gotest -run NONE -bench .
* > gotest -v
*/
//package white600
package white600_test
import (
"html/template"
"io/ioutil"
"path/filepath"
"strings"
"testing"
"github.com/TomSuzuki/white600"
)
type testFile struct {
markdown string
html string
}
func Test(t *testing.T) {
// test case (markdown, html)
dir := "./testcase/"
var testfile []testFile
// get list
files, _ := ioutil.ReadDir("./testcase/")
for _, file := range files {
if !file.IsDir() && filepath.Ext(file.Name()) == ".md" {
testfile = append(testfile, testFile{
markdown: dir + file.Name(),
html: dir + file.Name() + ".html",
})
}
}
// test
for i := range testfile {
test(testfile[i], t)
}
}
// speed test
func BenchmarkSpeed_white600(b *testing.B) {
file := "./testcase/00.md"
md, _ := ioutil.ReadFile(file)
b.ResetTimer()
for ct := 0; ct < 1500; ct++ {
white600.MarkdownToHTML(string(md))
}
}
func test(test testFile, t *testing.T) {
// load
b, _ := ioutil.ReadFile(test.html)
sample := string(b)
b, _ = ioutil.ReadFile(test.markdown)
answer := string(b)
// html -> markdown
answer = white600.MarkdownToHTML(answer)
// trim
sample = strings.NewReplacer("\r\n", "", "\r", "", "\n", "", " ", "", "'", "\"").Replace(sample)
answer = strings.NewReplacer("\r\n", "", "\r", "", "\n", "", " ", "", "'", "\"").Replace(answer)
// html
sampleHTML := template.HTML(sample)
answerHTML := template.HTML(answer)
// check
if sampleHTML != answerHTML {
t.Logf("☒ failed test: \t%s", test.markdown)
t.Logf(" - sample: %s", sampleHTML)
t.Logf(" - answer: %s", answerHTML)
t.Logf("")
t.Fail()
} else {
t.Logf("☑ success test: \t%s", test.markdown)
}
}