forked from andeya/goutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring_test.go
134 lines (121 loc) · 2.83 KB
/
string_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
package goutil
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestBytesToString(t *testing.T) {
bb := []byte("testing: BytesToString")
ss := BytesToString(bb)
t.Logf("type: %T, value: %v", ss, ss)
}
func TestStringToBytes(t *testing.T) {
s := "testing: StringToBytes"
b := StringToBytes(s)
t.Logf("type: %T, value: %v, val-string: %s\n", b, b, b)
b = append(b, '!')
t.Logf("after append:\ntype: %T, value: %v, val-string: %s\n", b, b, b)
}
func TestSnakeString(t *testing.T) {
data := [][2]string{
{"XxYy", "xx_yy"},
{"_XxYy", "_xx_yy"},
{"TcpRpc", "tcp_rpc"},
{"ID", "id"},
{"UserID", "user_id"},
{"RPC", "rpc"},
{"TCP_RPC", "tcp_rpc"},
{"wakeRPC", "wake_rpc"},
{"_TCP__RPC", "_tcp__rpc"},
{"_TcP__RpC_", "_tc_p__rp_c_"},
}
for _, p := range data {
r := SnakeString(p[0])
assert.Equal(t, p[1], r, p[0])
r = SnakeString(p[1])
assert.Equal(t, p[1], r, p[0])
}
}
func TestCamelString(t *testing.T) {
data := [][2]string{
{"_", "_"},
{"xx_yy", "XxYy"},
{"_xx_yy", "_XxYy"},
{"id", "Id"},
{"user_id", "UserId"},
{"rpc", "Rpc"},
{"tcp_rpc", "TcpRpc"},
{"wake_rpc", "WakeRpc"},
{"_tcp___rpc", "_Tcp__Rpc"},
{"_tc_p__rp_c__", "_TcP_RpC__"},
}
for _, p := range data {
r := CamelString(p[0])
assert.Equal(t, p[1], r, p[0])
r = CamelString(p[1])
assert.Equal(t, p[1], r, p[0])
}
}
func TestLintCamelString(t *testing.T) {
data := [][2]string{
{"_", "_"},
{"xx_yy", "XxYy"},
{"_xx_yy", "XxYy"},
{"id", "ID"},
{"user_id", "UserID"},
{"rpc", "RPC"},
{"tcp_rpc", "TCPRPC"},
{"wake_rpc", "WakeRPC"},
{"___tcp___rpc", "TCPRPC"},
{"_tc_p__rp_c__", "TcPRpC"},
}
for _, p := range data {
r := LintCamelString(p[0])
assert.Equal(t, p[1], r, p[0])
r = LintCamelString(p[1])
assert.Equal(t, p[1], r, p[0])
}
}
func TestHTMLEntityToUTF8(t *testing.T) {
want := `{"info":[["color","咖啡色|绿色"]]}`
got := HTMLEntityToUTF8(`{"info":[["color","ᕸᖹ⁐c;eff;⁐"]]}`, 16)
if got != want {
t.Fatalf("want: %q, got: %q", want, got)
}
}
func TestCodePointToUTF8(t *testing.T) {
got := CodePointToUTF8(`{"info":[["color","\u5496\u5561\u8272\u7c\u7eff\u8272"]]}`, 16)
want := `{"info":[["color","咖啡色|绿色"]]}`
if got != want {
t.Fatalf("want: %q, got: %q", want, got)
}
}
func TestSpaceInOne(t *testing.T) {
a := struct {
input string
output string
}{
input: `# authenticate method
// comment2
/* some other
comments */
`,
output: `# authenticate method
// comment2
/* some other
comments */
`,
}
r := SpaceInOne(a.input)
if r != a.output {
t.Fatalf("want: %q, got: %q", a.output, r)
}
}
func ExampleStringMarshalJSON() {
s := `<>&{}""`
fmt.Printf("%s\n", StringMarshalJSON(s, true))
fmt.Printf("%s\n", StringMarshalJSON(s, false))
// Output:
// "\u003c\u003e\u0026{}\"\""
// "<>&{}\"\""
}