1 //
2 // MarshalAsAttributeTest.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 System.Runtime.InteropServices;
34 using NUnit.Framework;
35 
36 namespace MonoTests.System.Runtime.InteropServices {
37 
38 	/// <summary>
39 	/// Summary description for MarshalAsAttributeTest.
40 	/// </summary>
41 	[TestFixture]
42 	public class MarshalAsAttributeTest
43 	{
44 #if !MOBILE
45 		private AssemblyBuilder dynAssembly;
46 		AssemblyName dynAsmName = new AssemblyName ();
47 		MarshalAsAttribute attr;
48 
MarshalAsAttributeTest()49 		public MarshalAsAttributeTest ()
50 		{
51 			//create a dynamic assembly with the required attribute
52 			//and check for the validity
53 
54 			dynAsmName.Name = "TestAssembly";
55 
56 			dynAssembly = Thread.GetDomain ().DefineDynamicAssembly (
57 				dynAsmName,AssemblyBuilderAccess.Run
58 				);
59 
60 			// Set the required Attribute of the assembly.
61 			Type attribute = typeof (MarshalAsAttribute);
62 			ConstructorInfo ctrInfo = attribute.GetConstructor (
63 				new Type [] { typeof (UnmanagedType) }
64 				);
65 			CustomAttributeBuilder attrBuilder =
66 				new CustomAttributeBuilder (ctrInfo, new object [1] { UnmanagedType.LPWStr });
67 			dynAssembly.SetCustomAttribute (attrBuilder);
68 			object [] attributes = dynAssembly.GetCustomAttributes (true);
69 			attr = attributes [0] as MarshalAsAttribute;
70 		}
71 
72 		[Test]
MarshalAsTest()73 		public void MarshalAsTest ()
74 		{
75 			Assert.AreEqual (
76 				attr.Value,
77 				UnmanagedType.LPWStr, "#1");
78 		}
79 
80 		[Test]
TypeIdTest()81 		public void TypeIdTest ()
82 		{
83 			Assert.AreEqual (
84 				attr.TypeId,
85 				typeof (MarshalAsAttribute), "#1"
86 				);
87 		}
88 
89 		[Test]
MatchTestForTrue()90 		public void MatchTestForTrue ()
91 		{
92 			Assert.AreEqual (
93 				attr.Match (attr),
94 				true, "#1");
95 		}
96 
97 		[Test]
MatchTestForFalse()98 		public void MatchTestForFalse ()
99 		{
100 			Assert.AreEqual (
101 				attr.Match (new MarshalAsAttribute (UnmanagedType.Bool)),
102 				false, "#1");
103 		}
104 #endif
105 		[Test]
CtorTest()106 		public void CtorTest ()
107 		{
108 			var a = new MarshalAsAttribute (UnmanagedType.Bool);
109 			Assert.AreEqual (UnmanagedType.Bool, a.Value);
110 
111 			a = new MarshalAsAttribute ((short)UnmanagedType.Bool);
112 			Assert.AreEqual (UnmanagedType.Bool, a.Value);
113 		}
114 
115 		[Test]
FieldsTest()116 		public void FieldsTest ()
117 		{
118 			var a = new MarshalAsAttribute (UnmanagedType.Bool);
119 
120 			Assert.Null (a.MarshalCookie);
121 			Assert.Null (a.MarshalType);
122 			Assert.Null (a.MarshalTypeRef);
123 			Assert.Null (a.SafeArrayUserDefinedSubType);
124 			Assert.AreEqual ((UnmanagedType)0, a.ArraySubType);
125 			Assert.AreEqual (VarEnum.VT_EMPTY, a.SafeArraySubType);
126 			Assert.AreEqual (0, a.SizeConst);
127 			Assert.AreEqual (0, a.IidParameterIndex);
128 			Assert.AreEqual (0, a.SizeParamIndex);
129 		}
130 	}
131 }
132 
133