github.com/zntrio/harp/v2@v2.0.9/pkg/template/engine/funcs_test.go (about) 1 // Licensed to Elasticsearch B.V. under one or more contributor 2 // license agreements. See the NOTICE file distributed with 3 // this work for additional information regarding copyright 4 // ownership. Elasticsearch B.V. licenses this file to you under 5 // the Apache License, Version 2.0 (the "License"); you may 6 // not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, 12 // software distributed under the License is distributed on an 13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 // KIND, either express or implied. See the License for the 15 // specific language governing permissions and limitations 16 // under the License. 17 18 package engine 19 20 import ( 21 "strings" 22 "testing" 23 "text/template" 24 25 "github.com/stretchr/testify/assert" 26 ) 27 28 func TestFuncs(t *testing.T) { 29 // TODO write tests for failure cases 30 tests := []struct { 31 tpl, expect string 32 vars interface{} 33 }{{ 34 tpl: `{{ toYaml . }}`, 35 expect: `foo: bar`, 36 vars: map[string]interface{}{"foo": "bar"}, 37 }, { 38 tpl: `{{ toToml . }}`, 39 expect: "foo = \"bar\"\n", 40 vars: map[string]interface{}{"foo": "bar"}, 41 }, { 42 tpl: `{{ toJson . }}`, 43 expect: `{"foo":"bar"}`, 44 vars: map[string]interface{}{"foo": "bar"}, 45 }, { 46 tpl: `{{ fromYaml . }}`, 47 expect: "map[hello:world]", 48 vars: `hello: world`, 49 }, { 50 tpl: `{{ fromYamlArray . }}`, 51 expect: "[one 2 map[name:helm]]", 52 vars: "- one\n- 2\n- name: helm\n", 53 }, { 54 tpl: `{{ fromYamlArray . }}`, 55 expect: "[one 2 map[name:helm]]", 56 vars: `["one", 2, { "name": "helm" }]`, 57 }, { 58 tpl: `{{ toToml . }}`, 59 expect: "\n[mast]\n sail = \"white\"\n", 60 vars: map[string]map[string]string{"mast": {"sail": "white"}}, 61 }, { 62 tpl: `{{ fromYaml . }}`, 63 expect: "map[Error:error unmarshaling JSON: while decoding JSON: json: cannot unmarshal array into Go value of type map[string]interface {}]", 64 vars: "- one\n- two\n", 65 }, { 66 tpl: `{{ fromJson .}}`, 67 expect: `map[hello:world]`, 68 vars: `{"hello":"world"}`, 69 }, { 70 tpl: `{{ fromJson . }}`, 71 expect: `map[Error:json: cannot unmarshal array into Go value of type map[string]interface {}]`, 72 vars: `["one", "two"]`, 73 }, { 74 tpl: `{{ fromJsonArray . }}`, 75 expect: `[one 2 map[name:helm]]`, 76 vars: `["one", 2, { "name": "helm" }]`, 77 }, { 78 tpl: `{{ fromJsonArray . }}`, 79 expect: `[json: cannot unmarshal object into Go value of type []interface {}]`, 80 vars: `{"hello": "world"}`, 81 }, { 82 tpl: `{{ merge .dict (fromYaml .yaml) }}`, 83 expect: `map[a:map[b:c]]`, 84 vars: map[string]interface{}{"dict": map[string]interface{}{"a": map[string]interface{}{"b": "c"}}, "yaml": `{"a":{"b":"d"}}`}, 85 }, { 86 tpl: `{{ merge (fromYaml .yaml) .dict }}`, 87 expect: `map[a:map[b:d]]`, 88 vars: map[string]interface{}{"dict": map[string]interface{}{"a": map[string]interface{}{"b": "c"}}, "yaml": `{"a":{"b":"d"}}`}, 89 }, { 90 tpl: `{{ fromYaml . }}`, 91 expect: `map[Error:error unmarshaling JSON: while decoding JSON: json: cannot unmarshal array into Go value of type map[string]interface {}]`, 92 vars: `["one", "two"]`, 93 }, { 94 tpl: `{{ fromYamlArray . }}`, 95 expect: `[error unmarshaling JSON: while decoding JSON: json: cannot unmarshal object into Go value of type []interface {}]`, 96 vars: `hello: world`, 97 }, { 98 tpl: `{{ jsonEscape . }}`, 99 expect: `backslash: \\, A: \u0026 \u003c`, 100 vars: `backslash: \, A: & <`, 101 }, { 102 tpl: `{{ jsonUnescape . }}`, 103 expect: `backslash: \, A: & <`, 104 vars: `"backslash: \\, A: \u0026 \u003c"`, 105 }, { 106 tpl: `{{ unquote . }}`, 107 expect: `{"channel":"buu","name":"john", "msg":"doe"}`, 108 vars: `"{\"channel\":\"buu\",\"name\":\"john\", \"msg\":\"doe\"}"`, 109 }, { 110 tpl: `{{ parseJwt . }}`, 111 expect: `{[{ <nil> HS256 [] map[typ:JWT]}] map[iat:1.516239022e+09 name:John Doe sub:1234567890]}`, 112 vars: `eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c`, 113 }, { 114 tpl: `{{ isodate 0 }}`, 115 expect: `1970-01-01T00:00:00Z`, 116 }} 117 118 for _, tt := range tests { 119 var b strings.Builder 120 err := template.Must(template.New("test").Funcs(FuncMap(nil)).Parse(tt.tpl)).Execute(&b, tt.vars) 121 assert.NoError(t, err) 122 assert.Equal(t, tt.expect, b.String(), tt.tpl) 123 } 124 }