-
Notifications
You must be signed in to change notification settings - Fork 2
/
errors_test.go
47 lines (40 loc) · 1.27 KB
/
errors_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
// Copyright (c) 2022, Janoš Guljaš <[email protected]>
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package schulze_test
import (
"errors"
"strings"
"testing"
"resenje.org/schulze"
)
func TestVoting_Vote_UnknownChoiceError(t *testing.T) {
v := schulze.NewVoting([]int{0, 2, 5, 7})
_, err := v.Vote(schulze.Ballot[int]{20: 1})
var verr *schulze.UnknownChoiceError[int]
if !errors.As(err, &verr) {
t.Fatalf("got error %v, want UnknownChoiceError", err)
}
if verr.Choice != 20 {
t.Fatalf("got unknown choice error choice %v, want %v", verr.Choice, 20)
}
if !strings.Contains(verr.Error(), "20") {
t.Fatal("choice index not found in error string")
}
}
func TestVote_UnknownChoiceError(t *testing.T) {
choices := []int{0, 2, 5, 7}
preferences := schulze.NewPreferences(len(choices))
_, err := schulze.Vote(choices, preferences, schulze.Ballot[int]{20: 1})
var verr *schulze.UnknownChoiceError[int]
if !errors.As(err, &verr) {
t.Fatalf("got error %v, want UnknownChoiceError", err)
}
if verr.Choice != 20 {
t.Fatalf("got unknown choice error choice %v, want %v", verr.Choice, 20)
}
if !strings.Contains(verr.Error(), "20") {
t.Fatal("choice index not found in error string")
}
}