builtin_fallback_test.go (1179B)
1 package tplc 2 3 import ( 4 "strings" 5 "testing" 6 7 "go.lair.cx/go-core/net/htmlx" 8 ) 9 10 func Test_TagFallback(t *testing.T) { 11 tests := []struct { 12 name string 13 src string 14 wantErr bool 15 }{ 16 { 17 "self-closing tag", 18 "<test />", 19 false, 20 }, 21 { 22 "empty tag", 23 "<test></test>", 24 false, 25 }, 26 { 27 "with contents", 28 "<test><div>hi, this is dummy. <a>Aaa-</a></div></test>", 29 false, 30 }, 31 { 32 "with plain texts", 33 "<test>Hello, World!</test>", 34 false, 35 }, 36 { 37 "with incomplete content", 38 "<test><br></test>", 39 false, 40 }, 41 { 42 "incomplete", 43 "<test>dummy.", 44 true, 45 }, 46 } 47 tagHandler := TagFallback("test") 48 for _, tt := range tests { 49 t.Run(tt.name, func(t *testing.T) { 50 var ( 51 writer = NewTestWriter() 52 tokenizer = htmlx.NewTokenizer(strings.NewReader(tt.src)) 53 ) 54 if kind := tokenizer.Next(); kind != htmlx.SelfClosingTagToken && kind != htmlx.StartTagToken { 55 t.Errorf("invalid test: unexpected token: %s", kind.String()) 56 return 57 } 58 if err := tagHandler(writer.builder, writer, tokenizer); (err != nil) != tt.wantErr { 59 t.Errorf("TagFallback.func() error = %v, wantErr %v", err, tt.wantErr) 60 } 61 }) 62 } 63 }