示例#1
0
        ////[TestMethod]
        public void TestNamespace()
        {
            string sourceCode =
@"namespace Namespace1
{
    public class Class1
    {
    }
}";
            CsLanguageService languageService = new CsLanguageService();
            CsDocument document1 = languageService.CreateCodeModel(sourceCode, "source1.cs", "test");
            CsDocument document2 = languageService.CreateCodeModel(sourceCode, "source2.cs", "test");

            Comparer comparer = new Comparer();
            comparer.AreEqual(document1, document2);
        }
示例#2
0
        /// <summary>
        /// Parses the given file.
        /// </summary>
        /// <param name="sourceCode">The source code to parse.</param>
        /// <param name="passNumber">The current pass number.</param>
        /// <param name="document">The parsed representation of the file.</param>
        /// <returns>Returns false if no further analysis should be done on this file, or
        /// true if the file should be parsed again during the next pass.</returns>
        public override bool ParseFile(SourceCode sourceCode, int passNumber, ref ICodeDocument document)
        {
            Param.RequireNotNull(sourceCode, "sourceCode");
            Param.RequireGreaterThanOrEqualToZero(passNumber, "passNumber");
            Param.Ignore(document);

            // The document is parsed on the first pass. On any subsequent passes, we do not do anything.
            if (passNumber == 0)
            {
                try
                {
                    using (TextReader reader = sourceCode.Read())
                    {
                        // Create the document.
                        if (reader == null)
                        {
                            this.AddViolation(sourceCode, 1, Rules.FileMustBeReadable);
                        }
                        else
                        {
                            CsLanguageService languageService = new CsLanguageService();
                            
                            document = new CsDocumentWrapper(
                                this, 
                                sourceCode, 
                                languageService.CreateCodeModel(reader, sourceCode.Name, sourceCode.Path));
                        }
                    }
                }
                catch (SyntaxException syntaxex)
                {
                    this.AddViolation(sourceCode, syntaxex.LineNumber, Rules.SyntaxException, syntaxex.Message);
                    document = null;
                }
            }

            return false;
        }
示例#3
0
        private void ReadAndWriteFile(string filePath)
        {
            Console.WriteLine("Checking file " + filePath);

            if (File.Exists(filePath))
            {
                string fileContents = null;

                try
                {
                    fileContents = File.ReadAllText(filePath);
                }
                catch (IOException)
                {
                }

                CsLanguageService languageService = new CsLanguageService(preprocessorDefinitions);
                CsDocument doc = null;

                try
                {
                    doc = languageService.CreateCodeModel(fileContents, Path.GetFileName(filePath), filePath);
                }
                catch (SyntaxException)
                {
                }
                catch (Exception ex)
                {
                    Assert.Fail("Exception from CodeModel: " + ex.GetType() + ", " + ex.Message + ". FilePath=" + filePath);
                }

                if (doc != null)
                {
                    using (StringWriter writer = new StringWriter())
                    {
                        doc.Write(writer);

                        this.CompareDocs(fileContents, writer.ToString(), filePath);
                    }
                }
            }
        }