This repository has been archived by the owner on Dec 5, 2023. It is now read-only.
forked from justinian/dice
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdice_test.go
82 lines (70 loc) · 1.65 KB
/
dice_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
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
77
78
79
80
81
82
package dice_test
import (
"testing"
. "github.com/justinian/dice"
)
func TestRoll(t *testing.T) {
roll := "3d8v3"
res, _, _ := Roll(roll)
if _, ok := res.(VsResult); !ok {
t.Fatalf("%s is not a VsResult", roll)
}
roll = "3d8+2test"
_, _, err := Roll(roll)
if err != nil {
t.Logf("err '%v' properly detected in %s", err, roll)
} else {
t.Fatalf("err not detected in %s", roll)
}
roll = "3b4bl"
_, reason, err := Roll(roll)
if reason == "4bl" {
t.Fatalf("malformed dice format read as reason, %s", roll)
}
if err != nil {
t.Logf("err '%v' properly detected in %s", err, roll)
}
roll = "9d9rv5"
res, _, _ = Roll(roll)
if _, ok := res.(VsResult); !ok {
t.Fatalf("%s is not a VsResult", roll)
}
}
func TestText(t *testing.T) {
roll := "1d20"
why := "death save"
res, reason, _ := Roll(roll + " " + why)
if res.Description() != roll {
t.Fatalf("desc does not match roll: %s", roll)
}
if reason != why {
t.Fatalf("reason does not match reason: %s", reason)
}
roll = "1d20v10"
res, _, _ = Roll(roll)
if res.Description() != roll {
t.Fatalf("desc does not match roll: %s", roll)
}
roll = "1w1b2y"
res, _, _ = Roll(roll)
if res.Description() != roll {
t.Fatalf("desc does not match roll: %s", roll)
}
}
func TestResultInt(t *testing.T) {
roll := "6d1" // aka 6
res, _, _ := Roll(roll)
if res.Int() != 6 {
t.Fatalf("%s does not evaluate to 6", roll)
}
roll = "10d10v1" // 10 successes
res, _, _ = Roll(roll)
if res.Int() != 10 {
t.Fatalf("%s fails to always roll at least 1", roll)
}
roll = "1w" // no success possible
res, _, _ = Roll(roll)
if res.Int() != 0 {
t.Fatalf("%s fails to roll zero successes", roll)
}
}