| 
 | 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