util_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. Copyright 2014 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 util
  14. import (
  15. "testing"
  16. "k8s.io/kubernetes/pkg/util/diff"
  17. )
  18. func TestStringDiff(t *testing.T) {
  19. diff := diff.StringDiff("aaabb", "aaacc")
  20. expect := "aaa\n\nA: bb\n\nB: cc\n\n"
  21. if diff != expect {
  22. t.Errorf("diff returned %v", diff)
  23. }
  24. }
  25. func TestCompileRegex(t *testing.T) {
  26. uncompiledRegexes := []string{"endsWithMe$", "^startingWithMe"}
  27. regexes, err := CompileRegexps(uncompiledRegexes)
  28. if err != nil {
  29. t.Errorf("Failed to compile legal regexes: '%v': %v", uncompiledRegexes, err)
  30. }
  31. if len(regexes) != len(uncompiledRegexes) {
  32. t.Errorf("Wrong number of regexes returned: '%v': %v", uncompiledRegexes, regexes)
  33. }
  34. if !regexes[0].MatchString("Something that endsWithMe") {
  35. t.Errorf("Wrong regex returned: '%v': %v", uncompiledRegexes[0], regexes[0])
  36. }
  37. if regexes[0].MatchString("Something that doesn't endsWithMe.") {
  38. t.Errorf("Wrong regex returned: '%v': %v", uncompiledRegexes[0], regexes[0])
  39. }
  40. if !regexes[1].MatchString("startingWithMe is very important") {
  41. t.Errorf("Wrong regex returned: '%v': %v", uncompiledRegexes[1], regexes[1])
  42. }
  43. if regexes[1].MatchString("not startingWithMe should fail") {
  44. t.Errorf("Wrong regex returned: '%v': %v", uncompiledRegexes[1], regexes[1])
  45. }
  46. }
  47. func TestAllPtrFieldsNil(t *testing.T) {
  48. testCases := []struct {
  49. obj interface{}
  50. expected bool
  51. }{
  52. {struct{}{}, true},
  53. {struct{ Foo int }{12345}, true},
  54. {&struct{ Foo int }{12345}, true},
  55. {struct{ Foo *int }{nil}, true},
  56. {&struct{ Foo *int }{nil}, true},
  57. {struct {
  58. Foo int
  59. Bar *int
  60. }{12345, nil}, true},
  61. {&struct {
  62. Foo int
  63. Bar *int
  64. }{12345, nil}, true},
  65. {struct {
  66. Foo *int
  67. Bar *int
  68. }{nil, nil}, true},
  69. {&struct {
  70. Foo *int
  71. Bar *int
  72. }{nil, nil}, true},
  73. {struct{ Foo *int }{new(int)}, false},
  74. {&struct{ Foo *int }{new(int)}, false},
  75. {struct {
  76. Foo *int
  77. Bar *int
  78. }{nil, new(int)}, false},
  79. {&struct {
  80. Foo *int
  81. Bar *int
  82. }{nil, new(int)}, false},
  83. {(*struct{})(nil), true},
  84. }
  85. for i, tc := range testCases {
  86. if AllPtrFieldsNil(tc.obj) != tc.expected {
  87. t.Errorf("case[%d]: expected %t, got %t", i, tc.expected, !tc.expected)
  88. }
  89. }
  90. }