gitea源码

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