-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers_test.go
More file actions
56 lines (49 loc) · 1.38 KB
/
helpers_test.go
File metadata and controls
56 lines (49 loc) · 1.38 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
package scraper
import (
"path"
"strconv"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestFormatString(t *testing.T) {
inputAndExpected := make(map[string]string, 0)
fillMap := func(mapp map[string]string) map[string]string {
mapp["{{PATH}} should be replaced"] = "path/to/somewhere should be replaced"
y, m, d := time.Now().Date()
var mon string
mo := strconv.Itoa(int(m))
if len(mo) == 1 {
for i := 0; i < len(mo); i++ {
mon += "0"
}
}
mon += strconv.Itoa(int(m))
mapp["Todays date is {{DATE}}"] = "Todays date is " + strconv.Itoa(d) + "." + mon + "." + strconv.Itoa(y)
return mapp
}
inputAndExpected = fillMap(inputAndExpected)
var vars []interface{}
vars = append(vars, time.Now())
funcs := make(map[string]interface{}, 0)
funcs["{{PATH}}"] = func(str string) string {
demoPath := path.Join("path", "to", "somewhere")
return strings.ReplaceAll(str, "{{PATH}}", demoPath)
}
funcs["{{DATE}}"] = func(str string, vars []interface{}) string {
y, m, d := vars[0].(time.Time).Date()
var mon string
mo := strconv.Itoa(int(m))
if len(mo) == 1 {
for i := 0; i < len(mo); i++ {
mon += "0"
}
}
mon += strconv.Itoa(int(m))
return strings.ReplaceAll(str, "{{DATE}}", strconv.Itoa(d)+"."+mon+"."+strconv.Itoa(y))
}
for k, v := range inputAndExpected {
assert.Equal(t, v, formatString(k, funcs, vars...))
}
}