1 // 2 // AssemblyFlagsAttributeTest.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.Reflection { 36 37 /// <summary> 38 /// Summary description for AssemblyFlagsAttributeTest. 39 /// </summary> 40 [TestFixture] 41 public class AssemblyFlagsAttributeTest 42 { 43 #if !MOBILE 44 private AssemblyBuilder dynAssembly; 45 AssemblyName dynAsmName = new AssemblyName (); 46 AssemblyFlagsAttribute attr; 47 AssemblyFlagsAttributeTest()48 public AssemblyFlagsAttributeTest () 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 (AssemblyFlagsAttribute); 61 ConstructorInfo ctrInfo = attribute.GetConstructor ( 62 new Type [] { typeof (AssemblyNameFlags) } 63 ); 64 CustomAttributeBuilder attrBuilder = 65 new CustomAttributeBuilder (ctrInfo, new object [1] { AssemblyNameFlags.PublicKey | AssemblyNameFlags.Retargetable }); 66 dynAssembly.SetCustomAttribute (attrBuilder); 67 object [] attributes = dynAssembly.GetCustomAttributes (true); 68 attr = attributes [0] as AssemblyFlagsAttribute; 69 } 70 71 [Test] AssemblyFlagsTest()72 public void AssemblyFlagsTest () 73 { 74 Assert.AreEqual ( 75 attr.AssemblyFlags, 76 (int)(AssemblyNameFlags.PublicKey | AssemblyNameFlags.Retargetable), "#1"); 77 } 78 79 [Test] TypeIdTest()80 public void TypeIdTest () 81 { 82 Assert.AreEqual ( 83 attr.TypeId, 84 typeof (AssemblyFlagsAttribute), "#1" 85 ); 86 } 87 88 [Test] MatchTestForTrue()89 public void MatchTestForTrue () 90 { 91 Assert.AreEqual ( 92 attr.Match (attr), 93 true, "#1"); 94 } 95 96 [Test] MatchTestForFalse()97 public void MatchTestForFalse () 98 { 99 Assert.AreEqual ( 100 attr.Match (new AssemblyFlagsAttribute (AssemblyNameFlags.None)), 101 false, "#1"); 102 } 103 #endif 104 [Test] CtorTest()105 public void CtorTest () 106 { 107 var a = new AssemblyFlagsAttribute (AssemblyNameFlags.PublicKey); 108 Assert.AreEqual ((int)AssemblyNameFlags.PublicKey, a.AssemblyFlags); 109 110 a = new AssemblyFlagsAttribute ((int)AssemblyNameFlags.PublicKey); 111 Assert.AreEqual ((int)AssemblyNameFlags.PublicKey, a.AssemblyFlags); 112 113 a = new AssemblyFlagsAttribute ((uint)AssemblyNameFlags.PublicKey); 114 Assert.AreEqual ((uint)AssemblyNameFlags.PublicKey, a.Flags); 115 } 116 } 117 } 118 119