1
0

strutil_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package strutil
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. )
  6. func TestToUpperFirst(t *testing.T) {
  7. tests := []struct {
  8. name string
  9. s string
  10. expStr string
  11. }{
  12. {
  13. name: "empty string",
  14. },
  15. {
  16. name: "first letter is a digit",
  17. s: "123 let's go",
  18. expStr: "123 let's go",
  19. },
  20. {
  21. name: "lower to upper",
  22. s: "this is a sentence",
  23. expStr: "This is a sentence",
  24. },
  25. {
  26. name: "already in upper case",
  27. s: "Let's go",
  28. expStr: "Let's go",
  29. },
  30. }
  31. for _, test := range tests {
  32. t.Run(test.name, func(t *testing.T) {
  33. assert.Equal(t, test.expStr, ToUpperFirst(test.s))
  34. })
  35. }
  36. }
  37. func TestRandomChars(t *testing.T) {
  38. cache := make(map[string]bool)
  39. for range 100 {
  40. chars, err := RandomChars(10)
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. if cache[chars] {
  45. t.Fatalf("Duplicated chars %q", chars)
  46. }
  47. cache[chars] = true
  48. }
  49. }
  50. func TestEllipsis(t *testing.T) {
  51. tests := []struct {
  52. name string
  53. str string
  54. threshold int
  55. want string
  56. }{
  57. {
  58. name: "empty string and zero threshold",
  59. str: "",
  60. threshold: 0,
  61. want: "",
  62. },
  63. {
  64. name: "smaller length than threshold",
  65. str: "ab",
  66. threshold: 3,
  67. want: "ab",
  68. },
  69. {
  70. name: "same length as threshold",
  71. str: "abc",
  72. threshold: 3,
  73. want: "abc",
  74. },
  75. {
  76. name: "greater length than threshold",
  77. str: "ab",
  78. threshold: 1,
  79. want: "a...",
  80. },
  81. }
  82. for _, test := range tests {
  83. t.Run(test.name, func(t *testing.T) {
  84. got := Ellipsis(test.str, test.threshold)
  85. assert.Equal(t, test.want, got)
  86. })
  87. }
  88. }
  89. func TestTruncate(t *testing.T) {
  90. tests := []struct {
  91. name string
  92. str string
  93. limit int
  94. want string
  95. }{
  96. {
  97. name: "empty string with zero limit",
  98. str: "",
  99. limit: 0,
  100. want: "",
  101. },
  102. {
  103. name: "smaller length than limit",
  104. str: "ab",
  105. limit: 3,
  106. want: "ab",
  107. },
  108. {
  109. name: "same length as limit",
  110. str: "abc",
  111. limit: 3,
  112. want: "abc",
  113. },
  114. {
  115. name: "greater length than limit",
  116. str: "ab",
  117. limit: 1,
  118. want: "a",
  119. },
  120. }
  121. for _, test := range tests {
  122. t.Run(test.name, func(t *testing.T) {
  123. got := Truncate(test.str, test.limit)
  124. assert.Equal(t, test.want, got)
  125. })
  126. }
  127. }
  128. func TestContainsFold(t *testing.T) {
  129. ss := []string{"Alice", "Bob", "Charlie"}
  130. assert.True(t, ContainsFold(ss, "alice"))
  131. assert.True(t, ContainsFold(ss, "Alice"))
  132. assert.True(t, ContainsFold(ss, "ALICE"))
  133. assert.True(t, ContainsFold(ss, "bob"))
  134. assert.False(t, ContainsFold(ss, "dave"))
  135. assert.False(t, ContainsFold(nil, "alice"))
  136. assert.False(t, ContainsFold([]string{}, "alice"))
  137. }