github.com/umeshredd/helm@v3.0.0-alpha.1+incompatible/pkg/engine/funcs_test.go (about)

     1  /*
     2  Copyright The Helm Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package engine
    18  
    19  import (
    20  	"strings"
    21  	"testing"
    22  	"text/template"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  func TestFuncs(t *testing.T) {
    28  	//TODO write tests for failure cases
    29  	tests := []struct {
    30  		tpl, expect string
    31  		vars        interface{}
    32  	}{{
    33  		tpl:    `All {{ required "A valid 'bases' is required" .bases }} of them!`,
    34  		expect: `All 2 of them!`,
    35  		vars:   map[string]interface{}{"bases": 2},
    36  	}, {
    37  		tpl:    `{{ toYaml . }}`,
    38  		expect: `foo: bar`,
    39  		vars:   map[string]interface{}{"foo": "bar"},
    40  	}, {
    41  		tpl:    `{{ toToml . }}`,
    42  		expect: "foo = \"bar\"\n",
    43  		vars:   map[string]interface{}{"foo": "bar"},
    44  	}, {
    45  		tpl:    `{{ toJson . }}`,
    46  		expect: `{"foo":"bar"}`,
    47  		vars:   map[string]interface{}{"foo": "bar"},
    48  	}, {
    49  		tpl:    `{{ fromYaml . }}`,
    50  		expect: "map[hello:world]",
    51  		vars:   `hello: world`,
    52  	}, {
    53  		// Regression for https://github.com/helm/helm/issues/2271
    54  		tpl:    `{{ toToml . }}`,
    55  		expect: "[mast]\n  sail = \"white\"\n",
    56  		vars:   map[string]map[string]string{"mast": {"sail": "white"}},
    57  	}, {
    58  		tpl:    `{{ fromYaml . }}`,
    59  		expect: "map[Error:yaml: unmarshal errors:\n  line 1: cannot unmarshal !!seq into map[string]interface {}]",
    60  		vars:   "- one\n- two\n",
    61  	}, {
    62  		tpl:    `{{ fromJson .}}`,
    63  		expect: `map[hello:world]`,
    64  		vars:   `{"hello":"world"}`,
    65  	}, {
    66  		tpl:    `{{ fromJson . }}`,
    67  		expect: `map[Error:json: cannot unmarshal array into Go value of type map[string]interface {}]`,
    68  		vars:   `["one", "two"]`,
    69  	}}
    70  
    71  	for _, tt := range tests {
    72  		var b strings.Builder
    73  		err := template.Must(template.New("test").Funcs(funcMap()).Parse(tt.tpl)).Execute(&b, tt.vars)
    74  		assert.NoError(t, err)
    75  		assert.Equal(t, tt.expect, b.String(), tt.tpl)
    76  	}
    77  }