config_list_test.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package fs
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/stretchr/testify/require"
  6. )
  7. func must(err error) {
  8. if err != nil {
  9. panic(err)
  10. }
  11. }
  12. func ExampleSpaceSepList() {
  13. for _, s := range []string{
  14. `remotea:test/dir remoteb:`,
  15. `"remotea:test/space dir" remoteb:`,
  16. `"remotea:test/quote""dir" remoteb:`,
  17. } {
  18. var l SpaceSepList
  19. must(l.Set(s))
  20. fmt.Printf("%#v\n", l)
  21. }
  22. // Output:
  23. // fs.SpaceSepList{"remotea:test/dir", "remoteb:"}
  24. // fs.SpaceSepList{"remotea:test/space dir", "remoteb:"}
  25. // fs.SpaceSepList{"remotea:test/quote\"dir", "remoteb:"}
  26. }
  27. func ExampleCommaSepList() {
  28. for _, s := range []string{
  29. `remotea:test/dir,remoteb:`,
  30. `"remotea:test/space dir",remoteb:`,
  31. `"remotea:test/quote""dir",remoteb:`,
  32. } {
  33. var l CommaSepList
  34. must(l.Set(s))
  35. fmt.Printf("%#v\n", l)
  36. }
  37. // Output:
  38. // fs.CommaSepList{"remotea:test/dir", "remoteb:"}
  39. // fs.CommaSepList{"remotea:test/space dir", "remoteb:"}
  40. // fs.CommaSepList{"remotea:test/quote\"dir", "remoteb:"}
  41. }
  42. func TestSpaceSepListSet(t *testing.T) {
  43. type tc struct {
  44. in string
  45. out SpaceSepList
  46. err string
  47. }
  48. tests := []tc{
  49. {``, nil, ""},
  50. {`\`, SpaceSepList{`\`}, ""},
  51. {`\\`, SpaceSepList{`\\`}, ""},
  52. {`potato`, SpaceSepList{`potato`}, ""},
  53. {`po\tato`, SpaceSepList{`po\tato`}, ""},
  54. {`potato\`, SpaceSepList{`potato\`}, ""},
  55. {`'potato`, SpaceSepList{`'potato`}, ""},
  56. {`pot'ato`, SpaceSepList{`pot'ato`}, ""},
  57. {`potato'`, SpaceSepList{`potato'`}, ""},
  58. {`"potato"`, SpaceSepList{`potato`}, ""},
  59. {`'potato'`, SpaceSepList{`'potato'`}, ""},
  60. {`potato apple`, SpaceSepList{`potato`, `apple`}, ""},
  61. {`potato\ apple`, SpaceSepList{`potato\`, `apple`}, ""},
  62. {`"potato apple"`, SpaceSepList{`potato apple`}, ""},
  63. {`"potato'apple"`, SpaceSepList{`potato'apple`}, ""},
  64. {`"potato''apple"`, SpaceSepList{`potato''apple`}, ""},
  65. {`"potato' 'apple"`, SpaceSepList{`potato' 'apple`}, ""},
  66. {`potato="apple"`, nil, `bare " in non-quoted-field`},
  67. {`apple "potato`, nil, "extraneous"},
  68. {`apple pot"ato`, nil, "bare \" in non-quoted-field"},
  69. {`potato"`, nil, "bare \" in non-quoted-field"},
  70. }
  71. for _, tc := range tests {
  72. var l SpaceSepList
  73. err := l.Set(tc.in)
  74. if tc.err == "" {
  75. require.NoErrorf(t, err, "input: %q", tc.in)
  76. } else {
  77. require.Containsf(t, err.Error(), tc.err, "input: %q", tc.in)
  78. }
  79. require.Equalf(t, tc.out, l, "input: %q", tc.in)
  80. }
  81. }