/rocks/Test/Mono.Cecil.Tests/TypeReferenceRocksTests.cs

http://github.com/jbevain/cecil · C# · 124 lines · 89 code · 35 blank · 0 comment · 0 complexity · 7f26c996458d70d3aee05ce4aa762c82 MD5 · raw file

  1. using System;
  2. using Mono.Cecil.Rocks;
  3. using NUnit.Framework;
  4. namespace Mono.Cecil.Tests {
  5. [TestFixture]
  6. public class TypeReferenceRocksTests {
  7. [Test]
  8. public void MakeArrayType ()
  9. {
  10. var @string = GetTypeReference (typeof (string));
  11. var string_array = @string.MakeArrayType ();
  12. Assert.IsInstanceOf (typeof (ArrayType), string_array);
  13. Assert.AreEqual (1, string_array.Rank);
  14. }
  15. [Test]
  16. public void MakeArrayTypeRank ()
  17. {
  18. var @string = GetTypeReference (typeof (string));
  19. var string_array = @string.MakeArrayType (3);
  20. Assert.IsInstanceOf (typeof (ArrayType), string_array);
  21. Assert.AreEqual (3, string_array.Rank);
  22. }
  23. [Test]
  24. public void MakePointerType ()
  25. {
  26. var @string = GetTypeReference (typeof (string));
  27. var string_ptr = @string.MakePointerType ();
  28. Assert.IsInstanceOf (typeof (PointerType), string_ptr);
  29. }
  30. [Test]
  31. public void MakeByReferenceType ()
  32. {
  33. var @string = GetTypeReference (typeof (string));
  34. var string_byref = @string.MakeByReferenceType ();
  35. Assert.IsInstanceOf (typeof (ByReferenceType), string_byref);
  36. }
  37. class OptionalModifier {}
  38. [Test]
  39. public void MakeOptionalModifierType ()
  40. {
  41. var @string = GetTypeReference (typeof (string));
  42. var modopt = GetTypeReference (typeof (OptionalModifier));
  43. var string_modopt = @string.MakeOptionalModifierType (modopt);
  44. Assert.IsInstanceOf (typeof (OptionalModifierType), string_modopt);
  45. Assert.AreEqual (modopt, string_modopt.ModifierType);
  46. }
  47. class RequiredModifier { }
  48. [Test]
  49. public void MakeRequiredModifierType ()
  50. {
  51. var @string = GetTypeReference (typeof (string));
  52. var modreq = GetTypeReference (typeof (RequiredModifierType));
  53. var string_modreq = @string.MakeRequiredModifierType (modreq);
  54. Assert.IsInstanceOf (typeof (RequiredModifierType), string_modreq);
  55. Assert.AreEqual (modreq, string_modreq.ModifierType);
  56. }
  57. [Test]
  58. public void MakePinnedType ()
  59. {
  60. var byte_array = GetTypeReference (typeof (byte []));
  61. var pinned_byte_array = byte_array.MakePinnedType ();
  62. Assert.IsInstanceOf (typeof (PinnedType), pinned_byte_array);
  63. }
  64. [Test]
  65. public void MakeSentinelType ()
  66. {
  67. var @string = GetTypeReference (typeof (string));
  68. var string_sentinel = @string.MakeSentinelType ();
  69. Assert.IsInstanceOf (typeof (SentinelType), string_sentinel);
  70. }
  71. class Foo<T1, T2> {}
  72. [Test]
  73. public void MakeGenericInstanceType ()
  74. {
  75. var foo = GetTypeReference (typeof (Foo<,>));
  76. var @string = GetTypeReference (typeof (string));
  77. var @int = GetTypeReference (typeof (int));
  78. var foo_string_int = foo.MakeGenericInstanceType (@string, @int);
  79. Assert.IsInstanceOf (typeof (GenericInstanceType), foo_string_int);
  80. Assert.AreEqual (2, foo_string_int.GenericArguments.Count);
  81. Assert.AreEqual (@string, foo_string_int.GenericArguments [0]);
  82. Assert.AreEqual (@int, foo_string_int.GenericArguments [1]);
  83. }
  84. static TypeReference GetTypeReference (Type type)
  85. {
  86. return ModuleDefinition.ReadModule (typeof (TypeReferenceRocksTests).Module.FullyQualifiedName).ImportReference (type);
  87. }
  88. }
  89. }