-
Notifications
You must be signed in to change notification settings - Fork 0
/
svach_test.go
executable file
·171 lines (134 loc) · 3.47 KB
/
svach_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
package svach_test
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"testing"
"github.com/nilsocket/svach"
)
var (
blnsDir = "blns"
assetDir = "assets"
blnsFile = filepath.Join(assetDir, "blns.json")
fileExistsCount = 0
)
var (
input, output []string
)
var s1 = svach.DefaultSvach
var s2, _ = svach.WithOpts("-", 235)
func init() {
input = fileData(blnsFile)
}
func TestAll(t *testing.T) {
os.RemoveAll(blnsDir)
testSvach(t, s1, "s1")
testSvach(t, s2, "s2")
}
func testSvach(t *testing.T, s *svach.Svach, prefix string) {
outputDir := filepath.Join(blnsDir, prefix+"name")
expectedOutputFile := filepath.Join(assetDir, prefix+"ExpectedName.json")
test(t, s.Name, outputDir, expectedOutputFile)
outputDir = filepath.Join(blnsDir, prefix+"clean")
expectedOutputFile = filepath.Join(assetDir, prefix+"ExpectedClean.json")
test(t, s.Clean, outputDir, expectedOutputFile)
}
func test(t *testing.T, fn func(string) string, outputDir, expectedOutputFile string) {
t.Log("\n\n" + expectedOutputFile + "\n")
output = testCommon(t, outputDir, fn)
// writeToFile(expectedOutputFile, output)
testValid(t, expectedOutputFile, output)
}
func testValid(t *testing.T, expectedOutputFile string, output []string) {
equal(t, fileData(expectedOutputFile), output)
}
func fileData(fileName string) []string {
data, _ := ioutil.ReadFile(fileName)
dataSlice := make([]string, 0, 550)
json.Unmarshal(data, &dataSlice)
return dataSlice
}
func equal(t *testing.T, expected, result []string) {
if (expected == nil) != (result == nil) {
t.Error("expected:", expected, "result:", result)
return
}
if len(expected) != len(result) {
t.Error("length doesn't match")
return
}
for i := range expected {
if expected[i] != result[i] {
t.Error(i+1, "expected:", expected[i], "got:", result[i])
t.Errorf("\n%+q\n%+q\n%+q\n", []byte(input[i]), []byte(expected[i]), []byte(result[i]))
return
}
}
}
func writeToFile(fileName string, data []string) {
encData, err := json.MarshalIndent(data, "", "")
if err != nil {
log.Println(err)
}
ioutil.WriteFile(fileName, encData, os.ModePerm)
}
func testCommon(t *testing.T, dir string, fn func(string) string) []string {
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
log.Fatalln(err)
}
output := make([]string, 0, 550)
for i, istr := range input {
res := fn(istr)
output = append(output, res)
createFile(t, i, dir, res)
}
return output
}
func createFile(t *testing.T, i int, dir, name string) {
file := filepath.Join(dir, name)
if fileExists(file) {
t.Log(i, ":", file, "already exists")
fileExistsCount++
return
}
f, err := os.Create(file)
if err != nil {
t.Error(i+2, file, err)
} else {
f.Close()
}
}
func fileExists(name string) bool {
_, err := os.Stat(name)
if os.IsNotExist(err) {
// if file doesn't exist return false
return false
}
return true
}
// Examples
func ExampleClean() {
res := svach.Clean(`.....Hello<>:/---\....W|orld?.!..`)
fmt.Println(res)
// Output: .Hello-.World.!
}
func ExampleWithOpts() {
s, _ := svach.WithOpts(" ", 6)
res := s.Name(`.....H<>e:l.!..`)
fmt.Println(res)
// Output: .H e l
}
func ExampleWithOpts_error() {
_, err := svach.WithOpts("?", 6)
fmt.Println(err)
// Output: Invalid characters like `., <, >, :, ", /, \, |, ?, *` exist in replaceStr
}
func ExampleName() {
// Incase of invalid filename, md5sum of filename is returned.
res := svach.Name(`<>:"/\|?*`)
fmt.Println(res)
// Output: 3e4bde3cb1e4c9cfa2db74bbc536d5e2
}