90 lines
2.2 KiB
Go
90 lines
2.2 KiB
Go
package recipe
|
||
|
||
import (
|
||
"path/filepath"
|
||
"testing"
|
||
)
|
||
|
||
// loadRecipes 加载配方注册表。
|
||
func loadRecipes(t *testing.T) *Registry {
|
||
t.Helper()
|
||
r, err := Load(filepath.Join("..", "..", "assets", "config", "recipes.json"))
|
||
if err != nil {
|
||
t.Fatalf("加载配方失败: %v", err)
|
||
}
|
||
return r
|
||
}
|
||
|
||
// TestMatchShaped 有序配方:火把 = 煤+木棍(工作台与合成表.md §4.1)。
|
||
// 注意:网格内容为物品名(不是 pattern 符号)。
|
||
func TestMatchShaped(t *testing.T) {
|
||
r := loadRecipes(t)
|
||
var g [3][3]string
|
||
g[0][0] = "coal"
|
||
g[1][0] = "stick"
|
||
rc, ok := r.MatchShaped(g)
|
||
if !ok || rc.Result.Item != "torch" {
|
||
t.Fatalf("火把配方匹配失败: %+v", rc)
|
||
}
|
||
// 图案外空格容错:整体右移一列也能匹配(归一化)
|
||
var g2 [3][3]string
|
||
g2[0][1] = "coal"
|
||
g2[1][1] = "stick"
|
||
if _, ok := r.MatchShaped(g2); !ok {
|
||
t.Fatal("归一化后应仍匹配火把配方")
|
||
}
|
||
}
|
||
|
||
// TestMatchShapeless 无序配方:木板(多重集比对,§4.2)。
|
||
func TestMatchShapeless(t *testing.T) {
|
||
r := loadRecipes(t)
|
||
rc, ok := r.MatchShapeless(map[string]int{"oak_log": 1})
|
||
if !ok || rc.Result.Item != "oak_planks" {
|
||
t.Fatalf("木板配方匹配失败: %+v", rc)
|
||
}
|
||
// 数量不符不匹配
|
||
if _, ok := r.MatchShapeless(map[string]int{"oak_log": 2}); ok {
|
||
t.Fatal("2 个原木不应匹配木板配方")
|
||
}
|
||
}
|
||
|
||
// TestMatchSmelting 熔炼配方:铁矿石 → 铁锭(§4.3)。
|
||
func TestMatchSmelting(t *testing.T) {
|
||
r := loadRecipes(t)
|
||
rc, ok := r.MatchSmelting("iron_ore")
|
||
if !ok || rc.Result.Item != "iron_ingot" {
|
||
t.Fatalf("熔炼配方匹配失败: %+v", rc)
|
||
}
|
||
if _, ok := r.MatchSmelting("not_exist"); ok {
|
||
t.Fatal("不存在的熔炼输入不应匹配")
|
||
}
|
||
}
|
||
|
||
func TestPlaceCellsShaped(t *testing.T) {
|
||
r := loadRecipes(t)
|
||
var table *Recipe
|
||
for _, rc := range r.CraftingRecipes() {
|
||
if rc.Result.Item == "crafting_table" {
|
||
table = rc
|
||
break
|
||
}
|
||
}
|
||
if table == nil {
|
||
t.Fatal("缺少工作台配方")
|
||
}
|
||
need := NeedCounts(table)
|
||
if need["oak_planks"] != 4 {
|
||
t.Fatalf("工作台应需 4 木板,实际 %v", need)
|
||
}
|
||
cells := PlaceCells(table)
|
||
n := 0
|
||
for _, c := range cells {
|
||
if c == "oak_planks" {
|
||
n++
|
||
}
|
||
}
|
||
if n != 4 {
|
||
t.Fatalf("填格应有 4 木板,实际 %v", cells)
|
||
}
|
||
}
|