Esempio n. 1
0
        public void ConvertedPythonCode()
        {
            NRefactoryToPythonConverter converter = new NRefactoryToPythonConverter(SupportedLanguage.CSharp);
            string python         = converter.Convert(csharp);
            string expectedPython = "class Foo(object):\r\n" +
                                    "\tdef __init__(self):\r\n" +
                                    "\t\tself._i = 0\r\n" +
                                    "\r\n" +
                                    "\tdef GetCount(self):\r\n" +
                                    "\t\tif self._i == 0:\r\n" +
                                    "\t\t\tif self._i == 0:\r\n" +
                                    "\t\t\t\tself._i = 10\r\n" +
                                    "\t\t\t\treturn 10\r\n" +
                                    "\t\t\telse:\r\n" +
                                    "\t\t\t\tself._i = 4\r\n" +
                                    "\t\t\t\treturn 4\r\n" +
                                    "\t\telse:\r\n" +
                                    "\t\t\tif self._i == 0:\r\n" +
                                    "\t\t\t\tself._i = 10\r\n" +
                                    "\t\t\t\treturn 10\r\n" +
                                    "\t\t\telse:\r\n" +
                                    "\t\t\t\tself._i = 4\r\n" +
                                    "\t\t\t\treturn 4";

            Assert.AreEqual(expectedPython, python);
        }
Esempio n. 2
0
        public void ConvertedPythonCode()
        {
            NRefactoryToPythonConverter converter = new NRefactoryToPythonConverter(SupportedLanguage.CSharp);

            converter.IndentString = "    ";
            string python         = converter.Convert(csharp);
            string expectedPython = "class Foo(object):\r\n" +
                                    "    \"\"\" <summary>\r\n" +
                                    "     Class Foo\r\n" +
                                    "     </summary>\r\n" +
                                    "    \"\"\"\r\n" +
                                    "    def Run(self):\r\n" +
                                    "        \"\"\" <summary>\r\n" +
                                    "         Run\r\n" +
                                    "         </summary>\r\n" +
                                    "        \"\"\"\r\n" +
                                    "        pass\r\n" +
                                    "\r\n" +
                                    "    def Stop(self):\r\n" +
                                    "        \"\"\" <summary> Stop </summary>\"\"\"\r\n" +
                                    "        pass\r\n" +
                                    "\r\n" +
                                    "    def __init__(self):\r\n" +
                                    "        \"\"\" <summary> Initialize.</summary>\"\"\"\r\n" +
                                    "        # Initialize j.\r\n" +
                                    "        j = 0 # Set to zero\r\n" +
                                    "        # test\r\n" +
                                    "        if j == 0:\r\n" +
                                    "            j = 2";

            Assert.AreEqual(expectedPython, python, python);
        }
        public void TabIndent()
        {
            MockTextEditorProperties properties = new MockTextEditorProperties();

            properties.ConvertTabsToSpaces = false;
            Assert.AreEqual("\t", NRefactoryToPythonConverter.GetIndentString(properties));
        }
        /// <summary>
        /// Converts C# and VB.NET files to Python and saves the files.
        /// </summary>
        protected override void ConvertFile(FileProjectItem sourceItem, FileProjectItem targetItem)
        {
            NRefactoryToPythonConverter converter = NRefactoryToPythonConverter.Create(sourceItem.Include);

            if (converter != null)
            {
                targetItem.Include = ChangeFileExtensionToPythonFileExtension(sourceItem.Include);

                string code       = GetParseableFileContent(sourceItem.FileName);
                string pythonCode = converter.Convert(code);

                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, GetDefaultFileEncoding());
            }
            else
            {
                LanguageConverterConvertFile(sourceItem, targetItem);
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Merges the generated code into the specified document.
        /// </summary>
        /// <param name="component">The designer host.</param>
        /// <param name="document">The document that the generated code will be merged into.</param>
        /// <param name="parseInfo">The current compilation unit for the <paramref name="document"/>.</param>
        public static void Merge(IDesignerHost host, IDocument document, ICompilationUnit compilationUnit, ITextEditorProperties textEditorProperties, IDesignerSerializationManager serializationManager)
        {
            // Get the document's initialize components method.
            IMethod method = GetInitializeComponents(compilationUnit);

            // Generate the python source code.
            PythonCodeDomSerializer serializer = new PythonCodeDomSerializer(NRefactoryToPythonConverter.GetIndentString(textEditorProperties));
            int indent = method.Region.BeginColumn;

            if (textEditorProperties.ConvertTabsToSpaces)
            {
                indent = (indent / textEditorProperties.IndentationSize);
                if (textEditorProperties.IndentationSize > 1)
                {
                    indent += 1;
                }
            }
            string rootNamespace = GetProjectRootNamespace(compilationUnit);
            string methodBody    = serializer.GenerateInitializeComponentMethodBody(host, serializationManager, rootNamespace, indent);

            // Merge the code.
            DomRegion methodRegion = GetBodyRegionInDocument(method);
            int       startOffset  = GetStartOffset(document, methodRegion);
            int       endOffset    = GetEndOffset(document, methodRegion);

            document.Replace(startOffset, endOffset - startOffset, methodBody);
        }
        public void ConvertedPythonCode()
        {
            NRefactoryToPythonConverter converter = new NRefactoryToPythonConverter(SupportedLanguage.CSharp);
            string python         = converter.Convert(csharp);
            string expectedPython = "class Foo(object):\r\n" +
                                    "\tdef __init__(self):\r\n" +
                                    "\t\tself._count = 0\r\n" +
                                    "\t\tself._i = 0\r\n" +
                                    "\r\n" +
                                    "\tdef get_Count(self):\r\n" +
                                    "\t\tif self._i == 0:\r\n" +
                                    "\t\t\treturn 10\r\n" +
                                    "\t\telse:\r\n" +
                                    "\t\t\treturn self._count\r\n" +
                                    "\r\n" +
                                    "\tdef set_Count(self, value):\r\n" +
                                    "\t\tif self._i == 1:\r\n" +
                                    "\t\t\tself._count = value\r\n" +
                                    "\t\telse:\r\n" +
                                    "\t\t\tself._count = value + 5\r\n" +
                                    "\r\n" +
                                    "\tCount = property(fget=get_Count, fset=set_Count)";

            Assert.AreEqual(expectedPython, python);
        }
        string GeneratePythonCode()
        {
            NRefactoryToPythonConverter converter = NRefactoryToPythonConverter.Create(view.PrimaryFileName);

            converter.IndentString = view.TextEditorOptions.IndentationString;
            return(converter.Convert(view.EditableView.Text));
        }
        public void CaseFallThrough()
        {
            string csharp = "class Foo\r\n" +
                            "{\r\n" +
                            "    public int Run(int i)\r\n" +
                            "    {\r\n" +
                            "        switch (i) {\r\n" +
                            "            case 10:\r\n" +
                            "            case 11:\r\n" +
                            "                return 0;\r\n" +
                            "            case 9:\r\n" +
                            "                return 2;\r\n" +
                            "            default:\r\n" +
                            "                return -1;\r\n" +
                            "        }\r\n" +
                            "    }\r\n" +
                            "}";

            string expectedPython = "class Foo(object):\r\n" +
                                    "  def Run(self, i):\r\n" +
                                    "    if i == 10 or i == 11:\r\n" +
                                    "      return 0\r\n" +
                                    "    elif i == 9:\r\n" +
                                    "      return 2\r\n" +
                                    "    else:\r\n" +
                                    "      return -1";

            NRefactoryToPythonConverter converter = new NRefactoryToPythonConverter(SupportedLanguage.CSharp);

            converter.IndentString = "  ";
            string code = converter.Convert(csharp);

            Assert.AreEqual(expectedPython, code);
        }
        public void ConvertedPythonCode()
        {
            NRefactoryToPythonConverter converter = new NRefactoryToPythonConverter(SupportedLanguage.CSharp);
            string python         = converter.Convert(csharp);
            string expectedPython = "class Class1(object):\r\n" +
                                    "\tdef __init__(self):\r\n" +
                                    "\t\tself._name = String.Empty\r\n" +
                                    "\t\tself._lastName = String.Empty\r\n" +
                                    "\r\n" +
                                    "\tdef get_Name(self):\r\n" +
                                    "\t\treturn self._name\r\n" +
                                    "\r\n" +
                                    "\tdef set_Name(self, value):\r\n" +
                                    "\t\tself._name = value\r\n" +
                                    "\r\n" +
                                    "\tName = property(fget=get_Name, fset=set_Name)\r\n" +
                                    "\r\n" +
                                    "\tdef get_LastName(self):\r\n" +
                                    "\t\treturn self._lastName\r\n" +
                                    "\r\n" +
                                    "\tdef set_LastName(self, value):\r\n" +
                                    "\t\tself._lastName = value\r\n" +
                                    "\r\n" +
                                    "\tLastName = property(fget=get_LastName, fset=set_LastName)\r\n" +
                                    "\r\n" +
                                    "\tdef Clone(self):\r\n" +
                                    "\t\treturn Class1(Name = \"First\", LastName = \"Last\")";

            Assert.AreEqual(expectedPython, python);
        }
        public void ExclusiveOrOperator()
        {
            NRefactoryToPythonConverter converter = new NRefactoryToPythonConverter(SupportedLanguage.CSharp);
            string python         = converter.Convert(GetCode(csharp, "^="));
            string expectedPython = GetCode(pythonCodeTemplate, "^=");

            Assert.AreEqual(expectedPython, python);
        }
        public void VBPowerOperator()
        {
            NRefactoryToPythonConverter converter = new NRefactoryToPythonConverter(SupportedLanguage.VBNet);
            string python         = converter.Convert(GetCode(vb, "^="));
            string expectedPython = GetCode(pythonCodeTemplate, "**=");

            Assert.AreEqual(expectedPython, python);
        }
Esempio n. 12
0
        public void GeneratedPythonSourceCode()
        {
            NRefactoryToPythonConverter converter = new NRefactoryToPythonConverter(SupportedLanguage.CSharp);
            string python         = converter.Convert(csharp);
            string expectedPython = "class Foo(object):\r\n\tpass";

            Assert.AreEqual(expectedPython, python);
        }
        public void TwoChaSpaceIndent()
        {
            MockTextEditorProperties properties = new MockTextEditorProperties();

            properties.ConvertTabsToSpaces = true;
            properties.IndentationSize     = 2;
            Assert.AreEqual("  ", NRefactoryToPythonConverter.GetIndentString(properties));
        }
Esempio n. 14
0
        /// <summary>
        /// Converts the C# Source into Python
        /// </summary>
        /// <param Name="sauce">Source code written by $user</param>
        /// <param Name="refs">References specified by $user</param>
        /// <param Name="name">$Name of the script</param>
        /// <returns>Source code in Python format</returns>
        public static String Compile(FileInfo sauce, string[] refs, string name)
        {
            NRefactoryToPythonConverter indentString = NRefactoryToPythonConverter.Create(sauce.FullName);

            indentString.IndentString = new string(' ', 2);
            string str = "";

            using (var tr = sauce.OpenText())
            {
                str = indentString.Convert(tr.ReadToEnd());
            }
            return(str);

            #region alternative[OBSOLETE]

            /*
             * String refss = "";
             * foreach (var @ref in refs)
             * {
             *  refss += ":\"" + @ref + "\"";
             * }
             *
             * String args = Application.dataPath + "/Mods/Scripts/Resource/cs-script/cscs.exe /cd /r" + refss + sauce.FullName;
             * if (Application.platform.Equals(RuntimePlatform.WindowsPlayer) ||
             *  Application.platform.Equals(RuntimePlatform.WindowsEditor))
             * {
             *  var process = new Process();
             *  var startInfo = new ProcessStartInfo
             *  {
             *      WindowStyle = ProcessWindowStyle.Hidden,
             *      FileName = "cmd.exe",
             *      Arguments = args
             *  };
             *  process.StartInfo = startInfo;
             *  process.Start();
             *  process.WaitForExit();
             * }
             * else if (Application.platform.Equals(RuntimePlatform.LinuxPlayer) ||
             *       Application.platform.Equals(RuntimePlatform.OSXEditor) ||
             *       Application.platform.Equals(RuntimePlatform.OSXPlayer))
             * {
             *  var psi = new ProcessStartInfo
             *  {
             *      WindowStyle = ProcessWindowStyle.Hidden,
             *      FileName = "/bin/bash.sh",
             *      UseShellExecute = false,
             *      RedirectStandardOutput = true,
             *      Arguments = args
             *  };
             *
             *  var p = Process.Start(psi);
             *  p.WaitForExit();
             * }
             */

            #endregion
        }
        public void GeneratedPythonSourceCode()
        {
            NRefactoryToPythonConverter converter = new NRefactoryToPythonConverter(SupportedLanguage.VBNet);
            string python         = converter.Convert(vb);
            string expectedPython = "class Class1(object):\r\n" +
                                    "\tpass";

            Assert.AreEqual(expectedPython, python);
        }
        public void ConvertedPythonCode()
        {
            string expectedCode = "class Foo(object):\r\n" +
                                  "\tdef Run(self):\r\n" +
                                  "\t\treturn self.ToString()";
            NRefactoryToPythonConverter converter = new NRefactoryToPythonConverter(SupportedLanguage.CSharp);
            string code = converter.Convert(csharp);

            Assert.AreEqual(expectedCode, code);
        }
        public void ConvertedPythonCode()
        {
            NRefactoryToPythonConverter converter = new NRefactoryToPythonConverter(SupportedLanguage.CSharp);
            string python         = converter.Convert(csharp);
            string expectedPython = "class Foo(object):\r\n" +
                                    "\tdef __init__(self):\r\n" +
                                    "\t\tself._i = 0";

            Assert.AreEqual(expectedPython, python);
        }
Esempio n. 18
0
 /// <summary>
 /// Converts the C# code to a code dom using the NRefactory
 /// library and then visits the code dom.
 /// </summary>
 void RunCSharpToPythonClick(object sender, EventArgs e)
 {
     try {
         Clear();
         NRefactoryToPythonConverter converter = new NRefactoryToPythonConverter(NRefactory.SupportedLanguage.CSharp);
         walkerOutputTextBox.Text = converter.Convert(codeTextBox.Text);
     } catch (Exception ex) {
         walkerOutputTextBox.Text = ex.ToString();
     }
 }
Esempio n. 19
0
        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);
        }
        public void ConvertedPythonCode()
        {
            NRefactoryToPythonConverter converter = new NRefactoryToPythonConverter(SupportedLanguage.CSharp);
            string python         = converter.Convert(csharp);
            string expectedPython = "class Foo(object):\r\n" +
                                    "\tdef Convert(self):\r\n" +
                                    "\t\ta = (b >> 16) & 0xffff\r\n" +
                                    "\t\treturn a";

            Assert.AreEqual(expectedPython, python);
        }
        public void ConvertedPythonCodeUsingFourSpacesAsIndent()
        {
            NRefactoryToPythonConverter converter = new NRefactoryToPythonConverter(SupportedLanguage.CSharp);

            converter.IndentString = new String(' ', 4);
            string python         = converter.Convert(csharp);
            string expectedPython = "class Foo(object):\r\n" +
                                    "    pass";

            Assert.AreEqual(expectedPython, python);
        }
        public void ConvertedPythonCode()
        {
            NRefactoryToPythonConverter converter = new NRefactoryToPythonConverter(SupportedLanguage.CSharp);
            string python         = converter.Convert(csharp);
            string expectedPython = "class Foo(object):\r\n" +
                                    "\tdef TestMe(self, test):\r\n" +
                                    "\t\ta = \"Ape\" if test else \"Monkey\"\r\n" +
                                    "\t\treturn a";

            Assert.AreEqual(expectedPython, python);
        }
        public void GeneratedPythonSourceCode()
        {
            NRefactoryToPythonConverter converter = new NRefactoryToPythonConverter(SupportedLanguage.CSharp);
            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);
        }
Esempio n. 24
0
        public static bool Convert(SupportedLanguage inputLanguage, string ProvidedSource, out string ConvertedSource, out string ErrorMessage)
        {
            NRefactoryToPythonConverter converter = new
                                                    NRefactoryToPythonConverter(inputLanguage);

            string convertedCode = converter.Convert(ProvidedSource);

            ConvertedSource = convertedCode;
            ErrorMessage    = "";

            return(true);
        }
Esempio n. 25
0
        public void ConvertedPythonCode()
        {
            NRefactoryToPythonConverter converter = new NRefactoryToPythonConverter(SupportedLanguage.CSharp);
            string python         = converter.Convert(csharp);
            string expectedPython = "class Foo(object):\r\n" +
                                    "\tdef CountDown(self):\r\n" +
                                    "\t\ti = 10\r\n" +
                                    "\t\twhile i > 0:\r\n" +
                                    "\t\t\ti -= 1";

            Assert.AreEqual(expectedPython, python);
        }
Esempio n. 26
0
        public void ConvertedPythonCode()
        {
            NRefactoryToPythonConverter converter = new NRefactoryToPythonConverter(SupportedLanguage.CSharp);

            converter.IndentString = "    ";
            string python         = converter.Convert(csharp);
            string expectedPython = "class Foo(object):\r\n" +
                                    "    def IsEqual(self, o):\r\n" +
                                    "        return Object.ReferenceEquals(o, None)";

            Assert.AreEqual(expectedPython, python, python);
        }
Esempio n. 27
0
        public void ConvertedPythonCode()
        {
            string expectedCode =
                "class Foo(Bar, IMyInterface):\r\n" +
                "    pass";
            NRefactoryToPythonConverter converter = new NRefactoryToPythonConverter(SupportedLanguage.CSharp);

            converter.IndentString = "    ";
            string code = converter.Convert(csharp);

            Assert.AreEqual(expectedCode, code);
        }
        public void ConvertedPythonCode()
        {
            NRefactoryToPythonConverter converter = new NRefactoryToPythonConverter(SupportedLanguage.CSharp);
            string python         = converter.Convert(csharp);
            string expectedPython = "class Foo(object):\r\n" +
                                    "\tdef Run(self, a):\r\n" +
                                    "\t\tif a == None:\r\n" +
                                    "\t\t\treturn 4\r\n" +
                                    "\t\treturn 2";

            Assert.AreEqual(expectedPython, python);
        }
        public void ConvertedPythonCode()
        {
            string expectedPython = "class Foo(object):\r\n" +
                                    "\tdef __init__(self):\r\n" +
                                    "\t\tdoc = XmlDocument()\r\n" +
                                    "\t\tdoc.LoadXml(\"<root/>\")";

            NRefactoryToPythonConverter converter = new NRefactoryToPythonConverter(SupportedLanguage.CSharp);
            string python = converter.Convert(csharp);

            Assert.AreEqual(expectedPython, python);
        }
        public void ConvertCSharpClassWithFieldsWhereFirstFieldDoesNotHaveInitialValue()
        {
            NRefactoryToPythonConverter converter = new NRefactoryToPythonConverter(SupportedLanguage.CSharp);

            converter.IndentString = "    ";
            string python         = converter.Convert(csharpClassWithTwoFieldsWhereFirstDoesNotHaveInitialValue);
            string expectedPython =
                "class Foo(object):\r\n" +
                "    def __init__(self):\r\n" +
                "        self._j = 1";

            Assert.AreEqual(expectedPython, python);
        }