Browse Source

Convert System.String and System.Int32 to 'str' and 'int' in python.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@4254 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Matt Ward 16 years ago
parent
commit
eb5ba0db01
  1. 22
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/NRefactoryToPythonConverter.cs
  2. 4
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/ArrayCastConversionTestFixture.cs
  3. 152
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/ArrayConversionTestFixture.cs
  4. 32
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/ForeachConversionWithMethodCallTestFixture.cs
  5. 4
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/GenericListConversionTestFixture.cs
  6. 40
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/TypeofConversionTestFixture.cs
  7. 1
      src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj

22
src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/NRefactoryToPythonConverter.cs

@ -210,7 +210,7 @@ namespace ICSharpCode.PythonBinding @@ -210,7 +210,7 @@ namespace ICSharpCode.PythonBinding
public object VisitArrayCreateExpression(ArrayCreateExpression arrayCreateExpression, object data)
{
string arrayType = arrayCreateExpression.CreateType.Type;
string arrayType = GetTypeName(arrayCreateExpression.CreateType);
if (arrayCreateExpression.ArrayInitializer.CreateExpressions.Count == 0) {
Append("System.Array.CreateInstance(" + arrayType);
if (arrayCreateExpression.Arguments.Count > 0) {
@ -1095,7 +1095,7 @@ namespace ICSharpCode.PythonBinding @@ -1095,7 +1095,7 @@ namespace ICSharpCode.PythonBinding
public object VisitTypeOfExpression(TypeOfExpression typeOfExpression, object data)
{
Append("clr.GetClrType(");
Append(typeOfExpression.TypeReference.Type);
Append(GetTypeName(typeOfExpression.TypeReference));
Append(")");
return null;
}
@ -1735,9 +1735,9 @@ namespace ICSharpCode.PythonBinding @@ -1735,9 +1735,9 @@ namespace ICSharpCode.PythonBinding
}
TypeReference typeRef = typeRefs[i];
if (typeRef.IsArrayType) {
Append("System.Array[" + typeRef.Type + "]");
Append("System.Array[" + GetTypeName(typeRef) + "]");
} else {
Append(typeRef.Type);
Append(GetTypeName(typeRef));
}
}
Append("]");
@ -1749,5 +1749,19 @@ namespace ICSharpCode.PythonBinding @@ -1749,5 +1749,19 @@ namespace ICSharpCode.PythonBinding
codeBuilder.AppendLine();
}
}
/// <summary>
/// If the type is String or Int32 then it returns "str" and "int".
/// </summary>
string GetTypeName(TypeReference typeRef)
{
string name = typeRef.Type;
if (name == typeof(String).FullName) {
return "str";
} else if ((name == typeof(int).FullName) || ((name == typeof(int).Name))) {
return "int";
}
return name;
}
}
}

4
src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/ArrayCastConversionTestFixture.cs

@ -35,8 +35,8 @@ namespace PythonBinding.Tests.Converter @@ -35,8 +35,8 @@ namespace PythonBinding.Tests.Converter
string python = converter.Convert(csharp);
string expectedPython = "class Foo(object):\r\n" +
"\tdef Assign(self):\r\n" +
"\t\telements = System.Array.CreateInstance(System.Int32, 10)\r\n" +
"\t\tlist = List[System.Array[System.Int32]]()\r\n" +
"\t\telements = System.Array.CreateInstance(int, 10)\r\n" +
"\t\tlist = List[System.Array[int]]()\r\n" +
"\t\tlist.Add(elements.Clone())";
Assert.AreEqual(expectedPython, python);

152
src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/ArrayConversionTestFixture.cs

@ -18,28 +18,154 @@ namespace PythonBinding.Tests.Converter @@ -18,28 +18,154 @@ namespace PythonBinding.Tests.Converter
[TestFixture]
public class ArrayConversionTestFixture
{
string csharp = "class Foo\r\n" +
string integerArray = "class Foo\r\n" +
"{\r\n" +
"\tpublic int[] Run()\r\n" +
"\t{" +
"\t\tint[] i = new int[] {1, 2, 3, 4};\r\n" +
"\t\ti[0] = 5;\r\n" +
"\t\treturn i;\r\n" +
"\t}\r\n" +
" public int[] Run()\r\n" +
" {" +
" int[] i = new int[] {1, 2, 3, 4};\r\n" +
" i[0] = 5;\r\n" +
" return i;\r\n" +
" }\r\n" +
"}";
string int32Array = "class Foo\r\n" +
"{\r\n" +
" public Int32[] Run()\r\n" +
" {" +
" Int32[] i = new Int32[] {1, 2, 3, 4};\r\n" +
" i[0] = 5;\r\n" +
" return i;\r\n" +
" }\r\n" +
"}";
string uintArray = "class Foo\r\n" +
"{\r\n" +
" public uint[] Run()\r\n" +
" {" +
" uint[] i = new uint[] {1, 2, 3, 4};\r\n" +
" i[0] = 5;\r\n" +
" return i;\r\n" +
" }\r\n" +
"}";
string stringArray = "class Foo\r\n" +
"{\r\n" +
" public string[] Run()\r\n" +
" {" +
" string[] i = new string[] {\"a\", \"b\"};\r\n" +
" i[0] = \"c\";\r\n" +
" return i;\r\n" +
" }\r\n" +
"}";
string barArray = "class Foo\r\n" +
"{\r\n" +
" public string[] Run()\r\n" +
" {" +
" Bar[] i = new Bar[] { new Bar(), new Bar()};\r\n" +
" i[0] = new Bar();\r\n" +
" return i;\r\n" +
" }\r\n" +
"}";
string uriArray = "class Foo\r\n" +
"{\r\n" +
" public Uri[] Run()\r\n" +
" {" +
" Uri[] i = new Uri[] {new Uri(\"a\"), new Uri(\"b\")};\r\n" +
" i[0] = new Uri(\"c\");\r\n" +
" return i;\r\n" +
" }\r\n" +
"}";
[Test]
public void ConvertedPythonCode()
public void ConvertedIntegerArrayCode()
{
string expectedCode = "class Foo(object):\r\n" +
"\tdef Run(self):\r\n" +
"\t\ti = System.Array[System.Int32]((1, 2, 3, 4))\r\n" +
"\t\ti[0] = 5\r\n" +
"\t\treturn i";
" def Run(self):\r\n" +
" i = System.Array[int]((1, 2, 3, 4))\r\n" +
" i[0] = 5\r\n" +
" return i";
NRefactoryToPythonConverter converter = new NRefactoryToPythonConverter(SupportedLanguage.CSharp);
string code = converter.Convert(csharp);
converter.IndentString = " ";
string code = converter.Convert(integerArray);
Assert.AreEqual(expectedCode, code);
}
[Test]
public void ConvertedStringArrayCode()
{
string expectedCode = "class Foo(object):\r\n" +
" def Run(self):\r\n" +
" i = System.Array[str]((\"a\", \"b\"))\r\n" +
" i[0] = \"c\"\r\n" +
" return i";
NRefactoryToPythonConverter converter = new NRefactoryToPythonConverter(SupportedLanguage.CSharp);
converter.IndentString = " ";
string code = converter.Convert(stringArray);
Assert.AreEqual(expectedCode, code);
}
[Test]
public void ConvertedBarArrayCode()
{
string expectedCode = "class Foo(object):\r\n" +
" def Run(self):\r\n" +
" i = System.Array[Bar]((Bar(), Bar()))\r\n" +
" i[0] = Bar()\r\n" +
" return i";
NRefactoryToPythonConverter converter = new NRefactoryToPythonConverter(SupportedLanguage.CSharp);
converter.IndentString = " ";
string code = converter.Convert(barArray);
Assert.AreEqual(expectedCode, code);
}
[Test]
public void ConvertedUriArrayCode()
{
string expectedCode = "class Foo(object):\r\n" +
" def Run(self):\r\n" +
" i = System.Array[Uri]((Uri(\"a\"), Uri(\"b\")))\r\n" +
" i[0] = Uri(\"c\")\r\n" +
" return i";
NRefactoryToPythonConverter converter = new NRefactoryToPythonConverter(SupportedLanguage.CSharp);
converter.IndentString = " ";
string code = converter.Convert(uriArray);
Assert.AreEqual(expectedCode, code);
}
[Test]
public void ConvertedUIntArrayCode()
{
string expectedCode = "class Foo(object):\r\n" +
" def Run(self):\r\n" +
" i = System.Array[System.UInt32]((1, 2, 3, 4))\r\n" +
" i[0] = 5\r\n" +
" return i";
NRefactoryToPythonConverter converter = new NRefactoryToPythonConverter(SupportedLanguage.CSharp);
converter.IndentString = " ";
string code = converter.Convert(uintArray);
Assert.AreEqual(expectedCode, code);
}
[Test]
public void ConvertedInt32ArrayCode()
{
string expectedCode = "class Foo(object):\r\n" +
" def Run(self):\r\n" +
" i = System.Array[int]((1, 2, 3, 4))\r\n" +
" i[0] = 5\r\n" +
" return i";
NRefactoryToPythonConverter converter = new NRefactoryToPythonConverter(SupportedLanguage.CSharp);
converter.IndentString = " ";
string code = converter.Convert(int32Array);
Assert.AreEqual(expectedCode, code);
}
}
}

32
src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/ForeachConversionWithMethodCallTestFixture.cs

@ -15,7 +15,7 @@ namespace PythonBinding.Tests.Converter @@ -15,7 +15,7 @@ namespace PythonBinding.Tests.Converter
[TestFixture]
public class ForeachConversionWithMethodCallTestFixture
{
string csharp = "class Foo\r\n" +
string environmentSpecialFolderCode = "class Foo\r\n" +
"{\r\n" +
" public void PrintEnvironmentVariables()\r\n" +
" {\r\n" +
@ -25,13 +25,24 @@ namespace PythonBinding.Tests.Converter @@ -25,13 +25,24 @@ namespace PythonBinding.Tests.Converter
" }\r\n" +
" }\r\n" +
"}";
string intCode = "class Foo\r\n" +
"{\r\n" +
" public void PrintIntegers(int[] items)\r\n" +
" {\r\n" +
" foreach (int i in items)\r\n" +
" {\r\n" +
" Console.WriteLine(i);\r\n" +
" }\r\n" +
" }\r\n" +
"}";
[Test]
public void ConvertedPythonCode()
public void ConvertedEnvironmentSpecialFolderCode()
{
NRefactoryToPythonConverter converter = new NRefactoryToPythonConverter(SupportedLanguage.CSharp);
converter.IndentString = " ";
string code = converter.Convert(csharp);
string code = converter.Convert(environmentSpecialFolderCode);
string expectedCode = "class Foo(object):\r\n" +
" def PrintEnvironmentVariables(self):\r\n" +
" enumerator = Environment.SpecialFolder.GetValues(clr.GetClrType(Environment.SpecialFolder)).GetEnumerator()\r\n" +
@ -40,5 +51,20 @@ namespace PythonBinding.Tests.Converter @@ -40,5 +51,20 @@ namespace PythonBinding.Tests.Converter
" Console.WriteLine(\"{0}={1}\\n\", folder, Environment.GetFolderPath(folder))";
Assert.AreEqual(expectedCode, code);
}
[Test]
public void ConvertedIntCode()
{
NRefactoryToPythonConverter converter = new NRefactoryToPythonConverter(SupportedLanguage.CSharp);
converter.IndentString = " ";
string code = converter.Convert(intCode);
string expectedCode = "class Foo(object):\r\n" +
" def PrintIntegers(self, items):\r\n" +
" enumerator = items.GetEnumerator()\r\n" +
" while enumerator.MoveNext():\r\n" +
" i = enumerator.Current\r\n" +
" Console.WriteLine(i)";
Assert.AreEqual(expectedCode, code);
}
}
}

4
src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/GenericListConversionTestFixture.cs

@ -44,8 +44,8 @@ namespace PythonBinding.Tests.Converter @@ -44,8 +44,8 @@ namespace PythonBinding.Tests.Converter
"\r\n" +
"class Foo(object):\r\n" +
" def __init__(self):\r\n" +
" list = List[System.String]()\r\n" +
" dictionary = Dictionary[System.String, System.Int32]()";
" list = List[str]()\r\n" +
" dictionary = Dictionary[str, int]()";
Assert.AreEqual(expectedPython, python);
}

40
src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/TypeofConversionTestFixture.cs

@ -0,0 +1,40 @@ @@ -0,0 +1,40 @@
// <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.NRefactory;
using ICSharpCode.PythonBinding;
using NUnit.Framework;
namespace PythonBinding.Tests.Converter
{
[TestFixture]
public class TypeofConversionTestFixture
{
string typeofIntCode = "class Foo\r\n" +
"{\r\n" +
" public string ToString()\r\n" +
" {\r\n" +
" typeof(int).FullName;\r\n" +
" }\r\n" +
"}";
[Test]
public void ConvertedTypeOfIntegerCode()
{
NRefactoryToPythonConverter converter = new NRefactoryToPythonConverter(SupportedLanguage.CSharp);
converter.IndentString = " ";
string python = converter.Convert(typeofIntCode);
string expectedPython = "class Foo(object):\r\n" +
" def ToString(self):\r\n" +
" clr.GetClrType(int).FullName";
Assert.AreEqual(expectedPython, python);
}
}
}

1
src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj

@ -148,6 +148,7 @@ @@ -148,6 +148,7 @@
<Compile Include="Converter\TernaryOperatorConversionTestFixture.cs" />
<Compile Include="Converter\ThrowExceptionConversionTestFixture.cs" />
<Compile Include="Converter\TryCatchFinallyConversionTestFixture.cs" />
<Compile Include="Converter\TypeofConversionTestFixture.cs" />
<Compile Include="Converter\UnaryOperatorConversionTests.cs" />
<Compile Include="Converter\UsingStatementConversionTestFixture.cs" />
<Compile Include="Converter\VBClassConversionTestFixture.cs" />

Loading…
Cancel
Save