builtin_template_test.go (2984B)
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_renderTag(t *testing.T) { 12 tests := []struct { 13 name string 14 src string 15 plainText bool 16 tagName string 17 customHandlers map[string]TagHandler 18 useCustomOnly bool 19 wantErr bool 20 want string 21 }{ 22 { 23 "root document", 24 `<raw>fmt.Println("Hello, World!")</raw>`, 25 false, 26 "", 27 nil, 28 false, 29 false, 30 "fmt.Println(\"Hello, World!\")\n", 31 }, 32 { 33 "with wrapper", 34 // When renderTags is called with tagName, starting tag is 35 // already parsed. 36 `<raw>fmt.Println("Hello, World!")</raw></wrapper>`, 37 false, 38 "wrapper", 39 nil, 40 false, 41 false, 42 "fmt.Println(\"Hello, World!\")\n", 43 }, 44 { 45 "unknown tag with custom handler only flag", 46 `<div>Hello!</div></test>`, 47 false, 48 "test", 49 map[string]TagHandler{}, 50 true, 51 true, 52 "", 53 }, 54 { 55 "custom tag with standard tag", 56 `<raw>fmt.Println("Hello, World!")</raw> 57 <custom>Hi!</custom></test>`, 58 false, 59 "test", 60 map[string]TagHandler{ 61 "custom": TagTestTransparent("custom"), 62 }, 63 false, 64 false, 65 "fmt.Println(\"Hello, World!\")\nHi!", 66 }, 67 { 68 "with self-closing content", 69 `<custom /></test>`, 70 false, 71 "test", 72 map[string]TagHandler{ 73 "custom": func(b *Builder, w *Writer, t *htmlx.Tokenizer) error { 74 if t.CurrentType() != htmlx.SelfClosingTagToken { 75 w.WriteGoCode("Not Good") 76 return TagTestTransparent("custom")(b, w, t) 77 } 78 w.WriteGoCode("Good") 79 return nil 80 }, 81 }, 82 true, 83 false, 84 "Good", 85 }, 86 { 87 "plain text", 88 ` 89 Hello, World! 90 91 And this is second paragraph. 92 </template>`, 93 true, 94 "template", 95 nil, 96 false, 97 false, 98 "w.WriteRaw(`\nHello, World!\n\nAnd this is second paragraph.\n`)\n", 99 }, 100 { 101 "plain text with tag", 102 ` 103 Hello, <string>"World"</string>! 104 And this is second line. 105 </template>`, 106 true, 107 "template", 108 nil, 109 false, 110 false, 111 "w.WriteRaw(`\nHello, `)\n" + 112 "w.WriteString(\"World\")\n" + 113 "w.WriteRaw(`!\nAnd this is second line.\n`)\n", 114 }, 115 { 116 "attributes binding", 117 `<a :href="href">Link</a></test>`, 118 false, 119 "test", 120 nil, 121 false, 122 false, 123 "w.WriteRaw(`<a href=\"`)\nw.WriteString(href)\nw.WriteRaw(`\">Link</a>`)\n", 124 }, 125 } 126 for _, tt := range tests { 127 t.Run(tt.name, func(t *testing.T) { 128 var ( 129 writer = NewTestWriter() 130 tokenizer = htmlx.NewTokenizer(strings.NewReader(tt.src)) 131 ) 132 err := renderTag( 133 writer.builder, 134 writer, 135 tokenizer, 136 tt.tagName, 137 tt.customHandlers, 138 tt.useCustomOnly, 139 tt.plainText, 140 ) 141 if (err != nil) != tt.wantErr { 142 t.Errorf("renderTag() error = %v, wantErr %v", err, tt.wantErr) 143 return 144 } 145 if err != nil { 146 return 147 } 148 if got := writer.String(); got != tt.want { 149 t.Errorf("renderTag() got = %s, want = %s", strconv.Quote(got), strconv.Quote(tt.want)) 150 } 151 }) 152 } 153 }