util_test.go 767 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package command
  2. import (
  3. "bytes"
  4. "testing"
  5. )
  6. func TestArgOrStdin(t *testing.T) {
  7. tests := []struct {
  8. args []string
  9. stdin string
  10. i int
  11. w string
  12. we error
  13. }{
  14. {
  15. args: []string{
  16. "a",
  17. },
  18. stdin: "b",
  19. i: 0,
  20. w: "a",
  21. we: nil,
  22. },
  23. {
  24. args: []string{
  25. "a",
  26. },
  27. stdin: "b",
  28. i: 1,
  29. w: "b",
  30. we: nil,
  31. },
  32. {
  33. args: []string{
  34. "a",
  35. },
  36. stdin: "",
  37. i: 1,
  38. w: "",
  39. we: ErrNoAvailSrc,
  40. },
  41. }
  42. for i, tt := range tests {
  43. var b bytes.Buffer
  44. b.Write([]byte(tt.stdin))
  45. g, ge := argOrStdin(tt.args, &b, tt.i)
  46. if g != tt.w {
  47. t.Errorf("#%d: expect %v, not %v", i, tt.w, g)
  48. }
  49. if ge != tt.we {
  50. t.Errorf("#%d: expect %v, not %v", i, tt.we, ge)
  51. }
  52. }
  53. }