forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add unit tests to
p/gov/proposal
(gnolang#2475)
## Description This PR adds additional unit tests to the `p/gov/proposal` package, and tidies the code a bit. <details><summary>Contributors' checklist...</summary> - [x] Added new tests, or not needed, or not feasible - [x] Provided an example (e.g. screenshot) to aid review or the PR is self-explanatory - [x] Updated the official documentation or not needed - [x] No breaking changes were made, or a `BREAKING CHANGE: xxx` message was included in the description - [x] Added references to related issues and PRs - [ ] Provided any useful hints for running manual tests - [ ] Added new benchmarks to [generated graphs](https://gnoland.github.io/benchmarks), if any. More info [here](https://github.com/gnolang/gno/blob/master/.benchmarks/README.md). </details> --------- Co-authored-by: Manfred Touron <[email protected]>
- Loading branch information
Showing
9 changed files
with
223 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
module gno.land/p/gov/proposal | ||
|
||
require gno.land/p/demo/uassert v0.0.0-latest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
package proposal | ||
|
||
import ( | ||
"errors" | ||
"std" | ||
"testing" | ||
|
||
"gno.land/p/demo/uassert" | ||
) | ||
|
||
func TestExecutor(t *testing.T) { | ||
t.Parallel() | ||
|
||
verifyProposalFailed := func(e Executor) { | ||
uassert.True(t, e.IsDone(), "expected proposal to be done") | ||
uassert.False(t, e.IsSuccessful(), "expected proposal to fail") | ||
|
||
if e.GetStatus() != Failed { | ||
t.Fatal("expected status to be Failed") | ||
} | ||
} | ||
|
||
verifyProposalSucceeded := func(e Executor) { | ||
uassert.True(t, e.IsDone(), "expected proposal to be done") | ||
uassert.True(t, e.IsSuccessful(), "expected proposal to be successful") | ||
|
||
if e.GetStatus() != Succeeded { | ||
t.Fatal("expected status to be Succeeded") | ||
} | ||
} | ||
|
||
t.Run("govdao not caller", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
var ( | ||
called = false | ||
|
||
cb = func() error { | ||
called = true | ||
|
||
return nil | ||
} | ||
) | ||
|
||
// Create the executor | ||
e := NewExecutor(cb) | ||
|
||
if e.GetStatus() != NotExecuted { | ||
t.Fatal("expected status to be NotExecuted") | ||
} | ||
|
||
// Execute as not the /r/gov/dao caller | ||
uassert.PanicsWithMessage(t, errNotGovDAO.Error(), func() { | ||
_ = e.Execute() | ||
}) | ||
|
||
uassert.False(t, called, "expected proposal to not execute") | ||
}) | ||
|
||
t.Run("execution successful", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
var ( | ||
called = false | ||
|
||
cb = func() error { | ||
called = true | ||
|
||
return nil | ||
} | ||
) | ||
|
||
// Create the executor | ||
e := NewExecutor(cb) | ||
|
||
if e.GetStatus() != NotExecuted { | ||
t.Fatal("expected status to be NotExecuted") | ||
} | ||
|
||
// Execute as the /r/gov/dao caller | ||
r := std.NewCodeRealm(daoPkgPath) | ||
std.TestSetRealm(r) | ||
|
||
uassert.NotPanics(t, func() { | ||
err := e.Execute() | ||
|
||
uassert.NoError(t, err) | ||
}) | ||
|
||
uassert.True(t, called, "expected proposal to execute") | ||
|
||
// Make sure the execution params are correct | ||
verifyProposalSucceeded(e) | ||
}) | ||
|
||
t.Run("execution unsuccessful", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
var ( | ||
called = false | ||
expectedErr = errors.New("unexpected") | ||
|
||
cb = func() error { | ||
called = true | ||
|
||
return expectedErr | ||
} | ||
) | ||
|
||
// Create the executor | ||
e := NewExecutor(cb) | ||
|
||
if e.GetStatus() != NotExecuted { | ||
t.Fatal("expected status to be NotExecuted") | ||
} | ||
|
||
// Execute as the /r/gov/dao caller | ||
r := std.NewCodeRealm(daoPkgPath) | ||
std.TestSetRealm(r) | ||
|
||
uassert.NotPanics(t, func() { | ||
err := e.Execute() | ||
|
||
uassert.ErrorIs(t, err, expectedErr) | ||
}) | ||
|
||
uassert.True(t, called, "expected proposal to execute") | ||
|
||
// Make sure the execution params are correct | ||
verifyProposalFailed(e) | ||
}) | ||
|
||
t.Run("proposal already executed", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
var ( | ||
called = false | ||
|
||
cb = func() error { | ||
called = true | ||
|
||
return nil | ||
} | ||
) | ||
|
||
// Create the executor | ||
e := NewExecutor(cb) | ||
|
||
if e.GetStatus() != NotExecuted { | ||
t.Fatal("expected status to be NotExecuted") | ||
} | ||
|
||
// Execute as the /r/gov/dao caller | ||
r := std.NewCodeRealm(daoPkgPath) | ||
std.TestSetRealm(r) | ||
|
||
uassert.NotPanics(t, func() { | ||
uassert.NoError(t, e.Execute()) | ||
}) | ||
|
||
uassert.True(t, called, "expected proposal to execute") | ||
|
||
// Make sure the execution params are correct | ||
verifyProposalSucceeded(e) | ||
|
||
// Attempt to execute the proposal again | ||
uassert.NotPanics(t, func() { | ||
err := e.Execute() | ||
|
||
uassert.ErrorIs(t, err, ErrAlreadyDone) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.