github.com/segmentio/kafka-go@v0.4.48-0.20240318174348-3f6244eb34fd/resource_test.go (about)

     1  package kafka
     2  
     3  import "testing"
     4  
     5  func TestResourceTypeMarshal(t *testing.T) {
     6  	for i := ResourceTypeUnknown; i <= ResourceTypeDelegationToken; i++ {
     7  		text, err := i.MarshalText()
     8  		if err != nil {
     9  			t.Errorf("couldn't marshal %d to text: %s", i, err)
    10  		}
    11  		var got ResourceType
    12  		err = got.UnmarshalText(text)
    13  		if err != nil {
    14  			t.Errorf("couldn't unmarshal %s to ResourceType: %s", text, err)
    15  		}
    16  		if got != i {
    17  			t.Errorf("got %d, want %d", got, i)
    18  		}
    19  	}
    20  }
    21  
    22  // Verify that the text version of ResourceTypeBroker is "Cluster".
    23  // This is added since ResourceTypeBroker and ResourceTypeCluster
    24  // have the same value.
    25  func TestResourceTypeBroker(t *testing.T) {
    26  	text, err := ResourceTypeBroker.MarshalText()
    27  	if err != nil {
    28  		t.Errorf("couldn't marshal %d to text: %s", ResourceTypeBroker, err)
    29  	}
    30  	if string(text) != "Cluster" {
    31  		t.Errorf("got %s, want %s", string(text), "Cluster")
    32  	}
    33  	var got ResourceType
    34  	err = got.UnmarshalText(text)
    35  	if err != nil {
    36  		t.Errorf("couldn't unmarshal %s to ResourceType: %s", text, err)
    37  	}
    38  	if got != ResourceTypeBroker {
    39  		t.Errorf("got %d, want %d", got, ResourceTypeBroker)
    40  	}
    41  }
    42  
    43  func TestPatternTypeMarshal(t *testing.T) {
    44  	for i := PatternTypeUnknown; i <= PatternTypePrefixed; i++ {
    45  		text, err := i.MarshalText()
    46  		if err != nil {
    47  			t.Errorf("couldn't marshal %d to text: %s", i, err)
    48  		}
    49  		var got PatternType
    50  		err = got.UnmarshalText(text)
    51  		if err != nil {
    52  			t.Errorf("couldn't unmarshal %s to PatternType: %s", text, err)
    53  		}
    54  		if got != i {
    55  			t.Errorf("got %d, want %d", got, i)
    56  		}
    57  	}
    58  }