1package govdao
2
3import (
4 "std"
5 "testing"
6
7 "gno.land/p/demo/testutils"
8 "gno.land/p/demo/urequire"
9 pproposal "gno.land/p/gov/proposal"
10)
11
12func TestPackage(t *testing.T) {
13 u1 := testutils.TestAddress("u1")
14 u2 := testutils.TestAddress("u2")
15 u3 := testutils.TestAddress("u3")
16
17 members = append(members, u1)
18 members = append(members, u2)
19 members = append(members, u3)
20
21 nu1 := testutils.TestAddress("random1")
22
23 out := Render("")
24
25 expected := "No proposals found :("
26 urequire.Equal(t, expected, out)
27
28 var called bool
29 ex := pproposal.NewExecutor(func() error {
30 called = true
31 return nil
32 })
33
34 std.TestSetOrigCaller(u1)
35 pid := Propose("dummy proposal", ex)
36
37 // try to vote not being a member
38 std.TestSetOrigCaller(nu1)
39
40 urequire.PanicsWithMessage(t, msgCallerNotAMember, func() {
41 VoteOnProposal(pid, "YES")
42 })
43
44 // try to vote several times
45 std.TestSetOrigCaller(u1)
46 urequire.NotPanics(t, func() {
47 VoteOnProposal(pid, "YES")
48 })
49 urequire.PanicsWithMessage(t, msgAlreadyVoted, func() {
50 VoteOnProposal(pid, "YES")
51 })
52
53 out = Render("0")
54 expected = `# Prop #0
55
56dummy proposal
57
58Status: active
59
60Voting status: YES: 1, NO: 0, percent: 33, members: 3
61
62Author: g1w5c47h6lta047h6lta047h6lta047h6ly5kscr
63
64`
65
66 urequire.Equal(t, expected, out)
67
68 std.TestSetOrigCaller(u2)
69 urequire.PanicsWithMessage(t, msgWrongVotingValue, func() {
70 VoteOnProposal(pid, "INCORRECT")
71 })
72 urequire.NotPanics(t, func() {
73 VoteOnProposal(pid, "NO")
74 })
75
76 out = Render("0")
77 expected = `# Prop #0
78
79dummy proposal
80
81Status: active
82
83Voting status: YES: 1, NO: 1, percent: 33, members: 3
84
85Author: g1w5c47h6lta047h6lta047h6lta047h6ly5kscr
86
87`
88
89 urequire.Equal(t, expected, out)
90
91 std.TestSetOrigCaller(u3)
92 urequire.NotPanics(t, func() {
93 VoteOnProposal(pid, "YES")
94 })
95
96 out = Render("0")
97 expected = `# Prop #0
98
99dummy proposal
100
101Status: accepted
102
103Voting status: YES: 2, NO: 1, percent: 66, members: 3
104
105Author: g1w5c47h6lta047h6lta047h6lta047h6ly5kscr
106
107`
108
109 urequire.Equal(t, expected, out)
110
111 // Add a new member, so non-executed proposals will change the voting status
112 u4 := testutils.TestAddress("u4")
113 members = append(members, u4)
114
115 out = Render("0")
116 expected = `# Prop #0
117
118dummy proposal
119
120Status: active
121
122Voting status: YES: 2, NO: 1, percent: 50, members: 4
123
124Author: g1w5c47h6lta047h6lta047h6lta047h6ly5kscr
125
126`
127
128 urequire.Equal(t, expected, out)
129
130 std.TestSetOrigCaller(u4)
131 urequire.NotPanics(t, func() {
132 VoteOnProposal(pid, "YES")
133 })
134
135 out = Render("0")
136 expected = `# Prop #0
137
138dummy proposal
139
140Status: accepted
141
142Voting status: YES: 3, NO: 1, percent: 75, members: 4
143
144Author: g1w5c47h6lta047h6lta047h6lta047h6ly5kscr
145
146`
147
148 urequire.Equal(t, expected, out)
149
150 ExecuteProposal(pid)
151 urequire.True(t, called)
152
153 out = Render("0")
154 expected = `# Prop #0
155
156dummy proposal
157
158Status: succeeded
159
160Voting status: YES: 3, NO: 1, percent: 75, members: 4
161
162Author: g1w5c47h6lta047h6lta047h6lta047h6ly5kscr
163
164`
165
166 urequire.Equal(t, expected, out)
167
168 // Add a new member and try to vote an already executed proposal
169 u5 := testutils.TestAddress("u5")
170 members = append(members, u5)
171 std.TestSetOrigCaller(u5)
172 urequire.PanicsWithMessage(t, msgPropExecuted, func() {
173 ExecuteProposal(pid)
174 })
175
176 // even if we added a new member the executed proposal is showing correctly the members that voted on it
177 out = Render("0")
178 expected = `# Prop #0
179
180dummy proposal
181
182Status: succeeded
183
184Voting status: YES: 3, NO: 1, percent: 75, members: 4
185
186Author: g1w5c47h6lta047h6lta047h6lta047h6ly5kscr
187
188`
189
190 urequire.Equal(t, expected, out)
191
192}
dao_test.gno
3.42 Kb ยท 192 lines