templatex_test.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package util
  2. import (
  3. "testing"
  4. "github.com/zeromicro/go-zero/tools/goctl/test"
  5. )
  6. func TestIsTemplate(t *testing.T) {
  7. executor := test.NewExecutor[string, bool]()
  8. executor.Add([]test.Data[string, bool]{
  9. {
  10. Name: "empty",
  11. Want: false,
  12. },
  13. {
  14. Name: "invalid",
  15. Input: "{foo}",
  16. Want: false,
  17. },
  18. {
  19. Name: "invalid",
  20. Input: "{.foo}",
  21. Want: false,
  22. },
  23. {
  24. Name: "invalid",
  25. Input: "$foo",
  26. Want: false,
  27. },
  28. {
  29. Name: "invalid",
  30. Input: "{{foo}}",
  31. Want: false,
  32. },
  33. {
  34. Name: "invalid",
  35. Input: "{{.}}",
  36. Want: false,
  37. },
  38. {
  39. Name: "valid",
  40. Input: "{{.foo}}",
  41. Want: true,
  42. },
  43. {
  44. Name: "valid",
  45. Input: "{{.foo.bar}}",
  46. Want: true,
  47. },
  48. }...)
  49. executor.Run(t, IsTemplateVariable)
  50. }
  51. func TestTemplateVariable(t *testing.T) {
  52. executor := test.NewExecutor[string, string]()
  53. executor.Add([]test.Data[string, string]{
  54. {
  55. Name: "empty",
  56. },
  57. {
  58. Name: "invalid",
  59. Input: "{foo}",
  60. },
  61. {
  62. Name: "invalid",
  63. Input: "{.foo}",
  64. },
  65. {
  66. Name: "invalid",
  67. Input: "$foo",
  68. },
  69. {
  70. Name: "invalid",
  71. Input: "{{foo}}",
  72. },
  73. {
  74. Name: "invalid",
  75. Input: "{{.}}",
  76. },
  77. {
  78. Name: "valid",
  79. Input: "{{.foo}}",
  80. Want: "foo",
  81. },
  82. {
  83. Name: "valid",
  84. Input: "{{.foo.bar}}",
  85. Want: "foo.bar",
  86. },
  87. }...)
  88. executor.Run(t, TemplateVariable)
  89. }