ui_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package cli
  2. import (
  3. "bytes"
  4. "io"
  5. "testing"
  6. )
  7. func TestBasicUi_implements(t *testing.T) {
  8. var _ Ui = new(BasicUi)
  9. }
  10. func TestBasicUi_Ask(t *testing.T) {
  11. in_r, in_w := io.Pipe()
  12. defer in_r.Close()
  13. defer in_w.Close()
  14. writer := new(bytes.Buffer)
  15. ui := &BasicUi{
  16. Reader: in_r,
  17. Writer: writer,
  18. }
  19. go in_w.Write([]byte("foo bar\nbaz\n"))
  20. result, err := ui.Ask("Name?")
  21. if err != nil {
  22. t.Fatalf("err: %s", err)
  23. }
  24. if writer.String() != "Name? " {
  25. t.Fatalf("bad: %#v", writer.String())
  26. }
  27. if result != "foo bar" {
  28. t.Fatalf("bad: %#v", result)
  29. }
  30. }
  31. func TestBasicUi_AskSecret(t *testing.T) {
  32. in_r, in_w := io.Pipe()
  33. defer in_r.Close()
  34. defer in_w.Close()
  35. writer := new(bytes.Buffer)
  36. ui := &BasicUi{
  37. Reader: in_r,
  38. Writer: writer,
  39. }
  40. go in_w.Write([]byte("foo bar\nbaz\n"))
  41. result, err := ui.AskSecret("Name?")
  42. if err != nil {
  43. t.Fatalf("err: %s", err)
  44. }
  45. if writer.String() != "Name? " {
  46. t.Fatalf("bad: %#v", writer.String())
  47. }
  48. if result != "foo bar" {
  49. t.Fatalf("bad: %#v", result)
  50. }
  51. }
  52. func TestBasicUi_Error(t *testing.T) {
  53. writer := new(bytes.Buffer)
  54. ui := &BasicUi{Writer: writer}
  55. ui.Error("HELLO")
  56. if writer.String() != "HELLO\n" {
  57. t.Fatalf("bad: %s", writer.String())
  58. }
  59. }
  60. func TestBasicUi_Error_ErrorWriter(t *testing.T) {
  61. writer := new(bytes.Buffer)
  62. ewriter := new(bytes.Buffer)
  63. ui := &BasicUi{Writer: writer, ErrorWriter: ewriter}
  64. ui.Error("HELLO")
  65. if ewriter.String() != "HELLO\n" {
  66. t.Fatalf("bad: %s", ewriter.String())
  67. }
  68. }
  69. func TestBasicUi_Output(t *testing.T) {
  70. writer := new(bytes.Buffer)
  71. ui := &BasicUi{Writer: writer}
  72. ui.Output("HELLO")
  73. if writer.String() != "HELLO\n" {
  74. t.Fatalf("bad: %s", writer.String())
  75. }
  76. }
  77. func TestBasicUi_Warn(t *testing.T) {
  78. writer := new(bytes.Buffer)
  79. ui := &BasicUi{Writer: writer}
  80. ui.Warn("HELLO")
  81. if writer.String() != "HELLO\n" {
  82. t.Fatalf("bad: %s", writer.String())
  83. }
  84. }
  85. func TestPrefixedUi_implements(t *testing.T) {
  86. var _ Ui = new(PrefixedUi)
  87. }
  88. func TestPrefixedUiError(t *testing.T) {
  89. ui := new(MockUi)
  90. p := &PrefixedUi{
  91. ErrorPrefix: "foo",
  92. Ui: ui,
  93. }
  94. p.Error("bar")
  95. if ui.ErrorWriter.String() != "foobar\n" {
  96. t.Fatalf("bad: %s", ui.ErrorWriter.String())
  97. }
  98. }
  99. func TestPrefixedUiInfo(t *testing.T) {
  100. ui := new(MockUi)
  101. p := &PrefixedUi{
  102. InfoPrefix: "foo",
  103. Ui: ui,
  104. }
  105. p.Info("bar")
  106. if ui.OutputWriter.String() != "foobar\n" {
  107. t.Fatalf("bad: %s", ui.OutputWriter.String())
  108. }
  109. }
  110. func TestPrefixedUiOutput(t *testing.T) {
  111. ui := new(MockUi)
  112. p := &PrefixedUi{
  113. OutputPrefix: "foo",
  114. Ui: ui,
  115. }
  116. p.Output("bar")
  117. if ui.OutputWriter.String() != "foobar\n" {
  118. t.Fatalf("bad: %s", ui.OutputWriter.String())
  119. }
  120. }
  121. func TestPrefixedUiWarn(t *testing.T) {
  122. ui := new(MockUi)
  123. p := &PrefixedUi{
  124. WarnPrefix: "foo",
  125. Ui: ui,
  126. }
  127. p.Warn("bar")
  128. if ui.ErrorWriter.String() != "foobar\n" {
  129. t.Fatalf("bad: %s", ui.ErrorWriter.String())
  130. }
  131. }