-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
09411bf
commit 7770bbd
Showing
6 changed files
with
144 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package utils | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/edaniels/golog" | ||
"go.uber.org/zap" | ||
"go.viam.com/test" | ||
) | ||
|
||
// InvalidLogger fulfills the ZapCompatibleLogger interface without a Named() or Sublogger() method, used to test | ||
// that utils.Sublogger() should fail without either of these methods. | ||
type InvalidLogger struct { | ||
name string | ||
} | ||
|
||
func (m *InvalidLogger) Desugar() *zap.Logger { | ||
return zap.NewNop() | ||
} | ||
|
||
func (m *InvalidLogger) With(args ...interface{}) *zap.SugaredLogger { | ||
return zap.NewNop().Sugar() | ||
} | ||
|
||
func (m *InvalidLogger) Debug(args ...interface{}) { | ||
} | ||
|
||
func (m *InvalidLogger) Debugf(template string, args ...interface{}) { | ||
} | ||
|
||
func (m *InvalidLogger) Debugw(msg string, keysAndValues ...interface{}) { | ||
} | ||
|
||
func (m *InvalidLogger) Info(args ...interface{}) { | ||
} | ||
|
||
func (m *InvalidLogger) Infof(template string, args ...interface{}) { | ||
} | ||
|
||
func (m *InvalidLogger) Infow(msg string, keysAndValues ...interface{}) { | ||
} | ||
|
||
func (m *InvalidLogger) Warn(args ...interface{}) { | ||
} | ||
|
||
func (m *InvalidLogger) Warnf(template string, args ...interface{}) { | ||
} | ||
|
||
func (m *InvalidLogger) Warnw(msg string, keysAndValues ...interface{}) { | ||
} | ||
|
||
func (m *InvalidLogger) Error(args ...interface{}) { | ||
} | ||
|
||
func (m *InvalidLogger) Errorf(template string, args ...interface{}) { | ||
} | ||
|
||
func (m *InvalidLogger) Errorw(msg string, keysAndValues ...interface{}) { | ||
} | ||
|
||
func (m *InvalidLogger) Fatal(args ...interface{}) { | ||
} | ||
|
||
func (m *InvalidLogger) Fatalf(template string, args ...interface{}) { | ||
} | ||
|
||
func (m *InvalidLogger) Fatalw(msg string, keysAndValues ...interface{}) { | ||
} | ||
|
||
// MockLogger fulfills the ZapCompatibleLogger interface by extending InvalidLogger with a Sublogger() method. This type | ||
// is used to simulate calling utils.Sublogger() on an RDK logger. | ||
type MockLogger struct { | ||
InvalidLogger | ||
name string | ||
} | ||
|
||
func (m *MockLogger) Sublogger(subname string) ZapCompatibleLogger { | ||
return &MockLogger{name: m.name + "." + subname} | ||
} | ||
|
||
func TestSubloggerWithZapLogger(t *testing.T) { | ||
logger := golog.NewTestLogger(t) | ||
sublogger := Sublogger(logger, "sub") | ||
test.That(t, sublogger, test.ShouldNotBeNil) | ||
test.That(t, sublogger, test.ShouldNotEqual, logger) | ||
test.That(t, reflect.TypeOf(sublogger), test.ShouldEqual, reflect.TypeOf(logger)) | ||
} | ||
|
||
func TestSubloggerWithMockRDKLogger(t *testing.T) { | ||
logger := &MockLogger{name: "main"} | ||
sublogger := Sublogger(logger, "sub") | ||
test.That(t, sublogger, test.ShouldNotBeNil) | ||
test.That(t, sublogger, test.ShouldNotEqual, logger) | ||
test.That(t, reflect.TypeOf(sublogger), test.ShouldEqual, reflect.TypeOf(logger)) | ||
} | ||
|
||
func TestSubloggerWithInvalidLogger(t *testing.T) { | ||
logger := &InvalidLogger{name: "main"} | ||
sublogger := Sublogger(logger, "sub") | ||
// Sublogger returns logger (itself) if creating a sublogger fails, which we expect | ||
test.That(t, sublogger, test.ShouldEqual, logger) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters