1
0

ini_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package flags
  2. import (
  3. "bytes"
  4. "strings"
  5. "testing"
  6. )
  7. func TestWriteIni(t *testing.T) {
  8. var opts helpOptions
  9. p := NewNamedParser("TestIni", Default)
  10. p.AddGroup("Application Options", "The application options", &opts)
  11. p.ParseArgs([]string{"-vv", "--intmap=a:2", "--intmap", "b:3"})
  12. inip := NewIniParser(p)
  13. var b bytes.Buffer
  14. inip.Write(&b, IniDefault|IniIncludeDefaults)
  15. got := b.String()
  16. expected := `[Application Options]
  17. ; Show verbose debug information
  18. verbose = true
  19. verbose = true
  20. ; A slice of pointers to string
  21. ; PtrSlice =
  22. ; Option only available in ini
  23. only-ini =
  24. [Other Options]
  25. ; A slice of strings
  26. ; StringSlice =
  27. ; A map from string to int
  28. int-map = a:2
  29. int-map = b:3
  30. `
  31. if got != expected {
  32. ret, err := helpDiff(got, expected)
  33. if err != nil {
  34. t.Errorf("Unexpected ini, expected:\n\n%s\n\nbut got\n\n%s", expected, got)
  35. } else {
  36. t.Errorf("Unexpected ini:\n\n%s", ret)
  37. }
  38. }
  39. }
  40. func TestReadIni(t *testing.T) {
  41. var opts helpOptions
  42. p := NewNamedParser("TestIni", Default)
  43. p.AddGroup("Application Options", "The application options", &opts)
  44. inip := NewIniParser(p)
  45. inic := `
  46. ; Show verbose debug information
  47. verbose = true
  48. verbose = true
  49. [Application Options]
  50. ; A slice of pointers to string
  51. ; PtrSlice =
  52. [Other Options]
  53. ; A slice of strings
  54. ; StringSlice =
  55. ; A map from string to int
  56. int-map = a:2
  57. int-map = b:3
  58. `
  59. b := strings.NewReader(inic)
  60. err := inip.Parse(b)
  61. if err != nil {
  62. t.Fatalf("Unexpected error: %s", err)
  63. }
  64. assertBoolArray(t, opts.Verbose, []bool{true, true})
  65. if v, ok := opts.Other.IntMap["a"]; !ok {
  66. t.Errorf("Expected \"a\" in Other.IntMap")
  67. } else if v != 2 {
  68. t.Errorf("Expected Other.IntMap[\"a\"] = 2, but got %v", v)
  69. }
  70. if v, ok := opts.Other.IntMap["b"]; !ok {
  71. t.Errorf("Expected \"b\" in Other.IntMap")
  72. } else if v != 3 {
  73. t.Errorf("Expected Other.IntMap[\"b\"] = 3, but got %v", v)
  74. }
  75. }
  76. func TestIniCommands(t *testing.T) {
  77. var opts struct {
  78. Value string `short:"v" long:"value"`
  79. Add struct {
  80. Name int `short:"n" long:"name" ini-name:"AliasName"`
  81. Other struct {
  82. O string `short:"o" long:"other"`
  83. } `group:"Other Options"`
  84. } `command:"add"`
  85. }
  86. p := NewNamedParser("TestIni", Default)
  87. p.AddGroup("Application Options", "The application options", &opts)
  88. inip := NewIniParser(p)
  89. inic := `[Application Options]
  90. value = some value
  91. [add]
  92. AliasName = 5
  93. [add.Other Options]
  94. other = subgroup
  95. `
  96. b := strings.NewReader(inic)
  97. err := inip.Parse(b)
  98. if err != nil {
  99. t.Fatalf("Unexpected error: %s", err)
  100. }
  101. assertString(t, opts.Value, "some value")
  102. if opts.Add.Name != 5 {
  103. t.Errorf("Expected opts.Add.Name to be 5, but got %v", opts.Add.Name)
  104. }
  105. assertString(t, opts.Add.Other.O, "subgroup")
  106. }
  107. func TestIniNoIni(t *testing.T) {
  108. var opts struct {
  109. Value string `short:"v" long:"value" no-ini:"yes"`
  110. }
  111. p := NewNamedParser("TestIni", Default)
  112. p.AddGroup("Application Options", "The application options", &opts)
  113. inip := NewIniParser(p)
  114. inic := `[Application Options]
  115. value = some value
  116. `
  117. b := strings.NewReader(inic)
  118. err := inip.Parse(b)
  119. if err == nil {
  120. t.Fatalf("Expected error")
  121. }
  122. assertError(t, err, ErrUnknownFlag, "unknown option: value")
  123. }