Skip to content

Commit 7841149

Browse files
committed
feat: register.AtMostOnce.Temp{Override,Clear}() methods
1 parent 7a59fdd commit 7841149

File tree

2 files changed

+104
-1
lines changed

2 files changed

+104
-1
lines changed

libevm/register/register.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2024 the libevm authors.
1+
// Copyright 2024-2025 the libevm authors.
22
//
33
// The libevm additions to go-ethereum are free software: you can redistribute
44
// them and/or modify them under the terms of the GNU Lesser General Public License
@@ -66,3 +66,28 @@ func (o *AtMostOnce[T]) TestOnlyClear() {
6666
o.v = nil
6767
})
6868
}
69+
70+
// TempOverride calls `fn`, overriding any registered `T`, but only for the life
71+
// of the call.
72+
//
73+
// It is valid to call this method with or without a prior call to
74+
// [AtMostOnce.Register].
75+
func (o *AtMostOnce[T]) TempOverride(with T, fn func()) {
76+
o.temp(&with, fn)
77+
}
78+
79+
// TempClear calls `fn`, clearing any registered `T`, but only for the life of
80+
// the call.
81+
//
82+
// It is valid to call this method with or without a prior call to
83+
// [AtMostOnce.Register].
84+
func (o *AtMostOnce[T]) TempClear(fn func()) {
85+
o.temp(nil, fn)
86+
}
87+
88+
func (o *AtMostOnce[T]) temp(with *T, fn func()) {
89+
old := o.v
90+
o.v = with
91+
fn()
92+
o.v = old
93+
}

libevm/register/register_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright 2025 the libevm authors.
2+
//
3+
// The libevm additions to go-ethereum are free software: you can redistribute
4+
// them and/or modify them under the terms of the GNU Lesser General Public License
5+
// as published by the Free Software Foundation, either version 3 of the License,
6+
// or (at your option) any later version.
7+
//
8+
// The libevm additions are distributed in the hope that they will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
11+
// General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU Lesser General Public License
14+
// along with the go-ethereum library. If not, see
15+
// <http://www.gnu.org/licenses/>.
16+
17+
package register
18+
19+
import (
20+
"testing"
21+
22+
"github.com/stretchr/testify/assert"
23+
"github.com/stretchr/testify/require"
24+
)
25+
26+
func TestAtMostOnce(t *testing.T) {
27+
var sut AtMostOnce[int]
28+
assertRegistered := func(t *testing.T, want int) {
29+
t.Helper()
30+
require.True(t, sut.Registered(), "Registered()")
31+
assert.Equal(t, want, sut.Get(), "Get()")
32+
}
33+
34+
const val int = 42
35+
require.NoError(t, sut.Register(val), "Register()")
36+
assertRegistered(t, val)
37+
38+
assert.PanicsWithValue(
39+
t, ErrReRegistration,
40+
func() { sut.MustRegister(0) },
41+
"MustRegister() after Register()",
42+
)
43+
44+
t.Run("TestOnlyClear", func(t *testing.T) {
45+
sut.TestOnlyClear()
46+
require.False(t, sut.Registered(), "Registered()")
47+
48+
t.Run("re-registration", func(t *testing.T) {
49+
sut.MustRegister(val)
50+
assertRegistered(t, val)
51+
})
52+
})
53+
if t.Failed() {
54+
return
55+
}
56+
57+
t.Run("TempOverride", func(t *testing.T) {
58+
t.Run("during", func(t *testing.T) {
59+
sut.TempOverride(val+1, func() {
60+
assertRegistered(t, val+1)
61+
})
62+
})
63+
t.Run("after", func(t *testing.T) {
64+
assertRegistered(t, val)
65+
})
66+
})
67+
68+
t.Run("TempClear", func(t *testing.T) {
69+
t.Run("during", func(t *testing.T) {
70+
sut.TempClear(func() {
71+
assert.False(t, sut.Registered(), "Registered()")
72+
})
73+
})
74+
t.Run("after", func(t *testing.T) {
75+
assertRegistered(t, val)
76+
})
77+
})
78+
}

0 commit comments

Comments
 (0)