| 1 | // Copyright (c) 2016 Uber Technologies, Inc. |
| 2 | // |
| 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | // of this software and associated documentation files (the "Software"), to deal |
| 5 | // in the Software without restriction, including without limitation the rights |
| 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | // copies of the Software, and to permit persons to whom the Software is |
| 8 | // furnished to do so, subject to the following conditions: |
| 9 | // |
| 10 | // The above copyright notice and this permission notice shall be included in |
| 11 | // all copies or substantial portions of the Software. |
| 12 | // |
| 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | // THE SOFTWARE. |
| 20 | |
| 21 | package zapcore_testscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore_test`/ |
| 22 | |
| 23 | import ( |
| 24 | "testingscip-go gomod github.com/golang/go/src go1.19 testing/" |
| 25 | |
| 26 | //revive:disable:dot-imports |
| 27 | . "go.uber.org/zap/zapcorescip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/" |
| 28 | "go.uber.org/zap/zaptest/observerscip-go gomod go.uber.org/zap . `go.uber.org/zap/zaptest/observer`/" |
| 29 | |
| 30 | "github.com/stretchr/testify/assertscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/" |
| 31 | "github.com/stretchr/testify/requirescip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/require`/" |
| 32 | ) |
| 33 | |
| 34 | func TestHooksscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore_test`/TestHooks().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) { |
| 35 | tests := []struct { |
| 36 | entryLevel Levelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Level# |
| 37 | coreLevel Levelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Level# |
| 38 | expectCall bool |
| 39 | }{ |
| 40 | {DebugLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/DebugLevel., InfoLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/InfoLevel., false}, |
| 41 | {InfoLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/InfoLevel., InfoLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/InfoLevel., true}, |
| 42 | {WarnLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/WarnLevel., InfoLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/InfoLevel., true}, |
| 43 | } |
| 44 | |
| 45 | for _, tt := range tests { |
| 46 | fac, logs := observerscip-go gomod go.uber.org/zap . `go.uber.org/zap/zaptest/observer`/.Newscip-go gomod go.uber.org/zap . `go.uber.org/zap/zaptest/observer`/New().(tt.coreLevel) |
| 47 | |
| 48 | // sanity check |
| 49 | requirescip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/require`/.Equalscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/require`/Equal().(t, tt.coreLevel, LevelOfscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/LevelOf().(fac), "Original logger has the wrong level") |
| 50 | |
| 51 | intField := makeInt64Fieldscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore_test`/makeInt64Field().("foo", 42) |
| 52 | ent := Entryscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Entry#{Messagescip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Entry#Message.: "bar", Levelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Entry#Level.: tt.entryLevel} |
| 53 | |
| 54 | var called int |
| 55 | f := func(e Entryscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Entry#) error { |
| 56 | called++ |
| 57 | assertscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/Equal().(t, ent, e, "Hook called with unexpected Entry.") |
| 58 | return nil |
| 59 | } |
| 60 | |
| 61 | h := RegisterHooksscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/RegisterHooks().(fac, f) |
| 62 | if ce := h.Withscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Core#With.([]Fieldscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Field#{intField}).Checkscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Core#Check.(ent, nil); ce != nil { |
| 63 | ce.Writescip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/CheckedEntry#Write().() |
| 64 | } |
| 65 | |
| 66 | t.Runscip-go gomod github.com/golang/go/src go1.19 testing/T#Run().("LevelOf", func(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) { |
| 67 | assertscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/Equal().(t, tt.coreLevel, LevelOfscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/LevelOf().(h), "Wrapped logger has the wrong log level") |
| 68 | }) |
| 69 | |
| 70 | if tt.expectCall { |
| 71 | assertscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/Equal().(t, 1, called, "Expected to call hook once.") |
| 72 | assertscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/Equal().( |
| 73 | t, |
| 74 | []observerscip-go gomod go.uber.org/zap . `go.uber.org/zap/zaptest/observer`/.LoggedEntryscip-go gomod go.uber.org/zap . `go.uber.org/zap/zaptest/observer`/LoggedEntry#{{Entryscip-go gomod go.uber.org/zap . `go.uber.org/zap/zaptest/observer`/LoggedEntry#Entry.: ent, Contextscip-go gomod go.uber.org/zap . `go.uber.org/zap/zaptest/observer`/LoggedEntry#Context.: []Fieldscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Field#{intField}}}, |
| 75 | logs.AllUntimedscip-go gomod go.uber.org/zap . `go.uber.org/zap/zaptest/observer`/ObservedLogs#AllUntimed().(), |
| 76 | "Unexpected logs written out.", |
| 77 | ) |
| 78 | } else { |
| 79 | assertscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/Equal().(t, 0, called, "Didn't expect to call hook.") |
| 80 | assertscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/Equal().(t, 0, logs.Lenscip-go gomod go.uber.org/zap . `go.uber.org/zap/zaptest/observer`/ObservedLogs#Len().(), "Unexpected logs written out.") |
| 81 | } |
| 82 | } |
| 83 | } |