This repository has been archived by the owner on Apr 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 354
/
transform_test.go
50 lines (42 loc) · 1.5 KB
/
transform_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
package survey
import (
"strings"
"testing"
)
func testStringTransformer(t *testing.T, f func(string) string) {
transformer := TransformString(f)
tests := []string{
"hello my name is",
"where are you from",
"does that matter?",
}
for _, tt := range tests {
if expected, got := f(tt), transformer(tt); expected != got {
t.Errorf("TransformString transformer failed to transform the answer, expected '%s' but got '%s'.", expected, got)
}
}
}
func TestTransformString(t *testing.T) {
testStringTransformer(t, strings.ToTitle) // all letters titled
testStringTransformer(t, strings.ToLower) // all letters lowercase
}
func TestComposeTransformers(t *testing.T) {
// create a transformer which makes no sense,
// remember: transformer can be used for any type
// we just test the built'n functions that
// happens to be for strings only.
transformer := ComposeTransformers(
Title,
ToLower,
)
ans := "my name is"
if expected, got := strings.ToLower(ans), transformer(ans); expected != got {
// the result should be lowercase.
t.Errorf("TestComposeTransformers transformer failed to transform the answer to title->lowercase, expected '%s' but got '%s'.", expected, got)
}
var emptyAns string
if expected, got := "", transformer(emptyAns); expected != got {
// TransformString transformers should be skipped and return zero value string
t.Errorf("TestComposeTransformers transformer failed to skip transforming on optional empty input, expected '%s' but got '%s'.", expected, got)
}
}