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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2024 Datadog, Inc.
package gotesting
import (
"sync"
"testing"
"github.com/stretchr/testify/assert"
)
// TestGetFieldPointerFrom tests the getFieldPointerFrom function.
func TestGetFieldPointerFrom(t *testing.T) {
// Create a mock struct with a private field
mockStruct := struct {
privateField string
}{
privateField: "testValue",
}
// Attempt to get a pointer to the private field
ptr, err := getFieldPointerFrom(&mockStruct, "privateField")
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if ptr == nil {
t.Fatal("Expected a valid pointer, got nil")
}
// Dereference the pointer to get the actual value
actualValue := (*string)(ptr)
if *actualValue != mockStruct.privateField {
t.Fatalf("Expected 'testValue', got %s", *actualValue)
}
// Modify the value through the pointer
*actualValue = "modified value"
if *actualValue != mockStruct.privateField {
t.Fatalf("Expected 'modified value', got %s", mockStruct.privateField)
}
// Attempt to get a pointer to a non-existent field
_, err = getFieldPointerFrom(&mockStruct, "nonExistentField")
if err == nil {
t.Fatal("Expected an error for non-existent field, got nil")
}
}
// TestGetInternalTestArray tests the getInternalTestArray function.
func TestGetInternalTestArray(t *testing.T) {
assert := assert.New(t)
// Get the internal test array from the mock testing.M
tests := getInternalTestArray(currentM)
assert.NotNil(tests)
// Check that the test array contains the expected test
var testNames []string
for _, v := range *tests {
testNames = append(testNames, v.Name)
assert.NotNil(v.F)
}
assert.Contains(testNames, "TestGetFieldPointerFrom")
assert.Contains(testNames, "TestGetInternalTestArray")
assert.Contains(testNames, "TestGetInternalBenchmarkArray")
assert.Contains(testNames, "TestCommonPrivateFields_AddLevel")
assert.Contains(testNames, "TestGetBenchmarkPrivateFields")
}
// TestGetInternalBenchmarkArray tests the getInternalBenchmarkArray function.
func TestGetInternalBenchmarkArray(t *testing.T) {
assert := assert.New(t)
// Get the internal benchmark array from the mock testing.M
benchmarks := getInternalBenchmarkArray(currentM)
assert.NotNil(benchmarks)
// Check that the benchmark array contains the expected benchmark
var testNames []string
for _, v := range *benchmarks {
testNames = append(testNames, v.Name)
assert.NotNil(v.F)
}
assert.Contains(testNames, "BenchmarkDummy")
}
// TestCommonPrivateFields_AddLevel tests the AddLevel method of commonPrivateFields.
func TestCommonPrivateFields_AddLevel(t *testing.T) {
// Create a commonPrivateFields struct with a mutex and a level
level := 1
commonFields := &commonPrivateFields{
mu: &sync.RWMutex{},
level: &level,
}
// Add a level and check the new level
newLevel := commonFields.AddLevel(1)
if newLevel != 2 || newLevel != *commonFields.level {
t.Fatalf("Expected level to be 2, got %d", newLevel)
}
// Subtract a level and check the new level
newLevel = commonFields.AddLevel(-1)
if newLevel != 1 || newLevel != *commonFields.level {
t.Fatalf("Expected level to be 1, got %d", newLevel)
}
}
// TestGetBenchmarkPrivateFields tests the getBenchmarkPrivateFields function.
func TestGetBenchmarkPrivateFields(t *testing.T) {
// Create a new testing.B instance
b := &testing.B{}
// Get the private fields of the benchmark
benchFields := getBenchmarkPrivateFields(b)
if benchFields == nil {
t.Fatal("Expected a valid benchmarkPrivateFields, got nil")
}
// Set values to the private fields
*benchFields.name = "BenchmarkTest"
*benchFields.level = 1
*benchFields.benchFunc = func(b *testing.B) {}
*benchFields.result = testing.BenchmarkResult{}
// Check that the private fields have the expected values
if benchFields.level == nil || *benchFields.level != 1 {
t.Fatalf("Expected level to be 1, got %v", *benchFields.level)
}
if benchFields.name == nil || *benchFields.name != b.Name() {
t.Fatalf("Expected name to be 'BenchmarkTest', got %v", *benchFields.name)
}
if benchFields.benchFunc == nil {
t.Fatal("Expected benchFunc to be set, got nil")
}
if benchFields.result == nil {
t.Fatal("Expected result to be set, got nil")
}
}
func BenchmarkDummy(*testing.B) {}
|