github.com/zxysilent/utils@v0.3.1/tmpl_test.go (about)

     1  package utils
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  )
     7  
     8  func TestLoadTmpl1(t *testing.T) {
     9  	_, err := LoadTmpl("./testdata/views1", nil)
    10  	if err == nil {
    11  		t.Error("must be error")
    12  	}
    13  }
    14  func TestLoadTmpl(t *testing.T) {
    15  	tmpl, err := LoadTmpl("./testdata/views", nil)
    16  	if err != nil {
    17  		t.Error(err.Error())
    18  	}
    19  	tmpls := tmpl.Templates()
    20  	for i := 0; i < len(tmpls); i++ {
    21  		if tmpls[i].Name() == "" {
    22  			t.Error("name eq ''")
    23  		}
    24  	}
    25  }
    26  
    27  func TestLoadTmplExec(t *testing.T) {
    28  	mod := struct {
    29  		Name string
    30  		Arr  []int
    31  	}{
    32  		Name: "testName",
    33  		Arr:  []int{1, 3, 5, 7},
    34  	}
    35  	tmpl, _ := LoadTmpl("./testdata/views", nil)
    36  	if tmpl == nil {
    37  		t.Error("error")
    38  	}
    39  	w := bytes.NewBuffer(nil)
    40  	tmpl.ExecuteTemplate(w, "subtmpl/subtmpl.html", mod)
    41  	if w.String() != "nametestName" {
    42  		t.Error("name neq 'nametestName'")
    43  	}
    44  }