github.com/aristanetworks/goarista@v0.0.0-20240514173732-cca2755bbd44/path/wildcard_test.go (about)

     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  
     5  package path
     6  
     7  import (
     8  	"encoding/json"
     9  	"testing"
    10  
    11  	"github.com/aristanetworks/goarista/key"
    12  	"github.com/aristanetworks/goarista/value"
    13  )
    14  
    15  type pseudoWildcard struct{}
    16  
    17  func (w pseudoWildcard) Key() interface{} {
    18  	return struct{}{}
    19  }
    20  
    21  func (w pseudoWildcard) String() string {
    22  	return "*"
    23  }
    24  
    25  func (w pseudoWildcard) Equal(other interface{}) bool {
    26  	o, ok := other.(pseudoWildcard)
    27  	return ok && w == o
    28  }
    29  
    30  func TestWildcardUniqueness(t *testing.T) {
    31  	if Wildcard.Equal(pseudoWildcard{}) {
    32  		t.Fatal("Wildcard is not unique")
    33  	}
    34  	if Wildcard.Equal(struct{}{}) {
    35  		t.Fatal("Wildcard is not unique")
    36  	}
    37  	if Wildcard.Equal(key.New("*")) {
    38  		t.Fatal("Wildcard is not unique")
    39  	}
    40  }
    41  
    42  func TestWildcardTypeIsNotAKey(t *testing.T) {
    43  	var intf interface{} = WildcardType{}
    44  	_, ok := intf.(key.Key)
    45  	if ok {
    46  		t.Error("WildcardType should not implement key.Key")
    47  	}
    48  }
    49  
    50  func TestWildcardTypeEqual(t *testing.T) {
    51  	k1 := key.New(WildcardType{})
    52  	k2 := key.New(WildcardType{})
    53  	if !k1.Equal(k2) {
    54  		t.Error("They should be equal")
    55  	}
    56  	if !Wildcard.Equal(k1) {
    57  		t.Error("They should be equal")
    58  	}
    59  }
    60  
    61  func TestWildcardTypeAsValue(t *testing.T) {
    62  	var k value.Value = WildcardType{}
    63  	w := WildcardType{}
    64  	if k.ToBuiltin() != w {
    65  		t.Error("WildcardType.ToBuiltin is not correct")
    66  	}
    67  }
    68  
    69  func TestWildcardMarshalJSON(t *testing.T) {
    70  	b, err := json.Marshal(Wildcard)
    71  	if err != nil {
    72  		t.Fatal(err)
    73  	}
    74  	expected := `{"_wildcard":{}}`
    75  	if string(b) != expected {
    76  		t.Errorf("Invalid Wildcard json representation.\nExpected: %s\nReceived: %s",
    77  			expected, string(b))
    78  	}
    79  }