github.com/xxRanger/go-ethereum@v1.8.23/swarm/storage/feed/topic_test.go (about)

     1  package feed
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/ethereum/go-ethereum/common/hexutil"
     7  )
     8  
     9  func TestTopic(t *testing.T) {
    10  	related, _ := hexutil.Decode("0xabcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789")
    11  	topicName := "test-topic"
    12  	topic, _ := NewTopic(topicName, related)
    13  	hex := topic.Hex()
    14  	expectedHex := "0xdfa89c750e3108f9c2aeef0123456789abcdef0123456789abcdef0123456789"
    15  	if hex != expectedHex {
    16  		t.Fatalf("Expected %s, got %s", expectedHex, hex)
    17  	}
    18  
    19  	var topic2 Topic
    20  	topic2.FromHex(hex)
    21  	if topic2 != topic {
    22  		t.Fatal("Expected recovered topic to be equal to original one")
    23  	}
    24  
    25  	if topic2.Name(related) != topicName {
    26  		t.Fatal("Retrieved name does not match")
    27  	}
    28  
    29  	bytes, err := topic2.MarshalJSON()
    30  	if err != nil {
    31  		t.Fatal(err)
    32  	}
    33  	expectedJSON := `"0xdfa89c750e3108f9c2aeef0123456789abcdef0123456789abcdef0123456789"`
    34  	equal, err := areEqualJSON(expectedJSON, string(bytes))
    35  	if err != nil {
    36  		t.Fatal(err)
    37  	}
    38  	if !equal {
    39  		t.Fatalf("Expected JSON to be %s, got %s", expectedJSON, string(bytes))
    40  	}
    41  
    42  	err = topic2.UnmarshalJSON(bytes)
    43  	if err != nil {
    44  		t.Fatal(err)
    45  	}
    46  	if topic2 != topic {
    47  		t.Fatal("Expected recovered topic to be equal to original one")
    48  	}
    49  
    50  }