github.com/henvic/wedeploycli@v1.7.6-0.20200319005353-3630f582f284/templates/templates_test.go (about)

     1  // Licensed under Apache license 2.0
     2  // SPDX-License-Identifier: Apache-2.0
     3  // Copyright 2013-2016 Docker, Inc.
     4  
     5  // NOTICE: export from moby/utils/templates/templates_test.go (modified)
     6  // https://github.com/moby/moby/blob/da0ccf8e61e4d5d4005e19fcf0115372f09840bf/utils/templates/templates_test.go
     7  // https://github.com/moby/moby/blob/da0ccf8e61e4d5d4005e19fcf0115372f09840bf/LICENSE
     8  
     9  package templates
    10  
    11  import (
    12  	"testing"
    13  	"text/template"
    14  
    15  	"github.com/henvic/wedeploy-sdk-go/jsonlib"
    16  )
    17  
    18  type mock struct {
    19  	ID         int
    20  	Name       string
    21  	unexported string
    22  }
    23  
    24  func mockf() string {
    25  	return "example"
    26  }
    27  
    28  func TestFuncs(t *testing.T) {
    29  	Funcs(template.FuncMap{
    30  		"mockf": mockf,
    31  	})
    32  
    33  	var got, err = Execute("{{mockf}}", nil)
    34  
    35  	if err != nil {
    36  		t.Errorf("Expected no error, got %v instead", err)
    37  	}
    38  
    39  	var want = "example"
    40  
    41  	if got != want {
    42  		t.Errorf("Expected value to be \"%v\", got \"%v\" instead", want, got)
    43  	}
    44  }
    45  
    46  func TestExecuteOrListExecute(t *testing.T) {
    47  	var want = `"Hello"`
    48  	var got, err = ExecuteOrList(`{{json .Name}}`, mock{104, "Hello", "bar"})
    49  
    50  	if err != nil {
    51  		t.Errorf("Expected no error, got %v instead", err)
    52  	}
    53  
    54  	if want != got {
    55  		t.Errorf("Wanted %v, got %v instead", want, got)
    56  	}
    57  }
    58  
    59  func TestExecuteOrListList(t *testing.T) {
    60  	var want = mock{104, "Hello", "not exported; doesn't matter"}
    61  
    62  	var got, err = ExecuteOrList("", mock{104, "Hello", "bar"})
    63  
    64  	if err != nil {
    65  		t.Errorf("Expected no error, got %v instead", err)
    66  	}
    67  
    68  	jsonlib.AssertJSONMarshal(t, got, want)
    69  }
    70  
    71  func TestExecuteOrListFailure(t *testing.T) {
    72  	var _, err = ExecuteOrList("", map[interface{}]string{})
    73  
    74  	var wantErr = `json: unsupported type: map[interface {}]string`
    75  
    76  	if err == nil || err.Error() != wantErr {
    77  		t.Errorf("Expected error to be %v, got %v instead", wantErr, err)
    78  	}
    79  }
    80  
    81  func TestExecuteStringFunction(t *testing.T) {
    82  	var want = "text/with/colon"
    83  	var got, err = Execute(`{{join (split . ":") "/"}}`, "text:with:colon")
    84  
    85  	if err != nil {
    86  		t.Errorf("Expected no error, got %v instead", err)
    87  	}
    88  
    89  	if want != got {
    90  		t.Errorf("Wanted %v, got %v instead", want, got)
    91  	}
    92  }
    93  
    94  func TestExecutePadWithSpaceFunction(t *testing.T) {
    95  	var want = "   Test       "
    96  	var got, err = Execute(`{{- pad . 3 7}}`, "Test")
    97  
    98  	if err != nil {
    99  		t.Errorf("Expected no error, got %v instead", err)
   100  	}
   101  
   102  	if want != got {
   103  		t.Errorf("Wanted %v, got %v instead", want, got)
   104  	}
   105  }
   106  
   107  func TestExecutePadWithSpaceFunctionEmpty(t *testing.T) {
   108  	var want = ""
   109  	var got, err = Execute(`{{- pad . 3 7}}`, "")
   110  
   111  	if err != nil {
   112  		t.Errorf("Expected no error, got %v instead", err)
   113  	}
   114  
   115  	if want != got {
   116  		t.Errorf("Wanted %v, got %v instead", want, got)
   117  	}
   118  }
   119  
   120  func TestExecuteStringFunctionJSON(t *testing.T) {
   121  	var want = `"Hello"`
   122  	var got, err = Execute(`{{json .Name}}`, mock{104, "Hello", "bar"})
   123  
   124  	if err != nil {
   125  		t.Errorf("Expected no error, got %v instead", err)
   126  	}
   127  
   128  	if want != got {
   129  		t.Errorf("Wanted %v, got %v instead", want, got)
   130  	}
   131  }
   132  
   133  func TestExecuteCompileError(t *testing.T) {
   134  	var wantErr = `template parsing error: template: :1: unexpected "\\" in command`
   135  	var _, err = Execute("this is a {{ \\ }}", "string")
   136  
   137  	if err == nil || err.Error() != wantErr {
   138  		t.Errorf("Wanted err to be %v, got %v instead", wantErr, err)
   139  	}
   140  }
   141  
   142  func TestExecuteRunError(t *testing.T) {
   143  	var wantErr = `can't execute template: template: :1:13: executing "" at <.>: can't give argument to non-function .`
   144  	var _, err = Execute("this is a {{ . . }}", 1)
   145  
   146  	if err == nil || err.Error() != wantErr {
   147  		t.Errorf("Wanted err to be %v, got %v instead", wantErr, err)
   148  	}
   149  }