github.com/codingfuture/orig-energi3@v0.8.4/swarm/storage/feed/topic_test.go (about)

     1  // Copyright 2018 The Energi Core Authors
     2  // Copyright 2018 The go-ethereum Authors
     3  // This file is part of the Energi Core library.
     4  //
     5  // The Energi Core library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The Energi Core library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the Energi Core library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package feed
    19  
    20  import (
    21  	"testing"
    22  
    23  	"github.com/ethereum/go-ethereum/common/hexutil"
    24  )
    25  
    26  func TestTopic(t *testing.T) {
    27  	related, _ := hexutil.Decode("0xabcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789")
    28  	topicName := "test-topic"
    29  	topic, _ := NewTopic(topicName, related)
    30  	hex := topic.Hex()
    31  	expectedHex := "0xdfa89c750e3108f9c2aeef0123456789abcdef0123456789abcdef0123456789"
    32  	if hex != expectedHex {
    33  		t.Fatalf("Expected %s, got %s", expectedHex, hex)
    34  	}
    35  
    36  	var topic2 Topic
    37  	topic2.FromHex(hex)
    38  	if topic2 != topic {
    39  		t.Fatal("Expected recovered topic to be equal to original one")
    40  	}
    41  
    42  	if topic2.Name(related) != topicName {
    43  		t.Fatal("Retrieved name does not match")
    44  	}
    45  
    46  	bytes, err := topic2.MarshalJSON()
    47  	if err != nil {
    48  		t.Fatal(err)
    49  	}
    50  	expectedJSON := `"0xdfa89c750e3108f9c2aeef0123456789abcdef0123456789abcdef0123456789"`
    51  	equal, err := areEqualJSON(expectedJSON, string(bytes))
    52  	if err != nil {
    53  		t.Fatal(err)
    54  	}
    55  	if !equal {
    56  		t.Fatalf("Expected JSON to be %s, got %s", expectedJSON, string(bytes))
    57  	}
    58  
    59  	err = topic2.UnmarshalJSON(bytes)
    60  	if err != nil {
    61  		t.Fatal(err)
    62  	}
    63  	if topic2 != topic {
    64  		t.Fatal("Expected recovered topic to be equal to original one")
    65  	}
    66  
    67  }