tpls

Extendable, Fast Template Engine for Go
git clone git://git.lair.cx/tpls
Log | Files | Refs | README | LICENSE

builtin_test.go (1329B)


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