github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/template_vars/vars_test.go (about)

     1  // Copyright 2023 The GitBundle Inc. All rights reserved.
     2  // Copyright 2017 The Gitea Authors. All rights reserved.
     3  // Use of this source code is governed by a MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package vars
     7  
     8  import (
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestExpandVars(t *testing.T) {
    15  	kases := []struct {
    16  		tmpl  string
    17  		data  map[string]string
    18  		out   string
    19  		error bool
    20  	}{
    21  		{
    22  			tmpl: "{a}",
    23  			data: map[string]string{
    24  				"a": "1",
    25  			},
    26  			out: "1",
    27  		},
    28  		{
    29  			tmpl: "expand {a}, {b} and {c}, with non-var { } {#}",
    30  			data: map[string]string{
    31  				"a": "1",
    32  				"b": "2",
    33  				"c": "3",
    34  			},
    35  			out: "expand 1, 2 and 3, with non-var { } {#}",
    36  		},
    37  		{
    38  			tmpl: "中文内容 {一}, {二} 和 {三} 中文结尾",
    39  			data: map[string]string{
    40  				"一": "11",
    41  				"二": "22",
    42  				"三": "33",
    43  			},
    44  			out: "中文内容 11, 22 和 33 中文结尾",
    45  		},
    46  		{
    47  			tmpl: "expand {{a}, {b} and {c}",
    48  			data: map[string]string{
    49  				"a": "foo",
    50  				"b": "bar",
    51  			},
    52  			out:   "expand {{a}, bar and {c}",
    53  			error: true,
    54  		},
    55  		{
    56  			tmpl:  "expand } {} and {",
    57  			out:   "expand } {} and {",
    58  			error: true,
    59  		},
    60  	}
    61  
    62  	for _, kase := range kases {
    63  		t.Run(kase.tmpl, func(t *testing.T) {
    64  			res, err := Expand(kase.tmpl, kase.data)
    65  			assert.EqualValues(t, kase.out, res)
    66  			if kase.error {
    67  				assert.Error(t, err)
    68  			} else {
    69  				assert.NoError(t, err)
    70  			}
    71  		})
    72  	}
    73  }