vspd/webapi/helpers_test.go
Jamie Holdstock e05ced391a Various minor cleanups.
- Updating and adding some copyrights which were missed recently.
- Slight improvements to some commenting and naming.
2021-06-01 07:17:11 +08:00

55 lines
1.6 KiB
Go

// Copyright (c) 2020 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package webapi
import (
"testing"
"github.com/decred/dcrd/chaincfg/v3"
)
func TestIsValidVoteChoices(t *testing.T) {
// Mainnet vote version 4 contains 2 agendas - sdiffalgorithm and lnsupport.
// Both agendas have vote choices yes/no/abstain.
voteVersion := uint32(4)
params := chaincfg.MainNetParams()
var tests = []struct {
voteChoices map[string]string
valid bool
}{
// Empty vote choices are allowed.
{map[string]string{}, true},
// Valid agenda, valid vote choice.
{map[string]string{"lnsupport": "yes"}, true},
{map[string]string{"sdiffalgorithm": "no", "lnsupport": "yes"}, true},
// Invalid agenda.
{map[string]string{"": "yes"}, false},
{map[string]string{"Fake agenda": "yes"}, false},
// Valid agenda, invalid vote choice.
{map[string]string{"lnsupport": "1234"}, false},
{map[string]string{"sdiffalgorithm": ""}, false},
// One valid choice, one invalid choice.
{map[string]string{"sdiffalgorithm": "no", "lnsupport": "1234"}, false},
{map[string]string{"sdiffalgorithm": "1234", "lnsupport": "no"}, false},
// One valid agenda, one invalid agenda.
{map[string]string{"fake": "abstain", "lnsupport": "no"}, false},
{map[string]string{"sdiffalgorithm": "abstain", "": "no"}, false},
}
for _, test := range tests {
err := validConsensusVoteChoices(params, voteVersion, test.voteChoices)
if (err == nil) != test.valid {
t.Fatalf("isValidVoteChoices failed for votechoices '%v'.", test.voteChoices)
}
}
}