-
-
Notifications
You must be signed in to change notification settings - Fork 398
/
Copy pathfeedback_test.go
123 lines (97 loc) · 3 KB
/
feedback_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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// This file is part of arduino-cli.
//
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to license@arduino.cc.
package feedback
import (
"bytes"
"encoding/json"
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
func TestOutputSelection(t *testing.T) {
reset()
myErr := new(bytes.Buffer)
myOut := new(bytes.Buffer)
SetOut(myOut)
SetErr(myErr)
SetFormat(Text)
// Could not change output stream after format has been set
require.Panics(t, func() { SetOut(nil) })
require.Panics(t, func() { SetErr(nil) })
// Coule not change output format twice
require.Panics(t, func() { SetFormat(JSON) })
Print("Hello")
require.Equal(t, myOut.String(), "Hello\n")
}
func TestJSONOutputStream(t *testing.T) {
reset()
require.Panics(t, func() { OutputStreams() })
SetFormat(JSON)
stdout, stderr, res := OutputStreams()
fmt.Fprint(stdout, "Hello")
fmt.Fprint(stderr, "Hello ERR")
d, err := json.Marshal(res())
require.NoError(t, err)
require.JSONEq(t, `{"stdout":"Hello","stderr":"Hello ERR"}`, string(d))
stdout.Write([]byte{0xc2, 'A'}) // Invaid UTF-8
d, err = json.Marshal(res())
require.NoError(t, err)
require.JSONEq(t, string(d), `{"stdout":"Hello\ufffdA","stderr":"Hello ERR"}`)
}
func TestJsonOutputOnCustomStreams(t *testing.T) {
reset()
myErr := new(bytes.Buffer)
myOut := new(bytes.Buffer)
SetOut(myOut)
SetErr(myErr)
SetFormat(JSON)
// Could not change output stream after format has been set
require.Panics(t, func() { SetOut(nil) })
require.Panics(t, func() { SetErr(nil) })
// Could not change output format twice
require.Panics(t, func() { SetFormat(JSON) })
Print("Hello") // Output interactive data
require.Equal(t, "", myOut.String())
require.Equal(t, "", myErr.String())
require.Equal(t, "Hello\n", bufferOut.String())
PrintResult(&testResult{Success: true})
require.JSONEq(t, myOut.String(), `{ "success": true }`)
require.Equal(t, myErr.String(), "")
myOut.Reset()
_, _, res := OutputStreams()
PrintResult(&testResult{Success: false, Output: res()})
require.JSONEq(t, `
{
"success": false,
"output": {
"stdout": "Hello\n",
"stderr": ""
}
}`, myOut.String())
require.Equal(t, myErr.String(), "")
}
type testResult struct {
Success bool `json:"success"`
Output *OutputStreamsResult `json:"output,omitempty"`
}
func (r *testResult) Data() interface{} {
return r
}
func (r *testResult) String() string {
if r.Success {
return "Success"
}
return "Failure"
}