62 lines
1.7 KiB
Go
62 lines
1.7 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("不存在的熔炼输入不应匹配")
|
||
}
|
||
}
|