topic_test.go 1.2 KB

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