github.com/x-helm/helm@v3.0.0-beta.3+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:    `{{ toYaml . }}`,
    34  		expect: `foo: bar`,
    35  		vars:   map[string]interface{}{"foo": "bar"},
    36  	}, {
    37  		tpl:    `{{ toToml . }}`,
    38  		expect: "foo = \"bar\"\n",
    39  		vars:   map[string]interface{}{"foo": "bar"},
    40  	}, {
    41  		tpl:    `{{ toJson . }}`,
    42  		expect: `{"foo":"bar"}`,
    43  		vars:   map[string]interface{}{"foo": "bar"},
    44  	}, {
    45  		tpl:    `{{ fromYaml . }}`,
    46  		expect: "map[hello:world]",
    47  		vars:   `hello: world`,
    48  	}, {
    49  		// Regression for https://github.com/helm/helm/issues/2271
    50  		tpl:    `{{ toToml . }}`,
    51  		expect: "[mast]\n  sail = \"white\"\n",
    52  		vars:   map[string]map[string]string{"mast": {"sail": "white"}},
    53  	}, {
    54  		tpl:    `{{ fromYaml . }}`,
    55  		expect: "map[Error:yaml: unmarshal errors:\n  line 1: cannot unmarshal !!seq into map[string]interface {}]",
    56  		vars:   "- one\n- two\n",
    57  	}, {
    58  		tpl:    `{{ fromJson .}}`,
    59  		expect: `map[hello:world]`,
    60  		vars:   `{"hello":"world"}`,
    61  	}, {
    62  		tpl:    `{{ fromJson . }}`,
    63  		expect: `map[Error:json: cannot unmarshal array into Go value of type map[string]interface {}]`,
    64  		vars:   `["one", "two"]`,
    65  	}}
    66  
    67  	for _, tt := range tests {
    68  		var b strings.Builder
    69  		err := template.Must(template.New("test").Funcs(funcMap()).Parse(tt.tpl)).Execute(&b, tt.vars)
    70  		assert.NoError(t, err)
    71  		assert.Equal(t, tt.expect, b.String(), tt.tpl)
    72  	}
    73  }