File: error_test.go.tmpl

package info (click to toggle)
golang-opentelemetry-otel 1.31.0-5
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid
  • size: 11,844 kB
  • sloc: makefile: 237; sh: 51
file content (80 lines) | stat: -rw-r--r-- 2,099 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Code created by gotmpl. DO NOT MODIFY.
// source: internal/shared/otlp/otlpmetric/transform/error_test.go.tmpl

// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package transform

import (
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

var (
	e0 = errMetric{m: pbMetrics[0], err: errUnknownAggregation}
	e1 = errMetric{m: pbMetrics[1], err: errUnknownTemporality}
)

type testingErr struct{}

func (testingErr) Error() string { return "testing error" }

// errFunc is a non-comparable error type.
type errFunc func() string

func (e errFunc) Error() string {
	return e()
}

func TestMultiErr(t *testing.T) {
	const name = "TestMultiErr"
	me := &multiErr{datatype: name}

	t.Run("ErrOrNil", func(t *testing.T) {
		require.NoError(t, me.errOrNil())
		me.errs = []error{e0}
		assert.Error(t, me.errOrNil())
	})

	var testErr testingErr
	t.Run("AppendError", func(t *testing.T) {
		me.append(testErr)
		assert.Equal(t, testErr, me.errs[len(me.errs)-1])
	})

	t.Run("AppendFlattens", func(t *testing.T) {
		other := &multiErr{datatype: "OtherTestMultiErr", errs: []error{e1}}
		me.append(other)
		assert.Equal(t, e1, me.errs[len(me.errs)-1])
	})

	t.Run("ErrorMessage", func(t *testing.T) {
		// Test the overall structure of the message, but not the exact
		// language so this doesn't become a change-indicator.
		msg := me.Error()
		lines := strings.Split(msg, "\n")
		assert.Lenf(t, lines, 4, "expected a 4 line error message, got:\n\n%s", msg)
		assert.Contains(t, msg, name)
		assert.Contains(t, msg, e0.Error())
		assert.Contains(t, msg, testErr.Error())
		assert.Contains(t, msg, e1.Error())
	})

	t.Run("ErrorIs", func(t *testing.T) {
		assert.ErrorIs(t, me, errUnknownAggregation)
		assert.ErrorIs(t, me, e0)
		assert.ErrorIs(t, me, testErr)
		assert.ErrorIs(t, me, errUnknownTemporality)
		assert.ErrorIs(t, me, e1)

		errUnknown := errFunc(func() string { return "unknown error" })
		assert.NotErrorIs(t, me, errUnknown)

		var empty multiErr
		assert.NotErrorIs(t, &empty, errUnknownTemporality)
	})
}