Browse Source

Python code converter now adds code to call the project's main entry method.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@4257 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Matt Ward 17 years ago
parent
commit
7319925fef
  1. 3
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/ConvertProjectToPythonProjectCommand.cs
  2. 19
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/NRefactoryToPythonConverter.cs
  3. 5
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/ConvertToPythonProjectCommandTestFixture.cs
  4. 46
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/GenerateMainMethodCallTestFixture.cs
  5. 6
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/StaticMethodConversionTestFixture.cs
  6. 1
      src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj

3
src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/ConvertProjectToPythonProjectCommand.cs

@ -67,6 +67,9 @@ namespace ICSharpCode.PythonBinding @@ -67,6 +67,9 @@ namespace ICSharpCode.PythonBinding
PythonProject pythonTargetProject = (PythonProject)targetItem.Project;
if ((converter.EntryPointMethods.Count > 0) && !pythonTargetProject.HasMainFile) {
pythonTargetProject.AddMainFile(targetItem.Include);
// Add code to call main method at the end of the file.
pythonCode += "\r\n\r\n" + converter.GenerateMainMethodCall(converter.EntryPointMethods[0]);
}
SaveFile(targetItem.FileName, pythonCode, textEditorProperties.Encoding);

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

@ -141,6 +141,25 @@ namespace ICSharpCode.PythonBinding @@ -141,6 +141,25 @@ namespace ICSharpCode.PythonBinding
public ReadOnlyCollection<MethodDeclaration> EntryPointMethods {
get { return entryPointMethods.AsReadOnly(); }
}
/// <summary>
/// Generates code to call the main entry point.
/// </summary>
public string GenerateMainMethodCall(MethodDeclaration methodDeclaration)
{
TypeDeclaration typeDeclaration = methodDeclaration.Parent as TypeDeclaration;
StringBuilder code = new StringBuilder();
code.Append(typeDeclaration.Name);
code.Append('.');
code.Append(methodDeclaration.Name);
code.Append('(');
if (methodDeclaration.Parameters.Count > 0) {
code.Append("None");
}
code.Append(')');
return code.ToString();
}
/// <summary>
/// Converts from the NRefactory's binary operator type to a string.

5
src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/ConvertToPythonProjectCommandTestFixture.cs

@ -110,7 +110,10 @@ namespace PythonBinding.Tests.Converter @@ -110,7 +110,10 @@ namespace PythonBinding.Tests.Converter
public void ExpectedCodeWrittenToFile()
{
NRefactoryToPythonConverter converter = new NRefactoryToPythonConverter();
string expectedCode = converter.Convert(sourceCode, SupportedLanguage.CSharp);
string expectedCode = converter.Convert(sourceCode, SupportedLanguage.CSharp) +
"\r\n" +
"\r\n" +
converter.GenerateMainMethodCall(converter.EntryPointMethods[0]);
List<ConvertedFile> expectedSavedFiles = new List<ConvertedFile>();
expectedSavedFiles.Add(new ConvertedFile(target.FileName, expectedCode, mockTextEditorProperties.Encoding));

46
src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/GenerateMainMethodCallTestFixture.cs

@ -0,0 +1,46 @@ @@ -0,0 +1,46 @@
// <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
{
/// <summary>
/// Tests the generation of code to call the class's Main method.
/// </summary>
[TestFixture]
public class GenerateMainMethodCallTestFixture
{
string mainMethodWithNoParametersCode = "class Foo\r\n" +
"{\r\n" +
" static void Main()\r\n" +
" {\r\n" +
" }\r\n" +
"\r\n" +
" static void Stop()\r\n" +
" {\r\n" +
" }\r\n" +
"\r\n" +
" public void Run()\r\n" +
" {\r\n" +
" }\r\n" +
"}";
[Test]
public void GeneratedMainMethodCallWithNoParametersCode()
{
NRefactoryToPythonConverter converter = new NRefactoryToPythonConverter(SupportedLanguage.CSharp);
converter.IndentString = " ";
converter.Convert(mainMethodWithNoParametersCode);
string code = converter.GenerateMainMethodCall(converter.EntryPointMethods[0]);
Assert.AreEqual("Foo.Main()", code);
}
}
}

6
src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/StaticMethodConversionTestFixture.cs

@ -76,5 +76,11 @@ namespace PythonBinding.Tests.Converter @@ -76,5 +76,11 @@ namespace PythonBinding.Tests.Converter
{
Assert.AreEqual("Main", converter.EntryPointMethods[0].Name);
}
[Test]
public void GenerateCodeToCallMainMethod()
{
Assert.AreEqual("Foo.Main(None)", converter.GenerateMainMethodCall(converter.EntryPointMethods[0]));
}
}
}

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

@ -106,6 +106,7 @@ @@ -106,6 +106,7 @@
<Compile Include="Converter\ConvertToPythonProjectCommandTestFixture.cs" />
<Compile Include="Converter\EventHandlerWithObjectCreationTestFixture.cs" />
<Compile Include="Converter\ForeachConversionWithMethodCallTestFixture.cs" />
<Compile Include="Converter\GenerateMainMethodCallTestFixture.cs" />
<Compile Include="Converter\GenericListConversionTestFixture.cs" />
<Compile Include="Converter\IntegerMethodParameterTestFixture.cs" />
<Compile Include="Converter\BaseClassReferenceTestFixture.cs" />

Loading…
Cancel
Save