labels_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 labels
  14. import (
  15. "testing"
  16. )
  17. func matches(t *testing.T, ls Set, want string) {
  18. if ls.String() != want {
  19. t.Errorf("Expected '%s', but got '%s'", want, ls.String())
  20. }
  21. }
  22. func TestSetString(t *testing.T) {
  23. matches(t, Set{"x": "y"}, "x=y")
  24. matches(t, Set{"foo": "bar"}, "foo=bar")
  25. matches(t, Set{"foo": "bar", "baz": "qup"}, "baz=qup,foo=bar")
  26. // TODO: Make our label representation robust enough to handle labels
  27. // with ",=!" characters in their names.
  28. }
  29. func TestLabelHas(t *testing.T) {
  30. labelHasTests := []struct {
  31. Ls Labels
  32. Key string
  33. Has bool
  34. }{
  35. {Set{"x": "y"}, "x", true},
  36. {Set{"x": ""}, "x", true},
  37. {Set{"x": "y"}, "foo", false},
  38. }
  39. for _, lh := range labelHasTests {
  40. if has := lh.Ls.Has(lh.Key); has != lh.Has {
  41. t.Errorf("%#v.Has(%#v) => %v, expected %v", lh.Ls, lh.Key, has, lh.Has)
  42. }
  43. }
  44. }
  45. func TestLabelGet(t *testing.T) {
  46. ls := Set{"x": "y"}
  47. if ls.Get("x") != "y" {
  48. t.Errorf("Set.Get is broken")
  49. }
  50. }