1 //
2 // AssemblyDefaultAliasAttributeTest.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 AssemblyDefaultAliasAttributeTest.
39 	/// </summary>
40 	[TestFixture]
41 	public class AssemblyDefaultAliasAttributeTest
42 	{
43 #if !MOBILE
44 		private AssemblyBuilder dynAssembly;
45 		AssemblyName dynAsmName = new AssemblyName ();
46 		AssemblyDefaultAliasAttribute attr;
47 
AssemblyDefaultAliasAttributeTest()48 		public AssemblyDefaultAliasAttributeTest ()
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 (AssemblyDefaultAliasAttribute);
61 			ConstructorInfo ctrInfo = attribute.GetConstructor (
62 				new Type [] { typeof (string) }
63 				);
64 			CustomAttributeBuilder attrBuilder =
65 				new CustomAttributeBuilder (ctrInfo, new object [1] { "corlib" });
66 			dynAssembly.SetCustomAttribute (attrBuilder);
67 			object [] attributes = dynAssembly.GetCustomAttributes (true);
68 			attr = attributes [0] as AssemblyDefaultAliasAttribute;
69 		}
70 
71 		[Test]
DefaultAliasTest()72 		public void DefaultAliasTest ()
73 		{
74 			Assert.AreEqual (
75 				attr.DefaultAlias,
76 				"corlib", "#1");
77 		}
78 
79 		[Test]
TypeIdTest()80 		public void TypeIdTest ()
81 		{
82 			Assert.AreEqual (
83 				attr.TypeId,
84 				typeof (AssemblyDefaultAliasAttribute), "#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 AssemblyDefaultAliasAttribute ("System")),
101 				false, "#1");
102 		}
103 #endif
104 		[Test]
CtorTest()105 		public void CtorTest ()
106 		{
107 			var a = new AssemblyDefaultAliasAttribute ("some text");
108 			Assert.AreEqual ("some text", a.DefaultAlias);
109 		}
110 	}
111 }
112 
113