PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Compilers/CSharp/Test/WinRT/Metadata/WinMdMetadataTests.cs

https://gitlab.com/sharadag/Roslyn
C# | 245 lines | 177 code | 28 blank | 40 comment | 6 complexity | 97253b29ede3eb89ee88ef36f3feff07 MD5 | raw file
  1. // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using Microsoft.CodeAnalysis;
  8. using Microsoft.CodeAnalysis.CSharp.Symbols.Metadata.PE;
  9. using Microsoft.CodeAnalysis.CSharp.Symbols;
  10. using Microsoft.CodeAnalysis.CSharp.Syntax;
  11. using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
  12. using Microsoft.CodeAnalysis.Test.Utilities;
  13. using Microsoft.CodeAnalysis.Text;
  14. using Roslyn.Test.Utilities;
  15. using Xunit;
  16. namespace Microsoft.CodeAnalysis.CSharp.UnitTests
  17. {
  18. // Unit tests for programs that use the Windows.winmd file.
  19. //
  20. // Checks to see that types are forwarded correctly, that
  21. // metadata files are loaded as they should, etc.
  22. public class WinMdMetadataTests : CSharpTestBase
  23. {
  24. /// <summary>
  25. /// Make sure that the members of a function are forwarded to their appropriate types.
  26. /// We do this by checking that the first parameter of
  27. /// Windows.UI.Text.ITextRange.SetPoint(Point p...) gets forwarded to the
  28. /// System.Runtime.WindowsRuntime assembly.
  29. /// </summary>
  30. [Fact]
  31. public void FunctionPrototypeForwarded()
  32. {
  33. var text = "public class A{};";
  34. var comp = CreateWinRtCompilation(text);
  35. var winmdlib = comp.ExternalReferences.Where(r => r.Display == "Windows").Single();
  36. var winmdNS = comp.GetReferencedAssemblySymbol(winmdlib);
  37. var wns1 = winmdNS.GlobalNamespace.GetMember<NamespaceSymbol>("Windows");
  38. wns1 = wns1.GetMember<NamespaceSymbol>("UI");
  39. wns1 = wns1.GetMember<NamespaceSymbol>("Text");
  40. var itextrange = wns1.GetMember<PENamedTypeSymbol>("ITextRange");
  41. var func = itextrange.GetMember<PEMethodSymbol>("SetPoint");
  42. var pt = ((PEParameterSymbol)(func.Parameters[0])).Type as PENamedTypeSymbol;
  43. Assert.Equal(pt.ContainingAssembly.Name, "System.Runtime.WindowsRuntime");
  44. }
  45. /// <summary>
  46. /// Make sure that a delegate defined in Windows.winmd has a public constructor
  47. /// (by default, all delegates in Windows.winmd have a private constructor).
  48. /// </summary>
  49. [Fact]
  50. public void DelegateConstructorMarkedPublic()
  51. {
  52. var text = "public class A{};";
  53. var comp = CreateWinRtCompilation(text);
  54. var winmdlib = comp.ExternalReferences.Where(r => r.Display == "Windows").Single();
  55. var winmdNS = comp.GetReferencedAssemblySymbol(winmdlib);
  56. var wns1 = winmdNS.GlobalNamespace.GetMember<NamespaceSymbol>("Windows");
  57. wns1 = wns1.GetMember<NamespaceSymbol>("UI");
  58. wns1 = wns1.GetMember<NamespaceSymbol>("Xaml");
  59. var itextrange = wns1.GetMember<PENamedTypeSymbol>("SuspendingEventHandler");
  60. var func = itextrange.GetMember<PEMethodSymbol>(".ctor");
  61. Assert.Equal(func.DeclaredAccessibility, Accessibility.Public);
  62. }
  63. /// <summary>
  64. /// Verify that Windows.Foundation.Uri forwards successfully
  65. /// to System.Uri
  66. /// </summary>
  67. [Fact]
  68. public void TypeForwardingRenaming()
  69. {
  70. var text = "public class A{};";
  71. var comp = CreateWinRtCompilation(text);
  72. var winmdlib = comp.ExternalReferences.Where(r => r.Display == "Windows").Single();
  73. var winmdNS = comp.GetReferencedAssemblySymbol(winmdlib);
  74. var wns1 = winmdNS.GlobalNamespace.GetMember<NamespaceSymbol>("Windows");
  75. wns1 = wns1.GetMember<NamespaceSymbol>("Foundation");
  76. var iref = wns1.GetMember<PENamedTypeSymbol>("IUriRuntimeClass");
  77. var func = iref.GetMember<PEMethodSymbol>("CombineUri");
  78. var ret = func.ReturnType;
  79. Assert.Equal(func.ReturnType.ToTestDisplayString(), "System.Uri");
  80. }
  81. /// <summary>
  82. /// Verify that WinMd types are marked as private so that the
  83. /// C# developer cannot instantiate them.
  84. /// </summary>
  85. [Fact]
  86. public void WinMdTypesDefPrivate()
  87. {
  88. var text = "public class A{};";
  89. var comp = CreateWinRtCompilation(text);
  90. var winmdlib = comp.ExternalReferences.Where(r => r.Display == "Windows").Single();
  91. var winmdNS = comp.GetReferencedAssemblySymbol(winmdlib);
  92. var wns1 = winmdNS.GlobalNamespace.GetMember<NamespaceSymbol>("Windows");
  93. var wns2 = wns1.GetMember<NamespaceSymbol>("Foundation");
  94. var clas = wns2.GetMember<PENamedTypeSymbol>("Point");
  95. Assert.Equal(clas.DeclaredAccessibility, Accessibility.Internal);
  96. }
  97. /// <summary>
  98. /// Verify that Windows.UI.Colors.Black is forwarded to the
  99. /// System.Runtime.WindowsRuntime.dll assembly.
  100. /// </summary>
  101. [Fact]
  102. public void WinMdColorType()
  103. {
  104. var text = "public class A{};";
  105. var comp = CreateWinRtCompilation(text);
  106. var winmdlib = comp.ExternalReferences.Where(r => r.Display == "Windows").Single();
  107. var winmdNS = comp.GetReferencedAssemblySymbol(winmdlib);
  108. var wns1 = winmdNS.GlobalNamespace.GetMember<NamespaceSymbol>("Windows");
  109. var wns2 = wns1.GetMember<NamespaceSymbol>("UI");
  110. var clas = wns2.GetMember<TypeSymbol>("Colors");
  111. var blk = clas.GetMembers("Black").Single();
  112. //The windows.winmd module points to a Windows.UI.Color which should be modified to belong
  113. //to System.Runtime.WindowsRuntime
  114. Assert.Equal(((Microsoft.CodeAnalysis.CSharp.Symbols.Metadata.PE.PENamedTypeSymbol)
  115. ((((Microsoft.CodeAnalysis.CSharp.Symbols.PropertySymbol)(blk)).GetMethod).ReturnType)).ContainingModule.ToString(),
  116. "System.Runtime.WindowsRuntime.dll");
  117. }
  118. /// <summary>
  119. /// Ensure that a simple program that uses projected types can compile
  120. /// and run.
  121. /// </summary>
  122. [ConditionalFact(typeof(OSVersionWin8))]
  123. public void WinMdColorTest()
  124. {
  125. var text = @"using Windows.UI;
  126. using Windows.Foundation;
  127. public class A{
  128. public static void Main(){
  129. var a = Colors.Black;
  130. System.Console.WriteLine(a.ToString());
  131. }
  132. };";
  133. CompileAndVerify(text, WinRtRefs, expectedOutput: "#FF000000");
  134. }
  135. /// <summary>
  136. /// Test that the metadata adapter correctly projects IReference to INullable
  137. /// </summary>
  138. [Fact]
  139. public void IReferenceToINullableType()
  140. {
  141. var text = "public class A{};";
  142. var comp = CreateWinRtCompilation(text);
  143. var winmdlib = comp.ExternalReferences.Where(r => r.Display == "Windows").Single();
  144. var winmdNS = comp.GetReferencedAssemblySymbol(winmdlib);
  145. var wns1 = winmdNS.GlobalNamespace.GetMember<NamespaceSymbol>("Windows");
  146. var wns2 = wns1.GetMember<NamespaceSymbol>("Globalization");
  147. var wns3 = wns2.GetMember<NamespaceSymbol>("NumberFormatting");
  148. var clas = wns3.GetMember<TypeSymbol>("DecimalFormatter");
  149. var puint = clas.GetMembers("ParseUInt").Single();
  150. // The return type of ParseUInt should be Nullable<ulong>, not IReference<ulong>
  151. Assert.Equal("ulong?",
  152. ((Microsoft.CodeAnalysis.CSharp.Symbols.ConstructedNamedTypeSymbol)
  153. (((Microsoft.CodeAnalysis.CSharp.Symbols.MethodSymbol)puint).ReturnType)).ToString());
  154. }
  155. /// <summary>
  156. /// Test that a program projects IReference to INullable.
  157. /// </summary>
  158. [Fact]
  159. public void WinMdIReferenceINullableTest()
  160. {
  161. var source =
  162. @"using System;
  163. using Windows.Globalization.NumberFormatting;
  164. public class C
  165. {
  166. public static void Main(string[] args)
  167. {
  168. var format = new DecimalFormatter();
  169. ulong result = format.ParseUInt(""10"") ?? 0;
  170. Console.WriteLine(result);
  171. result = format.ParseUInt(""-1"") ?? 0;
  172. Console.WriteLine(result);
  173. }
  174. }";
  175. var verifier = CompileAndVerifyOnWin8Only(source,
  176. expectedOutput: "10\r\n0");
  177. verifier.VerifyDiagnostics();
  178. }
  179. [Fact, WorkItem(1169511, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/1169511")]
  180. public void WinMdAssemblyQualifiedType()
  181. {
  182. var source =
  183. @"using System;
  184. [MyAttribute(typeof(C1))]
  185. public class C
  186. {
  187. public static void Main(string[] args)
  188. {
  189. }
  190. }
  191. public class MyAttribute : System.Attribute
  192. {
  193. public MyAttribute(System.Type type)
  194. {
  195. }
  196. }
  197. ";
  198. CompileAndVerify(
  199. source,
  200. WinRtRefs.Concat(new[] { AssemblyMetadata.CreateFromImage(TestResources.WinRt.W1).GetReference() }),
  201. symbolValidator: m =>
  202. {
  203. var module = (PEModuleSymbol)m;
  204. var c = (PENamedTypeSymbol)module.GlobalNamespace.GetTypeMember("C");
  205. var attributeHandle = module.Module.MetadataReader.GetCustomAttributes(c.Handle).Single();
  206. string value;
  207. module.Module.TryExtractStringValueFromAttribute(attributeHandle, out value);
  208. Assert.Equal("C1, W, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime", value);
  209. });
  210. }
  211. }
  212. }