|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package noop // import "go.opentelemetry.io/otel/log/noop" |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "reflect" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/stretchr/testify/assert" |
| 23 | + |
| 24 | + "go.opentelemetry.io/otel/log" |
| 25 | +) |
| 26 | + |
| 27 | +func TestImplementationNoPanics(t *testing.T) { |
| 28 | + // Check that if type has an embedded interface and that interface has |
| 29 | + // methods added to it than the No-Op implementation implements them. |
| 30 | + t.Run("LoggerProvider", assertAllExportedMethodNoPanic( |
| 31 | + reflect.ValueOf(LoggerProvider{}), |
| 32 | + reflect.TypeOf((*log.LoggerProvider)(nil)).Elem(), |
| 33 | + )) |
| 34 | + t.Run("Logger", assertAllExportedMethodNoPanic( |
| 35 | + reflect.ValueOf(Logger{}), |
| 36 | + reflect.TypeOf((*log.Logger)(nil)).Elem(), |
| 37 | + )) |
| 38 | +} |
| 39 | + |
| 40 | +func assertAllExportedMethodNoPanic(rVal reflect.Value, rType reflect.Type) func(*testing.T) { |
| 41 | + return func(t *testing.T) { |
| 42 | + for n := 0; n < rType.NumMethod(); n++ { |
| 43 | + mType := rType.Method(n) |
| 44 | + if !mType.IsExported() { |
| 45 | + t.Logf("ignoring unexported %s", mType.Name) |
| 46 | + continue |
| 47 | + } |
| 48 | + m := rVal.MethodByName(mType.Name) |
| 49 | + if !m.IsValid() { |
| 50 | + t.Errorf("unknown method for %s: %s", rVal.Type().Name(), mType.Name) |
| 51 | + } |
| 52 | + |
| 53 | + numIn := mType.Type.NumIn() |
| 54 | + if mType.Type.IsVariadic() { |
| 55 | + numIn-- |
| 56 | + } |
| 57 | + args := make([]reflect.Value, numIn) |
| 58 | + ctx := context.Background() |
| 59 | + for i := range args { |
| 60 | + aType := mType.Type.In(i) |
| 61 | + if aType.Name() == "Context" { |
| 62 | + // Do not panic on a nil context. |
| 63 | + args[i] = reflect.ValueOf(ctx) |
| 64 | + } else { |
| 65 | + args[i] = reflect.New(aType).Elem() |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + assert.NotPanicsf(t, func() { |
| 70 | + _ = m.Call(args) |
| 71 | + }, "%s.%s", rVal.Type().Name(), mType.Name) |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func TestNewTracerProvider(t *testing.T) { |
| 77 | + provider := NewLoggerProvider() |
| 78 | + assert.Equal(t, provider, LoggerProvider{}) |
| 79 | + logger := provider.Logger("") |
| 80 | + assert.Equal(t, logger, Logger{}) |
| 81 | +} |
0 commit comments