2013-03-24 17:31:18 -04:00
|
|
|
package packer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"cgl.tideland.biz/asserts"
|
2013-03-24 17:47:59 -04:00
|
|
|
"testing"
|
2013-03-24 17:31:18 -04:00
|
|
|
)
|
|
|
|
|
2013-04-20 21:55:02 -04:00
|
|
|
func testUi() *ReaderWriterUi {
|
|
|
|
return &ReaderWriterUi{
|
2013-06-14 18:17:03 -04:00
|
|
|
Reader: new(bytes.Buffer),
|
|
|
|
Writer: new(bytes.Buffer),
|
2013-04-20 21:55:02 -04:00
|
|
|
}
|
|
|
|
}
|
2013-03-24 17:31:18 -04:00
|
|
|
|
2013-06-03 16:35:43 -04:00
|
|
|
func TestColoredUi(t *testing.T) {
|
|
|
|
bufferUi := testUi()
|
2013-06-12 13:41:58 -04:00
|
|
|
ui := &ColoredUi{UiColorYellow, UiColorRed, bufferUi}
|
2013-06-03 16:35:43 -04:00
|
|
|
|
|
|
|
ui.Say("foo")
|
2013-06-12 13:41:58 -04:00
|
|
|
result := readWriter(bufferUi)
|
|
|
|
if result != "\033[1;33;40mfoo\033[0m\n" {
|
|
|
|
t.Fatalf("invalid output: %s", result)
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Message("foo")
|
|
|
|
result = readWriter(bufferUi)
|
|
|
|
if result != "\033[0;33;40mfoo\033[0m\n" {
|
|
|
|
t.Fatalf("invalid output: %s", result)
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Error("foo")
|
|
|
|
result = readWriter(bufferUi)
|
|
|
|
if result != "\033[1;31;40mfoo\033[0m\n" {
|
|
|
|
t.Fatalf("invalid output: %s", result)
|
|
|
|
}
|
2013-06-03 16:35:43 -04:00
|
|
|
}
|
|
|
|
|
2013-05-21 16:20:51 -04:00
|
|
|
func TestPrefixedUi(t *testing.T) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
|
|
|
bufferUi := testUi()
|
2013-06-03 14:30:38 -04:00
|
|
|
prefixUi := &PrefixedUi{"mitchell", "bar", bufferUi}
|
2013-05-21 16:20:51 -04:00
|
|
|
|
|
|
|
prefixUi.Say("foo")
|
|
|
|
assert.Equal(readWriter(bufferUi), "mitchell: foo\n", "should have prefix")
|
|
|
|
|
2013-06-03 14:30:38 -04:00
|
|
|
prefixUi.Message("foo")
|
|
|
|
assert.Equal(readWriter(bufferUi), "bar: foo\n", "should have prefix")
|
|
|
|
|
2013-05-21 16:20:51 -04:00
|
|
|
prefixUi.Error("bar")
|
|
|
|
assert.Equal(readWriter(bufferUi), "mitchell: bar\n", "should have prefix")
|
|
|
|
}
|
|
|
|
|
2013-06-03 16:35:43 -04:00
|
|
|
func TestColoredUi_ImplUi(t *testing.T) {
|
|
|
|
var raw interface{}
|
|
|
|
raw = &ColoredUi{}
|
|
|
|
if _, ok := raw.(Ui); !ok {
|
|
|
|
t.Fatalf("ColoredUi must implement Ui")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-21 16:20:51 -04:00
|
|
|
func TestPrefixedUi_ImplUi(t *testing.T) {
|
|
|
|
var raw interface{}
|
|
|
|
raw = &PrefixedUi{}
|
|
|
|
if _, ok := raw.(Ui); !ok {
|
|
|
|
t.Fatalf("PrefixedUi must implement Ui")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReaderWriterUi_ImplUi(t *testing.T) {
|
|
|
|
var raw interface{}
|
|
|
|
raw = &ReaderWriterUi{}
|
|
|
|
if _, ok := raw.(Ui); !ok {
|
|
|
|
t.Fatalf("ReaderWriterUi must implement Ui")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-08 18:12:48 -04:00
|
|
|
func TestReaderWriterUi_Error(t *testing.T) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
|
|
|
bufferUi := testUi()
|
|
|
|
|
|
|
|
bufferUi.Error("foo")
|
2013-05-21 02:43:37 -04:00
|
|
|
assert.Equal(readWriter(bufferUi), "foo\n", "basic output")
|
2013-05-08 18:12:48 -04:00
|
|
|
|
2013-05-27 18:12:48 -04:00
|
|
|
bufferUi.Error("5")
|
2013-05-21 02:43:37 -04:00
|
|
|
assert.Equal(readWriter(bufferUi), "5\n", "formatting")
|
2013-05-08 18:12:48 -04:00
|
|
|
}
|
|
|
|
|
2013-03-24 17:31:18 -04:00
|
|
|
func TestReaderWriterUi_Say(t *testing.T) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
2013-04-20 21:55:02 -04:00
|
|
|
bufferUi := testUi()
|
|
|
|
|
2013-03-24 17:31:18 -04:00
|
|
|
bufferUi.Say("foo")
|
2013-05-21 02:43:37 -04:00
|
|
|
assert.Equal(readWriter(bufferUi), "foo\n", "basic output")
|
2013-03-24 17:31:18 -04:00
|
|
|
|
2013-05-27 18:12:48 -04:00
|
|
|
bufferUi.Say("5")
|
2013-05-21 02:43:37 -04:00
|
|
|
assert.Equal(readWriter(bufferUi), "5\n", "formatting")
|
2013-03-24 17:31:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// This reads the output from the bytes.Buffer in our test object
|
|
|
|
// and then resets the buffer.
|
|
|
|
func readWriter(ui *ReaderWriterUi) (result string) {
|
|
|
|
buffer := ui.Writer.(*bytes.Buffer)
|
|
|
|
result = buffer.String()
|
|
|
|
buffer.Reset()
|
|
|
|
return
|
|
|
|
}
|