-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
62 lines (58 loc) · 2.03 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"bytes"
"fmt"
"io"
"os"
"strings"
"testing"
)
func Test_run(t *testing.T) {
type args struct {
reader io.Reader
args []string
}
tests := []struct {
name string
args args
wantOutput []byte
wantRc int
}{
{"simple", args{strings.NewReader("test"), []string{}}, []byte{34, 116, 101, 115, 116, 34, 10}, 0},
{"empty", args{strings.NewReader(""), []string{}}, []byte(""), 0},
{"zero", args{bytes.NewReader([]byte{0}), []string{}}, []byte{34, 92, 120, 48, 48, 34, 10}, 0},
{"avrox-string", args{strings.NewReader("test\n"), []string{"avrox", "string"}},
append([]byte("\x93\x00\x00\x01\x00\x01\x01\x01\n"), []byte("test\n")...), 0},
{"avrox-decimal", args{strings.NewReader("1.3\n"), []string{"avrox", "decimal"}},
[]byte("\x93\x00\x00\x01\x00\x06\x01\x04\x02\x042\xc8"), 0},
{"avrox-rawdate", args{strings.NewReader("0001-01-01"), []string{"avrox", "rawdate"}},
[]byte("\x93\x00\x00\x01\x00\a\x01\a\x00\x00\x00"), 0},
{"strip-lf", args{strings.NewReader("test\n"), []string{"avrox", "-s", "string"}},
append([]byte("\x93\x00\x00\x01\x00\x01\x01\x01\b"), []byte("test")...), 0},
{"unquote", args{strings.NewReader(`test\n`), []string{"avrox", "-u", "string"}},
append([]byte("\x93\x00\x00\x01\x00\x01\x01\x01\n"), []byte("test\n")...), 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
originalStdout := os.Stdout
// Create a new pipe and make it the stdout.
r, w, _ := os.Pipe()
os.Stdout = w
if gotRc := run(tt.args.reader, tt.args.args); gotRc != tt.wantRc {
t.Errorf("run() = %v, want %v", gotRc, tt.wantRc)
}
// Restore the original stdout.
os.Stdout = originalStdout
// Close the writer and read what was written.
_ = w.Close()
var buf bytes.Buffer
_, _ = buf.ReadFrom(r)
gotOutput := buf.Bytes()
// Check the output.
if !bytes.Equal(gotOutput, tt.wantOutput) {
fmt.Printf("%q, want %q", string(gotOutput), string(tt.wantOutput))
t.Errorf("run() = %v, want %v", gotOutput, tt.wantOutput)
}
})
}
}