forked from hyperledger-labs/go-perun
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmock_app_internal_test.go
145 lines (117 loc) · 4.96 KB
/
mock_app_internal_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2025 - See NOTICE file for copyright holders.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package channel
import (
"testing"
"github.com/stretchr/testify/assert"
wiretest "perun.network/go-perun/wire/test"
pkgtest "polycry.pt/poly-go/test"
)
func TestMockApp(t *testing.T) {
rng := pkgtest.Prng(t)
appID := newRandomAppID(rng)
app := NewMockApp(appID)
t.Run("App", func(t *testing.T) {
assert.Equal(t, appID, app.Def())
})
t.Run("StateApp", func(t *testing.T) {
MockStateAppTest(t, *app)
})
t.Run("ActionApp", func(t *testing.T) {
MockActionAppTest(t, *app)
})
t.Run("GenericSerializeable", func(t *testing.T) {
wiretest.GenericMarshalerTest(t, NewMockOp(OpValid))
wiretest.GenericMarshalerTest(t, NewMockOp(OpErr))
wiretest.GenericMarshalerTest(t, NewMockOp(OpTransitionErr))
wiretest.GenericMarshalerTest(t, NewMockOp(OpActionErr))
wiretest.GenericMarshalerTest(t, NewMockOp(OpPanic))
})
// We cant use VerifyClone here since it requires that the same type is returned by
// Clone() but in this case it returns Data instead of *MockOp
t.Run("CloneTest", func(t *testing.T) {
op := NewMockOp(OpValid)
op2 := op.Clone()
// Dont use Equal here since it compares the values and not addresses
assert.False(t, op == op2, "Clone should return a different address")
assert.Equal(t, op, op2, "Clone should return the same value")
})
}
func MockStateAppTest(t *testing.T, app MockApp) {
t.Helper()
stateValid := createState(OpValid)
stateErr := createState(OpErr)
stateTransErr := createState(OpTransitionErr)
stateActErr := createState(OpActionErr)
statePanic := createState(OpPanic)
t.Run("ValidTransition", func(t *testing.T) {
// ValidTransition only checks the first state.
assert.NoError(t, app.ValidTransition(nil, stateValid, nil, 0))
assert.Error(t, app.ValidTransition(nil, stateErr, nil, 0))
assert.True(t, IsStateTransitionError(app.ValidTransition(nil, stateTransErr, nil, 0)))
assert.True(t, IsActionError(app.ValidTransition(nil, stateActErr, nil, 0)))
assert.Panics(t, func() { assert.NoError(t, app.ValidTransition(nil, statePanic, nil, 0)) })
})
t.Run("ValidInit", func(t *testing.T) {
assert.NoError(t, app.ValidInit(nil, stateValid))
assert.Error(t, app.ValidInit(nil, stateErr))
assert.True(t, IsStateTransitionError(app.ValidInit(nil, stateTransErr)))
assert.True(t, IsActionError(app.ValidInit(nil, stateActErr)))
assert.Panics(t, func() { assert.NoError(t, app.ValidInit(nil, statePanic)) })
})
}
func MockActionAppTest(t *testing.T, app MockApp) {
t.Helper()
actValid := NewMockOp(OpValid)
actErr := NewMockOp(OpErr)
actTransErr := NewMockOp(OpTransitionErr)
actActErr := NewMockOp(OpActionErr)
actPanic := NewMockOp(OpPanic)
state := createState(OpValid)
t.Run("InitState", func(t *testing.T) {
_, _, err := app.InitState(nil, []Action{actValid})
// Sadly we can not check Allocation.valid() here, since it is private.
assert.NoError(t, err)
_, _, err = app.InitState(nil, []Action{actErr})
assert.Error(t, err)
_, _, err = app.InitState(nil, []Action{actTransErr})
assert.True(t, IsStateTransitionError(err))
_, _, err = app.InitState(nil, []Action{actActErr})
assert.True(t, IsActionError(err))
assert.Panics(t, func() { app.InitState(nil, []Action{actPanic}) }) //nolint:errcheck
})
t.Run("ValidAction", func(t *testing.T) {
assert.NoError(t, app.ValidAction(nil, nil, 0, actValid))
assert.Error(t, app.ValidAction(nil, nil, 0, actErr))
assert.True(t, IsStateTransitionError(app.ValidAction(nil, nil, 0, actTransErr)))
assert.True(t, IsActionError(app.ValidAction(nil, nil, 0, actActErr)))
assert.Panics(t, func() { app.ValidAction(nil, nil, 0, actPanic) }) //nolint:errcheck
})
t.Run("ApplyActions", func(t *testing.T) {
// ApplyActions increments the Version counter, so we cant pass nil as state.
retState, err := app.ApplyActions(nil, state, []Action{actValid})
assert.Equal(t, retState.Version, state.Version+1)
assert.NoError(t, err)
_, err = app.ApplyActions(nil, state, []Action{actErr})
assert.Error(t, err)
_, err = app.ApplyActions(nil, state, []Action{actTransErr})
assert.True(t, IsStateTransitionError(err))
_, err = app.ApplyActions(nil, state, []Action{actActErr})
assert.True(t, IsActionError(err))
assert.Panics(t, func() { app.ApplyActions(nil, state, []Action{actPanic}) }) //nolint:errcheck
})
}
func createState(op MockOp) *State {
return &State{ID: ID{}, Version: 0, Allocation: Allocation{}, Data: NewMockOp(op), IsFinal: false}
}