Browse Source
git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@3846 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61shortcuts
25 changed files with 412 additions and 26 deletions
@ -0,0 +1,51 @@
@@ -0,0 +1,51 @@
|
||||
// <file>
|
||||
// <copyright see="prj:///doc/copyright.txt"/>
|
||||
// <license see="prj:///doc/license.txt"/>
|
||||
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
|
||||
// <version>$Revision$</version>
|
||||
// </file>
|
||||
|
||||
using System; |
||||
using System.ComponentModel.Design; |
||||
using System.Reflection; |
||||
using IronPython.Compiler.Ast; |
||||
|
||||
namespace ICSharpCode.PythonBinding |
||||
{ |
||||
/// <summary>
|
||||
/// Creates objects from python code.
|
||||
/// </summary>
|
||||
public class PythonCodeDeserializer |
||||
{ |
||||
IDesignerHost designerHost; |
||||
const BindingFlags propertyBindingFlags = BindingFlags.Public | BindingFlags.GetField | BindingFlags.Static | BindingFlags.Instance; |
||||
|
||||
public PythonCodeDeserializer(IDesignerHost designerHost) |
||||
{ |
||||
this.designerHost = designerHost; |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Creates or gets the object specified in the python AST.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// Null if the node cannot be deserialized.
|
||||
/// </returns>
|
||||
public object Deserialize(Node node) |
||||
{ |
||||
if (node == null) { |
||||
throw new ArgumentNullException("node"); |
||||
} |
||||
|
||||
PythonControlFieldExpression field = PythonControlFieldExpression.Create(node as MemberExpression); |
||||
Type type = designerHost.GetType(PythonControlFieldExpression.GetPrefix(field.FullMemberName)); |
||||
if (type != null) { |
||||
PropertyInfo propertyInfo = type.GetProperty(field.MemberName, propertyBindingFlags); |
||||
if (propertyInfo != null) { |
||||
return propertyInfo.GetValue(type, null); |
||||
} |
||||
} |
||||
return null; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,28 @@
@@ -0,0 +1,28 @@
|
||||
// <file>
|
||||
// <copyright see="prj:///doc/copyright.txt"/>
|
||||
// <license see="prj:///doc/license.txt"/>
|
||||
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
|
||||
// <version>$Revision$</version>
|
||||
// </file>
|
||||
|
||||
using System; |
||||
using System.Windows.Forms; |
||||
|
||||
namespace ICSharpCode.PythonBinding |
||||
{ |
||||
public class PythonControlCursorProperty : PythonControlProperty |
||||
{ |
||||
public PythonControlCursorProperty() |
||||
{ |
||||
} |
||||
|
||||
public override bool IsDefaultValue(object propertyValue) |
||||
{ |
||||
Cursor cursor = propertyValue as Cursor; |
||||
if (cursor != null) { |
||||
return cursor == Cursors.Default; |
||||
} |
||||
return false; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,66 @@
@@ -0,0 +1,66 @@
|
||||
// <file>
|
||||
// <copyright see="prj:///doc/copyright.txt"/>
|
||||
// <license see="prj:///doc/license.txt"/>
|
||||
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
|
||||
// <version>$Revision$</version>
|
||||
// </file>
|
||||
|
||||
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 |
||||
{ |
||||
/// <summary>
|
||||
/// Tests that the string "System.Windows.Forms.Cursors.AppStarting" can be resolved by the
|
||||
/// PythonCodeDeserializer.
|
||||
/// </summary>
|
||||
[TestFixture] |
||||
public class CursorTypeResolutionTestFixture |
||||
{ |
||||
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() |
||||
{ |
||||
Assert.IsNotNull(rhsAssignmentNode); |
||||
} |
||||
|
||||
[Test] |
||||
public void DeserializedObjectIsCursorsAppStarting() |
||||
{ |
||||
Assert.AreEqual(Cursors.AppStarting, obj); |
||||
} |
||||
|
||||
[Test] |
||||
public void CursorsTypeResolved() |
||||
{ |
||||
Assert.AreEqual("System.Windows.Forms.Cursors", typeResolutionService.LastTypeNameResolved); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,53 @@
@@ -0,0 +1,53 @@
|
||||
// <file>
|
||||
// <copyright see="prj:///doc/copyright.txt"/>
|
||||
// <license see="prj:///doc/license.txt"/>
|
||||
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
|
||||
// <version>$Revision$</version>
|
||||
// </file>
|
||||
|
||||
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 GenerateCursorFormTestFixture |
||||
{ |
||||
string generatedPythonCode; |
||||
|
||||
[TestFixtureSetUp] |
||||
public void SetUpFixture() |
||||
{ |
||||
using (Form form = new Form()) { |
||||
form.Name = "MainForm"; |
||||
form.ClientSize = new Size(284, 264); |
||||
form.Cursor = Cursors.Help; |
||||
|
||||
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.ClientSize = System.Drawing.Size(284, 264)\r\n" + |
||||
" self.Cursor = System.Windows.Forms.Cursors.Help\r\n" + |
||||
" self.Name = \"MainForm\"\r\n" + |
||||
" self.ResumeLayout(False)\r\n" + |
||||
" self.PerformLayout()\r\n"; |
||||
|
||||
Assert.AreEqual(expectedCode, generatedPythonCode); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,53 @@
@@ -0,0 +1,53 @@
|
||||
// <file>
|
||||
// <copyright see="prj:///doc/copyright.txt"/>
|
||||
// <license see="prj:///doc/license.txt"/>
|
||||
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
|
||||
// <version>$Revision$</version>
|
||||
// </file>
|
||||
|
||||
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 LoadCursorTestFixture : 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.Cursor = System.Windows.Forms.Cursors.Hand\r\n" + |
||||
" self.Name = \"TestForm\"\r\n" + |
||||
" self.ResumeLayout(False)\r\n"; |
||||
Form form; |
||||
|
||||
[TestFixtureSetUp] |
||||
public void SetUpFixture() |
||||
{ |
||||
PythonFormWalker walker = new PythonFormWalker(this, new MockDesignerLoaderHost()); |
||||
form = walker.CreateForm(pythonCode); |
||||
} |
||||
|
||||
[TestFixtureTearDown] |
||||
public void TearDownFixture() |
||||
{ |
||||
form.Dispose(); |
||||
} |
||||
|
||||
[Test] |
||||
public void FormCursorIsHand() |
||||
{ |
||||
Assert.AreEqual(Cursors.Hand, form.Cursor); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,58 @@
@@ -0,0 +1,58 @@
|
||||
// <file>
|
||||
// <copyright see="prj:///doc/copyright.txt"/>
|
||||
// <license see="prj:///doc/license.txt"/>
|
||||
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
|
||||
// <version>$Revision$</version>
|
||||
// </file>
|
||||
|
||||
using System; |
||||
using ICSharpCode.PythonBinding; |
||||
using IronPython.Compiler.Ast; |
||||
using NUnit.Framework; |
||||
using PythonBinding.Tests.Utils; |
||||
|
||||
namespace PythonBinding.Tests.Designer |
||||
{ |
||||
[TestFixture] |
||||
public class PythonCodeDeserializerTests |
||||
{ |
||||
PythonCodeDeserializer deserializer; |
||||
|
||||
[TestFixtureSetUp] |
||||
public void SetUpFixture() |
||||
{ |
||||
deserializer = new PythonCodeDeserializer(new MockDesignerLoaderHost()); |
||||
} |
||||
|
||||
[Test] |
||||
[ExpectedException(typeof(ArgumentNullException))] |
||||
public void NullIronPythonAstNode() |
||||
{ |
||||
deserializer.Deserialize(null); |
||||
} |
||||
|
||||
[Test] |
||||
public void UnknownTypeName() |
||||
{ |
||||
string pythonCode = "self.Cursors = System.Windows.Forms.UnknownType.AppStarting"; |
||||
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)); |
||||
} |
||||
|
||||
[Test] |
||||
public void UnknownPropertyName() |
||||
{ |
||||
string pythonCode = "self.Cursors = System.Windows.Forms.Cursors.UnknownCursorsProperty"; |
||||
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)); |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue