Exemplo n.º 1
0
        public void CSharpHelperConvertsCSharpTextsToApexDictionary()
        {
            var csharp = @"
                class Test1 { public Test1(int x) { } }
                class Test2 : Test1 { private int x = 10; }
                enum SomeEnum { None, Unknown, Default }";

            var apexClasses = ApexSharpParser.ConvertToApex(csharp);

            Assert.AreEqual(3, apexClasses.Count);

            CompareLineByLine(
                @"class Test1
                {
                    public Test1(Integer x)
                    {
                    }
                }", apexClasses["Test1"]);

            CompareLineByLine(
                @"class Test2 extends Test1
                {
                    private Integer x = 10;
                }", apexClasses["Test2"]);

            CompareLineByLine(
                @"enum SomeEnum
                {
                    None,
                    Unknown,
                    Default
                }", apexClasses["SomeEnum"]);
        }
Exemplo n.º 2
0
        public void CSharpHelperParsesTheCSharpCodeAndReturnsTheSyntaxTree()
        {
            var unit = ApexSharpParser.ParseText(
                @"using System;
                using System.Collections;
                using System.Linq.Think;
                using System.Text;
                using system.debug;

                namespace HelloWorld
                {
                    class Program
                    {
                        static void Main(string[] args)
                        {
                            Console.WriteLine(""Hello, World!"");
                        }
                    }
                }");

            Assert.NotNull(unit);

            var txt = ApexSharpParser.ToCSharp(unit);

            Assert.NotNull(txt);
        }
Exemplo n.º 3
0
        public void SampleWalkerDisplaysTheSyntaxTreeStructure()
        {
            var unit = ApexSharpParser.ParseText(
                @"using System;
                using System.Collections;
                using System.Linq.Think;
                using System.Text;
                using system.debug;

                namespace Demo
                {
                    struct Program
                    {
                        static void Main(string[] args)
                        {
                            Console.WriteLine(""Hello, World!"");
                        }
                    }
                }");

            var walker = new SampleWalker();

            unit.Accept(walker);

            var tree = walker.ToString();

            Assert.False(string.IsNullOrWhiteSpace(tree));
        }
Exemplo n.º 4
0
 public void ClassUnitTestRoundtrip()
 {
     // formatted class doesn't match the converted class because of the testMethod modifiers
     CompareLineByLine(ApexSharpParser.IndentApex(ClassUnitTest_Original), ClassUnitTest_Formatted);
     CompareLineByLine(ApexToCSharpHelpers.ConvertToCSharp(ClassUnitTest_Original, Options), ClassUnitTest_CSharp1);
     CompareLineByLine(CSharpToApexHelpers.ConvertToApex(ClassUnitTest_CSharp1)[0], ClassUnitTest_Converted);
 }
Exemplo n.º 5
0
        public static void ConvertToApex(FileInfo cSharpFile, DirectoryInfo apexLocation, int version)
        {
            if (!apexLocation.Exists)
            {
                throw new DirectoryNotFoundException(apexLocation.FullName);
            }

            if (!cSharpFile.Exists)
            {
                throw new FileNotFoundException(cSharpFile.FullName);
            }

            // Convert C# to Apex
            var csharpCode = File.ReadAllText(cSharpFile.FullName);

            foreach (var convertedClass in ApexSharpParser.ConvertToApex(csharpCode))
            {
                var apexFileName = Path.ChangeExtension(convertedClass.Key, ".cls");
                var apexFile     = Path.Combine(apexLocation.FullName, apexFileName);
                File.WriteAllText(apexFile, convertedClass.Value);

                var metaFileName = Path.ChangeExtension(apexFile, ".cls-meta.xml");
                var metaFile     = new StringBuilder();

                metaFile.AppendLine("<?xml version = \"1.0\" encoding = \"UTF-8\"?>");
                metaFile.AppendLine("<ApexClass xmlns = \"http://soap.sforce.com/2006/04/metadata\">");
                metaFile.AppendLine($"<apiVersion>{version}.0</apiVersion>");
                metaFile.AppendLine("<status>Active</status>");
                metaFile.AppendLine("</ApexClass>");

                File.WriteAllText(metaFileName, metaFile.ToString());
                Console.WriteLine(metaFileName);
            }
        }
Exemplo n.º 6
0
        public static bool IsRelated(string apexText, string apexClassName)
        {
            if (string.IsNullOrWhiteSpace(apexText) || string.IsNullOrWhiteSpace(apexClassName))
            {
                return(false);
            }

            try
            {
                // try to parse and analyze the file
                var ast = ApexSharpParser.GetApexAst(apexText);

                // ignore the class itself
                if (ast is ClassDeclarationSyntax @class && Comparer.Equals(@class.Identifier, apexClassName))
                {
                    return(false);
                }
                else if (ast is EnumDeclarationSyntax @enum && Comparer.Equals(@enum.Identifier, apexClassName))
                {
                    return(false);
                }

                // inspect the code structure
                var visitor = new RelatedClassHelper(apexClassName);
                ast.Accept(visitor);
                return(visitor.ClassIsRelated);
            }
Exemplo n.º 7
0
        private ClassDeclarationSyntax ParseClass(string apexClass)
        {
            var result = ApexSharpParser.GetApexAst(apexClass) as ClassDeclarationSyntax;

            Assert.NotNull(result);
            return(result);
        }
Exemplo n.º 8
0
        public static List <FileFormatDto> FormatApexCode(DirectoryInfo apexFolderName)
        {
            List <FileFormatDto> dtoList = new List <FileFormatDto>();

            var files = Directory.GetFiles(apexFolderName.FullName, "*.cls", SearchOption.TopDirectoryOnly);

            foreach (var sourceFile in files)
            {
                FileInfo apexFileInfo = new FileInfo(sourceFile);

                Console.WriteLine($"Formatting file: {sourceFile}...");
                var backupFile = sourceFile + ".bak";

                File.Delete(backupFile);
                File.Move(sourceFile, backupFile);

                var apexCode  = File.ReadAllText(backupFile);
                var formatted = ApexSharpParser.IndentApex(apexCode);
                File.WriteAllText(sourceFile, formatted);
                File.Delete(backupFile);

                FileFormatDto dto = new FileFormatDto
                {
                    ApexFileName         = apexFileInfo.FullName,
                    ApexFileBeforeFormat = apexCode,
                    ApexFileAfterFormat  = formatted
                };
                dtoList.Add(dto);
            }

            Console.WriteLine($"Done. Formatted {dtoList.Count} files.");
            return(dtoList);
        }
Exemplo n.º 9
0
 private void Check(string apexOriginal, string apexFormatted, string csharp)
 {
     Assert.Multiple(() =>
     {
         CompareLineByLine(ApexSharpParser.IndentApex(apexOriginal), apexFormatted);
         CompareLineByLine(ApexToCSharpHelpers.ConvertToCSharp(apexOriginal, Options), csharp);
         CompareLineByLine(CSharpToApexHelpers.ConvertToApex(csharp)[0], apexFormatted);
     });
 }
Exemplo n.º 10
0
        protected void Check(BaseSyntax node, string expected)
        {
            string nows(string s) =>
            NoWhitespaceRegex.Replace(s, string.Empty);

            var nodeToApex = node.ToApex();

            Assert.AreEqual(nows(expected), nows(nodeToApex));
            CompareLineByLine(nodeToApex, ApexSharpParser.IndentApex(expected));
        }
Exemplo n.º 11
0
        public static string FormatApex(string apexCode, Settings settings = null)
        {
            if (string.IsNullOrWhiteSpace(apexCode))
            {
                return(string.Empty);
            }

            var apexAst = ApexSharpParser.GetApexAst(apexCode);

            return(GenerateApex(apexAst, settings));
        }
Exemplo n.º 12
0
 private string ToCSharp(string s)
 {
     try
     {
         return(ApexSharpParser.ConvertApexToCSharp(s));
     }
     catch (ParseException)
     {
         return(string.Empty);
     }
 }
Exemplo n.º 13
0
        public Dictionary <string, ClassReference> ProcessApexClasses(params string[] apexTexts)
        {
            if (apexTexts == null || !apexTexts.Any(t => !string.IsNullOrWhiteSpace(t)))
            {
                return(ProcessCSharpClasses());
            }

            var csharpClasses = apexTexts.Select(c => ApexSharpParser.ConvertApexToCSharp(c)).ToArray();

            return(ProcessCSharpClasses(csharpClasses));
        }
Exemplo n.º 14
0
        public CodeFormatTest()
        {
            DirectoryInfo directoryInfo = new DirectoryInfo(@"C:\Dev\codeformat\src\classes\");
            //DirectoryInfo directoryInfo = new DirectoryInfo(@"C:\DevSharp\ApexSharpFsb\FSB\ApexClasses\");
            List <FileFormatDto> results = ApexCodeFormater.FormatApexCode(directoryInfo);

            foreach (var result in results)
            {
                Console.WriteLine(result.ApexFileName);
                var apexAst = ApexSharpParser.GetApexAst(result.ApexFileAfterFormat);
            }
        }
Exemplo n.º 15
0
        public void SoqlStatementFormattingDoesntMessUpParameters()
        {
            var apex = @"class SoqlDemo {
                void soql() {
                    list<r> rr = [select c, min(b) d from r WHERE
                        c in :triggerNew AND b not in :triggerNew GROUP BY c HAVING count(b) > 1];
                }
            }";

            var formatted = ApexSharpParser.IndentApex(apex);

            Assert.IsFalse(formatted.Contains("trigger new"));
            Assert.IsFalse(formatted.Contains("GROUPBY"));
        }
Exemplo n.º 16
0
        protected void Check(string csharpUnit, params string[] apexClasses)
        {
            var csharpNode = ApexSharpParser.ParseText(csharpUnit);
            var apexNodes  = ApexSyntaxBuilder.GetApexSyntaxNodes(csharpNode);

            Assert.Multiple(() =>
            {
                Assert.AreEqual(apexClasses.Length, apexNodes.Count);
                foreach (var apexItem in apexNodes.Zip(apexClasses, (node, text) => new { node, text }))
                {
                    Check(apexItem.node, apexItem.text);
                }
            });
        }
Exemplo n.º 17
0
        public static void ConvertToApex(DirectoryInfo cSharpLocation, DirectoryInfo apexLocation, int version)
        {
            if (!apexLocation.Exists)
            {
                throw new DirectoryNotFoundException(apexLocation.FullName);
            }

            if (!cSharpLocation.Exists)
            {
                throw new DirectoryNotFoundException(cSharpLocation.FullName);
            }

            // Convert C# to Apex
            ApexSharpParser.ConvertToApex(cSharpLocation.FullName, apexLocation.FullName, version);
        }
Exemplo n.º 18
0
        public static void ConvertToCSharp(DirectoryInfo apexLocation, DirectoryInfo cSharpLocation, string nameSpace)
        {
            if (!apexLocation.Exists)
            {
                throw new DirectoryNotFoundException(apexLocation.FullName);
            }

            if (!cSharpLocation.Exists)
            {
                throw new DirectoryNotFoundException(cSharpLocation.FullName);
            }

            // Convert APEX to C#
            ApexSharpParser.ConvertToCSharp(apexLocation.FullName, cSharpLocation.FullName, nameSpace);
        }
Exemplo n.º 19
0
        public static void ConvertToApex()
        {
            // Location of your APEX and C# Files that we will be converting
            DirectoryInfo apexLocation   = new DirectoryInfo(@"\ApexSharp\SalesForce\src\classes\");
            DirectoryInfo cSharpLocation = new DirectoryInfo(@"\ApexSharp\Demo\CSharpClasses\");

            //// Convert to C# to Apex
            if (apexLocation.Exists && cSharpLocation.Exists)
            {
                ApexSharpParser.ConvertToApex(cSharpLocation.FullName, apexLocation.FullName, 40);
            }
            else
            {
                Console.WriteLine("Missing Directory");
            }
        }
Exemplo n.º 20
0
        public void EmptyClassIsGenerated()
        {
            var csharp = "class Test {}";
            var cs     = ApexSharpParser.ParseText(csharp);
            var apex   = ApexSyntaxBuilder.GetApexSyntaxNodes(cs);

            Assert.NotNull(apex);
            Assert.AreEqual(1, apex.Count);

            var cd = apex.OfType <ClassDeclarationSyntax>().FirstOrDefault();

            Assert.NotNull(cd);
            Assert.AreEqual("Test", cd.Identifier);

            Check(csharp, "class Test {}");
        }
Exemplo n.º 21
0
        public void CSharpHelperConvertsCSharpTextsToApex()
        {
            var csharp      = "class Test1 { public Test1(int x) { } } class Test2 : Test1 { private int x = 10; }";
            var apexClasses = ApexSharpParser.ToApex(csharp);

            Assert.AreEqual(2, apexClasses.Length);

            CompareLineByLine(
                @"class Test1
                {
                    public Test1(Integer x)
                    {
                    }
                }", apexClasses[0]);

            CompareLineByLine(
                @"class Test2 extends Test1
                {
                    private Integer x = 10;
                }", apexClasses[1]);
        }
Exemplo n.º 22
0
        public static void ConvertToCSharp(FileInfo apexFile, DirectoryInfo cSharpLocation, string nameSpace)
        {
            if (!apexFile.Exists)
            {
                throw new FileNotFoundException(apexFile.FullName);
            }

            if (!cSharpLocation.Exists)
            {
                throw new DirectoryNotFoundException(cSharpLocation.FullName);
            }

            // Convert APEX to C#
            var apexClass   = File.ReadAllText(apexFile.FullName);
            var csharpClass = ApexSharpParser.ConvertApexToCSharp(apexClass, nameSpace);

            // Write the converted file to the target directory
            var csharpClassName = Path.ChangeExtension(apexFile.Name, "cs");
            var csharpFullName  = Path.Combine(cSharpLocation.FullName, csharpClassName);

            File.WriteAllText(csharpFullName, csharpClass);
        }
Exemplo n.º 23
0
        public void TestCodeGeneration()
        {
            var apex = @"
                class Something
                {
                    @IsTest
                    public static void testPluckDecimals()
                    {
                        List<Account> accounts = testData();
                        LIST<decimal> revenues = Pluck.decimals(accounts, Account.AnnualRevenue);
                        System.deBUG(4, revenues.size());
                        System.assertNOTequals(100.0, revenues[0]); // SYSTEM
                        system.assertEquals(60.0, revenues[1]); // 'deBUG'
                        SYSTEM.assertEquals(150.0, revenues[2]);
                        System.Debug(150.0, revenues[3]);
                    }
                }";

            var parsed    = ApexSharpParser.GetApexAst(apex);
            var generated = ApexCleanCodeGen.GenerateApex(parsed);

            Assert.NotNull(generated);
            Assert.AreEqual(@"class Something
{
    @IsTest
    public static void testPluckDecimals()
    {
        List<Account> accounts = testData();
        List<Decimal> revenues = Pluck.decimals(accounts, Account.AnnualRevenue);
        System.debug(4, revenues.size());
        System.assertNotEquals(100.0, revenues[0]); // SYSTEM
        System.assertEquals(60.0, revenues[1]); // 'deBUG'
        System.assertEquals(150.0, revenues[2]);
        System.debug(150.0, revenues[3]);
    }
}
", generated);
        }
Exemplo n.º 24
0
        public void DetectTestClassExample1()
        {
            var ast = ApexSharpParser.GetApexAst("class Example1 {}");

            Assert.IsFalse(IsTestClass(ast));
        }
Exemplo n.º 25
0
 private void Check(string source, string expected) =>
 CompareLineByLine(ApexSharpParser.IndentApex(source), expected);
Exemplo n.º 26
0
        public static string NormalizeCode(string apexCode)
        {
            var apexAst = ApexSharpParser.GetApexAst(apexCode);

            return(GenerateApex(apexAst));
        }
Exemplo n.º 27
0
 // Convert Apex Code to C#
 public static string ConvertToCSharp(string apexCode, string @namespace = null)
 {
     return(ApexSharpParser.GetApexAst(apexCode).ToCSharp(@namespace: @namespace));
 }
Exemplo n.º 28
0
        public void DetectTestClassExample3()
        {
            var ast = ApexSharpParser.GetApexAst("@isTest enum Example2 { A, B, C }");

            Assert.IsFalse(IsTestClass(ast));
        }
Exemplo n.º 29
0
 // Convert Apex Code to C# with custom options
 public static string ConvertToCSharp(string apexCode, ApexSharpParserOptions options)
 {
     return(ApexSharpParser.GetApexAst(apexCode).ToCSharp(options));
 }
Exemplo n.º 30
0
        public void DetectTestClassExample2()
        {
            var ast = ApexSharpParser.GetApexAst("@isTest class Example2 {}");

            Assert.IsTrue(IsTestClass(ast));
        }