Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
slugalisk committed Jan 8, 2024
1 parent 52e04fd commit 45496fa
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 25 deletions.
10 changes: 7 additions & 3 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,10 @@ func (b *bot) ban(m dggchat.Message, s *dggchat.Session) {
}
}

var errInputFormat = errors.New("invalid input format")
var errInputBounds = errors.New("input out of bounds")
var errResultRangeBounds = errors.New("result range out of bounds")

func computeRoll(input string) (int, error) {

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

if matches == nil {
return 0, fmt.Errorf("invalid input format: %s", input)
return 0, fmt.Errorf("%w: %s", errInputFormat, input)
}

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

if numSides <= 0 || numDice <= 0 || numDice > 1000 {
return 0, errors.New("sides, count or modifier too large")
return 0, errInputBounds
}

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

// Roll the dice
Expand Down
48 changes: 26 additions & 22 deletions commands_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"fmt"
"testing"
)
Expand All @@ -23,29 +24,32 @@ func TestParseModifiers(t *testing.T) {
}

func TestComputeRoll(t *testing.T) {
testInputs := []string{
"!roll 2d2+100 foo biz baz",
"!roll 2d2 + 100",
"!roll 2d2 +100",
"!roll 2d2+ 100",
"!roll 2d2-100",
"!roll 2d2 - 100",
"!roll 2d2 -100",
"!roll 2d2- 100",
"!roll 2d2- 100 foo biz baz",
"!roll 23904823904823904823490d20 +1",
"!roll 2d20",
"!roll 20",
"!roll 20+10"}
testInputs := []struct {
input string
err error
}{
{input: "!roll 2d2+100 foo biz baz"},
{input: "!roll 2d2 + 100"},
{input: "!roll 2d2 +100"},
{input: "!roll 2d2+ 100"},
{input: "!roll 2d2-100"},
{input: "!roll 2d2 - 100"},
{input: "!roll 2d2 -100"},
{input: "!roll 2d2- 100"},
{input: "!roll 2d2- 100 foo biz baz"},
{input: "!roll 2d20"},
{input: "!roll 20"},
{input: "!roll 20+10"},
{input: "!roll 1d9223372036854775807"},
{input: "!roll 1d9223372036854775807+1", err: errResultRangeBounds},
{input: "!roll 9223372036854775807d1", err: errInputBounds},
}

for _, input := range testInputs {
result, err := computeRoll(input)
errorMessage := fmt.Sprintf("%v", err)
if err != nil {
if errorMessage != "Sides, count or modifier too large" {
t.Error(fmt.Sprintf("Error: %v\n %d", err, result))
}
for _, c := range testInputs {
_, err := computeRoll(c.input)
if !errors.Is(err, c.err) {
fmt.Printf(`"%s": unexpected error %s\n`, c.input, err)
t.FailNow()
}
}

}

0 comments on commit 45496fa

Please sign in to comment.