convert_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package flags
  2. import (
  3. "testing"
  4. "time"
  5. )
  6. func expectConvert(t *testing.T, o *Option, expected string) {
  7. s, err := convertToString(o.value, o.tag)
  8. if err != nil {
  9. t.Errorf("Unexpected error: %v", err)
  10. return
  11. }
  12. assertString(t, s, expected)
  13. }
  14. func TestConvertToString(t *testing.T) {
  15. d, _ := time.ParseDuration("1h2m4s")
  16. var opts = struct {
  17. String string `long:"string"`
  18. Int int `long:"int"`
  19. Int8 int8 `long:"int8"`
  20. Int16 int16 `long:"int16"`
  21. Int32 int32 `long:"int32"`
  22. Int64 int64 `long:"int64"`
  23. Uint uint `long:"uint"`
  24. Uint8 uint8 `long:"uint8"`
  25. Uint16 uint16 `long:"uint16"`
  26. Uint32 uint32 `long:"uint32"`
  27. Uint64 uint64 `long:"uint64"`
  28. Float32 float32 `long:"float32"`
  29. Float64 float64 `long:"float64"`
  30. Duration time.Duration `long:"duration"`
  31. Bool bool `long:"bool"`
  32. IntSlice []int `long:"int-slice"`
  33. IntFloatMap map[int]float64 `long:"int-float-map"`
  34. PtrBool *bool `long:"ptr-bool"`
  35. Interface interface{} `long:"interface"`
  36. Int32Base int32 `long:"int32-base" base:"16"`
  37. Uint32Base uint32 `long:"uint32-base" base:"16"`
  38. }{
  39. "string",
  40. -2,
  41. -1,
  42. 0,
  43. 1,
  44. 2,
  45. 1,
  46. 2,
  47. 3,
  48. 4,
  49. 5,
  50. 1.2,
  51. -3.4,
  52. d,
  53. true,
  54. []int{-3, 4, -2},
  55. map[int]float64{-2: 4.5},
  56. new(bool),
  57. float32(5.2),
  58. -5823,
  59. 4232,
  60. }
  61. p := NewNamedParser("test", Default)
  62. grp, _ := p.AddGroup("test group", "", &opts)
  63. expects := []string{
  64. "string",
  65. "-2",
  66. "-1",
  67. "0",
  68. "1",
  69. "2",
  70. "1",
  71. "2",
  72. "3",
  73. "4",
  74. "5",
  75. "1.2",
  76. "-3.4",
  77. "1h2m4s",
  78. "true",
  79. "[-3, 4, -2]",
  80. "{-2:4.5}",
  81. "false",
  82. "5.2",
  83. "-16bf",
  84. "1088",
  85. }
  86. for i, v := range grp.Options() {
  87. expectConvert(t, v, expects[i])
  88. }
  89. }
  90. func TestConvertToStringInvalidIntBase(t *testing.T) {
  91. var opts = struct {
  92. Int int `long:"int" base:"no"`
  93. }{
  94. 2,
  95. }
  96. p := NewNamedParser("test", Default)
  97. grp, _ := p.AddGroup("test group", "", &opts)
  98. o := grp.Options()[0]
  99. _, err := convertToString(o.value, o.tag)
  100. if err != nil {
  101. err = newErrorf(ErrMarshal, "%v", err)
  102. }
  103. assertError(t, err, ErrMarshal, "strconv.ParseInt: parsing \"no\": invalid syntax")
  104. }
  105. func TestConvertToStringInvalidUintBase(t *testing.T) {
  106. var opts = struct {
  107. Uint uint `long:"uint" base:"no"`
  108. }{
  109. 2,
  110. }
  111. p := NewNamedParser("test", Default)
  112. grp, _ := p.AddGroup("test group", "", &opts)
  113. o := grp.Options()[0]
  114. _, err := convertToString(o.value, o.tag)
  115. if err != nil {
  116. err = newErrorf(ErrMarshal, "%v", err)
  117. }
  118. assertError(t, err, ErrMarshal, "strconv.ParseInt: parsing \"no\": invalid syntax")
  119. }