builtin_typedef_test.go (1381B)
1 package tplc 2 3 import ( 4 "strconv" 5 "strings" 6 "testing" 7 8 "go.lair.cx/go-core/net/htmlx" 9 ) 10 11 func Test_handleTypeTag(t *testing.T) { 12 tests := []struct { 13 name string 14 src string 15 wantErr bool 16 want string 17 }{ 18 { 19 "self-closing tag", 20 "<interface class=\"test\" />", 21 false, 22 "type test interface {}\n\n", 23 }, 24 { 25 "empty tag", 26 "<interface class=\"test\"></interface>", 27 false, 28 "type test interface {}\n\n", 29 }, 30 { 31 "without class name", 32 "<interface>A()</interface>", 33 true, 34 "", 35 }, 36 { 37 "with content", 38 "<interface class=\"test\">A()</interface>", 39 false, 40 "type test interface {A()}\n\n", 41 }, 42 } 43 for _, tt := range tests { 44 t.Run(tt.name, func(t *testing.T) { 45 var ( 46 writer = NewTestWriter() 47 tokenizer = htmlx.NewTokenizer(strings.NewReader(tt.src)) 48 ) 49 if kind := tokenizer.Next(); kind != htmlx.SelfClosingTagToken && kind != htmlx.StartTagToken { 50 t.Errorf("invalid test: unexpected token: %s", kind.String()) 51 return 52 } 53 err := handleTypeTag(writer, tokenizer, "interface") 54 if (err != nil) != tt.wantErr { 55 t.Errorf("handleTypeTag() error = %v, wantErr %v", err, tt.wantErr) 56 return 57 } 58 if err != nil { 59 return 60 } 61 if got := writer.String(); got != tt.want { 62 t.Errorf("handleTypeTag() got = %s, want = %s", strconv.Quote(got), strconv.Quote(tt.want)) 63 } 64 }) 65 } 66 }