| 1 |
// Copyright 2025 The Hugo Authors. All rights reserved. |
| 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 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package hreflect |
| 15 |
|
| 16 |
import ( |
| 17 |
"context" |
| 18 |
"reflect" |
| 19 |
"testing" |
| 20 |
"time" |
| 21 |
|
| 22 |
qt "github.com/frankban/quicktest" |
| 23 |
"github.com/gohugoio/hugo/htesting/hqt" |
| 24 |
) |
| 25 |
|
| 26 |
type zeroStruct struct { |
| 27 |
zero bool |
| 28 |
} |
| 29 |
|
| 30 |
func (z zeroStruct) IsZero() bool { |
| 31 |
return z.zero |
| 32 |
} |
| 33 |
|
| 34 |
func TestIsTruthful(t *testing.T) { |
| 35 |
c := qt.New(t) |
| 36 |
|
| 37 |
var nilpointerZero *zeroStruct |
| 38 |
|
| 39 |
c.Assert(IsTruthful(true), qt.Equals, true) |
| 40 |
c.Assert(IsTruthful(false), qt.Equals, false) |
| 41 |
c.Assert(IsTruthful(time.Now()), qt.Equals, true) |
| 42 |
c.Assert(IsTruthful(time.Time{}), qt.Equals, false) |
| 43 |
c.Assert(IsTruthful(&zeroStruct{zero: false}), qt.Equals, true) |
| 44 |
c.Assert(IsTruthful(&zeroStruct{zero: true}), qt.Equals, false) |
| 45 |
c.Assert(IsTruthful(zeroStruct{zero: false}), qt.Equals, true) |
| 46 |
c.Assert(IsTruthful(zeroStruct{zero: true}), qt.Equals, false) |
| 47 |
c.Assert(IsTruthful(nil), qt.Equals, false) |
| 48 |
c.Assert(IsTruthful(nilpointerZero), qt.Equals, false) |
| 49 |
} |
| 50 |
|
| 51 |
func TestGetMethodByName(t *testing.T) { |
| 52 |
c := qt.New(t) |
| 53 |
v := reflect.ValueOf(&testStruct{}) |
| 54 |
tp := v.Type() |
| 55 |
|
| 56 |
c.Assert(GetMethodIndexByName(tp, "Method1"), qt.Equals, 0) |
| 57 |
c.Assert(GetMethodIndexByName(tp, "Method3"), qt.Equals, 2) |
| 58 |
c.Assert(GetMethodIndexByName(tp, "Foo"), qt.Equals, -1) |
| 59 |
} |
| 60 |
|
| 61 |
func TestIsContextType(t *testing.T) { |
| 62 |
c := qt.New(t) |
| 63 |
type k string |
| 64 |
ctx := context.Background() |
| 65 |
valueCtx := context.WithValue(ctx, k("key"), 32) |
| 66 |
c.Assert(IsContextType(reflect.TypeOf(ctx)), qt.IsTrue) |
| 67 |
c.Assert(IsContextType(reflect.TypeOf(valueCtx)), qt.IsTrue) |
| 68 |
} |
| 69 |
|
| 70 |
func TestToSliceAny(t *testing.T) { |
| 71 |
c := qt.New(t) |
| 72 |
|
| 73 |
checkOK := func(in any, expected []any) { |
| 74 |
out, ok := ToSliceAny(in) |
| 75 |
c.Assert(ok, qt.Equals, true) |
| 76 |
c.Assert(out, qt.DeepEquals, expected) |
| 77 |
} |
| 78 |
|
| 79 |
checkOK([]any{1, 2, 3}, []any{1, 2, 3}) |
| 80 |
checkOK([]int{1, 2, 3}, []any{1, 2, 3}) |
| 81 |
} |
| 82 |
|
| 83 |
type testIndirectStruct struct { |
| 84 |
S string |
| 85 |
} |
| 86 |
|
| 87 |
func (t *testIndirectStruct) GetS() string { |
| 88 |
return t.S |
| 89 |
} |
| 90 |
|
| 91 |
func (t testIndirectStruct) Foo() string { |
| 92 |
return "bar" |
| 93 |
} |
| 94 |
|
| 95 |
type testIndirectStructNoMethods struct { |
| 96 |
S string |
| 97 |
} |
| 98 |
|
| 99 |
func TestIsNil(t *testing.T) { |
| 100 |
c := qt.New(t) |
| 101 |
|
| 102 |
var ( |
| 103 |
nilPtr *testIndirectStruct |
| 104 |
nilIface any = nil |
| 105 |
nonNilIface any = &testIndirectStruct{S: "hello"} |
| 106 |
) |
| 107 |
|
| 108 |
c.Assert(IsNil(reflect.ValueOf(nilPtr)), qt.Equals, true) |
| 109 |
c.Assert(IsNil(reflect.ValueOf(nilIface)), qt.Equals, true) |
| 110 |
c.Assert(IsNil(reflect.ValueOf(nonNilIface)), qt.Equals, false) |
| 111 |
} |
| 112 |
|
| 113 |
func TestIndirectInterface(t *testing.T) { |
| 114 |
c := qt.New(t) |
| 115 |
|
| 116 |
var ( |
| 117 |
structWithMethods = testIndirectStruct{S: "hello"} |
| 118 |
structWithMethodsPointer = &testIndirectStruct{S: "hello"} |
| 119 |
structWithMethodsPointerAny any = structWithMethodsPointer |
| 120 |
structPointerToPointer = &structWithMethodsPointer |
| 121 |
structNoMethodsPtr = &testIndirectStructNoMethods{S: "no methods"} |
| 122 |
structNoMethods = testIndirectStructNoMethods{S: "no methods"} |
| 123 |
intValue = 32 |
| 124 |
intPtr = &intValue |
| 125 |
nilPtr *testIndirectStruct |
| 126 |
nilIface any = nil |
| 127 |
) |
| 128 |
|
| 129 |
ind := func(v any) any { |
| 130 |
c.Helper() |
| 131 |
vv, isNil := Indirect(reflect.ValueOf(v)) |
| 132 |
c.Assert(isNil, qt.IsFalse) |
| 133 |
return vv.Interface() |
| 134 |
} |
| 135 |
|
| 136 |
c.Assert(ind(intValue), hqt.IsSameType, 32) |
| 137 |
c.Assert(ind(intPtr), hqt.IsSameType, 32) |
| 138 |
c.Assert(ind(structNoMethodsPtr), hqt.IsSameType, structNoMethodsPtr) |
| 139 |
c.Assert(ind(structWithMethods), hqt.IsSameType, structWithMethods) |
| 140 |
c.Assert(ind(structNoMethods), hqt.IsSameType, structNoMethods) |
| 141 |
c.Assert(ind(structPointerToPointer), hqt.IsSameType, &testIndirectStruct{}) |
| 142 |
c.Assert(ind(structWithMethodsPointer), hqt.IsSameType, &testIndirectStruct{}) |
| 143 |
c.Assert(ind(structWithMethodsPointerAny), hqt.IsSameType, structWithMethodsPointer) |
| 144 |
|
| 145 |
vv, isNil := Indirect(reflect.ValueOf(nilPtr)) |
| 146 |
c.Assert(isNil, qt.IsTrue) |
| 147 |
c.Assert(vv, qt.Equals, reflect.ValueOf(nilPtr)) |
| 148 |
|
| 149 |
vv, isNil = Indirect(reflect.ValueOf(nilIface)) |
| 150 |
c.Assert(isNil, qt.IsFalse) |
| 151 |
c.Assert(vv, qt.Equals, reflect.ValueOf(nilIface)) |
| 152 |
} |
| 153 |
|
| 154 |
func BenchmarkIsContextType(b *testing.B) { |
| 155 |
const size = 1000 |
| 156 |
type k string |
| 157 |
b.Run("value", func(b *testing.B) { |
| 158 |
ctx := context.Background() |
| 159 |
ctxs := make([]reflect.Type, size) |
| 160 |
for i := range size { |
| 161 |
ctxs[i] = reflect.TypeOf(context.WithValue(ctx, k("key"), i)) |
| 162 |
} |
| 163 |
|
| 164 |
for i := 0; b.Loop(); i++ { |
| 165 |
idx := i % size |
| 166 |
if !IsContextType(ctxs[idx]) { |
| 167 |
b.Fatal("not context") |
| 168 |
} |
| 169 |
} |
| 170 |
}) |
| 171 |
|
| 172 |
b.Run("background", func(b *testing.B) { |
| 173 |
var ctxt reflect.Type = reflect.TypeOf(context.Background()) |
| 174 |
for b.Loop() { |
| 175 |
if !IsContextType(ctxt) { |
| 176 |
b.Fatal("not context") |
| 177 |
} |
| 178 |
} |
| 179 |
}) |
| 180 |
} |
| 181 |
|
| 182 |
func BenchmarkIsTruthFulValue(b *testing.B) { |
| 183 |
var ( |
| 184 |
stringHugo = reflect.ValueOf("Hugo") |
| 185 |
stringEmpty = reflect.ValueOf("") |
| 186 |
zero = reflect.ValueOf(time.Time{}) |
| 187 |
timeNow = reflect.ValueOf(time.Now()) |
| 188 |
boolTrue = reflect.ValueOf(true) |
| 189 |
boolFalse = reflect.ValueOf(false) |
| 190 |
nilPointer = reflect.ValueOf((*zeroStruct)(nil)) |
| 191 |
) |
| 192 |
|
| 193 |
for b.Loop() { |
| 194 |
IsTruthfulValue(stringHugo) |
| 195 |
IsTruthfulValue(stringEmpty) |
| 196 |
IsTruthfulValue(zero) |
| 197 |
IsTruthfulValue(timeNow) |
| 198 |
IsTruthfulValue(boolTrue) |
| 199 |
IsTruthfulValue(boolFalse) |
| 200 |
IsTruthfulValue(nilPointer) |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
type testStruct struct{} |
| 205 |
|
| 206 |
func (t *testStruct) Method1() string { |
| 207 |
return "Hugo" |
| 208 |
} |
| 209 |
|
| 210 |
func (t *testStruct) Method2() string { |
| 211 |
return "Hugo" |
| 212 |
} |
| 213 |
|
| 214 |
func (t *testStruct) Method3() string { |
| 215 |
return "Hugo" |
| 216 |
} |
| 217 |
|
| 218 |
func (t *testStruct) Method4() string { |
| 219 |
return "Hugo" |
| 220 |
} |
| 221 |
|
| 222 |
func (t *testStruct) Method5() string { |
| 223 |
return "Hugo" |
| 224 |
} |
| 225 |
|
| 226 |
func BenchmarkGetMethodByNameForType(b *testing.B) { |
| 227 |
tp := reflect.TypeFor[*testStruct]() |
| 228 |
methods := []string{"Method1", "Method2", "Method3", "Method4", "Method5"} |
| 229 |
|
| 230 |
for b.Loop() { |
| 231 |
for _, method := range methods { |
| 232 |
_ = GetMethodByNameForType(tp, method) |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
func BenchmarkGetMethodByName(b *testing.B) { |
| 238 |
v := reflect.ValueOf(&testStruct{}) |
| 239 |
methods := []string{"Method1", "Method2", "Method3", "Method4", "Method5"} |
| 240 |
|
| 241 |
for b.Loop() { |
| 242 |
for _, method := range methods { |
| 243 |
_ = GetMethodByName(v, method) |
| 244 |
} |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
func BenchmarkGetMethodByNamePara(b *testing.B) { |
| 249 |
v := reflect.ValueOf(&testStruct{}) |
| 250 |
methods := []string{"Method1", "Method2", "Method3", "Method4", "Method5"} |
| 251 |
|
| 252 |
b.ResetTimer() |
| 253 |
b.RunParallel(func(pb *testing.PB) { |
| 254 |
for pb.Next() { |
| 255 |
for _, method := range methods { |
| 256 |
_ = GetMethodByName(v, method) |
| 257 |
} |
| 258 |
} |
| 259 |
}) |
| 260 |
} |