diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonControlFieldExpression.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonControlFieldExpression.cs index 1b7ef2cc82..93aa4545e7 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonControlFieldExpression.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonControlFieldExpression.cs @@ -192,11 +192,19 @@ namespace ICSharpCode.PythonBinding { object obj = componentCreator.GetComponent(variableName); if (obj == null) { - return null; + obj = componentCreator.GetInstance(variableName); } - string[] memberNames = fullMemberName.Split('.'); - return GetMember(obj, memberNames, 2, memberNames.Length - 1); + if (obj != null) { + string[] memberNames = fullMemberName.Split('.'); + int startIndex = 2; + if (!ContainsSelfReference(memberNames)) { + // No self to skip over when searching for member. + startIndex = 1; + } + return GetMember(obj, memberNames, startIndex, memberNames.Length - 1); + } + return null; } /// @@ -281,5 +289,13 @@ namespace ICSharpCode.PythonBinding { return name.StartsWith("self.", StringComparison.InvariantCultureIgnoreCase); } + + static bool ContainsSelfReference(string[] members) + { + if (members.Length > 0) { + return "self".Equals(members[0], StringComparison.InvariantCultureIgnoreCase); + } + return false; + } } } diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadTreeViewTestFixture.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadTreeViewTestFixture.cs new file mode 100644 index 0000000000..722e519199 --- /dev/null +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadTreeViewTestFixture.cs @@ -0,0 +1,85 @@ +// +// +// +// +// $Revision$ +// + +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Drawing; +using System.IO; +using System.Windows.Forms; + +using ICSharpCode.PythonBinding; +using NUnit.Framework; +using PythonBinding.Tests.Utils; + +namespace PythonBinding.Tests.Designer +{ + [TestFixture] + public class LoadTreeViewTestFixture : LoadFormTestFixtureBase + { + public override string PythonCode { + get { + return "class MainForm(System.Windows.Forms.Form):\r\n" + + " def InitializeComponent(self):\r\n" + + " treeNode1 = System.Windows.Forms.TreeNode()\r\n" + + " treeNode2 = System.Windows.Forms.TreeNode()\r\n" + + " treeNode3 = System.Windows.Forms.TreeNode()\r\n" + + " self._treeView1 = System.Windows.Forms.TreeView()\r\n" + + " self.SuspendLayout()\r\n" + + " # \r\n" + + " # treeView1\r\n" + + " # \r\n" + + " treeNode1.Name = \"RootNode0\"\r\n" + + " treeNode1.Text = \"RootNode0.Text\"\r\n" + + " treeNode1.Nodes.AddRange(System.Array[System.Windows.Forms.TreeNode](\r\n" + + " [treeNode2]))\r\n" + + " treeNode2.Name = \"ChildNode0\"\r\n" + + " treeNode2.Text = \"ChildNode0.Text\"\r\n" + + " treeNode2.Nodes.AddRange(System.Array[System.Windows.Forms.TreeNode](\r\n" + + " [treeNode3]))\r\n" + + " treeNode3.Name = \"ChildNode1\"\r\n" + + " treeNode3.Text = \"ChildNode1.Text\"\r\n" + + " self._treeView1.Location = System.Drawing.Point(0, 0)\r\n" + + " self._treeView1.Name = \"treeView1\"\r\n" + + " self._treeView1.Nodes.AddRange(System.Array[System.Windows.Forms.TreeNode](\r\n" + + " [treeNode1]))\r\n" + + " self._treeView1.Size = System.Drawing.Size(100, 100)\r\n" + + " self._treeView1.TabIndex = 0\r\n" + + " # \r\n" + + " # MainForm\r\n" + + " # \r\n" + + " self.ClientSize = System.Drawing.Size(200, 300)\r\n" + + " self.Controls.Add(self._treeView1)\r\n" + + " self.Name = \"MainForm\"\r\n" + + " self.ResumeLayout(False)\r\n" + + " self.PerformLayout()\r\n"; + } + } + + public TreeView TreeView { + get { return Form.Controls[0] as TreeView; } + } + + [Test] + public void OneRootNode() + { + Assert.AreEqual(1, TreeView.Nodes.Count); + } + + [Test] + public void RootNodeHasOneChildNode() + { + Assert.AreEqual(1, TreeView.Nodes[0].Nodes.Count); + } + + [Test] + public void ChildNodeHasOneChildNode() + { + Assert.AreEqual(1, TreeView.Nodes[0].Nodes[0].Nodes.Count); + } + } +} diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/PythonControlFieldExpressionTests.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/PythonControlFieldExpressionTests.cs index 5558575861..6cd44e1b37 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/PythonControlFieldExpressionTests.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/PythonControlFieldExpressionTests.cs @@ -146,6 +146,24 @@ namespace PythonBinding.Tests.Designer Assert.IsNull(field.GetMember(creator)); } } + + [Test] + public void GetInstanceObjectInMethodCall() + { + string pythonCode = "treeNode1.Nodes.AddRange(System.Array[System.Windows.Forms.TreeNode](\r\n" + + " [treeNode2]))"; + + CallExpression callExpression = PythonParserHelper.GetCallExpression(pythonCode); + PythonControlFieldExpression field = PythonControlFieldExpression.Create(callExpression); + + TreeNode treeNode1 = new TreeNode(); + TreeNode treeNode2 = new TreeNode(); + MockComponentCreator creator = new MockComponentCreator(); + creator.AddInstance(treeNode1, "treeNode1"); + creator.AddInstance(treeNode2, "treeNode2"); + object member = field.GetMember(creator); + Assert.AreSame(treeNode1.Nodes, member); + } [Test] public void GetObjectInMethodCallFromSpecifiedObject() diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj b/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj index fafa1adfb3..8510a58ce7 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj @@ -238,6 +238,7 @@ + diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Utils/MockComponentCreator.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Utils/MockComponentCreator.cs index 25b1c9f3b7..6c8c7d6c08 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Utils/MockComponentCreator.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Utils/MockComponentCreator.cs @@ -83,6 +83,13 @@ namespace PythonBinding.Tests.Utils } return null; } + + public void AddInstance(object obj, string name) + { + CreatedInstance createdInstance = new CreatedInstance(obj.GetType(), new object[0], name, false); + createdInstance.Object = obj; + createdInstances.Add(createdInstance); + } public object CreateInstance(Type type, ICollection arguments, string name, bool addToContainer) {