PageRenderTime 30ms CodeModel.GetById 11ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/NRefactory/ICSharpCode.NRefactory.Tests/CSharp/Resolver/AttributeTests.cs

http://github.com/icsharpcode/ILSpy
C# | 121 lines | 90 code | 14 blank | 17 comment | 0 complexity | beaf2e3afa5a6862aa3cb8f17ba10191 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 ICSharpCode.NRefactory.Semantics;
 21using ICSharpCode.NRefactory.TypeSystem;
 22using NUnit.Framework;
 23
 24namespace ICSharpCode.NRefactory.CSharp.Resolver
 25{
 26	[TestFixture]
 27	public class AttributeTests : ResolverTestBase
 28	{
 29		[Test]
 30		public void NamespaceInAttributeContext()
 31		{
 32			string program = "using System; [$System.Runtime$.CompilerServices.IndexerName(\"bla\")] class Test { }";
 33			NamespaceResolveResult result = Resolve<NamespaceResolveResult>(program);
 34			Assert.AreEqual("System.Runtime", result.NamespaceName);
 35		}
 36		
 37		[Test]
 38		public void AttributeWithShortName()
 39		{
 40			string program = "using System; [$Obsolete$()] class Test {}";
 41			
 42			TypeResolveResult result = Resolve<TypeResolveResult>(program);
 43			Assert.AreEqual("System.ObsoleteAttribute", result.Type.FullName);
 44		}
 45		
 46		[Test]
 47		public void QualifiedAttributeWithShortName()
 48		{
 49			string program = "using System; [$System.Obsolete$()] class Test {}";
 50			
 51			TypeResolveResult result = Resolve<TypeResolveResult>(program);
 52			Assert.AreEqual("System.ObsoleteAttribute", result.Type.FullName);
 53		}
 54		
 55		[Test]
 56		public void AttributeConstructor1()
 57		{
 58			string program = "using System; [$LoaderOptimization(3)$] class Test { }";
 59			var mrr = Resolve<CSharpInvocationResolveResult>(program);
 60			Assert.AreEqual("System.LoaderOptimizationAttribute..ctor", mrr.Member.FullName);
 61			Assert.AreEqual("System.Byte", mrr.Member.Parameters[0].Type.FullName);
 62		}
 63		
 64		[Test]
 65		public void AttributeConstructor2()
 66		{
 67			string program = "using System; [$LoaderOptimization(LoaderOptimization.NotSpecified)$] class Test { }";
 68			var mrr = Resolve<CSharpInvocationResolveResult>(program);
 69			Assert.AreEqual("System.LoaderOptimizationAttribute..ctor", mrr.Member.FullName);
 70			Assert.AreEqual("System.LoaderOptimization", mrr.Member.Parameters[0].Type.FullName);
 71		}
 72		
 73		[Test]
 74		public void AttributeWithoutArgumentListRefersToConstructor()
 75		{
 76			string program = "using System; [$Obsolete$] class Test {}";
 77			
 78			var result = Resolve<CSharpInvocationResolveResult>(program);
 79			Assert.AreEqual("System.ObsoleteAttribute..ctor", result.Member.FullName);
 80		}
 81		
 82		[Test]
 83		public void AttributeArgumentInClassContext1()
 84		{
 85			string program = @"using System;
 86[AttributeUsage($XXX$)] class MyAttribute : Attribute {
 87	public const AttributeTargets XXX = AttributeTargets.All;
 88}
 89";
 90			var result = Resolve<MemberResolveResult>(program);
 91			Assert.AreEqual("MyAttribute.XXX", result.Member.FullName);
 92		}
 93		
 94		[Test]
 95		public void AttributeArgumentInClassContext2()
 96		{
 97			string program = @"using System; namespace MyNamespace {
 98[SomeAttribute($E.A$)] class Test { }
 99enum E { A, B }
100}
101";
102			var result = Resolve<MemberResolveResult>(program);
103			Assert.AreEqual("MyNamespace.E.A", result.Member.FullName);
104		}
105		
106		[Test]
107		public void SD_1384()
108		{
109			string program = @"using System;
110class Flags {
111	$[Flags]
112	enum Test { }$
113}";
114			TypeResolveResult result = Resolve<TypeResolveResult>(program);
115			Assert.AreEqual("Flags.Test", result.Type.FullName);
116			
117			var rt = result.Type.GetDefinition().Attributes[0].AttributeType;
118			Assert.AreEqual("System.FlagsAttribute", rt.FullName);
119		}
120	}
121}