Browse Source
git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@2937 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61shortcuts
5 changed files with 230 additions and 27 deletions
@ -0,0 +1,79 @@ |
|||||||
|
// <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.CodeDom; |
||||||
|
using System.CodeDom.Compiler; |
||||||
|
using ICSharpCode.PythonBinding; |
||||||
|
using NUnit.Framework; |
||||||
|
|
||||||
|
namespace PythonBinding.Tests.Converter |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// Tests that an array cast is correctly converted to Python.
|
||||||
|
/// </summary>
|
||||||
|
[TestFixture] |
||||||
|
public class ArrayCastConversionTestFixture |
||||||
|
{ |
||||||
|
string csharp = "class Foo\r\n" + |
||||||
|
"{\r\n" + |
||||||
|
" public void Assign()\r\n" + |
||||||
|
" {\r\n" + |
||||||
|
" int[] elements = new int[10];\r\n" + |
||||||
|
" List<int[]> list = new List<int[]>();\r\n" + |
||||||
|
" list.Add((int[])elements.Clone());\r\n" + |
||||||
|
" }\r\n" + |
||||||
|
"}"; |
||||||
|
|
||||||
|
CodeExpression expression; |
||||||
|
|
||||||
|
[TestFixtureSetUp] |
||||||
|
public void SetUpFixture() |
||||||
|
{ |
||||||
|
CSharpToPythonConverter converter = new CSharpToPythonConverter(); |
||||||
|
CodeCompileUnit unit = converter.ConvertToCodeCompileUnit(csharp); |
||||||
|
if (unit.Namespaces.Count > 0) { |
||||||
|
CodeNamespace ns = unit.Namespaces[0]; |
||||||
|
if (ns.Types.Count > 0) { |
||||||
|
CodeTypeDeclaration type = ns.Types[0]; |
||||||
|
if (type.Members.Count > 0) { |
||||||
|
CodeMemberMethod method = type.Members[0] as CodeMemberMethod; |
||||||
|
if (method != null && method.Statements.Count > 0) { |
||||||
|
CodeExpressionStatement statement = method.Statements[method.Statements.Count - 1] as CodeExpressionStatement; |
||||||
|
if (statement != null) { |
||||||
|
CodeMethodInvokeExpression methodInvokeExpression = statement.Expression as CodeMethodInvokeExpression; |
||||||
|
if (methodInvokeExpression != null && methodInvokeExpression.Parameters.Count > 0) { |
||||||
|
expression = methodInvokeExpression.Parameters[0]; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
[Test] |
||||||
|
public void CodeExpressionExists() |
||||||
|
{ |
||||||
|
Assert.IsNotNull(expression); |
||||||
|
} |
||||||
|
|
||||||
|
[Test] |
||||||
|
public void GeneratedPythonSourceCode() |
||||||
|
{ |
||||||
|
CSharpToPythonConverter converter = new CSharpToPythonConverter(); |
||||||
|
string python = converter.Convert(csharp); |
||||||
|
string expectedPython = "class Foo(object):\r\n" + |
||||||
|
"\tdef Assign(self):\r\n" + |
||||||
|
"\t\telements = System.Array.CreateInstance(int,0)\r\n" + |
||||||
|
"\t\tlist = List()\r\n" + |
||||||
|
"\t\tlist.Add(elements.Clone())"; |
||||||
|
|
||||||
|
Assert.AreEqual(expectedPython, python); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,89 @@ |
|||||||
|
// <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.CodeDom; |
||||||
|
using System.CodeDom.Compiler; |
||||||
|
using ICSharpCode.PythonBinding; |
||||||
|
using NUnit.Framework; |
||||||
|
|
||||||
|
namespace PythonBinding.Tests.Converter |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// Tests that a method call is converted correctly.
|
||||||
|
/// </summary>
|
||||||
|
[TestFixture] |
||||||
|
public class IntegerMethodParameterTestFixture |
||||||
|
{ |
||||||
|
string csharp = "class Foo\r\n" + |
||||||
|
"{\r\n" + |
||||||
|
" public void Print()\r\n" + |
||||||
|
" {\r\n" + |
||||||
|
" int i = 0;\r\n" + |
||||||
|
" PrintInt(i);\r\n" + |
||||||
|
" }\r\n" + |
||||||
|
"}"; |
||||||
|
|
||||||
|
CodeMethodReferenceExpression methodRefExpression; |
||||||
|
|
||||||
|
[TestFixtureSetUp] |
||||||
|
public void SetUpFixture() |
||||||
|
{ |
||||||
|
CSharpToPythonConverter converter = new CSharpToPythonConverter(); |
||||||
|
CodeCompileUnit unit = converter.ConvertToCodeCompileUnit(csharp); |
||||||
|
if (unit.Namespaces.Count > 0) { |
||||||
|
CodeNamespace ns = unit.Namespaces[0]; |
||||||
|
if (ns.Types.Count > 0) { |
||||||
|
CodeTypeDeclaration type = ns.Types[0]; |
||||||
|
if (type.Members.Count > 0) { |
||||||
|
CodeMemberMethod method = type.Members[0] as CodeMemberMethod; |
||||||
|
if (method != null && method.Statements.Count > 0) { |
||||||
|
CodeExpressionStatement statement = method.Statements[method.Statements.Count - 1] as CodeExpressionStatement; |
||||||
|
if (statement != null) { |
||||||
|
CodeMethodInvokeExpression methodInvokeExpression = statement.Expression as CodeMethodInvokeExpression; |
||||||
|
if (methodInvokeExpression != null) { |
||||||
|
methodRefExpression = methodInvokeExpression.Method; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
[Test] |
||||||
|
public void MethodRefExpressionExists() |
||||||
|
{ |
||||||
|
Assert.IsNotNull(methodRefExpression); |
||||||
|
} |
||||||
|
|
||||||
|
[Test] |
||||||
|
public void MethodNameIsPrintInt() |
||||||
|
{ |
||||||
|
Assert.AreEqual("PrintInt", methodRefExpression.MethodName); |
||||||
|
} |
||||||
|
|
||||||
|
[Test] |
||||||
|
public void MethodRefTargetObjectIsThisRef() |
||||||
|
{ |
||||||
|
Assert.IsInstanceOfType(typeof(CodeThisReferenceExpression), methodRefExpression.TargetObject); |
||||||
|
} |
||||||
|
|
||||||
|
[Test] |
||||||
|
public void GeneratedPythonSourceCode() |
||||||
|
{ |
||||||
|
CSharpToPythonConverter converter = new CSharpToPythonConverter(); |
||||||
|
string python = converter.Convert(csharp); |
||||||
|
string expectedPython = "class Foo(object):\r\n" + |
||||||
|
"\tdef Print(self):\r\n" + |
||||||
|
"\t\ti = 0\r\n" + |
||||||
|
"\t\tself.PrintInt(i)"; |
||||||
|
|
||||||
|
Assert.AreEqual(expectedPython, python); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue