2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-23 11:28:54 +00:00
restic/internal/ui/stdio_wrapper_test.go

85 lines
1.3 KiB
Go
Raw Normal View History

2018-04-22 09:57:20 +00:00
package ui
import (
"strings"
2018-04-22 09:57:20 +00:00
"testing"
"github.com/google/go-cmp/cmp"
)
func TestStdioWrapper(t *testing.T) {
var tests = []struct {
inputs [][]byte
output string
2018-04-22 09:57:20 +00:00
}{
{
inputs: [][]byte{
[]byte("foo"),
},
output: "foo\n",
2018-04-22 09:57:20 +00:00
},
{
inputs: [][]byte{
[]byte("foo"),
[]byte("bar"),
[]byte("\n"),
[]byte("baz"),
},
output: "foobar\n" +
2018-04-22 09:57:20 +00:00
"baz\n",
},
{
inputs: [][]byte{
[]byte("foo"),
[]byte("bar\nbaz\n"),
[]byte("bump\n"),
},
output: "foobar\n" +
"baz\n" +
2018-04-22 09:57:20 +00:00
"bump\n",
},
{
inputs: [][]byte{
[]byte("foo"),
[]byte("bar\nbaz\n"),
[]byte("bum"),
[]byte("p\nx"),
[]byte("x"),
[]byte("x"),
[]byte("z"),
},
output: "foobar\n" +
"baz\n" +
"bump\n" +
2018-04-22 09:57:20 +00:00
"xxxz\n",
},
}
for _, test := range tests {
t.Run("", func(t *testing.T) {
var output strings.Builder
w := newLineWriter(func(s string) { output.WriteString(s) })
2018-04-22 09:57:20 +00:00
for _, data := range test.inputs {
n, err := w.Write(data)
if err != nil {
t.Fatal(err)
}
if n != len(data) {
t.Errorf("invalid length returned by Write, want %d, got %d", len(data), n)
}
}
err := w.Close()
if err != nil {
t.Fatal(err)
}
if outstr := output.String(); outstr != test.output {
t.Error(cmp.Diff(test.output, outstr))
2018-04-22 09:57:20 +00:00
}
})
}
}