wildcard_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright (c) 2018 Arista Networks, Inc.
  2. // Use of this source code is governed by the Apache License 2.0
  3. // that can be found in the COPYING file.
  4. package path
  5. import (
  6. "encoding/json"
  7. "testing"
  8. "notabug.org/themusicgod1/goarista/key"
  9. "notabug.org/themusicgod1/goarista/value"
  10. )
  11. type pseudoWildcard struct{}
  12. func (w pseudoWildcard) Key() interface{} {
  13. return struct{}{}
  14. }
  15. func (w pseudoWildcard) String() string {
  16. return "*"
  17. }
  18. func (w pseudoWildcard) Equal(other interface{}) bool {
  19. o, ok := other.(pseudoWildcard)
  20. return ok && w == o
  21. }
  22. func TestWildcardUniqueness(t *testing.T) {
  23. if Wildcard.Equal(pseudoWildcard{}) {
  24. t.Fatal("Wildcard is not unique")
  25. }
  26. if Wildcard.Equal(struct{}{}) {
  27. t.Fatal("Wildcard is not unique")
  28. }
  29. if Wildcard.Equal(key.New("*")) {
  30. t.Fatal("Wildcard is not unique")
  31. }
  32. }
  33. func TestWildcardTypeIsNotAKey(t *testing.T) {
  34. var intf interface{} = WildcardType{}
  35. _, ok := intf.(key.Key)
  36. if ok {
  37. t.Error("WildcardType should not implement key.Key")
  38. }
  39. }
  40. func TestWildcardTypeEqual(t *testing.T) {
  41. k1 := key.New(WildcardType{})
  42. k2 := key.New(WildcardType{})
  43. if !k1.Equal(k2) {
  44. t.Error("They should be equal")
  45. }
  46. if !Wildcard.Equal(k1) {
  47. t.Error("They should be equal")
  48. }
  49. }
  50. func TestWildcardTypeAsValue(t *testing.T) {
  51. var k value.Value = WildcardType{}
  52. w := WildcardType{}
  53. if k.ToBuiltin() != w {
  54. t.Error("WildcardType.ToBuiltin is not correct")
  55. }
  56. }
  57. func TestWildcardMarshalJSON(t *testing.T) {
  58. b, err := json.Marshal(Wildcard)
  59. if err != nil {
  60. t.Fatal(err)
  61. }
  62. expected := `{"_wildcard":{}}`
  63. if string(b) != expected {
  64. t.Errorf("Invalid Wildcard json representation.\nExpected: %s\nReceived: %s",
  65. expected, string(b))
  66. }
  67. }