Skip to content

Commit 45496fa

Browse files
committed
fix tests
1 parent 52e04fd commit 45496fa

File tree

2 files changed

+33
-25
lines changed

2 files changed

+33
-25
lines changed

commands.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,10 @@ func (b *bot) ban(m dggchat.Message, s *dggchat.Session) {
527527
}
528528
}
529529

530+
var errInputFormat = errors.New("invalid input format")
531+
var errInputBounds = errors.New("input out of bounds")
532+
var errResultRangeBounds = errors.New("result range out of bounds")
533+
530534
func computeRoll(input string) (int, error) {
531535

532536
// Define a regular expression to extract dice rolling information
@@ -537,7 +541,7 @@ func computeRoll(input string) (int, error) {
537541
matches := regex.FindStringSubmatch(input)
538542

539543
if matches == nil {
540-
return 0, fmt.Errorf("invalid input format: %s", input)
544+
return 0, fmt.Errorf("%w: %s", errInputFormat, input)
541545
}
542546

543547
// Extract matched values
@@ -553,13 +557,13 @@ func computeRoll(input string) (int, error) {
553557
checkMod := modifier != 0
554558

555559
if numSides <= 0 || numDice <= 0 || numDice > 1000 {
556-
return 0, errors.New("sides, count or modifier too large")
560+
return 0, errInputBounds
557561
}
558562

559563
if math.MaxInt64/numSides < numDice ||
560564
(modifier > 0 && math.MaxInt64-numSides*numDice < modifier) ||
561565
(modifier < 0 && math.MinInt64+numSides*numDice > modifier) {
562-
return 0, errors.New("sides, count or modifier too large")
566+
return 0, errResultRangeBounds
563567
}
564568

565569
// Roll the dice

commands_test.go

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"errors"
45
"fmt"
56
"testing"
67
)
@@ -23,29 +24,32 @@ func TestParseModifiers(t *testing.T) {
2324
}
2425

2526
func TestComputeRoll(t *testing.T) {
26-
testInputs := []string{
27-
"!roll 2d2+100 foo biz baz",
28-
"!roll 2d2 + 100",
29-
"!roll 2d2 +100",
30-
"!roll 2d2+ 100",
31-
"!roll 2d2-100",
32-
"!roll 2d2 - 100",
33-
"!roll 2d2 -100",
34-
"!roll 2d2- 100",
35-
"!roll 2d2- 100 foo biz baz",
36-
"!roll 23904823904823904823490d20 +1",
37-
"!roll 2d20",
38-
"!roll 20",
39-
"!roll 20+10"}
27+
testInputs := []struct {
28+
input string
29+
err error
30+
}{
31+
{input: "!roll 2d2+100 foo biz baz"},
32+
{input: "!roll 2d2 + 100"},
33+
{input: "!roll 2d2 +100"},
34+
{input: "!roll 2d2+ 100"},
35+
{input: "!roll 2d2-100"},
36+
{input: "!roll 2d2 - 100"},
37+
{input: "!roll 2d2 -100"},
38+
{input: "!roll 2d2- 100"},
39+
{input: "!roll 2d2- 100 foo biz baz"},
40+
{input: "!roll 2d20"},
41+
{input: "!roll 20"},
42+
{input: "!roll 20+10"},
43+
{input: "!roll 1d9223372036854775807"},
44+
{input: "!roll 1d9223372036854775807+1", err: errResultRangeBounds},
45+
{input: "!roll 9223372036854775807d1", err: errInputBounds},
46+
}
4047

41-
for _, input := range testInputs {
42-
result, err := computeRoll(input)
43-
errorMessage := fmt.Sprintf("%v", err)
44-
if err != nil {
45-
if errorMessage != "Sides, count or modifier too large" {
46-
t.Error(fmt.Sprintf("Error: %v\n %d", err, result))
47-
}
48+
for _, c := range testInputs {
49+
_, err := computeRoll(c.input)
50+
if !errors.Is(err, c.err) {
51+
fmt.Printf(`"%s": unexpected error %s\n`, c.input, err)
52+
t.FailNow()
4853
}
4954
}
50-
5155
}

0 commit comments

Comments
 (0)