1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
7
using ICSharpCode.NRefactory.Parser;
8
using ICSharpCode.NRefactory.Parser.VB;
9
using ICSharpCode.NRefactory.Ast;
11
namespace ICSharpCode.NRefactory.Tests.Ast
14
public class AttributeSectionTests
17
public void AttributeOnStructure()
20
<StructLayout( LayoutKind.Explicit )> _
21
Public Structure MyUnion
23
<FieldOffset( 0 )> Public i As Integer
24
< FieldOffset( 0 )> Public d As Double
26
End Structure 'MyUnion
28
TypeDeclaration decl = ParseUtilVBNet.ParseGlobal<TypeDeclaration>(program);
29
Assert.AreEqual("StructLayout", decl.Attributes[0].Attributes[0].Name);
33
public void AttributeOnModule()
44
TypeDeclaration decl = ParseUtilVBNet.ParseGlobal<TypeDeclaration>(program);
45
Assert.AreEqual("HideModule", decl.Attributes[0].Attributes[0].Name);
49
public void GlobalAttributeVB()
51
string program = @"<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
55
TypeDeclaration decl = ParseUtilVBNet.ParseGlobal<TypeDeclaration>(program);
56
Assert.AreEqual("Microsoft.VisualBasic.CompilerServices.DesignerGenerated", decl.Attributes[0].Attributes[0].Name);
60
public void GlobalAttributeCSharp()
62
string program = @"[global::Microsoft.VisualBasic.CompilerServices.DesignerGenerated()]
63
[someprefix::DesignerGenerated()]
66
TypeDeclaration decl = ParseUtilCSharp.ParseGlobal<TypeDeclaration>(program);
67
Assert.AreEqual("Microsoft.VisualBasic.CompilerServices.DesignerGenerated", decl.Attributes[0].Attributes[0].Name);
68
Assert.AreEqual("someprefix.DesignerGenerated", decl.Attributes[1].Attributes[0].Name);
72
public void AssemblyAttributeCSharp()
74
string program = @"[assembly: System.Attribute()]";
75
AttributeSection decl = ParseUtilCSharp.ParseGlobal<AttributeSection>(program);
76
Assert.AreEqual(new Location(1, 1), decl.StartLocation);
77
Assert.AreEqual("assembly", decl.AttributeTarget);
81
public void AssemblyAttributeCSharpWithNamedArguments()
83
string program = @"[assembly: Foo(1, namedArg: 2, prop = 3)]";
84
AttributeSection decl = ParseUtilCSharp.ParseGlobal<AttributeSection>(program);
85
Assert.AreEqual("assembly", decl.AttributeTarget);
86
var a = decl.Attributes[0];
87
Assert.AreEqual("Foo", a.Name);
88
Assert.AreEqual(2, a.PositionalArguments.Count);
89
Assert.AreEqual(1, a.NamedArguments.Count);
90
Assert.AreEqual(1, ((PrimitiveExpression)a.PositionalArguments[0]).Value);
91
NamedArgumentExpression nae = a.PositionalArguments[1] as NamedArgumentExpression;
92
Assert.AreEqual("namedArg", nae.Name);
93
Assert.AreEqual(2, ((PrimitiveExpression)nae.Expression).Value);
94
nae = a.NamedArguments[0];
95
Assert.AreEqual("prop", nae.Name);
96
Assert.AreEqual(3, ((PrimitiveExpression)nae.Expression).Value);
100
public void ModuleAttributeCSharp()
102
string program = @"[module: System.Attribute()]";
103
AttributeSection decl = ParseUtilCSharp.ParseGlobal<AttributeSection>(program);
104
Assert.AreEqual(new Location(1, 1), decl.StartLocation);
105
Assert.AreEqual("module", decl.AttributeTarget);
109
public void TypeAttributeCSharp()
111
string program = @"[type: System.Attribute()] class Test {}";
112
TypeDeclaration type = ParseUtilCSharp.ParseGlobal<TypeDeclaration>(program);
113
AttributeSection decl = type.Attributes[0];
114
Assert.AreEqual(new Location(1, 1), decl.StartLocation);
115
Assert.AreEqual("type", decl.AttributeTarget);
119
public void AssemblyAttributeVBNet()
121
string program = @"<assembly: System.Attribute()>";
122
AttributeSection decl = ParseUtilVBNet.ParseGlobal<AttributeSection>(program);
123
Assert.AreEqual(new Location(1, 1), decl.StartLocation);
124
Assert.AreEqual("assembly", decl.AttributeTarget);
128
public void ModuleAttributeTargetEscapedVB()
130
// check that this doesn't crash the parser:
131
ParseUtilVBNet.ParseGlobal<AttributeSection>("<[Module]: SuppressMessageAttribute>", true);