util_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright 2015 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package command
  15. import (
  16. "bytes"
  17. "testing"
  18. )
  19. func TestArgOrStdin(t *testing.T) {
  20. tests := []struct {
  21. args []string
  22. stdin string
  23. i int
  24. w string
  25. we error
  26. }{
  27. {
  28. args: []string{
  29. "a",
  30. },
  31. stdin: "b",
  32. i: 0,
  33. w: "a",
  34. we: nil,
  35. },
  36. {
  37. args: []string{
  38. "a",
  39. },
  40. stdin: "b",
  41. i: 1,
  42. w: "b",
  43. we: nil,
  44. },
  45. {
  46. args: []string{
  47. "a",
  48. },
  49. stdin: "",
  50. i: 1,
  51. w: "",
  52. we: ErrNoAvailSrc,
  53. },
  54. }
  55. for i, tt := range tests {
  56. var b bytes.Buffer
  57. b.Write([]byte(tt.stdin))
  58. g, ge := argOrStdin(tt.args, &b, tt.i)
  59. if g != tt.w {
  60. t.Errorf("#%d: expect %v, not %v", i, tt.w, g)
  61. }
  62. if ge != tt.we {
  63. t.Errorf("#%d: expect %v, not %v", i, tt.we, ge)
  64. }
  65. }
  66. }