strings_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 strings
  14. import (
  15. "testing"
  16. )
  17. func TestSplitQualifiedName(t *testing.T) {
  18. testCases := []struct {
  19. input string
  20. output []string
  21. }{
  22. {"kubernetes.io/blah", []string{"kubernetes.io", "blah"}},
  23. {"blah", []string{"", "blah"}},
  24. {"kubernetes.io/blah/blah", []string{"kubernetes.io", "blah"}},
  25. }
  26. for i, tc := range testCases {
  27. namespace, name := SplitQualifiedName(tc.input)
  28. if namespace != tc.output[0] || name != tc.output[1] {
  29. t.Errorf("case[%d]: expected (%q, %q), got (%q, %q)", i, tc.output[0], tc.output[1], namespace, name)
  30. }
  31. }
  32. }
  33. func TestJoinQualifiedName(t *testing.T) {
  34. testCases := []struct {
  35. input []string
  36. output string
  37. }{
  38. {[]string{"kubernetes.io", "blah"}, "kubernetes.io/blah"},
  39. {[]string{"blah", ""}, "blah"},
  40. {[]string{"kubernetes.io", "blah"}, "kubernetes.io/blah"},
  41. }
  42. for i, tc := range testCases {
  43. res := JoinQualifiedName(tc.input[0], tc.input[1])
  44. if res != tc.output {
  45. t.Errorf("case[%d]: expected %q, got %q", i, tc.output, res)
  46. }
  47. }
  48. }
  49. func TestShortenString(t *testing.T) {
  50. testCases := []struct {
  51. input string
  52. out_len int
  53. output string
  54. }{
  55. {"kubernetes.io", 5, "kuber"},
  56. {"blah", 34, "blah"},
  57. {"kubernetes.io", 13, "kubernetes.io"},
  58. }
  59. for i, tc := range testCases {
  60. res := ShortenString(tc.input, tc.out_len)
  61. if res != tc.output {
  62. t.Errorf("case[%d]: expected %q, got %q", i, tc.output, res)
  63. }
  64. }
  65. }