public void ModuleAttribute() { string program = @"[module: System.Attribute()]"; AttributeSection decl = ParseUtilCSharp.ParseGlobal <AttributeSection>(program); Assert.AreEqual(new TextLocation(1, 1), decl.StartLocation); Assert.AreEqual("module", decl.AttributeTarget); Assert.AreEqual(SyntaxTree.MemberRole, decl.Role); }
public void PragmaWarning() { string program = "#pragma warning disable 809"; var ppd = ParseUtilCSharp.ParseGlobal <PragmaWarningPreprocssorDirective>(program); Assert.AreEqual(PreProcessorDirectiveType.Pragma, ppd.Type); Assert.IsTrue(ppd.Disable); Assert.IsTrue(ppd.WarningList.Contains(809)); }
public void PragmaChecksum() { string program = "#pragma checksum \"file.cs\" \"{3673e4ca-6098-4ec1-890f-8fceb2a794a2}\" \"{012345678AB}\""; var ppd = ParseUtilCSharp.ParseGlobal <PreProcessorDirective>(program); Assert.IsFalse(ppd is PragmaWarningPreprocssorDirective); Assert.AreEqual(PreProcessorDirectiveType.Pragma, ppd.Type); Assert.AreEqual("checksum \"file.cs\" \"{3673e4ca-6098-4ec1-890f-8fceb2a794a2}\" \"{012345678AB}\"", ppd.Argument); }
public void SimplePartialClassTypeDeclarationTest() { TypeDeclaration td = ParseUtilCSharp.ParseGlobal <TypeDeclaration>("partial class MyClass { }"); Assert.IsFalse(td.IsNull); Assert.AreEqual(ClassType.Class, td.ClassType); Assert.AreEqual("MyClass", td.Name); Assert.AreEqual(Modifiers.Partial, td.Modifiers); }
public void TypeAttribute() { string program = @"[type: System.Attribute()] class Test {}"; TypeDeclaration type = ParseUtilCSharp.ParseGlobal <TypeDeclaration>(program); AttributeSection decl = type.Attributes.Single(); Assert.AreEqual(new TextLocation(1, 1), decl.StartLocation); Assert.AreEqual("type", decl.AttributeTarget); }
public void AttributeWithEmptyParenthesis() { string program = @"[Attr()] class Test {}"; TypeDeclaration type = ParseUtilCSharp.ParseGlobal <TypeDeclaration>(program); var attr = type.Attributes.Single().Attributes.Single(); Assert.IsFalse(attr.GetChildByRole(Roles.LPar).IsNull); Assert.IsFalse(attr.GetChildByRole(Roles.RPar).IsNull); }
public void SimpleClassTypeDeclarationTest() { TypeDeclaration td = ParseUtilCSharp.ParseGlobal <TypeDeclaration>("class MyClass : My.Base.Class { }"); Assert.AreEqual(ClassType.Class, td.ClassType); Assert.AreEqual("MyClass", td.Name); Assert.AreEqual("My.Base.Class", td.BaseTypes.First().ToString()); Assert.AreEqual(Modifiers.None, td.Modifiers); }
public void SimpleClassTypeDeclarationTest() { TypeDeclaration td = ParseUtilCSharp.ParseGlobal <TypeDeclaration>("class MyClass : My.Base.Class { }"); Assert.AreEqual(ClassType.Class, td.ClassType); Assert.AreEqual("MyClass", td.Name); //Assert.AreEqual("My.Base.Class", td.BaseTypes[0].Type); Assert.Ignore("need to check base type"); // TODO Assert.AreEqual(Modifiers.None, td.Modifiers); }
public void SimpleCommentWith4Slashes() { string program = @"namespace NS { //// Comment }"; NamespaceDeclaration ns = ParseUtilCSharp.ParseGlobal <NamespaceDeclaration>(program); var c = ns.GetChildrenByRole(Roles.Comment).Single(); Assert.AreEqual(CommentType.SingleLine, c.CommentType); Assert.AreEqual("// Comment", c.Content); }
public void DocumentationComment() { string program = @"namespace NS { /// Comment }"; NamespaceDeclaration ns = ParseUtilCSharp.ParseGlobal <NamespaceDeclaration>(program); var c = ns.GetChildrenByRole(Roles.Comment).Single(); Assert.AreEqual(CommentType.Documentation, c.CommentType); Assert.AreEqual(" Comment", c.Content); }
public void EmptyMultilineCommnet() { string program = @"namespace NS { /**/ }"; NamespaceDeclaration ns = ParseUtilCSharp.ParseGlobal <NamespaceDeclaration>(program); var c = ns.GetChildrenByRole(Roles.Comment).Single(); Assert.AreEqual(CommentType.MultiLine, c.CommentType); Assert.AreEqual("", c.Content); }
public void NestedClassesTest() { TypeDeclaration td = ParseUtilCSharp.ParseGlobal <TypeDeclaration>("class MyClass { partial class P1 {} public partial class P2 {} static class P3 {} internal static class P4 {} }"); Assert.IsFalse(td.IsNull); Assert.AreEqual(ClassType.Class, td.ClassType); Assert.AreEqual("MyClass", td.Name); Assert.AreEqual(Modifiers.Partial, ((TypeDeclaration)td.Members.ElementAt(0)).Modifiers); Assert.AreEqual(Modifiers.Partial | Modifiers.Public, ((TypeDeclaration)td.Members.ElementAt(1)).Modifiers); Assert.AreEqual(Modifiers.Static, ((TypeDeclaration)td.Members.ElementAt(2)).Modifiers); Assert.AreEqual(Modifiers.Static | Modifiers.Internal, ((TypeDeclaration)td.Members.ElementAt(3)).Modifiers); }
public void NestedNamespaceTest() { string program = "namespace N1 {//TestNamespace\n" + " namespace N2 {// Declares a namespace named N2 within N1.\n" + " }\n" + "}\n"; NamespaceDeclaration ns = ParseUtilCSharp.ParseGlobal <NamespaceDeclaration>(program); Assert.AreEqual("N1", ns.Name); Assert.AreEqual("N2", ns.Children.OfType <NamespaceDeclaration>().Single().Name); }
public void AssemblyAttributeCSharpWithNamedArguments() { string program = @"[assembly: Foo(1, namedArg: 2, prop = 3)]"; AttributeSection decl = ParseUtilCSharp.ParseGlobal <AttributeSection>(program); Assert.AreEqual("assembly", decl.AttributeTarget); var a = decl.Attributes.Single(); Assert.AreEqual("Foo", a.Type); Assert.AreEqual(3, a.Arguments.Count()); // TODO: check arguments }
public void EnumWithCommaAtEnd() { TypeDeclaration td = ParseUtilCSharp.ParseGlobal <TypeDeclaration>("enum MyEnum { A, }"); Assert.AreEqual( new Role[] { AstNode.Roles.Keyword, AstNode.Roles.Identifier, AstNode.Roles.LBrace, TypeDeclaration.MemberRole, AstNode.Roles.Comma, AstNode.Roles.RBrace }, td.Children.Select(c => c.Role).ToArray()); }
public void SimpleClassRegionTest() { const string program = "class MyClass\n{\n}\n"; TypeDeclaration td = ParseUtilCSharp.ParseGlobal <TypeDeclaration>(program); Assert.AreEqual(1, td.StartLocation.Line, "StartLocation.Y"); Assert.AreEqual(1, td.StartLocation.Column, "StartLocation.X"); TextLocation bodyStartLocation = td.NameToken.EndLocation; Assert.AreEqual(1, bodyStartLocation.Line, "BodyStartLocation.Y"); Assert.AreEqual(14, bodyStartLocation.Column, "BodyStartLocation.X"); Assert.AreEqual(3, td.EndLocation.Line, "EndLocation.Y"); Assert.AreEqual(2, td.EndLocation.Column, "EndLocation.Y"); }
public void SimpleClassRegionTest() { const string program = "class MyClass\n{\n}\n"; TypeDeclaration td = ParseUtilCSharp.ParseGlobal <TypeDeclaration>(program); Assert.AreEqual(1, td.StartLocation.Line, "StartLocation.Y"); Assert.AreEqual(1, td.StartLocation.Column, "StartLocation.X"); TextLocation bodyStartLocation = td.GetChildByRole(AstNode.Roles.LBrace).PrevSibling.EndLocation; Assert.AreEqual(1, bodyStartLocation.Line, "BodyStartLocation.Y"); Assert.AreEqual(14, bodyStartLocation.Column, "BodyStartLocation.X"); Assert.AreEqual(3, td.EndLocation.Line, "EndLocation.Y"); Assert.AreEqual(2, td.EndLocation.Column, "EndLocation.Y"); }
public void EnumWithSemicolonAtEnd() { TypeDeclaration td = ParseUtilCSharp.ParseGlobal <TypeDeclaration>("enum MyEnum { A };"); Assert.AreEqual( new Role[] { Roles.EnumKeyword, Roles.Identifier, Roles.LBrace, Roles.TypeMemberRole, Roles.RBrace, Roles.Semicolon }, td.Children.Select(c => c.Role).ToArray()); }
public void AttributesUsingNamespaceAlias() { string program = @"[global::Microsoft.VisualBasic.CompilerServices.DesignerGenerated()] [someprefix::DesignerGenerated()] public class Form1 { }"; TypeDeclaration decl = ParseUtilCSharp.ParseGlobal <TypeDeclaration>(program); Assert.AreEqual(2, decl.Attributes.Count); Assert.AreEqual("global::Microsoft.VisualBasic.CompilerServices.DesignerGenerated", decl.Attributes.First().Attributes.Single().Type.ToString()); Assert.AreEqual("someprefix::DesignerGenerated", decl.Attributes.Last().Attributes.Single().Type.ToString()); }
public void PragmaWarningLocations() { string program = "#pragma warning disable 809"; var ppd = ParseUtilCSharp.ParseGlobal <PragmaWarningPreprocessorDirective>(program); Assert.AreEqual(new TextLocation(1, 1), ppd.StartLocation); Assert.AreEqual(new TextLocation(1, 1), ppd.PragmaToken.StartLocation); Assert.AreEqual(new TextLocation(1, 8), ppd.PragmaToken.EndLocation); Assert.AreEqual(new TextLocation(1, 9), ppd.WarningToken.StartLocation); Assert.AreEqual(new TextLocation(1, 16), ppd.WarningToken.EndLocation); Assert.AreEqual(new TextLocation(1, 17), ppd.DisableToken.StartLocation); Assert.AreEqual(new TextLocation(1, 24), ppd.DisableToken.EndLocation); var id = ppd.Warnings.Single(); Assert.AreEqual(new TextLocation(1, 25), id.StartLocation); Assert.AreEqual(new TextLocation(1, 28), id.EndLocation); Assert.AreEqual(new TextLocation(1, 28), ppd.EndLocation); }
public void TwoAttributesWithOptionalCommaInSameSectionLocations() { string program = @"[A, B,] class Test {}"; TypeDeclaration type = ParseUtilCSharp.ParseGlobal <TypeDeclaration>(program); var attributeSection = type.Attributes.Single(); var firstAttribute = attributeSection.Attributes.First(); Assert.AreEqual(2, firstAttribute.StartLocation.Column); Assert.AreEqual(3, firstAttribute.EndLocation.Column); var lastAttribute = attributeSection.Attributes.Last(); Assert.AreEqual(5, lastAttribute.StartLocation.Column); Assert.AreEqual(6, lastAttribute.EndLocation.Column); Assert.AreEqual(1, attributeSection.StartLocation.Column); Assert.AreEqual(8, attributeSection.EndLocation.Column); }
public void InactiveIf() { string program = @"namespace NS { #if SOMETHING class A {} #endif }"; NamespaceDeclaration ns = ParseUtilCSharp.ParseGlobal <NamespaceDeclaration>(program); Assert.AreEqual(0, ns.Members.Count); Assert.AreEqual(new Role[] { Roles.NamespaceKeyword, Roles.Identifier, Roles.LBrace, Roles.PreProcessorDirective, Roles.Comment, Roles.PreProcessorDirective, Roles.RBrace }, ns.Children.Select(c => c.Role).ToArray()); var pp = ns.GetChildrenByRole(Roles.PreProcessorDirective); Assert.AreEqual(PreProcessorDirectiveType.If, pp.First().Type); Assert.IsFalse(pp.First().Take); Assert.AreEqual("SOMETHING", pp.First().Argument); Assert.AreEqual(new TextLocation(2, 2), pp.First().StartLocation); Assert.AreEqual(new TextLocation(2, 15), pp.First().EndLocation); var comment = ns.GetChildByRole(Roles.Comment); Assert.AreEqual(CommentType.InactiveCode, comment.CommentType); Assert.AreEqual(new TextLocation(3, 1), comment.StartLocation); Assert.AreEqual(new TextLocation(4, 2), comment.EndLocation); Assert.AreEqual("\tclass A {}\n\t", comment.Content.Replace("\r", "")); Assert.AreEqual(PreProcessorDirectiveType.Endif, pp.Last().Type); Assert.AreEqual(string.Empty, pp.Last().Argument); Assert.AreEqual(new TextLocation(4, 2), pp.Last().StartLocation); Assert.AreEqual(new TextLocation(4, 8), pp.Last().EndLocation); }
public void AssemblyAttributeCSharpWithNamedArguments() { string program = @"[assembly: Foo(1, namedArg: 2, prop = 3)]"; AttributeSection decl = ParseUtilCSharp.ParseGlobal <AttributeSection>(program); Assert.AreEqual("assembly", decl.AttributeTarget); var a = decl.Attributes.Single(); Assert.AreEqual("Foo", a.Type); Assert.AreEqual(3, a.Arguments.Count()); Assert.IsTrue(a.Arguments.ElementAt(0).IsMatch(new PrimitiveExpression(1))); Assert.IsTrue(a.Arguments.ElementAt(1).IsMatch(new NamedArgumentExpression { Identifier = "namedArg", Expression = new PrimitiveExpression(2) })); Assert.IsTrue(a.Arguments.ElementAt(2).IsMatch(new AssignmentExpression { Left = new IdentifierExpression("prop"), Operator = AssignmentOperatorType.Assign, Right = new PrimitiveExpression(3) })); }
public void CommentOnEndOfIfDirective() { string program = @"namespace NS { #if SOMETHING // comment class A { } #endif }"; NamespaceDeclaration ns = ParseUtilCSharp.ParseGlobal <NamespaceDeclaration>(program); Assert.AreEqual(0, ns.Members.Count); Assert.AreEqual(new Role[] { Roles.NamespaceKeyword, Roles.Identifier, Roles.LBrace, Roles.PreProcessorDirective, Roles.Comment, Roles.Comment, Roles.PreProcessorDirective, Roles.RBrace }, ns.Children.Where(c => !(c is NewLineNode)).Select(c => c.Role).ToArray()); Assert.AreEqual(CommentType.SingleLine, ns.GetChildrenByRole(Roles.Comment).First().CommentType); Assert.AreEqual(CommentType.InactiveCode, ns.GetChildrenByRole(Roles.Comment).Last().CommentType); }
public void AttributesOnTypeParameter() { string program = @"class Test<[A,B]C> {}"; TypeDeclaration type = ParseUtilCSharp.ParseGlobal <TypeDeclaration>(program); Assert.IsTrue( new TypeParameterDeclaration { Attributes = { new AttributeSection { Attributes = { new Attribute { Type = new SimpleType("A") }, new Attribute { Type = new SimpleType("B") } } } }, Name = "C" }.IsMatch(type.TypeParameters.Single())); }