@@ -31,17 +31,20 @@ func TestOutputSelection(t *testing.T) {
3131 myOut := new (bytes.Buffer )
3232 SetOut (myOut )
3333 SetErr (myErr )
34+
35+ // Set the log level to minimal to ensure the message is printed
36+ SetLogLevel (LogLevelMinimal )
3437 SetFormat (Text )
3538
36- // Could not change output stream after format has been set
3739 require .Panics (t , func () { SetOut (nil ) })
3840 require .Panics (t , func () { SetErr (nil ) })
39-
40- // Coule not change output format twice
4141 require .Panics (t , func () { SetFormat (JSON ) })
4242
43- Print ("Hello" )
44- require .Equal (t , myOut .String (), "Hello\n " )
43+ Minimal ("Hello" )
44+
45+ require .Equal (t , "Hello\n " , myErr .String ())
46+ // The standard output stream should be empty
47+ require .Empty (t , myOut .String ())
4548}
4649
4750func TestJSONOutputStream (t * testing.T ) {
@@ -65,59 +68,42 @@ func TestJSONOutputStream(t *testing.T) {
6568 require .JSONEq (t , string (d ), `{"stdout":"Hello\ufffdA","stderr":"Hello ERR"}` )
6669}
6770
68- func TestJsonOutputOnCustomStreams (t * testing.T ) {
71+ func TestJsonOutput (t * testing.T ) {
6972 reset ()
7073
74+ // Setup custom buffers for stdout and stderr
7175 myErr := new (bytes.Buffer )
7276 myOut := new (bytes.Buffer )
7377 SetOut (myOut )
7478 SetErr (myErr )
75- SetFormat (JSON )
7679
77- // Could not change output stream after format has been set
78- require .Panics (t , func () { SetOut (nil ) })
79- require .Panics (t , func () { SetErr (nil ) })
80- // Could not change output format twice
81- require .Panics (t , func () { SetFormat (JSON ) })
80+ // Set the state for the test
81+ SetLogLevel (LogLevelMinimal )
82+ SetFormat (JSON )
8283
83- Print ("Hello" ) // Output interactive data
84+ // --- Test 1: Verify a log message ---
85+ Minimal ("Hello" )
8486
85- require .Equal (t , "" , myOut .String ())
86- require .Equal (t , "" , myErr .String ())
87- require .Equal (t , "Hello\n " , bufferOut .String ())
87+ // In JSON mode, logs are JSON Lines sent to stdout (myOut).
88+ expectedLog := `{"message": "Hello"}` // Assuming your printLogMessage does this
89+ require .JSONEq (t , expectedLog , myOut .String ())
90+ require .Empty (t , myErr .String (), "Stderr should be empty for a log message" )
91+ myOut .Reset () // Reset the buffer for the next test
8892
93+ // --- Test 2: Verify a result message ---
8994 PrintResult (& testResult {Success : true })
9095
91- require .JSONEq (t , myOut .String (), `{ "success": true }` )
92- require .Equal (t , myErr .String (), "" )
93- myOut .Reset ()
94-
95- _ , _ , res := OutputStreams ()
96- PrintResult (& testResult {Success : false , Output : res ()})
97-
98- require .JSONEq (t , `
99- {
100- "success": false,
101- "output": {
102- "stdout": "Hello\n",
103- "stderr": ""
104- }
105- }` , myOut .String ())
106- require .Equal (t , myErr .String (), "" )
96+ // PrintResult should output a well-formed JSON object to stdout (myOut).
97+ expectedResult := `{
98+ "success": true
99+ }`
100+ require .JSONEq (t , expectedResult , myOut .String ())
101+ require .Empty (t , myErr .String (), "Stderr should be empty for a success result" )
107102}
108103
109104type testResult struct {
110- Success bool `json:"success"`
111- Output * OutputStreamsResult `json:"output,omitempty"`
105+ Success bool `json:"success"`
112106}
113107
114- func (r * testResult ) Data () interface {} {
115- return r
116- }
117-
118- func (r * testResult ) String () string {
119- if r .Success {
120- return "Success"
121- }
122- return "Failure"
123- }
108+ func (r testResult ) String () string { return fmt .Sprintf ("Success: %t" , r .Success ) }
109+ func (r testResult ) Data () interface {} { return r }
0 commit comments