1 // 2 // AttributeUsageAttributeTest.cs 3 // 4 // Authors: 5 // Alexander Köplinger (alkpli@microsoft.com) 6 // 7 // (c) 2017 Microsoft 8 // 9 // Permission is hereby granted, free of charge, to any person obtaining 10 // a copy of this software and associated documentation files (the 11 // "Software"), to deal in the Software without restriction, including 12 // without limitation the rights to use, copy, modify, merge, publish, 13 // distribute, sublicense, and/or sell copies of the Software, and to 14 // permit persons to whom the Software is furnished to do so, subject to 15 // the following conditions: 16 // 17 // The above copyright notice and this permission notice shall be 18 // included in all copies or substantial portions of the Software. 19 // 20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 27 // 28 29 using System; 30 using System.Threading; 31 using System.Reflection; 32 using System.Reflection.Emit; 33 using NUnit.Framework; 34 35 namespace MonoTests.System { 36 37 /// <summary> 38 /// Summary description for AttributeUsageAttributeTest. 39 /// </summary> 40 [TestFixture] 41 public class AttributeUsageAttributeTest 42 { 43 #if !MOBILE 44 private AssemblyBuilder dynAssembly; 45 AssemblyName dynAsmName = new AssemblyName (); 46 AttributeUsageAttribute attr; 47 AttributeUsageAttributeTest()48 public AttributeUsageAttributeTest () 49 { 50 //create a dynamic assembly with the required attribute 51 //and check for the validity 52 53 dynAsmName.Name = "TestAssembly"; 54 55 dynAssembly = Thread.GetDomain ().DefineDynamicAssembly ( 56 dynAsmName,AssemblyBuilderAccess.Run 57 ); 58 59 // Set the required Attribute of the assembly. 60 Type attribute = typeof (AttributeUsageAttribute); 61 ConstructorInfo ctrInfo = attribute.GetConstructor ( 62 new Type [] { typeof (AttributeTargets) } 63 ); 64 CustomAttributeBuilder attrBuilder = 65 new CustomAttributeBuilder (ctrInfo, new object [1] { AttributeTargets.Assembly }); 66 dynAssembly.SetCustomAttribute (attrBuilder); 67 object [] attributes = dynAssembly.GetCustomAttributes (true); 68 attr = attributes [0] as AttributeUsageAttribute; 69 } 70 71 [Test] AttributeUsageTest()72 public void AttributeUsageTest () 73 { 74 Assert.AreEqual ( 75 attr.ValidOn, 76 AttributeTargets.Assembly, "#1"); 77 Assert.AreEqual ( 78 attr.AllowMultiple, 79 false, "#2"); 80 Assert.AreEqual ( 81 attr.Inherited, 82 true, "#3"); 83 } 84 85 [Test] TypeIdTest()86 public void TypeIdTest () 87 { 88 Assert.AreEqual ( 89 attr.TypeId, 90 typeof (AttributeUsageAttribute), "#1" 91 ); 92 } 93 94 [Test] MatchTestForTrue()95 public void MatchTestForTrue () 96 { 97 Assert.AreEqual ( 98 attr.Match (attr), 99 true, "#1"); 100 } 101 102 [Test] MatchTestForFalse()103 public void MatchTestForFalse () 104 { 105 Assert.AreEqual ( 106 attr.Match (new AttributeUsageAttribute (AttributeTargets.Method)), 107 false, "#1"); 108 } 109 #endif 110 [Test] CtorTest()111 public void CtorTest () 112 { 113 var a = new AttributeUsageAttribute (AttributeTargets.Method); 114 Assert.AreEqual (AttributeTargets.Method, a.ValidOn); 115 Assert.False (a.AllowMultiple); 116 Assert.True (a.Inherited); 117 } 118 119 [Test] PropertyTest()120 public void PropertyTest () 121 { 122 var a = new AttributeUsageAttribute (AttributeTargets.Method | AttributeTargets.Class); 123 a.AllowMultiple = true; 124 a.Inherited = false; 125 Assert.AreEqual (AttributeTargets.Method | AttributeTargets.Class, a.ValidOn); 126 Assert.True (a.AllowMultiple); 127 Assert.False (a.Inherited); 128 } 129 } 130 } 131 132