45 lines
1.9 KiB
Go
45 lines
1.9 KiB
Go
package content
|
|
|
|
import (
|
|
"math"
|
|
"testing"
|
|
)
|
|
|
|
func TestCatalogRejectsInvalidStatsAndItemRequirements(t *testing.T) {
|
|
for name, mutate := range map[string]func(*Catalog){
|
|
"unknown stat": func(c *Catalog) { c.Stats.Base["admin"] = 1 },
|
|
"negative primary": func(c *Catalog) { c.Stats.Base["constitution"] = -1 },
|
|
"nonfinite coefficient": func(c *Catalog) { c.Stats.Coefficients["luck"]["criticalChance"] = math.NaN() },
|
|
"unrelated coefficient": func(c *Catalog) {
|
|
delete(c.Stats.Coefficients["charisma"], "buffEffectiveness")
|
|
c.Stats.Coefficients["charisma"]["healthPoints"] = 1
|
|
},
|
|
"unknown modifier": func(c *Catalog) { c.Titles[0].Effects.Modifiers = []StatModifier{{Stat: "dailySeconds", Flat: 100}} },
|
|
"unsafe percentage": func(c *Catalog) { c.Titles[0].Effects.Modifiers = []StatModifier{{Stat: "strength", Percent: -101}} },
|
|
"invalid slot": func(c *Catalog) { c.Items[0].Slot = "ring9" },
|
|
"unknown title": func(c *Catalog) { c.Items[0].Requirements.Titles = []string{"missing"} },
|
|
"duplicate item": func(c *Catalog) { c.Items = append(c.Items, c.Items[0]) },
|
|
"stack collision": func(c *Catalog) { c.Items[0].ID = "wood" },
|
|
"invalid drop chance": func(c *Catalog) { c.Items[0].DropChance = 101 },
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
c, err := DefaultCatalog()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
mutate(&c)
|
|
if err = c.Validate(); err == nil {
|
|
t.Fatal("invalid catalog accepted")
|
|
}
|
|
})
|
|
}
|
|
c, err := DefaultCatalog()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
c.Items = append(c.Items, ItemDefinition{ID: "cursed_ring", Name: "Cursed ring", Slot: "ring", Weight: 1, DropChance: 2, Requirements: ItemRequirements{Level: 2, Stats: map[string]float64{"constitution": 3}, Titles: []string{"wayfarer"}}, Modifiers: []StatModifier{{Stat: "constitution", Flat: 2, Percent: 10}, {Stat: "moveSpeed", Percent: -10}}})
|
|
if err = c.Validate(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|