stringutils_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package stringutils2
  15. import (
  16. "reflect"
  17. "strconv"
  18. "testing"
  19. )
  20. func TestEscapeString(t *testing.T) {
  21. type args struct {
  22. str string
  23. pairs [][]string
  24. }
  25. tests := []struct {
  26. name string
  27. args args
  28. want string
  29. }{
  30. {
  31. name: "normalInput",
  32. args: args{
  33. str: "abcd\n\"Te\\rst\"ddd\"$Test\"aaa\n$TTT",
  34. pairs: nil,
  35. },
  36. want: `abcd\n\"Te\\rst\"ddd\"\$Test\"aaa\n\$TTT`,
  37. },
  38. }
  39. for _, tt := range tests {
  40. t.Run(tt.name, func(t *testing.T) {
  41. if got := EscapeString(tt.args.str, tt.args.pairs); got != tt.want {
  42. t.Errorf("EscapeString() = %v, want %v", got, tt.want)
  43. }
  44. })
  45. }
  46. }
  47. func TestSplitByQuotation(t *testing.T) {
  48. type args struct {
  49. line string
  50. }
  51. tests := []struct {
  52. name string
  53. args args
  54. want []string
  55. wantErr bool
  56. }{
  57. {
  58. name: "normalInput",
  59. args: args{`"abc" addf "sada"`},
  60. want: []string{"abc", " addf ", "sada"},
  61. wantErr: false,
  62. },
  63. {
  64. name: "errorInput",
  65. args: args{`"abc" "addf "sada"`},
  66. want: nil,
  67. wantErr: true,
  68. },
  69. }
  70. for _, tt := range tests {
  71. t.Run(tt.name, func(t *testing.T) {
  72. got, err := SplitByQuotation(tt.args.line)
  73. if (err != nil) != tt.wantErr {
  74. t.Errorf("SplitByQuotation() error = %v, wantErr %v", err, tt.wantErr)
  75. return
  76. }
  77. if !reflect.DeepEqual(got, tt.want) {
  78. t.Errorf("SplitByQuotation() = %v, want %v", got, tt.want)
  79. }
  80. })
  81. }
  82. }
  83. func TestEscapeEchoString(t *testing.T) {
  84. type args struct {
  85. str string
  86. }
  87. tests := []struct {
  88. name string
  89. args args
  90. want string
  91. wantErr bool
  92. }{
  93. {
  94. name: "normalInput",
  95. args: args{"abcd\n\"Te\\rst\"ddd\"$Test\"aaa\n$TTT"},
  96. want: `abcd\n\"Te\\\\rst\"ddd\"\$Test\"aaa\n\$TTT`,
  97. wantErr: false,
  98. },
  99. {
  100. name: "echoInput",
  101. args: args{"SUBSYSTEM==\"usb\", ATTRS{idVendor}==\"1d6b\", ATTRS{idProduct}==\"0001\", RUN+=\"/bin/sh -c 'echo enabled > /sys$env{DEVPATH}/../power/wakeup'\""},
  102. want: `SUBSYSTEM==\"usb\", ATTRS{idVendor}==\"1d6b\", ATTRS{idProduct}==\"0001\", RUN+=\"/bin/sh -c 'echo enabled > /sys\$env{DEVPATH}/../power/wakeup'\"`,
  103. wantErr: false,
  104. },
  105. }
  106. for _, tt := range tests {
  107. t.Run(tt.name, func(t *testing.T) {
  108. got, err := EscapeEchoString(tt.args.str)
  109. if (err != nil) != tt.wantErr {
  110. t.Errorf("EscapeEchoString() error = %v, wantErr %v", err, tt.wantErr)
  111. return
  112. }
  113. if got != tt.want {
  114. t.Errorf("EscapeEchoString() = %v, want %v", got, tt.want)
  115. }
  116. })
  117. }
  118. }
  119. func TestGetCharTypeCount(t *testing.T) {
  120. cases := []struct {
  121. in string
  122. want int
  123. }{
  124. {
  125. in: "",
  126. want: 0,
  127. },
  128. {
  129. in: "123",
  130. want: 1,
  131. },
  132. {
  133. in: "abc",
  134. want: 1,
  135. },
  136. {
  137. in: "abcAbc",
  138. want: 2,
  139. },
  140. {
  141. in: "123dbA",
  142. want: 3,
  143. },
  144. {
  145. in: "123@Acv",
  146. want: 4,
  147. },
  148. {
  149. in: "中文",
  150. want: 1,
  151. },
  152. }
  153. for _, c := range cases {
  154. got := GetCharTypeCount(c.in)
  155. if got != c.want {
  156. t.Errorf("GetCharTypeCount %s want %d got %d", c.in, c.want, got)
  157. }
  158. }
  159. }
  160. func TestRoleName(t *testing.T) {
  161. cases := []struct {
  162. in string
  163. want string
  164. random bool
  165. length int
  166. }{
  167. {
  168. in: "小琪",
  169. random: true,
  170. length: 17,
  171. },
  172. {
  173. in: "123^567",
  174. want: "123567",
  175. },
  176. }
  177. for _, c := range cases {
  178. got := GenerateRoleName(c.in)
  179. if c.random {
  180. if len(got) != c.length {
  181. t.Errorf("GenerateRoleName %s random want %d length got %d(%s)", c.in, c.length, len(got), got)
  182. }
  183. } else if got != c.want {
  184. t.Errorf("GenerateRoleName %s want %s got %s", c.in, c.want, got)
  185. }
  186. }
  187. }
  188. func TestFilterEmpty(t *testing.T) {
  189. cases := []struct {
  190. input []string
  191. want []string
  192. }{
  193. {
  194. input: []string{
  195. "",
  196. },
  197. want: []string{},
  198. },
  199. }
  200. for _, c := range cases {
  201. got := FilterEmpty(c.input)
  202. if !reflect.DeepEqual(got, c.want) {
  203. t.Errorf("want: %#v got: %#v", c.want, got)
  204. }
  205. }
  206. }
  207. func TestPrettyFloat(t *testing.T) {
  208. cases := []struct {
  209. in float64
  210. prec int
  211. want string
  212. }{
  213. {
  214. in: 3.1415926,
  215. prec: 2,
  216. want: "3.14",
  217. },
  218. {
  219. in: 3.1415926,
  220. prec: 3,
  221. want: "3.142",
  222. },
  223. {
  224. in: 3.89999999,
  225. prec: 2,
  226. want: "3.9",
  227. },
  228. {
  229. in: 3.88999999,
  230. prec: 2,
  231. want: "3.89",
  232. },
  233. {
  234. in: 3.99999999,
  235. prec: 2,
  236. want: "4",
  237. },
  238. {
  239. in: 0.000020000001,
  240. prec: 2,
  241. want: "0.00002",
  242. },
  243. {
  244. in: 0.000021000001,
  245. prec: 2,
  246. want: "0.000021",
  247. },
  248. {
  249. in: 0.000021100001,
  250. prec: 2,
  251. want: "0.000021",
  252. },
  253. {
  254. in: -3.15,
  255. prec: 1,
  256. want: "-3.2",
  257. },
  258. {
  259. in: 3.5,
  260. prec: 0,
  261. want: "4",
  262. },
  263. {
  264. in: 0.999999999999,
  265. prec: 2,
  266. want: "1",
  267. },
  268. }
  269. for _, c := range cases {
  270. got := PrettyFloat(c.in, c.prec)
  271. if got != c.want {
  272. t.Errorf("%s precision %d want %s got %s", strconv.FormatFloat(c.in, 'f', -1, 64), c.prec, c.want, got)
  273. }
  274. }
  275. }