File: t_strings_test.go

package info (click to toggle)
easygen 5.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 848 kB
  • sloc: sh: 14; makefile: 13
file content (246 lines) | stat: -rw-r--r-- 4,786 bytes parent folder | download
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package easygen_test

import (
	"bytes"
	"testing"

	"github.com/go-easygen/easygen"
)

type testItem struct {
	tmplStr, expected string
}

//var tmpl *template.Template

func testStringManipulation(t *testing.T, d []testItem) {
	tmpl = easygen.NewTemplate().Funcs(easygen.FuncDefs())
	for _, tc := range d {
		buf := bytes.NewBufferString("")
		easygen.Process0(tmpl, buf, tc.tmplStr, "test/strings")

		v := buf.String()
		//t.Log(v)
		if v != tc.expected {
			t.Errorf("'%s' expects '%s', got '%s'", tc.tmplStr, tc.expected, v)
		}
	}
}

// for standalone test, change package to `main` and the next func def to,
// func main() {
func TestStringManipulation(t *testing.T) {

	testData := []testItem{
		// == standard strings functions
		{
			`{{ stringsContains "seafood" "foo" }}`,
			"true",
		},
		{
			`{{ stringsContains "seafood" "bar" }}`,
			"false",
		},
		{
			`{{ stringsContainsAny "team" "i" }}`,
			"false",
		},
		{
			`{{ stringsContainsAny "failure" "u & i" }}`,
			"true",
		},
		{
			`{{ stringsCount "cheese" "e" }}`,
			"3",
		},
		{
			`{{ stringsEqualFold "Go" "go" }}`,
			"true",
		},
		{
			`{{ stringsFields "  foo bar  baz   " }}`,
			"[foo bar baz]",
		},
		{
			`{{ stringsIndex "go gopher" "go" }}`,
			"0",
		},
		{
			`{{ stringsLastIndex "go gopher" "go" }}`,
			"3",
		},
		{
			`ba{{ stringsRepeat "na" 2 }}`,
			"banana",
		},
		{
			`{{ stringsReplace "oink oink oink" "k" "ky" 2 }}`,
			"oinky oinky oink",
		},
		{
			`{{ stringsReplace "oink oink oink" "oink" "moo" -1 }}`,
			"moo moo moo",
		},
		{
			`{{ stringsSplitN "a,b,c" "," 0 }}`,
			`[]`,
		},
		{
			`{{ stringsSplitAfter "a,b,c" "," }}`,
			`[a, b, c]`,
		},
		{
			`{{ stringsSplitAfterN "a,b,c" "," 2 }}`,
			`[a, b,c]`,
		},
		{
			`{{ stringsTitle "her royal highness" }}`,
			"Her Royal Highness",
		},
		{
			`{{ stringsToLower "Gopher" }}`,
			"gopher",
		},
		{
			`{{ stringsToUpper "Gopher" }}`,
			"GOPHER",
		},
		{
			`{{ stringsTrimSpace " \t\n a lone gopher \n\t\r\n" }}`,
			"a lone gopher",
		},
		{
			`{{ stringsTrim " !!! Achtung !!! " "! " }}`,
			"Achtung",
		},
		{
			`{{ stringsTrimPrefix "Goodbye,, world!" "Goodbye," }}`,
			", world!",
		},
		{
			`{{ stringsTrimSuffix "Hello, goodbye, etc!" "goodbye, etc!" }}`,
			"Hello, ",
		},
		// aliases
		{
			`The {{if eq .StrTest "these rights belong to those people"}}eq says Yea{{else}}eq says Nay{{end}} but {{if eqf .StrTest "these rights belong to those people"}}eqf says Yea{{else}}eqf says Nay{{end}}.`,
			"The eq says Nay but eqf says Yea.",
		},
		{
			`{{$s := sprintf .StrTest}} {{$s}}`,
			" These rights belong to those people",
		},
		{
			`{{$s := sprintf "%s, %.2f" .StrTest 12.3456}} {{$s}}`,
			" These rights belong to those people, 12.35",
		},

		// == standard regexp functions
		{
			`{{ regexpFindString "peach punch" "p([a-z]+)ch" }}`,
			"peach",
		},
		{
			`{{ regexpFindAllString "peach punch" "p([a-z]+)ch" -1 }}`,
			"[peach punch]",
		},
		{
			`{{ regexpFindAllString "peach punch" "p([a-z]+)ch" 1 }}`,
			"[peach]",
		},
		{
			`{{ regexpFindStringIndex "peach punch" "p([a-z]+)ch" }}`,
			"[0 5]",
		},
		{
			`{{ regexpFindStringSubmatch "peach punch" "p([a-z]+)ch" }}`,
			"[peach ea]",
		},
		{
			`{{ regexpFindStringSubmatchIndex "peach punch" "p([a-z]+)ch" }}`,
			"[0 5 1 3]",
		},
		//
		{
			`{{ regexpMatchString "HTTPS://site/" "(?i)^http" }}`,
			"true",
		},
		//
		{
			`{{ regexpReplaceAllLiteralString "html HTML Html aa uml bb Uml" "(?i)html|uml" "XML" }}`,
			"XML XML XML aa XML bb XML",
		},
		{
			`{{ regexpReplaceAllString .StrTest "(?i)th[eo]se" "the" }}`,
			"the rights belong to the people",
		},
		{
			`{{ regexpReplaceAllString "This and these are for THOSE people" "(?i)(this|th[eo]se)" "<b>${1}</b>" }}`,
			"<b>This</b> and <b>these</b> are for <b>THOSE</b> people",
		},
		// {
		// 	`{{ regexpReplaceAllStringFunc "a peach" "p([a-z]+)ch" stringsToUpper }}`,
		// 	"a PEACH",
		// },
		// == my added strings functions
		{
			`{{ indent 2 "a" }}`,
			"a",
		},
		{
			`{{ indent 2 "a\nb\nc" }}`,
			"a\n  b\n  c",
		},
		{
			`{{ pindent 2 "a" }}`,
			"  a",
		},
		{
			`{{ pindent 2 "a\nb\nc" }}`,
			"  a\n  b\n  c",
		},
		{
			`{{ coalesce "a" }}`,
			"a",
		},
		{
			`{{ coalesce "" "b" }}`,
			"b",
		},
		{
			`{{ coalesce "" "" "c" }}`,
			"c",
		},
		{
			`{{ coalesce "" "" "" "" }}`,
			"",
		},
		{
			`{{ coalesce "" }}`,
			"",
		},
		{
			`{{ coalesce .StrTest "Something else" }}`,
			"These rights belong to those people",
		},
		{
			`{{ coalesce .StrEmpty "Something else" }}`,
			"Something else",
		},
		// The following failed the template.Execute before but now fixed
		{
			`{{ coalesce .StrNone "Not exist" }}`,
			"Not exist",
		},
	}

	testStringManipulation(t, testData)
}

/*
	{
		`{{  }}`,
		"",
	},

*/