| 25 | ) |
| 26 | |
| 27 | func TestFuncs(t *testing.T) { |
| 28 | //TODO write tests for failure cases |
| 29 | tests := []struct { |
| 30 | tpl, expect string |
| 31 | vars any |
| 32 | }{{ |
| 33 | tpl: `{{ toYaml . }}`, |
| 34 | expect: `foo: bar`, |
| 35 | vars: map[string]any{"foo": "bar"}, |
| 36 | }, { |
| 37 | tpl: `{{ toYamlPretty . }}`, |
| 38 | expect: "baz:\n - 1\n - 2\n - 3", |
| 39 | vars: map[string]any{"baz": []int{1, 2, 3}}, |
| 40 | }, { |
| 41 | tpl: `{{ toToml . }}`, |
| 42 | expect: "foo = \"bar\"\n", |
| 43 | vars: map[string]any{"foo": "bar"}, |
| 44 | }, { |
| 45 | tpl: `{{ fromToml . }}`, |
| 46 | expect: "map[hello:world]", |
| 47 | vars: `hello = "world"`, |
| 48 | }, { |
| 49 | tpl: `{{ fromToml . }}`, |
| 50 | expect: "map[table:map[keyInTable:valueInTable subtable:map[keyInSubtable:valueInSubTable]]]", |
| 51 | vars: ` |
| 52 | [table] |
| 53 | keyInTable = "valueInTable" |
| 54 | [table.subtable] |
| 55 | keyInSubtable = "valueInSubTable"`, |
| 56 | }, { |
| 57 | tpl: `{{ fromToml . }}`, |
| 58 | expect: "map[tableArray:[map[keyInElement0:valueInElement0] map[keyInElement1:valueInElement1]]]", |
| 59 | vars: ` |
| 60 | [[tableArray]] |
| 61 | keyInElement0 = "valueInElement0" |
| 62 | [[tableArray]] |
| 63 | keyInElement1 = "valueInElement1"`, |
| 64 | }, { |
| 65 | tpl: `{{ fromToml . }}`, |
| 66 | expect: "map[Error:toml: line 1: unexpected EOF; expected key separator '=']", |
| 67 | vars: "one", |
| 68 | }, { |
| 69 | tpl: `{{ toJson . }}`, |
| 70 | expect: `{"foo":"bar"}`, |
| 71 | vars: map[string]any{"foo": "bar"}, |
| 72 | }, { |
| 73 | tpl: `{{ fromYaml . }}`, |
| 74 | expect: "map[hello:world]", |
| 75 | vars: `hello: world`, |
| 76 | }, { |
| 77 | tpl: `{{ fromYamlArray . }}`, |
| 78 | expect: "[one 2 map[name:helm]]", |
| 79 | vars: "- one\n- 2\n- name: helm\n", |
| 80 | }, { |
| 81 | tpl: `{{ fromYamlArray . }}`, |
| 82 | expect: "[one 2 map[name:helm]]", |
| 83 | vars: `["one", 2, { "name": "helm" }]`, |
| 84 | }, { |