diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonCodeDeserializer.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonCodeDeserializer.cs index a317322bf5..22bdfc5319 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonCodeDeserializer.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonCodeDeserializer.cs @@ -6,6 +6,7 @@ // using System; +using System.Collections.Generic; using System.ComponentModel.Design; using System.Reflection; using IronPython.Compiler.Ast; @@ -17,14 +18,28 @@ namespace ICSharpCode.PythonBinding /// public class PythonCodeDeserializer { - IDesignerHost designerHost; - const BindingFlags propertyBindingFlags = BindingFlags.Public | BindingFlags.GetField | BindingFlags.Static | BindingFlags.Instance; - - public PythonCodeDeserializer(IDesignerHost designerHost) + IComponentCreator componentCreator; + + public PythonCodeDeserializer(IComponentCreator componentCreator) { - this.designerHost = designerHost; + this.componentCreator = componentCreator; } + /// + /// Gets the arguments passed to the call expression. + /// + public static List GetArguments(CallExpression expression) + { + List args = new List(); + foreach (Arg a in expression.Args) { + ConstantExpression constantExpression = a.Expression as ConstantExpression; + if (constantExpression != null) { + args.Add(constantExpression.Value); + } + } + return args; + } + /// /// Creates or gets the object specified in the python AST. /// @@ -36,10 +51,48 @@ namespace ICSharpCode.PythonBinding if (node == null) { throw new ArgumentNullException("node"); } - - PythonControlFieldExpression field = PythonControlFieldExpression.Create(node as MemberExpression); - Type type = designerHost.GetType(PythonControlFieldExpression.GetPrefix(field.FullMemberName)); + + MemberExpression memberExpression = node as MemberExpression; + CallExpression callExpression = node as CallExpression; + if (callExpression != null) { + return Deserialize(callExpression); + } + return Deserialize(memberExpression); + } + + /// + /// Deserializes expressions of the form: + /// + /// 1) System.Drawing.Color.FromArgb(0, 192, 0) + /// + object Deserialize(CallExpression callExpression) + { + MemberExpression memberExpression = callExpression.Target as MemberExpression; + PythonControlFieldExpression field = PythonControlFieldExpression.Create(memberExpression); + Type type = GetType(field); + if (type != null) { + foreach (MethodInfo method in type.GetMethods(BindingFlags.Public | BindingFlags.Static)) { + if (method.Name == field.MemberName) { + if (method.GetParameters().Length == callExpression.Args.Length) { + return method.Invoke(null, GetArguments(callExpression).ToArray()); + } + } + } + } + return null; + } + + /// + /// Deserializes expressions of the form: + /// + /// 1) System.Windows.Forms.Cursors.AppStarting + /// + object Deserialize(MemberExpression memberExpression) + { + PythonControlFieldExpression field = PythonControlFieldExpression.Create(memberExpression); + Type type = GetType(field); if (type != null) { + BindingFlags propertyBindingFlags = BindingFlags.Public | BindingFlags.GetField | BindingFlags.Static | BindingFlags.Instance; PropertyInfo propertyInfo = type.GetProperty(field.MemberName, propertyBindingFlags); if (propertyInfo != null) { return propertyInfo.GetValue(type, null); @@ -47,5 +100,10 @@ namespace ICSharpCode.PythonBinding } return null; } + + Type GetType(PythonControlFieldExpression field) + { + return componentCreator.GetType(PythonControlFieldExpression.GetPrefix(field.FullMemberName)); + } } } diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonControlDefaultPropertyValues.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonControlDefaultPropertyValues.cs index ae532140ad..47f50b3325 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonControlDefaultPropertyValues.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonControlDefaultPropertyValues.cs @@ -37,6 +37,8 @@ namespace ICSharpCode.PythonBinding defaultPropertyValues.Add("AutoScrollMargin", new Size(0, 0)); defaultPropertyValues.Add("Location", new Point(0, 0)); defaultPropertyValues.Add("Padding", Padding.Empty); + defaultPropertyValues.Add("BackColor", Control.DefaultBackColor); + defaultPropertyValues.Add("ForeColor", Control.DefaultForeColor); } /// @@ -96,19 +98,13 @@ namespace ICSharpCode.PythonBinding return defaultPropertyValue.Equals(propertyValue); } - if (propertyInfo.Name == "BackColor") { - // Default is Control.DefaultBackColor - return true; - } else if (propertyInfo.Name == "Icon") { + if (propertyInfo.Name == "Icon") { return true; } else if (propertyInfo.Name == "TransparencyKey") { return true; } else if (propertyInfo.Name == "Font") { // Default is Control.DefaultFont return true; - } else if (propertyInfo.Name == "ForeColor") { - // Default is Control.DefaultForeColor - return true; } return false; } diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonDesignerLoader.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonDesignerLoader.cs index b590d1991f..b6950e7ade 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonDesignerLoader.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonDesignerLoader.cs @@ -91,7 +91,7 @@ namespace ICSharpCode.PythonBinding { // Create designer root object. this.serializationManager = serializationManager; - PythonFormWalker visitor = new PythonFormWalker(this, base.LoaderHost); + PythonFormWalker visitor = new PythonFormWalker(this); visitor.CreateForm(generator.ViewContent.DesignerCodeFileContent); } } diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonForm.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonForm.cs index 6e0625dbbb..7140f02c9d 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonForm.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonForm.cs @@ -142,11 +142,7 @@ namespace ICSharpCode.PythonBinding /// Appends a property to the InitializeComponents method. /// void AppendProperty(object obj, PropertyDescriptor propertyDescriptor) - { - if (propertyDescriptor.Name == "Text") { - Console.WriteLine("asfads"); - } - + { object propertyValue = propertyDescriptor.GetValue(obj); if (propertyValue == null) { return; diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonFormWalker.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonFormWalker.cs index 717bd723d5..e753490c57 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonFormWalker.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonFormWalker.cs @@ -31,10 +31,10 @@ namespace ICSharpCode.PythonBinding string formName = String.Empty; PythonCodeDeserializer deserializer; - public PythonFormWalker(IComponentCreator componentCreator, IDesignerHost designerHost) + public PythonFormWalker(IComponentCreator componentCreator) { this.componentCreator = componentCreator; - deserializer = new PythonCodeDeserializer(designerHost); + deserializer = new PythonCodeDeserializer(componentCreator); } /// @@ -107,12 +107,21 @@ namespace ICSharpCode.PythonBinding MemberExpression memberExpression = node.Target as MemberExpression; if (memberExpression != null) { string name = PythonControlFieldExpression.GetMemberName(memberExpression); - if (walkingAssignment) { - Type type = GetType(name); - List args = GetArguments(node); - object instance = componentCreator.CreateInstance(type, args, fieldExpression.MemberName, false); - if (!SetPropertyValue(form, fieldExpression.MemberName, instance)) { - AddComponent(fieldExpression.MemberName, instance); + if (walkingAssignment) { + Type type = componentCreator.GetType(name); + if (type != null) { + List args = PythonCodeDeserializer.GetArguments(node); + object instance = componentCreator.CreateInstance(type, args, fieldExpression.MemberName, false); + if (!SetPropertyValue(form, fieldExpression.MemberName, instance)) { + AddComponent(fieldExpression.MemberName, instance); + } + } else { + object obj = deserializer.Deserialize(node); + if (obj != null) { + SetPropertyValue(form, fieldExpression.MemberName, obj); + } else { + throw new PythonFormWalkerException(String.Format("Could not find type '{0}'.", name)); + } } } else if (name == "self.Controls.Add") { string controlName = PythonControlFieldExpression.GetControlNameBeingAdded(node); @@ -127,21 +136,6 @@ namespace ICSharpCode.PythonBinding SetPropertyValue(fieldExpression.MemberName, node.Name.ToString()); return false; } - - /// - /// Gets the arguments passed to the call expression. - /// - static List GetArguments(CallExpression expression) - { - List args = new List(); - foreach (Arg a in expression.Args) { - ConstantExpression constantExpression = a.Expression as ConstantExpression; - if (constantExpression != null) { - args.Add(constantExpression.Value); - } - } - return args; - } static bool IsInitializeComponentMethod(FunctionDefinition node) { @@ -185,16 +179,7 @@ namespace ICSharpCode.PythonBinding } return propertyValue; } - - Type GetType(string typeName) - { - Type type = componentCreator.GetType(typeName); - if (type == null) { - throw new PythonFormWalkerException(String.Format("Could not find type '{0}'.", typeName)); - } - return type; - } - + /// /// Looks for the control with the specified name in the objects that have been /// created whilst processing the InitializeComponent method. @@ -218,15 +203,6 @@ namespace ICSharpCode.PythonBinding createdObjects.Add(variableName, component); } - static string GetFirstArgumentAsString(CallExpression node) - { - List args = GetArguments(node); - if (args.Count > 0) { - return args[0] as String; - } - return null; - } - Control GetCurrentControl() { string variableName = PythonControlFieldExpression.GetVariableNameFromSelfReference(fieldExpression.FullMemberName); diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonPropertyValueAssignment.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonPropertyValueAssignment.cs index c38b4ec313..ebfd89a242 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonPropertyValueAssignment.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonPropertyValueAssignment.cs @@ -41,18 +41,21 @@ namespace ICSharpCode.PythonBinding } else if (propertyType.IsEnum) { return propertyType.FullName + "." + propertyValue.ToString(); } else if (propertyType == typeof(Cursor)) { - return GetCursorToString(propertyValue as Cursor); + return GetCursorAsString(propertyValue as Cursor); } else if (propertyType == typeof(Point)) { Point point = (Point)propertyValue; return point.GetType().FullName + "(" + point.X + ", " + point.Y + ")"; } else if (propertyType == typeof(Padding)) { Padding padding = (Padding)propertyValue; return padding.GetType().FullName + "(" + padding.Left + ", " + padding.Top + ", " + padding.Right + ", " + padding.Bottom + ")"; + } else if (propertyType == typeof(Color)) { + Color color = (Color)propertyValue; + return GetColorAsString(color); } return propertyValue.ToString(); } - static string GetCursorToString(Cursor cursor) + static string GetCursorAsString(Cursor cursor) { foreach (PropertyInfo propertyInfo in typeof(Cursors).GetProperties(BindingFlags.Public | BindingFlags.Static)) { Cursor standardCursor = (Cursor)propertyInfo.GetValue(null, null); @@ -62,5 +65,28 @@ namespace ICSharpCode.PythonBinding } return String.Empty; } + + static string GetColorAsString(Color color) + { + if (color.IsSystemColor) { + return GetColorAsString(color, typeof(SystemColors)); + } else if (color.IsNamedColor) { + return GetColorAsString(color, typeof(Color)); + } + + // Custom color. + return color.GetType().FullName + ".FromArgb(" + color.R + ", " + color.G + ", " + color.B + ")"; + } + + static string GetColorAsString(Color color, Type type) + { + foreach (PropertyInfo property in type.GetProperties(BindingFlags.Public | BindingFlags.Static)) { + Color standardColor = (Color)property.GetValue(null, null); + if (color == standardColor) { + return type.FullName + "." + standardColor.Name; + } + } + return String.Empty; + } } } diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/CursorTypeResolutionTestFixture.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/CursorTypeResolutionTestFixture.cs index a4e026f299..9c95351632 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/CursorTypeResolutionTestFixture.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/CursorTypeResolutionTestFixture.cs @@ -22,45 +22,23 @@ namespace PythonBinding.Tests.Designer /// PythonCodeDeserializer. /// [TestFixture] - public class CursorTypeResolutionTestFixture + public class CursorTypeResolutionTestFixture : DeserializeAssignmentTestFixtureBase { - string pythonCode = "self.Cursors = System.Windows.Forms.Cursors.AppStarting"; - Node rhsAssignmentNode; - object obj; - MockDesignerLoaderHost mockDesignerLoaderHost; - MockTypeResolutionService typeResolutionService; - - [TestFixtureSetUp] - public void SetUpFixture() - { - PythonParser parser = new PythonParser(); - PythonAst ast = parser.CreateAst(@"snippet.py", pythonCode); - SuiteStatement suiteStatement = (SuiteStatement)ast.Body; - AssignmentStatement assignment = suiteStatement.Statements[0] as AssignmentStatement; - rhsAssignmentNode = assignment.Right; - - mockDesignerLoaderHost = new MockDesignerLoaderHost(); - typeResolutionService = mockDesignerLoaderHost.TypeResolutionService; - PythonCodeDeserializer deserializer = new PythonCodeDeserializer(mockDesignerLoaderHost); - obj = deserializer.Deserialize(rhsAssignmentNode); - } - - [Test] - public void RhsAssignmentNodeExists() + public override string GetPythonCode() { - Assert.IsNotNull(rhsAssignmentNode); + return "self.Cursors = System.Windows.Forms.Cursors.AppStarting"; } [Test] public void DeserializedObjectIsCursorsAppStarting() { - Assert.AreEqual(Cursors.AppStarting, obj); + Assert.AreEqual(Cursors.AppStarting, deserializedObject); } [Test] public void CursorsTypeResolved() { - Assert.AreEqual("System.Windows.Forms.Cursors", typeResolutionService.LastTypeNameResolved); + Assert.AreEqual("System.Windows.Forms.Cursors", base.LastTypeNameResolved); } } } diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/DeserializeAssignmentTestFixtureBase.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/DeserializeAssignmentTestFixtureBase.cs new file mode 100644 index 0000000000..d785e43978 --- /dev/null +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/DeserializeAssignmentTestFixtureBase.cs @@ -0,0 +1,48 @@ +// +// +// +// +// $Revision$ +// + +using System; +using System.ComponentModel; +using System.ComponentModel.Design; +using System.Drawing; +using System.Windows.Forms; +using ICSharpCode.PythonBinding; +using IronPython.Compiler.Ast; +using NUnit.Framework; +using PythonBinding.Tests.Utils; + +namespace PythonBinding.Tests.Designer +{ + /// + /// Base class for all tests of the PythonCodeDeserialize when deserializing an + /// assignment. + /// + public abstract class DeserializeAssignmentTestFixtureBase : LoadFormTestFixtureBase + { + protected Node rhsAssignmentNode; + protected object deserializedObject; + protected MockDesignerLoaderHost mockDesignerLoaderHost; + protected MockTypeResolutionService typeResolutionService; + + [TestFixtureSetUp] + public void SetUpFixture() + { + PythonParser parser = new PythonParser(); + PythonAst ast = parser.CreateAst(@"snippet.py", GetPythonCode()); + SuiteStatement suiteStatement = (SuiteStatement)ast.Body; + AssignmentStatement assignment = suiteStatement.Statements[0] as AssignmentStatement; + rhsAssignmentNode = assignment.Right; + + mockDesignerLoaderHost = new MockDesignerLoaderHost(); + typeResolutionService = mockDesignerLoaderHost.TypeResolutionService; + PythonCodeDeserializer deserializer = new PythonCodeDeserializer(this); + deserializedObject = deserializer.Deserialize(rhsAssignmentNode); + } + + public abstract string GetPythonCode(); + } +} diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/DeserializeColorFromArgbTestFixture.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/DeserializeColorFromArgbTestFixture.cs new file mode 100644 index 0000000000..a4455dd857 --- /dev/null +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/DeserializeColorFromArgbTestFixture.cs @@ -0,0 +1,45 @@ +// +// +// +// +// $Revision$ +// + +using System; +using System.ComponentModel; +using System.ComponentModel.Design; +using System.Drawing; +using System.Windows.Forms; +using ICSharpCode.PythonBinding; +using IronPython.Compiler.Ast; +using NUnit.Framework; +using PythonBinding.Tests.Utils; + +namespace PythonBinding.Tests.Designer +{ + /// + /// Tests that the string "System.Drawing.Color.FromArgb(0, 192, 10)" can be converted to an object by the + /// PythonCodeDeserializer. + /// + [TestFixture] + public class DeserializeColorFromArgbTestFixture : DeserializeAssignmentTestFixtureBase + { + public override string GetPythonCode() + { + return "self.BackColor = System.Drawing.Color.FromArgb(0, 192, 10)"; + } + + [Test] + public void DeserializedObjectIsExpectedCustomColor() + { + Color customColor = Color.FromArgb(0, 192, 10); + Assert.AreEqual(customColor, deserializedObject); + } + + [Test] + public void ColorTypeResolved() + { + Assert.AreEqual("System.Drawing.Color", LastTypeNameResolved); + } + } +} diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateFormColorTestFixture.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateFormColorTestFixture.cs new file mode 100644 index 0000000000..4b0ba6d411 --- /dev/null +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateFormColorTestFixture.cs @@ -0,0 +1,63 @@ +// +// +// +// +// $Revision$ +// + +using System; +using System.Drawing; +using System.Windows.Forms; +using ICSharpCode.PythonBinding; +using NUnit.Framework; +using PythonBinding.Tests.Utils; + +namespace PythonBinding.Tests.Designer +{ + [TestFixture] + public class GenerateFormColorTestFixture + { + string generatedPythonCode; + + [TestFixtureSetUp] + public void SetUpFixture() + { + using (Form form = new Form()) { + form.Name = "MainForm"; + form.ClientSize = new Size(284, 264); + form.BackColor = SystemColors.HotTrack; + form.ForeColor = Color.Red; + + string indentString = " "; + PythonForm pythonForm = new PythonForm(indentString); + generatedPythonCode = pythonForm.GenerateInitializeComponentMethod(form); + } + } + + [Test] + public void GeneratedCode() + { + string expectedCode = "def InitializeComponent(self):\r\n" + + " self.SuspendLayout()\r\n" + + " # \r\n" + + " # MainForm\r\n" + + " # \r\n" + + " self.BackColor = System.Drawing.SystemColors.HotTrack\r\n" + + " self.ClientSize = System.Drawing.Size(284, 264)\r\n" + + " self.ForeColor = System.Drawing.Color.Red\r\n" + + " self.Name = \"MainForm\"\r\n" + + " self.Visible = False\r\n" + + " self.ResumeLayout(False)\r\n" + + " self.PerformLayout()\r\n"; + + Assert.AreEqual(expectedCode, generatedPythonCode); + } + + [Test] + public void ConvertCustomColorToString() + { + Color customColor = Color.FromArgb(0, 192, 10); + Assert.AreEqual("System.Drawing.Color.FromArgb(0, 192, 10)", PythonPropertyValueAssignment.ToString(customColor)); + } + } +} diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/IsDefaultPropertyValueTests.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/IsDefaultPropertyValueTests.cs index 70c69f3da4..84bf411c99 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/IsDefaultPropertyValueTests.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/IsDefaultPropertyValueTests.cs @@ -210,6 +210,34 @@ namespace PythonBinding.Tests.Designer { form.Padding = new Padding(10, 10, 10, 10); Assert.IsFalse(defaultPropertyValues.IsDefaultValue("Padding", form)); + } + + [Test] + public void BackColorPropertyDefaultIsControlDefaultBackColor() + { + form.BackColor = Control.DefaultBackColor; + Assert.IsTrue(defaultPropertyValues.IsDefaultValue("BackColor", form)); + } + + [Test] + public void NonDefaultBackColorProperty() + { + form.BackColor = Color.Blue; + Assert.IsFalse(defaultPropertyValues.IsDefaultValue("BackColor", form)); + } + + [Test] + public void ForeColorPropertyDefaultIsControlDefaultForeColor() + { + form.ForeColor = Control.DefaultForeColor; + Assert.IsTrue(defaultPropertyValues.IsDefaultValue("ForeColor", form)); + } + + [Test] + public void NonDefaultForeColorProperty() + { + form.ForeColor = Color.Blue; + Assert.IsFalse(defaultPropertyValues.IsDefaultValue("ForeColor", form)); } } } diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadAccessibleRoleTestFixture.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadAccessibleRoleTestFixture.cs index 2dc3836612..5a0f688914 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadAccessibleRoleTestFixture.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadAccessibleRoleTestFixture.cs @@ -34,7 +34,7 @@ namespace PythonBinding.Tests.Designer [TestFixtureSetUp] public void SetUpFixture() { - PythonFormWalker walker = new PythonFormWalker(this, new MockDesignerLoaderHost()); + PythonFormWalker walker = new PythonFormWalker(this); form = walker.CreateForm(pythonCode); } diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadColorFromArgbTestFixture.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadColorFromArgbTestFixture.cs new file mode 100644 index 0000000000..5f7d192362 --- /dev/null +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadColorFromArgbTestFixture.cs @@ -0,0 +1,53 @@ +// +// +// +// +// $Revision$ +// + +using System; +using System.Collections.Generic; +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 LoadColorFromArgbTestFixture : LoadFormTestFixtureBase + { + string pythonCode = "class TestForm(System.Windows.Forms.Form):\r\n" + + " def InitializeComponent(self):\r\n" + + " self.SuspendLayout()\r\n" + + " # \r\n" + + " # TestForm\r\n" + + " # \r\n" + + " self.BackColor = System.Drawing.Color.FromArgb(10, 190, 0)\r\n" + + " self.Name = \"TestForm\"\r\n" + + " self.ResumeLayout(False)\r\n"; + Form form; + + [TestFixtureSetUp] + public void SetUpFixture() + { + PythonFormWalker walker = new PythonFormWalker(this); + form = walker.CreateForm(pythonCode); + } + + [TestFixtureTearDown] + public void TearDownFixture() + { + form.Dispose(); + } + + [Test] + public void FormBackColor() + { + Assert.AreEqual(Color.FromArgb(10, 190, 0), form.BackColor); + } + } +} diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadCursorTestFixture.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadCursorTestFixture.cs index 2a38166251..034a6e50cc 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadCursorTestFixture.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadCursorTestFixture.cs @@ -34,7 +34,7 @@ namespace PythonBinding.Tests.Designer [TestFixtureSetUp] public void SetUpFixture() { - PythonFormWalker walker = new PythonFormWalker(this, new MockDesignerLoaderHost()); + PythonFormWalker walker = new PythonFormWalker(this); form = walker.CreateForm(pythonCode); } diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadFormTestFixtureBase.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadFormTestFixtureBase.cs index 486ae26a02..3a4def4a94 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadFormTestFixtureBase.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadFormTestFixtureBase.cs @@ -88,6 +88,10 @@ namespace PythonBinding.Tests.Designer protected List TypeNames { get { return typeNames; } } + + protected string LastTypeNameResolved { + get { return TypeNames[TypeNames.Count - 1]; } + } protected CreatedInstance GetCreatedInstance(Type type) { diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadFormWithBooleanPropertiesSetTestFixture.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadFormWithBooleanPropertiesSetTestFixture.cs index 643470c7b3..c2b5f460b8 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadFormWithBooleanPropertiesSetTestFixture.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadFormWithBooleanPropertiesSetTestFixture.cs @@ -35,7 +35,7 @@ namespace PythonBinding.Tests.Designer [TestFixtureSetUp] public void SetUpFixture() { - PythonFormWalker walker = new PythonFormWalker(this, new MockDesignerLoaderHost()); + PythonFormWalker walker = new PythonFormWalker(this); form = walker.CreateForm(pythonCode); } diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadSimpleFormTestFixture.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadSimpleFormTestFixture.cs index 698b875cc9..f17222e3f7 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadSimpleFormTestFixture.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadSimpleFormTestFixture.cs @@ -37,7 +37,7 @@ namespace PythonBinding.Tests.Designer [TestFixtureSetUp] public void SetUpFixture() { - PythonFormWalker walker = new PythonFormWalker(this, new MockDesignerLoaderHost()); + PythonFormWalker walker = new PythonFormWalker(this); form = walker.CreateForm(pythonCode); if (CreatedComponents.Count > 0) { diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadTextBoxTestFixture.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadTextBoxTestFixture.cs index f5d15e662d..ca960c39b7 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadTextBoxTestFixture.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadTextBoxTestFixture.cs @@ -41,7 +41,7 @@ namespace PythonBinding.Tests.Designer [TestFixtureSetUp] public void SetUpFixture() { - PythonFormWalker walker = new PythonFormWalker(this, new MockDesignerLoaderHost()); + PythonFormWalker walker = new PythonFormWalker(this); form = walker.CreateForm(pythonCode); if (form.Controls.Count > 0) { textBox = form.Controls[0] as TextBox; diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/MissingInitializeComponentMethodTestFixture.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/MissingInitializeComponentMethodTestFixture.cs index f57cf815d3..9b53443c29 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/MissingInitializeComponentMethodTestFixture.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/MissingInitializeComponentMethodTestFixture.cs @@ -38,7 +38,7 @@ namespace PythonBinding.Tests.Designer [ExpectedException(typeof(PythonFormWalkerException))] public void PythonFormWalkerExceptionThrown() { - PythonFormWalker walker = new PythonFormWalker(this, new MockDesignerLoaderHost()); + PythonFormWalker walker = new PythonFormWalker(this); walker.CreateForm(pythonCode); Assert.Fail("Exception should have been thrown before this."); } @@ -50,7 +50,7 @@ namespace PythonBinding.Tests.Designer public void ClassWithNoBody() { ClassDefinition classDef = new ClassDefinition(new SymbolId(10), null, null); - PythonFormWalker walker = new PythonFormWalker(this, new MockDesignerLoaderHost()); + PythonFormWalker walker = new PythonFormWalker(this); walker.Walk(classDef); } @@ -63,7 +63,7 @@ namespace PythonBinding.Tests.Designer { List lhs = new List(); AssignmentStatement assign = new AssignmentStatement(lhs.ToArray(), null); - PythonFormWalker walker = new PythonFormWalker(this, new MockDesignerLoaderHost()); + PythonFormWalker walker = new PythonFormWalker(this); walker.Walk(assign); } } diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/PythonCodeDeserializerTests.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/PythonCodeDeserializerTests.cs index 6d125ae1fe..dcce830fdc 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/PythonCodeDeserializerTests.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/PythonCodeDeserializerTests.cs @@ -14,14 +14,14 @@ using PythonBinding.Tests.Utils; namespace PythonBinding.Tests.Designer { [TestFixture] - public class PythonCodeDeserializerTests + public class PythonCodeDeserializerTests : LoadFormTestFixtureBase { PythonCodeDeserializer deserializer; [TestFixtureSetUp] public void SetUpFixture() { - deserializer = new PythonCodeDeserializer(new MockDesignerLoaderHost()); + deserializer = new PythonCodeDeserializer(this); } [Test] @@ -52,6 +52,18 @@ namespace PythonBinding.Tests.Designer SuiteStatement suiteStatement = (SuiteStatement)ast.Body; AssignmentStatement assignment = suiteStatement.Statements[0] as AssignmentStatement; + Assert.IsNull(deserializer.Deserialize(assignment.Right)); + } + + [Test] + public void UnknownTypeNameInCallExpression() + { + string pythonCode = "self.Cursors = System.Windows.Forms.UnknownType.CreateDefaultCursor()"; + PythonParser parser = new PythonParser(); + PythonAst ast = parser.CreateAst(@"snippet.py", pythonCode); + SuiteStatement suiteStatement = (SuiteStatement)ast.Body; + AssignmentStatement assignment = suiteStatement.Statements[0] as AssignmentStatement; + Assert.IsNull(deserializer.Deserialize(assignment.Right)); } } diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/TextBoxNotAddedToFormTestFixture.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/TextBoxNotAddedToFormTestFixture.cs index 21e0871a40..3cd170e0db 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/TextBoxNotAddedToFormTestFixture.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/TextBoxNotAddedToFormTestFixture.cs @@ -45,7 +45,7 @@ namespace PythonBinding.Tests.Designer [TestFixtureSetUp] public void SetUpFixture() { - PythonFormWalker walker = new PythonFormWalker(this, new MockDesignerLoaderHost()); + PythonFormWalker walker = new PythonFormWalker(this); form = walker.CreateForm(pythonCode); } diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/UnknownTypeTestFixture.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/UnknownTypeTestFixture.cs index a83ed74b22..d5e70dc5a7 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/UnknownTypeTestFixture.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/UnknownTypeTestFixture.cs @@ -38,7 +38,7 @@ namespace PythonBinding.Tests.Designer [ExpectedException(typeof(PythonFormWalkerException))] public void PythonFormWalkerExceptionThrown() { - PythonFormWalker walker = new PythonFormWalker(this, new MockDesignerLoaderHost()); + PythonFormWalker walker = new PythonFormWalker(this); walker.CreateForm(pythonCode); Assert.Fail("Exception should have been thrown before this."); } diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj b/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj index 860b31f85c..64d2a8f92d 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj @@ -146,6 +146,8 @@ + + @@ -153,6 +155,7 @@ + @@ -165,6 +168,7 @@ + diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Utils/MockTypeResolutionService.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Utils/MockTypeResolutionService.cs index 7c056c3d02..e60b2e38ab 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Utils/MockTypeResolutionService.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Utils/MockTypeResolutionService.cs @@ -7,6 +7,7 @@ using System; using System.ComponentModel.Design; +using System.Drawing; using System.Reflection; using System.Windows.Forms; @@ -53,6 +54,11 @@ namespace PythonBinding.Tests.Utils return type; } + type = typeof(Color).Assembly.GetType(name, false); + if (type != null) { + return type; + } + return Type.GetType(name); }