MCPcopy
hub / github.com/uber-go/zap / TestSugarFatalLogging

Function TestSugarFatalLogging

sugar_test.go:477–511  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

475}
476
477func TestSugarFatalLogging(t *testing.T) {
478 tests := []struct {
479 loggerLevel zapcore.Level
480 f func(*SugaredLogger)
481 expectedMsg string
482 }{
483 {FatalLevel + 1, func(s *SugaredLogger) { s.Fatal("foo") }, ""},
484 {FatalLevel, func(s *SugaredLogger) { s.Fatal("foo") }, "foo"},
485 {DebugLevel, func(s *SugaredLogger) { s.Fatal("foo") }, "foo"},
486 {FatalLevel + 1, func(s *SugaredLogger) { s.Fatalf("%s", "foo") }, ""},
487 {FatalLevel, func(s *SugaredLogger) { s.Fatalf("%s", "foo") }, "foo"},
488 {DebugLevel, func(s *SugaredLogger) { s.Fatalf("%s", "foo") }, "foo"},
489 {FatalLevel + 1, func(s *SugaredLogger) { s.Fatalw("foo") }, ""},
490 {FatalLevel, func(s *SugaredLogger) { s.Fatalw("foo") }, "foo"},
491 {DebugLevel, func(s *SugaredLogger) { s.Fatalw("foo") }, "foo"},
492 {FatalLevel + 1, func(s *SugaredLogger) { s.Fatalln("foo") }, ""},
493 {FatalLevel, func(s *SugaredLogger) { s.Fatalln("foo") }, "foo"},
494 {DebugLevel, func(s *SugaredLogger) { s.Fatalln("foo") }, "foo"},
495 }
496
497 for _, tt := range tests {
498 withSugar(t, tt.loggerLevel, nil, func(sugar *SugaredLogger, logs *observer.ObservedLogs) {
499 stub := exit.WithStub(func() { tt.f(sugar) })
500 assert.True(t, stub.Exited, "Expected all calls to fatal logger methods to exit process.")
501 if tt.expectedMsg != "" {
502 assert.Equal(t, []observer.LoggedEntry{{
503 Context: []Field{},
504 Entry: zapcore.Entry{Message: tt.expectedMsg, Level: FatalLevel},
505 }}, logs.AllUntimed(), "Unexpected log output.")
506 } else {
507 assert.Equal(t, 0, logs.Len(), "Didn't expect any log output.")
508 }
509 })
510 }
511}
512
513func TestSugarAddCaller(t *testing.T) {
514 tests := []struct {

Callers

nothing calls this directly

Calls 8

WithStubFunction · 0.92
withSugarFunction · 0.85
FatalwMethod · 0.80
AllUntimedMethod · 0.80
FatalMethod · 0.45
FatalfMethod · 0.45
FatallnMethod · 0.45
LenMethod · 0.45

Tested by

no test coverage detected