-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Fix misleading 'Property not found' error and add SerializedProperty fallback (#765) #766
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
7a0d507
2f5963a
aace073
bad1057
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| using System.Text.RegularExpressions; | ||
| using NUnit.Framework; | ||
| using UnityEngine; | ||
| using UnityEngine.TestTools; | ||
| using Newtonsoft.Json.Linq; | ||
| using MCPForUnity.Editor.Helpers; | ||
| using MCPForUnity.Editor.Tools; | ||
| using MCPForUnity.Editor.Tools.GameObjects; | ||
|
|
||
| namespace MCPForUnityTests.Editor.Tools | ||
| { | ||
| /// <summary> | ||
| /// Tests for GameObjectComponentHelpers.SetComponentPropertiesInternal error reporting. | ||
| /// Reproduces issue #765: conversion failures incorrectly reported as "Property not found". | ||
| /// </summary> | ||
| public class GameObjectComponentHelpersErrorTests | ||
| { | ||
| private GameObject testGo; | ||
|
|
||
| [SetUp] | ||
| public void SetUp() | ||
| { | ||
| testGo = new GameObject("ErrorTestGO"); | ||
| CommandRegistry.Initialize(); | ||
| } | ||
|
|
||
| [TearDown] | ||
| public void TearDown() | ||
| { | ||
| if (testGo != null) | ||
| Object.DestroyImmediate(testGo); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// When a property exists but conversion fails, the error should say | ||
| /// "Failed to convert" rather than "Property not found. Did you mean: X?" | ||
| /// </summary> | ||
| [Test] | ||
| public void SetComponentProperties_ConversionFailure_ReportsConversionError_NotPropertyNotFound() | ||
| { | ||
| // Expect conversion error logs from PropertyConversion (once from local SetProperty, | ||
| // once from ComponentOps fallback trying reflection before SerializedProperty) | ||
| LogAssert.Expect(LogType.Error, new Regex("Error converting token")); | ||
| LogAssert.Expect(LogType.Error, new Regex("Error converting token")); | ||
| // Expect the warning log from SetComponentPropertiesInternal | ||
| LogAssert.Expect(LogType.Warning, new Regex("Failed to set")); | ||
|
|
||
| var audioSource = testGo.AddComponent<AudioSource>(); | ||
|
|
||
| // spatialBlend is a float property — passing an array triggers conversion failure | ||
| var props = new JObject { ["spatialBlend"] = JArray.Parse("[1, 2, 3]") }; | ||
|
|
||
| var result = GameObjectComponentHelpers.SetComponentPropertiesInternal( | ||
| testGo, "AudioSource", props, audioSource); | ||
|
|
||
| Assert.IsNotNull(result, "Should return an error response"); | ||
| Assert.IsInstanceOf<ErrorResponse>(result); | ||
|
|
||
| var errorResponse = (ErrorResponse)result; | ||
|
|
||
| // The error message must NOT say "not found" for a property that exists | ||
| Assert.IsFalse( | ||
| errorResponse.Error.Contains("not found"), | ||
| $"Error should report conversion failure, not 'not found'. Got: {errorResponse.Error}"); | ||
|
Comment on lines
+54
to
+62
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: # Find the ErrorResponse class definition
fd -e cs -x grep -l "class ErrorResponse" {}Repository: CoplayDev/unity-mcp Length of output: 103 🏁 Script executed: # Search for ErrorResponse class definition with context
rg -n "class ErrorResponse" --type=cs -A 20Repository: CoplayDev/unity-mcp Length of output: 1574 🏁 Script executed: # Also search for any properties or methods that might concatenate error messages
rg -n "ErrorResponse" --type=cs -B 2 -A 10 | head -100Repository: CoplayDev/unity-mcp Length of output: 8341 🏁 Script executed: # Get the full ErrorResponse class definition
sed -n '35,80p' MCPForUnity/Editor/Helpers/Response.csRepository: CoplayDev/unity-mcp Length of output: 1334 🏁 Script executed: # Look for the ErrorResponse constructor
rg -n "public ErrorResponse" MCPForUnity/Editor/Helpers/Response.cs -A 15Repository: CoplayDev/unity-mcp Length of output: 554 Test assertions do not validate the intended behavior — they check the wrong error property.
new ErrorResponse($"One or more properties failed on '{componentTypeName}'.", new { errors = failures })Results in Consequently:
Both assertions should inspect the individual failure messages in 🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| /// <summary> | ||
| /// When a property genuinely doesn't exist, the error should still say "not found" with suggestions. | ||
| /// </summary> | ||
| [Test] | ||
| public void SetComponentProperties_NonexistentProperty_ReportsNotFound() | ||
| { | ||
| // Expect the "not found" warning | ||
| LogAssert.Expect(LogType.Warning, new Regex("not found")); | ||
|
|
||
| var audioSource = testGo.AddComponent<AudioSource>(); | ||
|
|
||
| var props = new JObject { ["totallyFakeProperty"] = 42 }; | ||
|
|
||
| var result = GameObjectComponentHelpers.SetComponentPropertiesInternal( | ||
| testGo, "AudioSource", props, audioSource); | ||
|
|
||
| Assert.IsNotNull(result); | ||
| Assert.IsInstanceOf<ErrorResponse>(result); | ||
|
|
||
| var errorResponse = (ErrorResponse)result; | ||
|
|
||
| Assert.IsTrue( | ||
| errorResponse.Error.Contains("not found") || errorResponse.Error.Contains("failed"), | ||
| $"Error for nonexistent property should say 'not found'. Got: {errorResponse.Error}"); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Valid property setting should still succeed. | ||
| /// </summary> | ||
| [Test] | ||
| public void SetComponentProperties_ValidProperty_Succeeds() | ||
| { | ||
| var audioSource = testGo.AddComponent<AudioSource>(); | ||
|
|
||
| var props = new JObject { ["volume"] = 0.42f }; | ||
|
|
||
| var result = GameObjectComponentHelpers.SetComponentPropertiesInternal( | ||
| testGo, "AudioSource", props, audioSource); | ||
|
|
||
| Assert.IsNull(result, "Should return null on success (no errors)"); | ||
| Assert.AreEqual(0.42f, audioSource.volume, 0.001f); | ||
| } | ||
| } | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.