giteabot backport / giteabot (push) Canceled after 0s
giteabot / giteabot (push) Canceled after 0s
release-nightly / nightly-binary (push) Canceled after 0s
release-nightly / nightly-container (push) Canceled after 0s
cache-seeder / gobuild (push) Canceled after 0s
cache-seeder / lint (bindata, lint-backend) (push) Canceled after 0s
release-nightly-snapcraft / build-and-publish (push) Canceled after 0s
Includes direct password setup links in registration emails. Assisted-by: Codex:GPT-5
50 lines
1.1 KiB
Go
50 lines
1.1 KiB
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) {
|
|
t.Run("RepeatCall", func(t *testing.T) {
|
|
callCount := 0
|
|
o := OnceValue[int]{Func: func() int {
|
|
callCount++
|
|
return 42
|
|
}}
|
|
assert.Equal(t, 42, o.Value())
|
|
assert.Equal(t, 42, o.Value())
|
|
assert.Equal(t, 1, callCount)
|
|
o.Reset()
|
|
assert.Equal(t, 42, o.Value())
|
|
assert.Equal(t, 2, callCount)
|
|
assert.Equal(t, 42, o.Value())
|
|
assert.Equal(t, 2, callCount)
|
|
})
|
|
|
|
t.Run("Panic", func(t *testing.T) {
|
|
callCount := 0
|
|
doPanic := true
|
|
o := OnceValue[int]{Func: func() int {
|
|
callCount++
|
|
if doPanic {
|
|
panic("some error")
|
|
}
|
|
return 42
|
|
}}
|
|
assert.PanicsWithValue(t, "some error", func() { o.Value() })
|
|
assert.PanicsWithValue(t, "some error", func() { 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)
|
|
})
|
|
}
|