Files
XericUIActionVessel/Tests~/RecipeProbe/Program.cs
T
lrss3 a019183609 feat(反射生成 UI): 字段配方落在宿主侧——预制体放零件,配方决定拼哪几块
新增 Recipes/(纯逻辑 + 一个预制体组件):
- FieldRecipeSet / FieldPlan:类型→配方的匹配与合并,逐字段 override 只覆盖写了的项。
  两条规矩:最具体的赢(写命名空间只认该类型,裸名按尾段匹配);没匹配到就说"没有配方",
  绝不回退默认配方。
- FieldLayout:给定计划 + 预制体真实有的槽位/输入元素,算出开哪些关哪些;
  返回值只答"能不能装",缺装饰件照样装但必须进 problems(注释写死这条契约)。
- FieldPartsAssembler:预制体上的槽位(label/required-mark/note/icon/legend/input/error,
  input 之下放各输入元素),Collect() 一次按名字收齐,Apply() 只改激活态。
- FieldRecipeTable:配方表资产。菜单路径集中成 HostMenuRoots(宿主不能反向引用蓝图包的路径表)。

XuiavrgInspectorManager 接入:预制体挑好挂上父级后 ApplyFieldRecipe(entry, instance)。
没指定配方表 → 说一句并按预制体原样;预制体没有槽位组件 → 原样;
匹配失败或装配有问题 → 逐条 LogWarning,点名 Entry、来源配方、缺的是什么。

新增 Tests~/RecipeProbe(dotnet 编两份纯逻辑文件):VERDICT RECIPE-PROBE PASS (19/19)。
判据(主工程根):bash check_flowgraph_extensions.sh
  -> host Lrss3.Xericuiactionvessel sources=209 errors=0 / VERDICT FGEXT-BUILD PASS
2026-11-05 14:10:11 +08:00

163 lines
9.0 KiB
C#

// 字段配方表的匹配/合并门:纯逻辑,不启 Unity,直接编包里的 Runtime/Form/FieldRecipeSet.cs。
// 判的是"最具体的赢 / 没配方就说不匹配 / 逐字段例外只覆盖写了的项",不是画面。
using System;
using System.Collections.Generic;
using XericUI.ReflectionUIGenerator;
internal static class Program
{
static int failures;
static int checks;
static void Check(bool ok, string what)
{
checks++;
Console.WriteLine((ok ? " PASS " : " FAIL ") + what);
if (!ok) failures++;
}
static FieldRecipe Recipe(string type, params RecipePart[] parts)
{
var r = new FieldRecipe { typeName = type };
r.parts.Clear();
r.parts.AddRange(parts);
return r;
}
static int Main()
{
Console.WriteLine("== field recipe probe ==");
var set = new FieldRecipeSet();
set.recipes.Add(Recipe("UnityEngine.Vector2", RecipePart.Label, RecipePart.Input));
set.recipes.Add(Recipe("Single", RecipePart.Label, RecipePart.Input, RecipePart.Note, RecipePart.Legend));
var single = set.recipes[1];
single.inputElement = "slider";
single.noteWhen = "onHover";
var overrideMax = new FieldOverride { fieldName = "maxSize", hasInputOverride = true, inputOverride = "input-field-numeric" };
single.overrides.Add(overrideMax);
single.overrides.Add(new FieldOverride
{
fieldName = "enabled", hasRequired = true, required = true, requiredMessage = "必须选",
addParts = true, added = new List<RecipePart> { RecipePart.RequiredMark },
removeParts = true, removed = new List<RecipePart> { RecipePart.Legend }
});
FieldPlan plan;
string why;
Check(set.TryPlan("UnityEngine.Vector2", "size", out plan, out why)
&& plan.parts.Count == 2 && plan.inputElement == "input-field",
"exact type name matches and keeps the recipe's parts in order");
set.recipes.Add(Recipe("Color", RecipePart.Label, RecipePart.Input));
Check(set.TryPlan("UnityEngine.Color", "tint", out plan, out why) && plan.source == "recipe:Color",
"a bare recipe name matches the tail of any namespace's type");
Check(!set.TryPlan("My.Ns.Vector2", "size", out plan, out why) && why.Contains("no recipe"),
"a namespaced recipe does NOT match a different type with the same tail");
var specificity = new FieldRecipeSet();
specificity.recipes.Add(Recipe("Vector2", RecipePart.Label, RecipePart.Input));
specificity.recipes.Add(Recipe("UnityEngine.Vector2", RecipePart.Label, RecipePart.Note, RecipePart.Input));
Check(specificity.TryPlan("UnityEngine.Vector2", "v", out plan, out why)
&& plan.source == "recipe:UnityEngine.Vector2" && plan.parts.Contains(RecipePart.Note),
"the most specific recipe wins (full name beats bare name)");
Check(!set.TryPlan("UnityEngine.Quaternion", "rot", out plan, out why) && why.Contains("no recipe"),
"no recipe is reported as missing, not resolved to a guess: " + why);
var ambiguous = new FieldRecipeSet();
ambiguous.recipes.Add(Recipe("Vector2", RecipePart.Label, RecipePart.Input));
ambiguous.recipes.Add(Recipe("Vector2", RecipePart.Label, RecipePart.Note, RecipePart.Input));
Check(!ambiguous.TryPlan("Baz.Vector2", "v", out plan, out why) && why.Contains("ambiguous"),
"two equally specific recipes are refused with both names: " + why);
Check(set.TryPlan("Single", "volume", out plan, out why)
&& plan.inputElement == "slider" && plan.noteWhen == "onHover"
&& plan.parts.Contains(RecipePart.Legend) && !plan.parts.Contains(RecipePart.RequiredMark)
&& plan.source == "recipe:Single",
"without an override the recipe decides everything");
Check(set.TryPlan("Single", "maxSize", out plan, out why)
&& plan.inputElement == "input-field-numeric" && plan.noteWhen == "onHover"
&& plan.source.EndsWith("+ override:maxSize"),
"an override replaces only what it writes");
Check(set.TryPlan("Single", "enabled", out plan, out why)
&& plan.required && plan.requiredMessage == "必须选"
&& plan.parts.Contains(RecipePart.RequiredMark) && !plan.parts.Contains(RecipePart.Legend),
"an override can add and remove parts (required mark in, legend out)");
var before = plan.parts.Count;
Check(set.TryPlan("Single", "enabled", out plan, out why) && plan.parts.Count == before
&& plan.parts.FindAll(p => p == RecipePart.RequiredMark).Count == 1,
"re-planning the same field never duplicates a part (idempotent)");
var broken = new FieldRecipeSet();
broken.recipes.Add(Recipe("", RecipePart.Label, RecipePart.Input));
broken.recipes.Add(Recipe("Single"));
broken.recipes.Add(Recipe("Single", RecipePart.Label, RecipePart.Input));
var bad = broken.Validate();
Check(bad.Count == 3 && bad[0].Contains("empty typeName") && bad[1].Contains("no parts") && bad[2].Contains("duplicate"),
"Validate names every table defect (empty type / no parts / duplicate): " + string.Join(" | ", bad.ToArray()));
var keyless = new FieldRecipeSet();
var withKeyless = Recipe("Single", RecipePart.Label, RecipePart.Input);
withKeyless.overrides.Add(new FieldOverride { fieldName = "" });
keyless.recipes.Add(withKeyless);
Check(keyless.Validate().Count == 1 && keyless.Validate()[0].Contains("no fieldName"),
"an override without a fieldName is a table error, not a silent no-op");
// ---- 装配决策:槽位/元素对不上一句都不能少说 ----
var simple = new FieldPlan { source = "recipe:Single" };
simple.parts.Add(RecipePart.Label);
simple.parts.Add(RecipePart.Input);
simple.inputElement = "input-field";
List<string> on;
List<string> problems;
Check(FieldLayout.Resolve(simple, new[] { "label", "input" }, new[] { "input-field", "slider" }, out on, out problems)
&& on.Contains("label") && on.Contains("input") && on.Contains("input/input-field") && problems.Count == 0,
"a well-formed plan activates exactly its slots plus the chosen element");
Check(!FieldLayout.Resolve(simple, new[] { "label" }, new[] { "input-field" }, out on, out problems)
&& problems[0].Contains("no such slot"),
"a part the prefab has no slot for is a problem, not a silently dropped widget: " + problems[0]);
var wrongElement = new FieldPlan { source = "recipe:Single" };
wrongElement.parts.Add(RecipePart.Input);
wrongElement.inputElement = "enum-dropdown";
Check(!FieldLayout.Resolve(wrongElement, new[] { "input" }, new[] { "input-field", "slider" }, out on, out problems)
&& problems[0].Contains("enum-dropdown") && problems[0].Contains("slider"),
"an unknown input element names itself AND lists what the prefab really has: " + problems[0]);
var needsMark = new FieldPlan { source = "recipe:Single", required = true, requiredMessage = "必须填" };
needsMark.parts.Add(RecipePart.Label);
var markOk = FieldLayout.Resolve(needsMark, new[] { "label" }, null, out on, out problems);
Check(markOk && problems[0].Contains("required-mark") && on.Contains("label"),
"a required field with no place to say so still assembles, but reports it (ok=" + markOk + "): " + problems[0]);
var withMark = new FieldPlan { source = "recipe:Single", required = true };
withMark.parts.Add(RecipePart.Label);
Check(FieldLayout.Resolve(withMark, new[] { "label", "required-mark", "error" }, null, out on, out problems)
&& on.Contains("required-mark") && on.Contains("error"),
"required-mark joins the active list on its own, and the error slot is always ready");
Check(!FieldLayout.Resolve(withMark, new string[0], null, out on, out problems)
&& problems[0].Contains("no slots at all"),
"a prefab exposing nothing is called out (that is a broken prefab, not an empty layout)");
var elementWithoutInput = new FieldPlan { source = "recipe:Single", inputElement = "slider" };
elementWithoutInput.parts.Add(RecipePart.Label);
Check(!FieldLayout.Resolve(elementWithoutInput, new[] { "label" }, new[] { "slider" }, out on, out problems)
&& problems[0].Contains("no Input part"),
"naming an input element without planning the Input slot is refused");
Console.WriteLine();
if (failures == 0)
{
Console.WriteLine("VERDICT RECIPE-PROBE PASS (" + checks + "/" + checks + ")");
return 0;
}
Console.WriteLine("VERDICT RECIPE-PROBE FAIL (" + failures + "/" + checks + " failed)");
return 1;
}
}