...
1 package observability
2
3 import (
4 "os"
5 "testing"
6
7 "github.com/stretchr/testify/assert"
8 )
9
10 func TestMain(m *testing.M) {
11 os.Exit(m.Run())
12 }
13 func TestLogIfStateChanged(t *testing.T) {
14 tests := map[string]struct {
15 initialState bool
16 newState bool
17 expectLog bool
18 }{
19 "Changed": {
20 initialState: true,
21 newState: false,
22 expectLog: true,
23 },
24 "NotChanged": {
25 initialState: true,
26 newState: true,
27 expectLog: false,
28 },
29 }
30
31 for name, tc := range tests {
32 t.Run(name, func(t *testing.T) {
33 stateLogger := StateLogger{
34 lastState: tc.initialState,
35 log: &spyLogger{},
36 }
37
38 stateLogger.LogIfStateChanged(tc.newState)
39 assert.Equal(t, tc.newState, stateLogger.lastState)
40 logger := stateLogger.log.(*spyLogger)
41 assert.Equal(t, tc.expectLog, logger.hasLogged)
42 })
43 }
44 }
45
46 type spyLogger struct {
47 hasLogged bool
48 }
49
50 func (s *spyLogger) Info(_ string, _ ...interface{}) {
51 s.hasLogged = true
52 }
53
View as plain text