builtin_import_test.go (1230B)
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_TagImport(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 "<import />", 21 false, 22 "", 23 }, 24 { 25 "empty tag", 26 "<import></import>", 27 false, 28 "", 29 }, 30 { 31 "import pkg", 32 "<import>\"pkg\"</import>", 33 false, 34 "import (\"pkg\")\n\n", 35 }, 36 { 37 "incomplete", 38 "<import>\"pkg\"", 39 true, 40 "", 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 := TagImport(writer.builder, writer, tokenizer) 54 if (err != nil) != tt.wantErr { 55 t.Errorf("TagImport() 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("renderTag() got = %s, want = %s", strconv.Quote(got), strconv.Quote(tt.want)) 63 } 64 }) 65 } 66 }