From 94718ae84b46a0356dea4fa461498fabb3e49d19 Mon Sep 17 00:00:00 2001 From: Matt Ward Date: Thu, 21 May 2009 18:14:18 +0000 Subject: [PATCH] Python forms designer does not generate comments for a control if no property or event handler code was generated for that control. git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@4095 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../Project/Src/PythonCodeBuilder.cs | 16 +++++ .../Project/Src/PythonDesignerComponent.cs | 18 +++--- .../Project/Src/PythonListViewComponent.cs | 2 +- .../Designer/GenerateSimpleFormTestFixture.cs | 2 +- .../GenerateToolTipFormTestFixture.cs | 63 +++++++++++++++++++ .../Test/Designer/PythonCodeBuilderTests.cs | 28 +++++++++ .../Test/PythonBinding.Tests.csproj | 1 + 7 files changed, 121 insertions(+), 9 deletions(-) create mode 100644 src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateToolTipFormTestFixture.cs diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonCodeBuilder.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonCodeBuilder.cs index 7cb790dc88..ef24bb2fe6 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonCodeBuilder.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonCodeBuilder.cs @@ -20,6 +20,11 @@ namespace ICSharpCode.PythonBinding { } + public PythonCodeBuilder(int initialIndent) + { + indent = initialIndent; + } + /// /// Gets or sets the string used for indenting. /// @@ -77,5 +82,16 @@ namespace ICSharpCode.PythonBinding { indent--; } + + public int Indent { + get { return indent; } + } + + /// + /// Gets the length of the current code string. + /// + public int Length { + get { return codeBuilder.Length; } + } } } diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonDesignerComponent.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonDesignerComponent.cs index caba05755d..638c17980c 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonDesignerComponent.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonDesignerComponent.cs @@ -240,7 +240,6 @@ namespace ICSharpCode.PythonBinding /// public virtual void AppendComponent(PythonCodeBuilder codeBuilder) { - AppendComment(codeBuilder); AppendComponentProperties(codeBuilder); AppendChildComponentProperties(codeBuilder); } @@ -372,7 +371,7 @@ namespace ICSharpCode.PythonBinding /// public void AppendComponentProperties(PythonCodeBuilder codeBuilder) { - AppendComponentProperties(codeBuilder, true, false, false); + AppendComponentProperties(codeBuilder, true, false, true); } /// @@ -520,13 +519,18 @@ namespace ICSharpCode.PythonBinding /// public void AppendComponentProperties(PythonCodeBuilder codeBuilder, bool addComponentNameToProperty, bool addChildComponentProperties, bool addComment) { - if (addComment) { + PythonCodeBuilder propertiesBuilder = new PythonCodeBuilder(codeBuilder.Indent); + propertiesBuilder.IndentString = codeBuilder.IndentString; + + AppendProperties(propertiesBuilder); + AppendEventHandlers(propertiesBuilder, eventBindingService); + + // Add comment if we have added some properties or event handlers. + if (addComment && propertiesBuilder.Length > 0) { AppendComment(codeBuilder); } - - AppendProperties(codeBuilder); - AppendEventHandlers(codeBuilder, eventBindingService); - + codeBuilder.Append(propertiesBuilder.ToString()); + if (addChildComponentProperties) { AppendChildComponentProperties(codeBuilder); } diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonListViewComponent.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonListViewComponent.cs index cb0fa94788..64a9c3d9ab 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonListViewComponent.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonListViewComponent.cs @@ -48,7 +48,7 @@ namespace ICSharpCode.PythonBinding { AppendComment(codeBuilder); AppendListViewItemProperties(codeBuilder); - AppendComponentProperties(codeBuilder); + AppendComponentProperties(codeBuilder, true, false, false); AppendChildComponentProperties(codeBuilder); } diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateSimpleFormTestFixture.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateSimpleFormTestFixture.cs index b85d8d2654..f35270c287 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateSimpleFormTestFixture.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateSimpleFormTestFixture.cs @@ -44,7 +44,7 @@ namespace PythonBinding.Tests.Designer codeBuilder.IncreaseIndent(); PythonDesignerRootComponent designerRootComponent = new PythonDesignerRootComponent(form); propertyOwnerName = designerRootComponent.GetPropertyOwnerName(); - designerRootComponent.AppendComponentProperties(codeBuilder); + designerRootComponent.AppendComponentProperties(codeBuilder, true, false, false); formPropertiesCode = codeBuilder.ToString(); } } diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateToolTipFormTestFixture.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateToolTipFormTestFixture.cs new file mode 100644 index 0000000000..2764fcb080 --- /dev/null +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateToolTipFormTestFixture.cs @@ -0,0 +1,63 @@ +// +// +// +// +// $Revision$ +// + +using System; +using System.ComponentModel; +using System.ComponentModel.Design; +using System.Drawing; +using System.Windows.Forms; +using ICSharpCode.PythonBinding; +using NUnit.Framework; +using PythonBinding.Tests.Utils; + +namespace PythonBinding.Tests.Designer +{ + [TestFixture] + public class GenerateToolTipFormTestFixture + { + string generatedPythonCode; + + [TestFixtureSetUp] + public void SetUpFixture() + { + using (DesignSurface designSurface = new DesignSurface(typeof(Form))) { + IDesignerHost host = (IDesignerHost)designSurface.GetService(typeof(IDesignerHost)); + IEventBindingService eventBindingService = new MockEventBindingService(host); + Form form = (Form)host.RootComponent; + form.ClientSize = new Size(284, 264); + + PropertyDescriptorCollection descriptors = TypeDescriptor.GetProperties(form); + PropertyDescriptor descriptor = descriptors.Find("Name", false); + descriptor.SetValue(form, "MainForm"); + + ToolTip toolTip = (ToolTip)host.CreateComponent(typeof(ToolTip), "toolTip1"); + + string indentString = " "; + PythonControl pythonForm = new PythonControl(indentString); + generatedPythonCode = pythonForm.GenerateInitializeComponentMethod(form); + } + } + + [Test] + public void GeneratedCode() + { + string expectedCode = "def InitializeComponent(self):\r\n" + + " self._components = System.ComponentModel.Container()\r\n" + + " self._toolTip1 = System.Windows.Forms.ToolTip(self._components)\r\n" + + " self.SuspendLayout()\r\n" + + " # \r\n" + + " # MainForm\r\n" + + " # \r\n" + + " self.ClientSize = System.Drawing.Size(284, 264)\r\n" + + " self.Name = \"MainForm\"\r\n" + + " self.ResumeLayout(False)\r\n" + + " self.PerformLayout()\r\n"; + + Assert.AreEqual(expectedCode, generatedPythonCode, generatedPythonCode); + } + } +} diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/PythonCodeBuilderTests.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/PythonCodeBuilderTests.cs index 80dba222af..a602ceb816 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/PythonCodeBuilderTests.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/PythonCodeBuilderTests.cs @@ -72,5 +72,33 @@ namespace PythonBinding.Tests.Designer codeBuilder.AppendIndentedLine("abc"); Assert.AreEqual("\tabc\r\n", codeBuilder.ToString()); } + + [Test] + public void InitialIndentWhenCodeBuilderCreatedIsZero() + { + Assert.AreEqual(0, codeBuilder.Indent); + } + + [Test] + public void IncreaseIndentByOne() + { + codeBuilder.IncreaseIndent(); + Assert.AreEqual(1, codeBuilder.Indent); + } + + [Test] + public void LengthAfterAddingText() + { + codeBuilder.Append("abc"); + Assert.AreEqual(3, codeBuilder.Length); + } + + [Test] + public void IndentPassedToConstructor() + { + codeBuilder = new PythonCodeBuilder(2); + codeBuilder.AppendIndented("abc"); + Assert.AreEqual("\t\tabc", codeBuilder.ToString()); + } } } diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj b/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj index b2f610c82f..1d7aafd84d 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj @@ -190,6 +190,7 @@ +