-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
80 lines (70 loc) · 2.19 KB
/
Copy pathmain_test.go
File metadata and controls
80 lines (70 loc) · 2.19 KB
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
package main
import (
"bytes"
"io"
"os"
"testing"
"github.com/stretchr/testify/require"
)
// TestSetupLoggingTrue tests the setupLogging function when the logging
// parameter is true.
func TestSetupLoggingTrue(t *testing.T) {
setupLogging("true")
require.NotNil(t, logs, "Expected logs to be initialised")
require.Equal(t, true, logs.Enabled, "Expected logs to be enabled")
}
// TestSetupLoggingFalse tests the setupLogging function when the logging
// parameter is false.
func TestSetupLoggingFalse(t *testing.T) {
setupLogging("false")
require.NotNil(t, logs, "Expected logs to be initialised")
require.Equal(t, false, logs.Enabled, "Expected logs to be disabled")
}
// TestSetupLoggingInvalid tests the setupLogging function when the logging
// parameter is invalid.
func TestSetupLoggingInvalid(t *testing.T) {
setupLogging("invalid")
require.NotNil(t, logs, "Expected logs to be initialised")
require.Equal(t, false, logs.Enabled, "Expected logs to be disabled")
}
// TestSetupLoggingEmpty tests the setupLogging function when the logging
// parameter is empty.
func TestSetupLoggingEmpty(t *testing.T) {
setupLogging("")
require.NotNil(t, logs, "Expected logs to be initialised")
require.Equal(t, false, logs.Enabled, "Expected logs to be disabled")
}
// TestPrintlnLogging tests the Println function of the Logs struct.
func TestPrintlnLogging(t *testing.T) {
setupLogging("true")
output1 := captureOutput(func() {
logs.Println("Test")
})
require.Equal(t, "Test\n", output1, "Expected output to be 'Test\n'")
setupLogging("false")
output2 := captureOutput(func() {
logs.Println("Test")
})
require.Equal(t, "", output2, "Expected output to be ''")
}
// captureOutput is a helper function to capture the output of a function.
// This is used to test the output of the display functions.
func captureOutput(f func()) string {
// Store the old stdout and replace it with a pipe.
original := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
// Run the function.
f()
// Close the pipe and restore stdout.
w.Close()
os.Stdout = original
// Read the output from the pipe.
var buf bytes.Buffer
_, err := io.Copy(&buf, r)
if err != nil {
panic(err)
}
// Return the output.
return buf.String()
}