github.com/lmittmann/w3@v0.20.0/internal/hexutil/hash_test.go (about) 1 package hexutil_test 2 3 import ( 4 "math/big" 5 "strconv" 6 "testing" 7 8 "github.com/ethereum/go-ethereum/common" 9 "github.com/lmittmann/w3/internal/hexutil" 10 ) 11 12 var hashTests = []struct { 13 Raw string 14 Val hexutil.Hash 15 WantEnc string 16 }{ 17 {"0x0", hexutil.Hash{}, "0x0"}, 18 {"0x00", hexutil.Hash{}, "0x0"}, 19 {"0xc0fe", (hexutil.Hash)(common.BigToHash(big.NewInt(0xc0fe))), "0xc0fe"}, 20 {"0x000000000000000000000000000000000000000000000000000000000000c0fe", (hexutil.Hash)(common.BigToHash(big.NewInt(0xc0fe))), "0xc0fe"}, 21 } 22 23 func TestHashUnmarshalText(t *testing.T) { 24 for i, test := range hashTests { 25 t.Run(strconv.Itoa(i), func(t *testing.T) { 26 got := hexutil.Hash{} 27 if err := got.UnmarshalText([]byte(test.Raw)); err != nil { 28 t.Fatal(err) 29 } 30 31 if want := (common.Hash)(test.Val); want.Cmp((common.Hash)(got)) != 0 { 32 t.Fatalf("want %v, got %v", want, (common.Hash)(got)) 33 } 34 }) 35 } 36 } 37 38 func TestHashMarshalText(t *testing.T) { 39 for i, test := range hashTests { 40 t.Run(strconv.Itoa(i), func(t *testing.T) { 41 got, err := test.Val.MarshalText() 42 if err != nil { 43 t.Fatal(err) 44 } 45 46 if want := test.WantEnc; string(got) != want { 47 t.Fatalf("want %q, got %q", want, string(got)) 48 } 49 }) 50 } 51 }