123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- package cli
- import (
- "bytes"
- "io"
- "testing"
- )
- func TestBasicUi_implements(t *testing.T) {
- var _ Ui = new(BasicUi)
- }
- func TestBasicUi_Ask(t *testing.T) {
- in_r, in_w := io.Pipe()
- defer in_r.Close()
- defer in_w.Close()
- writer := new(bytes.Buffer)
- ui := &BasicUi{
- Reader: in_r,
- Writer: writer,
- }
- go in_w.Write([]byte("foo bar\nbaz\n"))
- result, err := ui.Ask("Name?")
- if err != nil {
- t.Fatalf("err: %s", err)
- }
- if writer.String() != "Name? " {
- t.Fatalf("bad: %#v", writer.String())
- }
- if result != "foo bar" {
- t.Fatalf("bad: %#v", result)
- }
- }
- func TestBasicUi_AskSecret(t *testing.T) {
- in_r, in_w := io.Pipe()
- defer in_r.Close()
- defer in_w.Close()
- writer := new(bytes.Buffer)
- ui := &BasicUi{
- Reader: in_r,
- Writer: writer,
- }
- go in_w.Write([]byte("foo bar\nbaz\n"))
- result, err := ui.AskSecret("Name?")
- if err != nil {
- t.Fatalf("err: %s", err)
- }
- if writer.String() != "Name? " {
- t.Fatalf("bad: %#v", writer.String())
- }
- if result != "foo bar" {
- t.Fatalf("bad: %#v", result)
- }
- }
- func TestBasicUi_Error(t *testing.T) {
- writer := new(bytes.Buffer)
- ui := &BasicUi{Writer: writer}
- ui.Error("HELLO")
- if writer.String() != "HELLO\n" {
- t.Fatalf("bad: %s", writer.String())
- }
- }
- func TestBasicUi_Error_ErrorWriter(t *testing.T) {
- writer := new(bytes.Buffer)
- ewriter := new(bytes.Buffer)
- ui := &BasicUi{Writer: writer, ErrorWriter: ewriter}
- ui.Error("HELLO")
- if ewriter.String() != "HELLO\n" {
- t.Fatalf("bad: %s", ewriter.String())
- }
- }
- func TestBasicUi_Output(t *testing.T) {
- writer := new(bytes.Buffer)
- ui := &BasicUi{Writer: writer}
- ui.Output("HELLO")
- if writer.String() != "HELLO\n" {
- t.Fatalf("bad: %s", writer.String())
- }
- }
- func TestBasicUi_Warn(t *testing.T) {
- writer := new(bytes.Buffer)
- ui := &BasicUi{Writer: writer}
- ui.Warn("HELLO")
- if writer.String() != "HELLO\n" {
- t.Fatalf("bad: %s", writer.String())
- }
- }
- func TestPrefixedUi_implements(t *testing.T) {
- var _ Ui = new(PrefixedUi)
- }
- func TestPrefixedUiError(t *testing.T) {
- ui := new(MockUi)
- p := &PrefixedUi{
- ErrorPrefix: "foo",
- Ui: ui,
- }
- p.Error("bar")
- if ui.ErrorWriter.String() != "foobar\n" {
- t.Fatalf("bad: %s", ui.ErrorWriter.String())
- }
- }
- func TestPrefixedUiInfo(t *testing.T) {
- ui := new(MockUi)
- p := &PrefixedUi{
- InfoPrefix: "foo",
- Ui: ui,
- }
- p.Info("bar")
- if ui.OutputWriter.String() != "foobar\n" {
- t.Fatalf("bad: %s", ui.OutputWriter.String())
- }
- }
- func TestPrefixedUiOutput(t *testing.T) {
- ui := new(MockUi)
- p := &PrefixedUi{
- OutputPrefix: "foo",
- Ui: ui,
- }
- p.Output("bar")
- if ui.OutputWriter.String() != "foobar\n" {
- t.Fatalf("bad: %s", ui.OutputWriter.String())
- }
- }
- func TestPrefixedUiWarn(t *testing.T) {
- ui := new(MockUi)
- p := &PrefixedUi{
- WarnPrefix: "foo",
- Ui: ui,
- }
- p.Warn("bar")
- if ui.ErrorWriter.String() != "foobar\n" {
- t.Fatalf("bad: %s", ui.ErrorWriter.String())
- }
- }
|