flags_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package flags
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/zeromicro/go-zero/tools/goctl/test"
  6. )
  7. func TestFlags_Get(t *testing.T) {
  8. setTestData(t, []byte(`{"host":"0.0.0.0","port":8888,"service":{"host":"{{.host}}","port":"{{.port}}","invalid":"{{.service.invalid}}"}}`))
  9. f := MustLoad()
  10. executor := test.NewExecutor[string, string]()
  11. executor.Add([]test.Data[string, string]{
  12. {
  13. Name: "key_host",
  14. Input: "host",
  15. Want: "0.0.0.0",
  16. },
  17. {
  18. Name: "key_port",
  19. Input: "port",
  20. Want: "8888",
  21. },
  22. {
  23. Name: "key_service.host",
  24. Input: "service.host",
  25. Want: "0.0.0.0",
  26. },
  27. {
  28. Name: "key_service.port",
  29. Input: "service.port",
  30. Want: "8888",
  31. },
  32. {
  33. Name: "key_not_exists",
  34. Input: "service.port.invalid",
  35. },
  36. {
  37. Name: "key_service.invalid",
  38. Input: "service.invalid",
  39. E: fmt.Errorf("the variable can not be self: %q", "service.invalid"),
  40. },
  41. }...)
  42. executor.RunE(t, f.Get)
  43. }
  44. func Test_Get(t *testing.T) {
  45. setTestData(t, []byte(`{"host":"0.0.0.0","port":8888,"service":{"host":"{{.host}}","port":"{{.port}}","invalid":"{{.service.invalid}}"}}`))
  46. executor := test.NewExecutor[string, string]()
  47. executor.Add([]test.Data[string, string]{
  48. {
  49. Name: "key_host",
  50. Input: "host",
  51. Want: "0.0.0.0",
  52. },
  53. {
  54. Name: "key_port",
  55. Input: "port",
  56. Want: "8888",
  57. },
  58. {
  59. Name: "key_service.host",
  60. Input: "service.host",
  61. Want: "0.0.0.0",
  62. },
  63. {
  64. Name: "key_service.port",
  65. Input: "service.port",
  66. Want: "8888",
  67. },
  68. {
  69. Name: "key_not_exists",
  70. Input: "service.port.invalid",
  71. },
  72. }...)
  73. executor.Run(t, Get)
  74. }