generate_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 api
  14. import (
  15. "strings"
  16. "testing"
  17. )
  18. type nameGeneratorFunc func(base string) string
  19. func (fn nameGeneratorFunc) GenerateName(base string) string {
  20. return fn(base)
  21. }
  22. func TestGenerateName(t *testing.T) {
  23. testCases := []struct {
  24. meta ObjectMeta
  25. base string
  26. returned string
  27. }{
  28. {
  29. returned: "",
  30. },
  31. {
  32. meta: ObjectMeta{
  33. GenerateName: "test",
  34. },
  35. base: "test",
  36. returned: "test",
  37. },
  38. {
  39. meta: ObjectMeta{
  40. Name: "foo",
  41. GenerateName: "test",
  42. },
  43. base: "test",
  44. returned: "foo",
  45. },
  46. }
  47. for i, testCase := range testCases {
  48. GenerateName(nameGeneratorFunc(func(base string) string {
  49. if base != testCase.base {
  50. t.Errorf("%d: unexpected call with base", i)
  51. }
  52. return "test"
  53. }), &testCase.meta)
  54. expect := testCase.returned
  55. if expect != testCase.meta.Name {
  56. t.Errorf("%d: unexpected name: %#v", i, testCase.meta)
  57. }
  58. }
  59. }
  60. func TestSimpleNameGenerator(t *testing.T) {
  61. meta := &ObjectMeta{
  62. GenerateName: "foo",
  63. }
  64. GenerateName(SimpleNameGenerator, meta)
  65. if !strings.HasPrefix(meta.Name, "foo") || meta.Name == "foo" {
  66. t.Errorf("unexpected name: %#v", meta)
  67. }
  68. }