marshal_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package flags
  2. import (
  3. "fmt"
  4. "testing"
  5. )
  6. type marshalled string
  7. func (m *marshalled) UnmarshalFlag(value string) error {
  8. if value == "yes" {
  9. *m = "true"
  10. } else if value == "no" {
  11. *m = "false"
  12. } else {
  13. return fmt.Errorf("`%s' is not a valid value, please specify `yes' or `no'", value)
  14. }
  15. return nil
  16. }
  17. func (m marshalled) MarshalFlag() (string, error) {
  18. if m == "true" {
  19. return "yes", nil
  20. }
  21. return "no", nil
  22. }
  23. type marshalledError bool
  24. func (m marshalledError) MarshalFlag() (string, error) {
  25. return "", newErrorf(ErrMarshal, "Failed to marshal")
  26. }
  27. func TestUnmarshal(t *testing.T) {
  28. var opts = struct {
  29. Value marshalled `short:"v"`
  30. }{}
  31. ret := assertParseSuccess(t, &opts, "-v=yes")
  32. assertStringArray(t, ret, []string{})
  33. if opts.Value != "true" {
  34. t.Errorf("Expected Value to be \"true\"")
  35. }
  36. }
  37. func TestUnmarshalDefault(t *testing.T) {
  38. var opts = struct {
  39. Value marshalled `short:"v" default:"yes"`
  40. }{}
  41. ret := assertParseSuccess(t, &opts)
  42. assertStringArray(t, ret, []string{})
  43. if opts.Value != "true" {
  44. t.Errorf("Expected Value to be \"true\"")
  45. }
  46. }
  47. func TestUnmarshalOptional(t *testing.T) {
  48. var opts = struct {
  49. Value marshalled `short:"v" optional:"yes" optional-value:"yes"`
  50. }{}
  51. ret := assertParseSuccess(t, &opts, "-v")
  52. assertStringArray(t, ret, []string{})
  53. if opts.Value != "true" {
  54. t.Errorf("Expected Value to be \"true\"")
  55. }
  56. }
  57. func TestUnmarshalError(t *testing.T) {
  58. var opts = struct {
  59. Value marshalled `short:"v"`
  60. }{}
  61. assertParseFail(t, ErrMarshal, fmt.Sprintf("invalid argument for flag `%cv' (expected flags.marshalled): `invalid' is not a valid value, please specify `yes' or `no'", defaultShortOptDelimiter), &opts, "-vinvalid")
  62. }
  63. func TestUnmarshalPositionalError(t *testing.T) {
  64. var opts = struct {
  65. Args struct {
  66. Value marshalled
  67. } `positional-args:"yes"`
  68. }{}
  69. parser := NewParser(&opts, Default&^PrintErrors)
  70. _, err := parser.ParseArgs([]string{"invalid"})
  71. msg := "`invalid' is not a valid value, please specify `yes' or `no'"
  72. if err == nil {
  73. assertFatalf(t, "Expected error: %s", msg)
  74. return
  75. }
  76. if err.Error() != msg {
  77. assertErrorf(t, "Expected error message %#v, but got %#v", msg, err.Error())
  78. }
  79. }
  80. func TestMarshalError(t *testing.T) {
  81. var opts = struct {
  82. Value marshalledError `short:"v"`
  83. }{}
  84. p := NewParser(&opts, Default)
  85. o := p.Command.Groups()[0].Options()[0]
  86. _, err := convertToString(o.value, o.tag)
  87. assertError(t, err, ErrMarshal, "Failed to marshal")
  88. }