Files
ngzz-mc/internal/entity/breeding_test.go

67 lines
1.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package entity
import (
"path/filepath"
"testing"
)
// TestBreed 动物繁殖:成年+冷却完成 → 幼崽;冷却中拒绝;上限拒绝(动物体系.md §3)。
func TestBreed(t *testing.T) {
m := NewManager()
w := flatWorld{}
a := m.Spawn(KindAnimal, 0.5, 63, 0.5)
a.Data = &Animal{}
b := m.Spawn(KindAnimal, 1.5, 63, 0.5)
b.Data = &Animal{}
baby := m.Breed(a, b, w)
if baby == nil {
t.Fatal("应繁殖出幼崽")
}
if d, ok := baby.Data.(*Animal); !ok || d.GrowthTicks == 0 {
t.Fatalf("幼崽数据异常: %+v", baby.Data)
}
// 冷却中:拒绝
if m.Breed(a, b, w) != nil {
t.Fatal("冷却中不应繁殖")
}
// 冷却恢复后可再次繁殖
ad := a.Data.(*Animal)
ad.BreedCooldown = 0
bd := b.Data.(*Animal)
bd.BreedCooldown = 0
if m.Breed(a, b, w) == nil {
t.Fatal("冷却结束后应能繁殖")
}
}
// TestBreedCap 每区块上限(动物体系.md §7 防无限繁殖)。
func TestBreedCap(t *testing.T) {
m := NewManager()
w := flatWorld{}
a := m.Spawn(KindAnimal, 0.5, 63, 0.5)
a.Data = &Animal{}
b := m.Spawn(KindAnimal, 1.5, 63, 0.5)
b.Data = &Animal{}
for i := 0; i < 12; i++ {
m.Spawn(KindAnimal, 2.5, 63, 2.5+float64(i))
}
if m.Breed(a, b, w) != nil {
t.Fatal("超上限不应繁殖")
}
}
// TestLoadTrades 交易表解析与职业查询(村民系统.md §7)。
func TestLoadTrades(t *testing.T) {
tbl, err := LoadTrades(filepath.Join("..", "..", "assets", "config", "villager_trades.json"))
if err != nil {
t.Fatalf("加载交易表失败: %v", err)
}
if offers := tbl.OffersFor(0); len(offers) != 2 {
t.Fatalf("农民交易期望 2 条,实际 %d", len(offers))
}
if offers := tbl.OffersFor(2); len(offers) != 2 || offers[0].Buy != "coal" {
t.Fatalf("铁匠交易异常: %+v", offers)
}
}