code.gitea.io/gitea@v1.19.3/modules/templates/vars/vars_test.go (about)

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