github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/swarm/storage/feed/topic_test.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 19:16:44</date>
    10  //</624450119579734016>
    11  
    12  package feed
    13  
    14  import (
    15  	"testing"
    16  
    17  	"github.com/ethereum/go-ethereum/common/hexutil"
    18  )
    19  
    20  func TestTopic(t *testing.T) {
    21  	related, _ := hexutil.Decode("0xabcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789")
    22  	topicName := "test-topic"
    23  	topic, _ := NewTopic(topicName, related)
    24  	hex := topic.Hex()
    25  	expectedHex := "0xdfa89c750e3108f9c2aeef0123456789abcdef0123456789abcdef0123456789"
    26  	if hex != expectedHex {
    27  		t.Fatalf("Expected %s, got %s", expectedHex, hex)
    28  	}
    29  
    30  	var topic2 Topic
    31  	topic2.FromHex(hex)
    32  	if topic2 != topic {
    33  		t.Fatal("Expected recovered topic to be equal to original one")
    34  	}
    35  
    36  	if topic2.Name(related) != topicName {
    37  		t.Fatal("Retrieved name does not match")
    38  	}
    39  
    40  	bytes, err := topic2.MarshalJSON()
    41  	if err != nil {
    42  		t.Fatal(err)
    43  	}
    44  	expectedJSON := `"0xdfa89c750e3108f9c2aeef0123456789abcdef0123456789abcdef0123456789"`
    45  	equal, err := areEqualJSON(expectedJSON, string(bytes))
    46  	if err != nil {
    47  		t.Fatal(err)
    48  	}
    49  	if !equal {
    50  		t.Fatalf("Expected JSON to be %s, got %s", expectedJSON, string(bytes))
    51  	}
    52  
    53  	err = topic2.UnmarshalJSON(bytes)
    54  	if err != nil {
    55  		t.Fatal(err)
    56  	}
    57  	if topic2 != topic {
    58  		t.Fatal("Expected recovered topic to be equal to original one")
    59  	}
    60  
    61  }
    62