| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package noop // import "go.opentelemetry.io/otel/log/noop" |
| 5 |
|
| 6 |
import ( |
| 7 |
"reflect" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/stretchr/testify/assert" |
| 11 |
|
| 12 |
"go.opentelemetry.io/otel/log" |
| 13 |
) |
| 14 |
|
| 15 |
func TestImplementationNoPanics(t *testing.T) { |
| 16 |
// Check that if type has an embedded interface and that interface has |
| 17 |
// methods added to it than the No-Op implementation implements them. |
| 18 |
t.Run("LoggerProvider", assertAllExportedMethodNoPanic( |
| 19 |
reflect.ValueOf(LoggerProvider{}), |
| 20 |
reflect.TypeFor[log.LoggerProvider](), |
| 21 |
)) |
| 22 |
t.Run("Logger", assertAllExportedMethodNoPanic( |
| 23 |
reflect.ValueOf(Logger{}), |
| 24 |
reflect.TypeFor[log.Logger](), |
| 25 |
)) |
| 26 |
} |
| 27 |
|
| 28 |
func assertAllExportedMethodNoPanic(rVal reflect.Value, rType reflect.Type) func(*testing.T) { |
| 29 |
return func(t *testing.T) { |
| 30 |
for n := 0; n < rType.NumMethod(); n++ { |
| 31 |
mType := rType.Method(n) |
| 32 |
if !mType.IsExported() { |
| 33 |
t.Logf("ignoring unexported %s", mType.Name) |
| 34 |
continue |
| 35 |
} |
| 36 |
m := rVal.MethodByName(mType.Name) |
| 37 |
if !m.IsValid() { |
| 38 |
t.Errorf("unknown method for %s: %s", rVal.Type().Name(), mType.Name) |
| 39 |
} |
| 40 |
|
| 41 |
numIn := mType.Type.NumIn() |
| 42 |
if mType.Type.IsVariadic() { |
| 43 |
numIn-- |
| 44 |
} |
| 45 |
args := make([]reflect.Value, numIn) |
| 46 |
ctx := t.Context() |
| 47 |
for i := range args { |
| 48 |
aType := mType.Type.In(i) |
| 49 |
if aType.Name() == "Context" { |
| 50 |
// Do not panic on a nil context. |
| 51 |
args[i] = reflect.ValueOf(ctx) |
| 52 |
} else { |
| 53 |
args[i] = reflect.New(aType).Elem() |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
assert.NotPanicsf(t, func() { |
| 58 |
_ = m.Call(args) |
| 59 |
}, "%s.%s", rVal.Type().Name(), mType.Name) |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
func TestNewTracerProvider(t *testing.T) { |
| 65 |
provider := NewLoggerProvider() |
| 66 |
assert.Equal(t, LoggerProvider{}, provider) |
| 67 |
logger := provider.Logger("") |
| 68 |
assert.Equal(t, Logger{}, logger) |
| 69 |
} |