mirror of
https://github.com/go-gitea/gitea.git
synced 2026-10-28 17:10:27 +01:00
Consolidate all Go tests. Coverage is identical to before. Go tests are around 5% faster while being 7% smaller. No non-test code is touched.
41 lines
911 B
Go
41 lines
911 B
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package util
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestOnceValue(t *testing.T) {
|
|
for _, name := range []string{"RepeatCall", "Panic"} {
|
|
t.Run(name, func(t *testing.T) {
|
|
callCount := 0
|
|
doPanic := name == "Panic"
|
|
o := OnceValue[int]{Func: func() int {
|
|
callCount++
|
|
if doPanic {
|
|
panic("some error")
|
|
}
|
|
return 42
|
|
}}
|
|
if doPanic {
|
|
assert.PanicsWithValue(t, "some error", func() { o.Value() })
|
|
assert.PanicsWithValue(t, "some error", func() { o.Value() })
|
|
} else {
|
|
assert.Equal(t, 42, o.Value())
|
|
assert.Equal(t, 42, o.Value())
|
|
}
|
|
assert.Equal(t, 1, callCount)
|
|
doPanic = false
|
|
o.Reset()
|
|
assert.Equal(t, 42, o.Value())
|
|
assert.Equal(t, 2, callCount)
|
|
assert.Equal(t, 42, o.Value())
|
|
assert.Equal(t, 2, callCount)
|
|
})
|
|
}
|
|
}
|