257 lines
4.2 KiB
Go
257 lines
4.2 KiB
Go
package dice
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func TestDieExp_roll(t *testing.T) {
|
|
type fields struct {
|
|
Size int
|
|
Count int
|
|
KeepHigh int
|
|
KeepLow int
|
|
Explode bool
|
|
}
|
|
type results struct {
|
|
min int
|
|
max int
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
fields fields
|
|
want results
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "simple roll",
|
|
fields: fields{
|
|
Size: 6,
|
|
Count: 1,
|
|
KeepHigh: 0,
|
|
KeepLow: 0,
|
|
Explode: false,
|
|
},
|
|
want: results{
|
|
min: 1,
|
|
max: 6,
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "explode d1",
|
|
fields: fields{
|
|
Size: 1,
|
|
Count: 1,
|
|
KeepHigh: 0,
|
|
KeepLow: 0,
|
|
Explode: true,
|
|
},
|
|
want: results{
|
|
min: 1,
|
|
max: 1,
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "larger roll",
|
|
fields: fields{
|
|
Size: 1000,
|
|
Count: 5,
|
|
KeepHigh: 0,
|
|
KeepLow: 0,
|
|
Explode: false,
|
|
},
|
|
want: results{
|
|
min: 5,
|
|
max: 5000,
|
|
},
|
|
wantErr: false,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
d := DieExp{
|
|
Size: tt.fields.Size,
|
|
Count: tt.fields.Count,
|
|
KeepHigh: tt.fields.KeepHigh,
|
|
KeepLow: tt.fields.KeepLow,
|
|
Explode: tt.fields.Explode,
|
|
}
|
|
got, err := d.Roll()
|
|
total := 0
|
|
for _, v := range got {
|
|
total += v.Result
|
|
}
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("roll() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
if ((total < tt.want.min) || (total > tt.want.max)) && (!tt.wantErr) {
|
|
t.Errorf("roll() got = %v, want between %v and %v", total, tt.want.min, tt.want.max)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestDieExp_roll_keep_high(t *testing.T) {
|
|
d := DieExp{
|
|
Size: 20,
|
|
Count: 10,
|
|
KeepHigh: 2,
|
|
KeepLow: 0,
|
|
Explode: false,
|
|
}
|
|
got, err := d.Roll()
|
|
if err != nil {
|
|
t.Errorf("roll() error = %v", err)
|
|
return
|
|
}
|
|
if len(got) != 2 {
|
|
t.Errorf("roll() got = %v, want length 2", got)
|
|
}
|
|
}
|
|
|
|
func TestDieExp_roll_keep_low(t *testing.T) {
|
|
d := DieExp{
|
|
Size: 20,
|
|
Count: 10,
|
|
KeepHigh: 0,
|
|
KeepLow: 2,
|
|
Explode: false,
|
|
}
|
|
got, err := d.Roll()
|
|
if err != nil {
|
|
t.Errorf("roll() error = %v", err)
|
|
return
|
|
}
|
|
if len(got) != 2 {
|
|
t.Errorf("roll() got = %v, want length 2", got)
|
|
}
|
|
}
|
|
|
|
func TestDieExp_roll_keep_high_low(t *testing.T) {
|
|
d := DieExp{
|
|
Size: 20,
|
|
Count: 10,
|
|
KeepHigh: 5,
|
|
KeepLow: 2,
|
|
Explode: false,
|
|
}
|
|
got, err := d.Roll()
|
|
if err != nil {
|
|
t.Errorf("roll() error = %v", err)
|
|
return
|
|
}
|
|
if len(got) != 2 {
|
|
t.Errorf("roll() got = %v, want length 2", got)
|
|
}
|
|
}
|
|
|
|
func TestDieExp_roll_keep_low_too_high(t *testing.T) {
|
|
d := DieExp{
|
|
Size: 20,
|
|
Count: 2,
|
|
KeepHigh: 0,
|
|
KeepLow: 5,
|
|
Explode: false,
|
|
}
|
|
_, err := d.Roll()
|
|
if err == nil {
|
|
t.Errorf("expected roll error, got nil")
|
|
}
|
|
}
|
|
|
|
func TestCreateFromExp(t *testing.T) {
|
|
type args struct {
|
|
exp string
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
want DieExp
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "test 1",
|
|
args: args{
|
|
exp: "1d20",
|
|
},
|
|
want: DieExp{
|
|
Size: 20,
|
|
Count: 1,
|
|
KeepHigh: 0,
|
|
KeepLow: 0,
|
|
Explode: false,
|
|
Add: 0,
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "test 2",
|
|
args: args{
|
|
exp: "d20",
|
|
},
|
|
want: DieExp{
|
|
Size: 20,
|
|
Count: 1,
|
|
KeepHigh: 0,
|
|
KeepLow: 0,
|
|
Explode: false,
|
|
Add: 0,
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "test 3",
|
|
args: args{
|
|
exp: "3d20h2l1e+1",
|
|
},
|
|
want: DieExp{
|
|
Size: 20,
|
|
Count: 3,
|
|
KeepHigh: 2,
|
|
KeepLow: 1,
|
|
Explode: true,
|
|
Add: 1,
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "test 4",
|
|
args: args{
|
|
exp: "3d20h2l1e+1t",
|
|
},
|
|
want: DieExp{},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "test 5",
|
|
args: args{
|
|
exp: "3d20h2l1e-20",
|
|
},
|
|
want: DieExp{
|
|
Size: 20,
|
|
Count: 3,
|
|
KeepHigh: 2,
|
|
KeepLow: 1,
|
|
Explode: true,
|
|
Add: -20,
|
|
},
|
|
wantErr: false,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := CreateFromExp(tt.args.exp)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("CreateFromExp() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
t.Errorf("CreateFromExp() got = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|