github.com/lmittmann/w3@v0.20.0/internal/hexutil/bytes_test.go (about)

     1  package hexutil_test
     2  
     3  import (
     4  	"bytes"
     5  	"strconv"
     6  	"testing"
     7  
     8  	"github.com/lmittmann/w3/internal/hexutil"
     9  )
    10  
    11  var bytesTests = []struct {
    12  	Raw     string
    13  	Val     hexutil.Bytes
    14  	WantEnc string
    15  }{
    16  	{"0xc0fe", hexutil.Bytes{0xc0, 0xfe}, "0xc0fe"},
    17  	{"c0fe", hexutil.Bytes{0xc0, 0xfe}, "0xc0fe"},
    18  	{"0Xc0fe", hexutil.Bytes{0xc0, 0xfe}, "0xc0fe"},
    19  }
    20  
    21  func TestBytesUnmarshalText(t *testing.T) {
    22  	for i, test := range bytesTests {
    23  		t.Run(strconv.Itoa(i), func(t *testing.T) {
    24  			var got hexutil.Bytes
    25  			if err := got.UnmarshalText([]byte(test.Raw)); err != nil {
    26  				t.Fatal(err)
    27  			}
    28  			if want := test.Val; !bytes.Equal(want, got) {
    29  				t.Fatalf("want %x, got %x", want, got)
    30  			}
    31  		})
    32  	}
    33  }
    34  
    35  func TestBytesMarshalText(t *testing.T) {
    36  	for i, test := range bytesTests {
    37  		t.Run(strconv.Itoa(i), func(t *testing.T) {
    38  			got, err := test.Val.MarshalText()
    39  			if err != nil {
    40  				t.Fatal(err)
    41  			}
    42  			if want := test.WantEnc; want != string(got) {
    43  				t.Fatalf("want %s, got %s", test.WantEnc, string(got))
    44  			}
    45  		})
    46  	}
    47  }