github.com/coveo/gotemplate@v2.7.7+incompatible/template/template_test.go (about)

     1  package template
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  )
     7  
     8  func Test_getTargetFile(t *testing.T) {
     9  	t.Parallel()
    10  
    11  	type args struct {
    12  		fileName   string
    13  		sourcePath string
    14  		targetPath string
    15  	}
    16  	tests := []struct {
    17  		name string
    18  		args args
    19  		want string
    20  	}{
    21  		{"Simple move", args{"/source/file", "/source", "/target"}, "/target/file"},
    22  		{"Relative", args{"source/file", "/source", "/target"}, "/target/source/file"},
    23  	}
    24  	for _, tt := range tests {
    25  		t.Run(tt.name, func(t *testing.T) {
    26  			if got := getTargetFile(tt.args.fileName, tt.args.sourcePath, tt.args.targetPath); got != tt.want {
    27  				t.Errorf("targetFile() = %v, want %v", got, tt.want)
    28  			}
    29  		})
    30  	}
    31  }
    32  
    33  func Test_templateWithErrors(t *testing.T) {
    34  	t.Parallel()
    35  
    36  	template, _ := NewTemplate(".", nil, "", nil)
    37  	template.SetOption(StrictErrorCheck, true)
    38  	tests := []struct {
    39  		name    string
    40  		content string
    41  		err     error
    42  	}{
    43  		{"Empty template", "", nil},
    44  		{"Non closed brace", "{{", fmt.Errorf("Non closed brace:1: unexpected unclosed action in command in: {{")},
    45  		{"Non opened brace", "}}", nil},
    46  		{"Undefined value", "@value", fmt.Errorf("Undefined value:1:4: Undefined value value in: @value")},
    47  		{"2 Undefined values", "@(value1 + value2)", fmt.Errorf("2 Undefined values:1:8: Undefined value value1 in: @(value1 + value2)\n2 Undefined values:1:21: Undefined value value2 in: @(value1 + value2)")},
    48  		{"Several errors", "@(value1)\n@nonExistingFunc()\n{{\n", fmt.Errorf("Several errors:2: function \"nonExistingFunc\" not defined in: @nonExistingFunc()\nSeveral errors:3: unexpected unclosed action in command in: {{\nSeveral errors:1:4: Undefined value value1 in: @(value1)")},
    49  	}
    50  	for _, tt := range tests {
    51  		t.Run(tt.name, func(t *testing.T) {
    52  			if _, err := template.ProcessContent(tt.content, tt.name); err != tt.err {
    53  				if err != nil && tt.err != nil && err.Error() == tt.err.Error() {
    54  					return
    55  				}
    56  				t.Errorf("ProcessContent()=\n%v\n\nWant:\n%v", err, tt.err)
    57  			}
    58  		})
    59  	}
    60  }