slice_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package slice
  14. import (
  15. "reflect"
  16. "testing"
  17. )
  18. func TestCopyStrings(t *testing.T) {
  19. var src1 []string
  20. dest1 := CopyStrings(src1)
  21. if !reflect.DeepEqual(src1, dest1) {
  22. t.Errorf("%v and %v are not equal", src1, dest1)
  23. }
  24. src2 := []string{}
  25. dest2 := CopyStrings(src2)
  26. if !reflect.DeepEqual(src2, dest2) {
  27. t.Errorf("%v and %v are not equal", src2, dest2)
  28. }
  29. src3 := []string{"a", "c", "b"}
  30. dest3 := CopyStrings(src3)
  31. if !reflect.DeepEqual(src3, dest3) {
  32. t.Errorf("%v and %v are not equal", src3, dest3)
  33. }
  34. src3[0] = "A"
  35. if reflect.DeepEqual(src3, dest3) {
  36. t.Errorf("CopyStrings didn't make a copy")
  37. }
  38. }
  39. func TestSortStrings(t *testing.T) {
  40. src := []string{"a", "c", "b"}
  41. dest := SortStrings(src)
  42. expected := []string{"a", "b", "c"}
  43. if !reflect.DeepEqual(dest, expected) {
  44. t.Errorf("SortString didn't sort the strings")
  45. }
  46. if !reflect.DeepEqual(src, expected) {
  47. t.Errorf("SortString didn't sort in place")
  48. }
  49. }
  50. func TestContainsString(t *testing.T) {
  51. src := []string{"aa", "bb", "cc"}
  52. if !ContainsString(src, "bb", nil) {
  53. t.Errorf("ContainsString didn't find the string as expected")
  54. }
  55. modifier := func(s string) string {
  56. if s == "cc" {
  57. return "ee"
  58. }
  59. return s
  60. }
  61. if !ContainsString(src, "ee", modifier) {
  62. t.Errorf("ContainsString didn't find the string by modifier")
  63. }
  64. }
  65. func TestRemoveString(t *testing.T) {
  66. modifier := func(s string) string {
  67. if s == "ab" {
  68. return "ee"
  69. }
  70. return s
  71. }
  72. tests := []struct {
  73. testName string
  74. input []string
  75. remove string
  76. modifier func(s string) string
  77. want []string
  78. }{
  79. {
  80. testName: "Nil input slice",
  81. input: nil,
  82. remove: "",
  83. modifier: nil,
  84. want: nil,
  85. },
  86. {
  87. testName: "Slice doesn't contain the string",
  88. input: []string{"a", "ab", "cdef"},
  89. remove: "NotPresentInSlice",
  90. modifier: nil,
  91. want: []string{"a", "ab", "cdef"},
  92. },
  93. {
  94. testName: "All strings removed, result is nil",
  95. input: []string{"a"},
  96. remove: "a",
  97. modifier: nil,
  98. want: nil,
  99. },
  100. {
  101. testName: "No modifier func, one string removed",
  102. input: []string{"a", "ab", "cdef"},
  103. remove: "ab",
  104. modifier: nil,
  105. want: []string{"a", "cdef"},
  106. },
  107. {
  108. testName: "No modifier func, all(three) strings removed",
  109. input: []string{"ab", "a", "ab", "cdef", "ab"},
  110. remove: "ab",
  111. modifier: nil,
  112. want: []string{"a", "cdef"},
  113. },
  114. {
  115. testName: "Removed both the string and the modifier func result",
  116. input: []string{"a", "cd", "ab", "ee"},
  117. remove: "ee",
  118. modifier: modifier,
  119. want: []string{"a", "cd"},
  120. },
  121. }
  122. for _, tt := range tests {
  123. if got := RemoveString(tt.input, tt.remove, tt.modifier); !reflect.DeepEqual(got, tt.want) {
  124. t.Errorf("%v: RemoveString(%v, %q, %T) = %v WANT %v", tt.testName, tt.input, tt.remove, tt.modifier, got, tt.want)
  125. }
  126. }
  127. }