|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package logtest |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + |
| 13 | + "go.opentelemetry.io/otel/log" |
| 14 | +) |
| 15 | + |
| 16 | +var y2k = time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC) |
| 17 | + |
| 18 | +type mockTestingT struct { |
| 19 | + errors []string |
| 20 | +} |
| 21 | + |
| 22 | +func (m *mockTestingT) Errorf(format string, args ...any) { |
| 23 | + m.errors = append(m.errors, format) |
| 24 | +} |
| 25 | + |
| 26 | +func TestAssertEqual(t *testing.T) { |
| 27 | + a := Recording{ |
| 28 | + Scope{Name: t.Name()}: []Record{ |
| 29 | + {Body: log.StringValue("msg"), Attributes: []log.KeyValue{log.String("foo", "bar"), log.Int("n", 1)}}, |
| 30 | + }, |
| 31 | + } |
| 32 | + b := Recording{ |
| 33 | + Scope{Name: t.Name()}: []Record{ |
| 34 | + {Body: log.StringValue("msg"), Attributes: []log.KeyValue{log.Int("n", 1), log.String("foo", "bar")}}, |
| 35 | + }, |
| 36 | + } |
| 37 | + |
| 38 | + got := AssertEqual(t, a, b) |
| 39 | + assert.True(t, got, "expected recordings to be equal") |
| 40 | +} |
| 41 | + |
| 42 | +func TestAssertEqualRecording(t *testing.T) { |
| 43 | + tests := []struct { |
| 44 | + name string |
| 45 | + a Recording |
| 46 | + b Recording |
| 47 | + opts []AssertOption |
| 48 | + want bool |
| 49 | + }{ |
| 50 | + { |
| 51 | + name: "equal recordings", |
| 52 | + a: Recording{ |
| 53 | + Scope{Name: t.Name()}: []Record{ |
| 54 | + { |
| 55 | + Timestamp: y2k, |
| 56 | + Context: context.Background(), |
| 57 | + Attributes: []log.KeyValue{log.Int("n", 1), log.String("foo", "bar")}, |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + b: Recording{ |
| 62 | + Scope{Name: t.Name()}: []Record{ |
| 63 | + { |
| 64 | + Timestamp: y2k, |
| 65 | + Context: context.Background(), |
| 66 | + Attributes: []log.KeyValue{log.String("foo", "bar"), log.Int("n", 1)}, |
| 67 | + }, |
| 68 | + }, |
| 69 | + }, |
| 70 | + want: true, |
| 71 | + }, |
| 72 | + { |
| 73 | + name: "different recordings", |
| 74 | + a: Recording{ |
| 75 | + Scope{Name: t.Name()}: []Record{ |
| 76 | + {Attributes: []log.KeyValue{log.String("foo", "bar")}}, |
| 77 | + }, |
| 78 | + }, |
| 79 | + b: Recording{ |
| 80 | + Scope{Name: t.Name()}: []Record{ |
| 81 | + {Attributes: []log.KeyValue{log.Int("n", 1)}}, |
| 82 | + }, |
| 83 | + }, |
| 84 | + want: false, |
| 85 | + }, |
| 86 | + { |
| 87 | + name: "equal empty scopes", |
| 88 | + a: Recording{ |
| 89 | + Scope{Name: t.Name()}: nil, |
| 90 | + }, |
| 91 | + b: Recording{ |
| 92 | + Scope{Name: t.Name()}: []Record{}, |
| 93 | + }, |
| 94 | + want: true, |
| 95 | + }, |
| 96 | + { |
| 97 | + name: "equal empty attributes", |
| 98 | + a: Recording{ |
| 99 | + Scope{Name: t.Name()}: []Record{ |
| 100 | + {Body: log.StringValue("msg"), Attributes: []log.KeyValue{}}, |
| 101 | + }, |
| 102 | + }, |
| 103 | + b: Recording{ |
| 104 | + Scope{Name: t.Name()}: []Record{ |
| 105 | + {Body: log.StringValue("msg"), Attributes: nil}, |
| 106 | + }, |
| 107 | + }, |
| 108 | + want: true, |
| 109 | + }, |
| 110 | + } |
| 111 | + |
| 112 | + for _, tc := range tests { |
| 113 | + t.Run(tc.name, func(t *testing.T) { |
| 114 | + mockT := &mockTestingT{} |
| 115 | + result := assertEqual(mockT, tc.a, tc.b, tc.opts...) |
| 116 | + if result != tc.want { |
| 117 | + t.Errorf("AssertEqual() = %v, want %v", result, tc.want) |
| 118 | + } |
| 119 | + if !tc.want && len(mockT.errors) == 0 { |
| 120 | + t.Errorf("expected Errorf call but got none") |
| 121 | + } |
| 122 | + }) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +func TestAssertEqualRecord(t *testing.T) { |
| 127 | + tests := []struct { |
| 128 | + name string |
| 129 | + a Record |
| 130 | + b Record |
| 131 | + opts []AssertOption |
| 132 | + want bool |
| 133 | + }{ |
| 134 | + { |
| 135 | + name: "equal records", |
| 136 | + a: Record{ |
| 137 | + Timestamp: y2k, |
| 138 | + Context: context.Background(), |
| 139 | + Attributes: []log.KeyValue{log.Int("n", 1), log.String("foo", "bar")}, |
| 140 | + }, |
| 141 | + b: Record{ |
| 142 | + Timestamp: y2k, |
| 143 | + Context: context.Background(), |
| 144 | + Attributes: []log.KeyValue{log.String("foo", "bar"), log.Int("n", 1)}, |
| 145 | + }, |
| 146 | + want: true, |
| 147 | + }, |
| 148 | + { |
| 149 | + name: "different records", |
| 150 | + a: Record{ |
| 151 | + Attributes: []log.KeyValue{log.String("foo", "bar")}, |
| 152 | + }, |
| 153 | + b: Record{ |
| 154 | + Attributes: []log.KeyValue{log.Int("n", 1)}, |
| 155 | + }, |
| 156 | + want: false, |
| 157 | + }, |
| 158 | + } |
| 159 | + |
| 160 | + for _, tc := range tests { |
| 161 | + t.Run(tc.name, func(t *testing.T) { |
| 162 | + mockT := &mockTestingT{} |
| 163 | + result := assertEqual(mockT, tc.a, tc.b, tc.opts...) |
| 164 | + if result != tc.want { |
| 165 | + t.Errorf("AssertEqual() = %v, want %v", result, tc.want) |
| 166 | + } |
| 167 | + if !tc.want && len(mockT.errors) == 0 { |
| 168 | + t.Errorf("expected Errorf call but got none") |
| 169 | + } |
| 170 | + }) |
| 171 | + } |
| 172 | +} |
0 commit comments