PageRenderTime 45ms CodeModel.GetById 8ms app.highlight 31ms RepoModel.GetById 1ms app.codeStats 1ms

/NRefactory/ICSharpCode.NRefactory.Tests/CSharp/Parser/GeneralScope/AttributeSectionTests.cs

http://github.com/icsharpcode/ILSpy
C# | 261 lines | 219 code | 24 blank | 18 comment | 0 complexity | 1c13232442e0663deacbe3c515aff04e MD5 | raw file
  1// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
  2// 
  3// Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4// software and associated documentation files (the "Software"), to deal in the Software
  5// without restriction, including without limitation the rights to use, copy, modify, merge,
  6// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7// to whom the Software is furnished to do so, subject to the following conditions:
  8// 
  9// The above copyright notice and this permission notice shall be included in all copies or
 10// substantial portions of the Software.
 11// 
 12// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 13// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 14// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 15// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 16// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 17// DEALINGS IN THE SOFTWARE.
 18
 19using System;
 20using System.IO;
 21using System.Linq;
 22using System.Text.RegularExpressions;
 23using ICSharpCode.NRefactory.PatternMatching;
 24using ICSharpCode.NRefactory.TypeSystem;
 25using NUnit.Framework;
 26
 27namespace ICSharpCode.NRefactory.CSharp.Parser.GeneralScope
 28{
 29	[TestFixture]
 30	public class AttributeSectionTests
 31	{
 32		[Test]
 33		public void AttributesUsingNamespaceAlias()
 34		{
 35			string program = @"[global::Microsoft.VisualBasic.CompilerServices.DesignerGenerated()]
 36[someprefix::DesignerGenerated()]
 37public class Form1 {
 38}";
 39			
 40			TypeDeclaration decl = ParseUtilCSharp.ParseGlobal<TypeDeclaration>(program);
 41			Assert.AreEqual(2, decl.Attributes.Count);
 42			Assert.AreEqual("global::Microsoft.VisualBasic.CompilerServices.DesignerGenerated",
 43			                decl.Attributes.First().Attributes.Single().Type.ToString());
 44			Assert.AreEqual("someprefix::DesignerGenerated", decl.Attributes.Last().Attributes.Single().Type.ToString());
 45		}
 46		
 47		[Test]
 48		public void AssemblyAttribute()
 49		{
 50			string program = @"[assembly: System.Attribute()]";
 51			AttributeSection decl = ParseUtilCSharp.ParseGlobal<AttributeSection>(program);
 52			Assert.AreEqual(new TextLocation(1, 1), decl.StartLocation);
 53			Assert.AreEqual("assembly", decl.AttributeTarget);
 54		}
 55		
 56		[Test]
 57		public void ModuleAttribute()
 58		{
 59			string program = @"[module: System.Attribute()]";
 60			AttributeSection decl = ParseUtilCSharp.ParseGlobal<AttributeSection>(program);
 61			Assert.AreEqual(new TextLocation(1, 1), decl.StartLocation);
 62			Assert.AreEqual("module", decl.AttributeTarget);
 63		}
 64		
 65		[Test]
 66		public void TypeAttribute()
 67		{
 68			string program = @"[type: System.Attribute()] class Test {}";
 69			TypeDeclaration type = ParseUtilCSharp.ParseGlobal<TypeDeclaration>(program);
 70			AttributeSection decl = type.Attributes.Single();
 71			Assert.AreEqual(new TextLocation(1, 1), decl.StartLocation);
 72			Assert.AreEqual("type", decl.AttributeTarget);
 73		}
 74		
 75		[Test]
 76		public void AttributeWithoutParenthesis()
 77		{
 78			string program = @"[Attr] class Test {}";
 79			TypeDeclaration type = ParseUtilCSharp.ParseGlobal<TypeDeclaration>(program);
 80			var attr = type.Attributes.Single().Attributes.Single();
 81			Assert.IsTrue(attr.GetChildByRole(Roles.LPar).IsNull);
 82			Assert.IsTrue(attr.GetChildByRole(Roles.RPar).IsNull);
 83		}
 84		
 85		[Test, Ignore("Parser bug - parenthesis are missing")]
 86		public void AttributeWithEmptyParenthesis()
 87		{
 88			string program = @"[Attr()] class Test {}";
 89			TypeDeclaration type = ParseUtilCSharp.ParseGlobal<TypeDeclaration>(program);
 90			var attr = type.Attributes.Single().Attributes.Single();
 91			Assert.IsFalse(attr.GetChildByRole(Roles.LPar).IsNull);
 92			Assert.IsFalse(attr.GetChildByRole(Roles.RPar).IsNull);
 93		}
 94		
 95		[Test]
 96		public void TwoAttributesInSameSection()
 97		{
 98			ParseUtilCSharp.AssertGlobal(
 99				@"[A, B] class Test {}",
100				new TypeDeclaration {
101					ClassType = ClassType.Class,
102					Name = "Test",
103					Attributes = {
104						new AttributeSection {
105							Attributes = {
106								new Attribute { Type = new SimpleType("A") },
107								new Attribute { Type = new SimpleType("B") }
108							}
109						}}});
110		}
111	
112		[Test]
113		public void TwoAttributesInSameSectionLocations()
114		{
115			string program = @"[A, B] class Test {}";
116			TypeDeclaration type = ParseUtilCSharp.ParseGlobal<TypeDeclaration>(program);
117			var attributeSection = type.Attributes.Single();
118			
119			var firstAttribute = attributeSection.Attributes.First();
120			Assert.AreEqual(2, firstAttribute.StartLocation.Column);
121			Assert.AreEqual(3, firstAttribute.EndLocation.Column); 
122			
123			var lastAttribute = attributeSection.Attributes.Last();
124			Assert.AreEqual(5, lastAttribute.StartLocation.Column);
125			Assert.AreEqual(6, lastAttribute.EndLocation.Column);
126			
127			Assert.AreEqual(1, attributeSection.StartLocation.Column);
128			Assert.AreEqual(7, attributeSection.EndLocation.Column);
129		}
130		
131		[Test]
132		public void TwoAttributesWithOptionalCommaInSameSectionLocations()
133		{
134			string program = @"[A, B,] class Test {}";
135			TypeDeclaration type = ParseUtilCSharp.ParseGlobal<TypeDeclaration>(program);
136			var attributeSection = type.Attributes.Single();
137			
138			var firstAttribute = attributeSection.Attributes.First();
139			Assert.AreEqual(2, firstAttribute.StartLocation.Column);
140			Assert.AreEqual(3, firstAttribute.EndLocation.Column); 
141			
142			var lastAttribute = attributeSection.Attributes.Last();
143			Assert.AreEqual(5, lastAttribute.StartLocation.Column);
144			Assert.AreEqual(6, lastAttribute.EndLocation.Column);
145			
146			Assert.AreEqual(1, attributeSection.StartLocation.Column);
147			Assert.AreEqual(8, attributeSection.EndLocation.Column);
148		}
149		
150		[Test]
151		public void AttributesOnTypeParameter()
152		{
153			ParseUtilCSharp.AssertGlobal(
154				"class Test<[A,B]C> {}",
155				new TypeDeclaration {
156					ClassType = ClassType.Class,
157					Name = "Test",
158					TypeParameters = {
159						new TypeParameterDeclaration {
160							Attributes = {
161								new AttributeSection {
162									Attributes = {
163										new Attribute { Type = new SimpleType("A") },
164										new Attribute { Type = new SimpleType("B") }
165									}
166								}
167							},
168							Name = "C"
169						}
170					}});
171		}
172		
173		[Test]
174		public void AttributeOnMethodParameter()
175		{
176			ParseUtilCSharp.AssertTypeMember(
177				"void M([In] int p);",
178				new MethodDeclaration {
179					ReturnType = new PrimitiveType("void"),
180					Name = "M",
181					Parameters = {
182						new ParameterDeclaration {
183							Attributes = { new AttributeSection(new Attribute { Type = new SimpleType("In") }) },
184							Type = new PrimitiveType("int"),
185							Name = "p"
186						}
187					}});
188		}
189		
190		[Test]
191		public void AttributeOnSetterValue()
192		{
193			ParseUtilCSharp.AssertTypeMember(
194				"int P { get; [param: In] set; }",
195				new PropertyDeclaration {
196					ReturnType = new PrimitiveType("int"),
197					Name = "P",
198					Getter = new Accessor(),
199					Setter = new Accessor {
200						Attributes = {
201							new AttributeSection {
202								AttributeTarget = "param",
203								Attributes = { new Attribute { Type = new SimpleType("In") } },
204							} },
205					}});
206		}
207		
208		// TODO: Tests for other contexts where attributes can appear
209		
210		[Test]
211		public void AttributeWithNamedArguments ()
212		{
213			ParseUtilCSharp.AssertTypeMember (
214				@"[A(0, a:1, b=2)] class Test {}",
215				new TypeDeclaration {
216					ClassType = ClassType.Class,
217					Name = "Test",
218					Attributes = {
219						new AttributeSection {
220							Attributes = {
221								new Attribute {
222									Type = new SimpleType("A"),
223									Arguments = {
224										new PrimitiveExpression(0),
225										new NamedArgumentExpression("a", new PrimitiveExpression(1)),
226										new NamedExpression("b", new PrimitiveExpression(2))
227									}
228								}
229							}
230						}
231					}});
232		}
233		
234		[Ignore("Fixme!")]
235		[Test]
236		public void AssemblyAttributeBeforeNamespace()
237		{
238			var syntaxTree = SyntaxTree.Parse("using System; [assembly: Attr] namespace X {}");
239			Assert.AreEqual(
240				new Type[] {
241					typeof(UsingDeclaration),
242					typeof(AttributeSection),
243					typeof(NamespaceDeclaration)
244				}, syntaxTree.Children.Select(c => c.GetType()).ToArray());
245		}
246		
247		[Ignore("Fixme!")]
248		[Test]
249		public void AssemblyAttributeBeforeClass()
250		{
251			var syntaxTree = SyntaxTree.Parse("using System; [assembly: Attr] class X {}");
252			Assert.AreEqual(
253				new Type[] {
254					typeof(UsingDeclaration),
255					typeof(AttributeSection),
256					typeof(TypeDeclaration)
257				}, syntaxTree.Children.Select(c => c.GetType()).ToArray());
258			Assert.That(((TypeDeclaration)syntaxTree.LastChild).Attributes, Is.Empty);
259		}
260	}
261}