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