i18n_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. "testing"
  18. )
  19. func TestIsUtf8(t *testing.T) {
  20. cases := []struct {
  21. In string
  22. Want bool
  23. }{
  24. {"中文", true},
  25. {"this is eng", false},
  26. }
  27. for _, c := range cases {
  28. got := IsUtf8(c.In)
  29. if got != c.Want {
  30. t.Errorf("IsUtf8 %s got %v want %v", c.In, got, c.Want)
  31. }
  32. }
  33. }
  34. func TestRemoveUtf8Strings(t *testing.T) {
  35. cases := []struct {
  36. in []string
  37. want []string
  38. }{
  39. {
  40. in: []string{},
  41. want: []string{},
  42. },
  43. {
  44. in: []string{"en", "中文"},
  45. want: []string{"en"},
  46. },
  47. {
  48. in: []string{"中文"},
  49. want: []string{},
  50. },
  51. }
  52. for _, c := range cases {
  53. if got := RemoveUtf8Strings(c.in); !reflect.DeepEqual(got, c.want) {
  54. t.Errorf("RemoveUtf8Strings %s got %v want %v", c.in, got, c.want)
  55. }
  56. }
  57. }
  58. func TestIsPrintableAscii(t *testing.T) {
  59. cases := []struct {
  60. in string
  61. want bool
  62. }{
  63. {
  64. in: "passw0rd",
  65. want: true,
  66. },
  67. {
  68. in: string([]byte{128, 45, 48}),
  69. want: false,
  70. },
  71. {
  72. in: "中文",
  73. want: false,
  74. },
  75. }
  76. for _, c := range cases {
  77. got := IsPrintableAsciiString(c.in)
  78. if got != c.want {
  79. t.Errorf("%s IsPringtableAsciiString got %v want %v", c.in, got, c.want)
  80. }
  81. }
  82. }