-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patherrors_test.go
More file actions
76 lines (65 loc) · 2.2 KB
/
Copy patherrors_test.go
File metadata and controls
76 lines (65 loc) · 2.2 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright (c) 2022, Janoš Guljaš <janos@resenje.org>
// 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[int](len(choices))
_, err := schulze.Vote(preferences, choices, 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 TestValidateChoices_Errors(t *testing.T) {
if err := schulze.ValidateChoices([]string{}); !errors.Is(err, schulze.ErrEmptyChoices) {
t.Fatalf("got error %v, want ErrEmptyChoices", err)
}
err := schulze.ValidateChoices([]string{"A", "B", "A"})
var dupErr *schulze.DuplicateChoiceError[string]
if !errors.As(err, &dupErr) {
t.Fatalf("got error %v, want DuplicateChoiceError", err)
}
if dupErr.Choice != "A" {
t.Fatalf("got choice %v, want A", dupErr.Choice)
}
}
func TestInvalidPreferencesError(t *testing.T) {
choices := []string{"A", "B"}
badPrefs := make([]int, 2) // wanted 4
_, err := schulze.Vote(badPrefs, choices, schulze.Ballot[string]{"A": 1})
var invErr *schulze.InvalidPreferencesError
if !errors.As(err, &invErr) {
t.Fatalf("got error %v, want InvalidPreferencesError", err)
}
if invErr.Length != 2 || invErr.Wanted != 4 {
t.Fatalf("got lengths %d/%d, want 2/4", invErr.Length, invErr.Wanted)
}
}