github.com/amplia-iiot/yutil@v1.0.1-0.20231229120411-5d96a4c5a136/internal/replace/engine_jinja2_test.go (about)

     1  /*
     2  Copyright (c) 2023 Adrian Haasler GarcĂ­a <dev@ahaasler.com>
     3  
     4  Permission is hereby granted, free of charge, to any person obtaining a copy
     5  of this software and associated documentation files (the "Software"), to deal
     6  in the Software without restriction, including without limitation the rights
     7  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8  copies of the Software, and to permit persons to whom the Software is
     9  furnished to do so, subject to the following conditions:
    10  
    11  The above copyright notice and this permission notice shall be included in all
    12  copies or substantial portions of the Software.
    13  
    14  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20  SOFTWARE.
    21  */
    22  
    23  package replace
    24  
    25  import (
    26  	"testing"
    27  
    28  	itesting "github.com/amplia-iiot/yutil/internal/testing"
    29  )
    30  
    31  func TestJinja2Replace(t *testing.T) {
    32  	for name, i := range map[string]struct {
    33  		content      string
    34  		replacements map[string]interface{}
    35  		expected     string
    36  		expectedErr  string
    37  	}{
    38  		"single replace": {
    39  			content:      "Hello, {{ name }}!",
    40  			replacements: map[string]interface{}{"name": "World"},
    41  			expected:     "Hello, World!",
    42  		},
    43  		"replace with underscore": {
    44  			content:      "Hello, {{ first_name }}!",
    45  			replacements: map[string]interface{}{"first_name": "Adrian"},
    46  			expected:     "Hello, Adrian!",
    47  		},
    48  		"map": {
    49  			content: "{{ root_node.child_node.leaf_node }}",
    50  			replacements: map[string]interface{}{"root_node": map[string]interface{}{
    51  				"child_node": map[string]interface{}{
    52  					"leaf_node": "value",
    53  				},
    54  			}},
    55  			expected: "value",
    56  		},
    57  		"map with brackets": {
    58  			content: "{{ root_node['child_node']['leaf_node'] }}",
    59  			replacements: map[string]interface{}{"root_node": map[string]interface{}{
    60  				"child_node": map[string]interface{}{
    61  					"leaf_node": "value",
    62  				},
    63  			}},
    64  			expected: "value",
    65  		},
    66  		"variable": {
    67  			content:      "{%- set data = 'value' %}{{ data }}",
    68  			replacements: map[string]interface{}{},
    69  			expected:     "value",
    70  		},
    71  		"variable array": {
    72  			content:      `{%- set data = ["value1", "value2"] %}{{ data }}`,
    73  			replacements: map[string]interface{}{},
    74  			expected:     "['value1', 'value2']",
    75  		},
    76  		"array union": {
    77  			content: "{{ one_and_two + three_and_four + five_and_six }}",
    78  			replacements: map[string]interface{}{
    79  				"one_and_two":    []int{1, 2},
    80  				"three_and_four": []int{3, 4},
    81  				"five_and_six":   []int{5, 6},
    82  			},
    83  			expected: "[1, 2, 3, 4, 5, 6]",
    84  		},
    85  		"array union with true conditional": {
    86  			content: "{{ one_and_two + (three_and_four if condition else []) }}",
    87  			replacements: map[string]interface{}{
    88  				"one_and_two":    []int{1, 2},
    89  				"three_and_four": []int{3, 4},
    90  				"condition":      true,
    91  			},
    92  			expected: "[1, 2, 3, 4]",
    93  		},
    94  		"array union with false conditional": {
    95  			content: "{{ one_and_two + (three_and_four if condition else []) }}",
    96  			replacements: map[string]interface{}{
    97  				"one_and_two":    []int{1, 2},
    98  				"three_and_four": []int{3, 4},
    99  				"condition":      false,
   100  			},
   101  			expected: "[1, 2]",
   102  		},
   103  		"array union with string comparisson": {
   104  			content: "{{ one_and_two + (three_and_four if (data == 'value') else []) }}",
   105  			replacements: map[string]interface{}{
   106  				"one_and_two":    []int{1, 2},
   107  				"three_and_four": []int{3, 4},
   108  				"data":           "value",
   109  			},
   110  			expected: "[1, 2, 3, 4]",
   111  		},
   112  		"array union with string comparisson as child_node": {
   113  			content: "{{ one_and_two + (three_and_four if (root_node.leaf_node == 'value') else []) }}",
   114  			replacements: map[string]interface{}{
   115  				"one_and_two":    []int{1, 2},
   116  				"three_and_four": []int{3, 4},
   117  				"root_node": map[string]interface{}{
   118  					"leaf_node": "value",
   119  				},
   120  			},
   121  			expected: "[1, 2, 3, 4]",
   122  		},
   123  		"array union with string comparisson as child_node with brackets": {
   124  			content: "{{ one_and_two + (three_and_four if (root_node['leaf_node'] == 'value') else [] ) }}",
   125  			replacements: map[string]interface{}{
   126  				"one_and_two":    []int{1, 2},
   127  				"three_and_four": []int{3, 4},
   128  				"root_node": map[string]interface{}{
   129  					"leaf_node": "value",
   130  				},
   131  			},
   132  			expected: "[1, 2, 3, 4]",
   133  		},
   134  		"array union with string comparisson as child_node with brackets and variables": {
   135  			content: "{%- set one_and_two = [1, 2] %}{%- set three_and_four = [3, 4] %}{{ one_and_two + (three_and_four if (root_node['leaf_node'] == 'value') else [] ) }}",
   136  			replacements: map[string]interface{}{
   137  				"root_node": map[string]interface{}{
   138  					"leaf_node": "value",
   139  				},
   140  			},
   141  			expected: "[1, 2, 3, 4]",
   142  		},
   143  		"array iteration": {
   144  			content: "{% for key, value in root_node.items() %}{{ key }}={{value}}{% endfor %}",
   145  			replacements: map[string]interface{}{
   146  				"root_node": map[string]interface{}{
   147  					"one": 1,
   148  				},
   149  			},
   150  			expected: "one=1",
   151  		},
   152  	} {
   153  		t.Run(name, func(t *testing.T) {
   154  			result, err := jinja2.Replace(i.content, i.replacements)
   155  			itesting.AssertError(t, i.expectedErr, err)
   156  			itesting.AssertEqual(t, i.expected, result)
   157  		})
   158  	}
   159  }