parser_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package kingpin
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestParserExpandFromFile(t *testing.T) {
  9. f, err := ioutil.TempFile("", "")
  10. assert.NoError(t, err)
  11. defer os.Remove(f.Name())
  12. f.WriteString("hello\nworld\n")
  13. f.Close()
  14. app := New("test", "")
  15. arg0 := app.Arg("arg0", "").String()
  16. arg1 := app.Arg("arg1", "").String()
  17. _, err = app.Parse([]string{"@" + f.Name()})
  18. assert.NoError(t, err)
  19. assert.Equal(t, "hello", *arg0)
  20. assert.Equal(t, "world", *arg1)
  21. }
  22. func TestParserExpandFromFileLeadingArg(t *testing.T) {
  23. f, err := ioutil.TempFile("", "")
  24. assert.NoError(t, err)
  25. defer os.Remove(f.Name())
  26. f.WriteString("hello\nworld\n")
  27. f.Close()
  28. app := New("test", "")
  29. arg0 := app.Arg("arg0", "").String()
  30. arg1 := app.Arg("arg1", "").String()
  31. arg2 := app.Arg("arg2", "").String()
  32. _, err = app.Parse([]string{"prefix", "@" + f.Name()})
  33. assert.NoError(t, err)
  34. assert.Equal(t, "prefix", *arg0)
  35. assert.Equal(t, "hello", *arg1)
  36. assert.Equal(t, "world", *arg2)
  37. }
  38. func TestParserExpandFromFileTrailingArg(t *testing.T) {
  39. f, err := ioutil.TempFile("", "")
  40. assert.NoError(t, err)
  41. defer os.Remove(f.Name())
  42. f.WriteString("hello\nworld\n")
  43. f.Close()
  44. app := New("test", "")
  45. arg0 := app.Arg("arg0", "").String()
  46. arg1 := app.Arg("arg1", "").String()
  47. arg2 := app.Arg("arg2", "").String()
  48. _, err = app.Parse([]string{"@" + f.Name(), "suffix"})
  49. assert.NoError(t, err)
  50. assert.Equal(t, "hello", *arg0)
  51. assert.Equal(t, "world", *arg1)
  52. assert.Equal(t, "suffix", *arg2)
  53. }
  54. func TestParserExpandFromFileMultipleSurroundingArgs(t *testing.T) {
  55. f, err := ioutil.TempFile("", "")
  56. assert.NoError(t, err)
  57. defer os.Remove(f.Name())
  58. f.WriteString("hello\nworld\n")
  59. f.Close()
  60. app := New("test", "")
  61. arg0 := app.Arg("arg0", "").String()
  62. arg1 := app.Arg("arg1", "").String()
  63. arg2 := app.Arg("arg2", "").String()
  64. arg3 := app.Arg("arg3", "").String()
  65. _, err = app.Parse([]string{"prefix", "@" + f.Name(), "suffix"})
  66. assert.NoError(t, err)
  67. assert.Equal(t, "prefix", *arg0)
  68. assert.Equal(t, "hello", *arg1)
  69. assert.Equal(t, "world", *arg2)
  70. assert.Equal(t, "suffix", *arg3)
  71. }
  72. func TestParserExpandFromFileMultipleFlags(t *testing.T) {
  73. f, err := ioutil.TempFile("", "")
  74. assert.NoError(t, err)
  75. defer os.Remove(f.Name())
  76. f.WriteString("--flag1=f1\n--flag2=f2\n")
  77. f.Close()
  78. app := New("test", "")
  79. flag0 := app.Flag("flag0", "").String()
  80. flag1 := app.Flag("flag1", "").String()
  81. flag2 := app.Flag("flag2", "").String()
  82. flag3 := app.Flag("flag3", "").String()
  83. _, err = app.Parse([]string{"--flag0=f0", "@" + f.Name(), "--flag3=f3"})
  84. assert.NoError(t, err)
  85. assert.Equal(t, "f0", *flag0)
  86. assert.Equal(t, "f1", *flag1)
  87. assert.Equal(t, "f2", *flag2)
  88. assert.Equal(t, "f3", *flag3)
  89. }
  90. func TestParseContextPush(t *testing.T) {
  91. app := New("test", "")
  92. app.Command("foo", "").Command("bar", "")
  93. c := tokenize([]string{"foo", "bar"}, false)
  94. a := c.Next()
  95. assert.Equal(t, TokenArg, a.Type)
  96. b := c.Next()
  97. assert.Equal(t, TokenArg, b.Type)
  98. c.Push(b)
  99. c.Push(a)
  100. a = c.Next()
  101. assert.Equal(t, "foo", a.Value)
  102. b = c.Next()
  103. assert.Equal(t, "bar", b.Value)
  104. }