Search Apps Documentation Source Content File Folder Download Copy Actions Download

options_test.gno

2.86 Kb · 119 lines
  1package definition_test
  2
  3import (
  4	"testing"
  5	"time"
  6
  7	"gno.land/p/nt/commondao/v0"
  8	"gno.land/p/nt/commondao/v0/exts/definition"
  9	"gno.land/p/nt/urequire/v0"
 10)
 11
 12func TestOptions(t *testing.T) {
 13	tests := []struct {
 14		name     string
 15		option   func() definition.Option
 16		panicMsg string
 17	}{
 18		{
 19			name: "custom voting choices",
 20			option: func() definition.Option {
 21				return definition.WithVoteChoices([]commondao.VoteChoice{"A", "B", "C"})
 22			},
 23		},
 24		{
 25			name: "no voting choices",
 26			option: func() definition.Option {
 27				return definition.WithVoteChoices([]commondao.VoteChoice{})
 28			},
 29			panicMsg: "proposal requires at least two voting choices: got 0",
 30		},
 31		{
 32			name: "one voting choice",
 33			option: func() definition.Option {
 34				return definition.WithVoteChoices([]commondao.VoteChoice{"A"})
 35			},
 36			panicMsg: "proposal requires at least two voting choices: got 1",
 37		},
 38		{
 39			name: "duplicated voting choice",
 40			option: func() definition.Option {
 41				return definition.WithVoteChoices([]commondao.VoteChoice{"A", "B", "A"})
 42			},
 43			panicMsg: "proposal voting choice is duplicated: A",
 44		},
 45		{
 46			name: "voting period",
 47			option: func() definition.Option {
 48				return definition.WithVotingPeriod(time.Hour * 24)
 49			},
 50		},
 51		{
 52			name: "zero voting period",
 53			option: func() definition.Option {
 54				return definition.WithVotingPeriod(0)
 55			},
 56		},
 57		{
 58			name: "negative voting period",
 59			option: func() definition.Option {
 60				return definition.WithVotingPeriod(-time.Hour)
 61			},
 62			panicMsg: "voting period of proposal must be greater or equal to zero",
 63		},
 64		{
 65			name: "tally callback",
 66			option: func() definition.Option {
 67				return definition.WithTally(func(commondao.VotingContext) (bool, error) { return true, nil })
 68			},
 69		},
 70		{
 71			name: "nil tally callback",
 72			option: func() definition.Option {
 73				return definition.WithTally(nil)
 74			},
 75			panicMsg: "proposal tally callback is nil",
 76		},
 77		{
 78			name: "validator callback",
 79			option: func() definition.Option {
 80				return definition.WithValidation(func() error { return nil })
 81			},
 82		},
 83		{
 84			name: "nil validator callback",
 85			option: func() definition.Option {
 86				return definition.WithValidation(nil)
 87			},
 88			panicMsg: "proposal validation callback is nil",
 89		},
 90		{
 91			name: "nil executor callback",
 92			option: func() definition.Option {
 93				return definition.WithExecutor(nil)
 94			},
 95			panicMsg: "proposal executor callback is nil",
 96		},
 97	}
 98
 99	for _, tt := range tests {
100		t.Run(tt.name, func(t *testing.T) {
101			// Arrange
102			var option definition.Option
103
104			// Act
105			fn := func() {
106				option = tt.option()
107				_ = definition.MustNew("Title", "Body", option)
108			}
109
110			// Assert
111			if tt.panicMsg != "" {
112				urequire.PanicsWithMessage(t, tt.panicMsg, fn, "expect panic with message")
113			} else {
114				urequire.NotPanics(t, fn, "expect no panic")
115				urequire.True(t, option != nil, "expect a non nil option")
116			}
117		})
118	}
119}