github.com/lmittmann/w3@v0.20.0/internal/abi/lexer_test.go (about) 1 package abi 2 3 import ( 4 "errors" 5 "strconv" 6 "testing" 7 8 "github.com/google/go-cmp/cmp" 9 "github.com/google/go-cmp/cmp/cmpopts" 10 "github.com/lmittmann/w3/internal" 11 ) 12 13 func TestLexer(t *testing.T) { 14 tests := []struct { 15 Input string 16 WantItems []*item 17 WantErr error 18 }{ 19 {Input: "", WantItems: []*item{{itemTypeEOF, ""}}}, 20 {Input: "uint256", WantItems: []*item{{itemTypeID, "uint256"}, {itemTypeEOF, ""}}}, 21 {Input: "uint256[1]", WantItems: []*item{{itemTypeID, "uint256"}, {itemTypePunct, "["}, {itemTypeNum, "1"}, {itemTypePunct, "]"}, {itemTypeEOF, ""}}}, 22 {Input: "uint balance", WantItems: []*item{{itemTypeID, "uint"}, {itemTypeID, "balance"}, {itemTypeEOF, ""}}}, 23 {Input: "1", WantItems: []*item{{itemTypeNum, "1"}, {itemTypeEOF, ""}}}, 24 25 {Input: "0", WantErr: errors.New("unexpected character: 0")}, 26 {Input: "uint256[0]", WantErr: errors.New("unexpected character: 0")}, 27 } 28 29 for i, test := range tests { 30 t.Run(strconv.Itoa(i), func(t *testing.T) { 31 items, err := lex(test.Input) 32 if diff := cmp.Diff(test.WantErr, err, internal.EquateErrors()); diff != "" { 33 t.Fatalf("Err: (-want, +got)\n%s", diff) 34 } 35 if diff := cmp.Diff(test.WantItems, items, cmpopts.EquateEmpty()); diff != "" { 36 t.Fatalf("Items: (-want, +got)\n%s", diff) 37 } 38 }) 39 } 40 } 41 42 func lex(input string) ([]*item, error) { 43 l := newLexer(input) 44 45 var items []*item 46 for { 47 item, err := l.nextItem() 48 if err != nil { 49 return nil, err 50 } 51 52 items = append(items, item) 53 if item.Typ == itemTypeEOF { 54 break 55 } 56 } 57 return items, nil 58 }