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

http://github.com/jbevain/cecil · C# · 97 lines · 75 code · 22 blank · 0 comment · 0 complexity · b7fb2c7316b569eecd8a1d2dfebfb5e2 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Mono.Cecil.Rocks;
  5. using NUnit.Framework;
  6. namespace Mono.Cecil.Tests {
  7. [TestFixture]
  8. public class TypeDefinitionRocksTests {
  9. class Foo {
  10. static Foo ()
  11. {
  12. }
  13. public Foo (int a)
  14. {
  15. }
  16. public Foo (int a, string s)
  17. {
  18. }
  19. public static void Bar ()
  20. {
  21. }
  22. void Baz ()
  23. {
  24. }
  25. }
  26. [Test]
  27. public void GetConstructors ()
  28. {
  29. var foo = typeof (Foo).ToDefinition ();
  30. var ctors = foo.GetConstructors ().Select (ctor => ctor.FullName);
  31. var expected = new [] {
  32. "System.Void Mono.Cecil.Tests.TypeDefinitionRocksTests/Foo::.cctor()",
  33. "System.Void Mono.Cecil.Tests.TypeDefinitionRocksTests/Foo::.ctor(System.Int32)",
  34. "System.Void Mono.Cecil.Tests.TypeDefinitionRocksTests/Foo::.ctor(System.Int32,System.String)",
  35. };
  36. AssertSet (expected, ctors);
  37. }
  38. static void AssertSet<T> (IEnumerable<T> expected, IEnumerable<T> actual)
  39. {
  40. Assert.IsFalse (expected.Except (actual).Any ());
  41. Assert.IsTrue (expected.Intersect (actual).SequenceEqual (expected));
  42. }
  43. [Test]
  44. public void GetStaticConstructor ()
  45. {
  46. var foo = typeof (Foo).ToDefinition ();
  47. var cctor = foo.GetStaticConstructor ();
  48. Assert.IsNotNull (cctor);
  49. Assert.AreEqual ("System.Void Mono.Cecil.Tests.TypeDefinitionRocksTests/Foo::.cctor()", cctor.FullName);
  50. }
  51. [Test]
  52. public void GetMethods ()
  53. {
  54. var foo = typeof (Foo).ToDefinition ();
  55. var methods = foo.GetMethods ().ToArray ();
  56. Assert.AreEqual (2, methods.Length);
  57. Assert.AreEqual ("System.Void Mono.Cecil.Tests.TypeDefinitionRocksTests/Foo::Bar()", methods [0].FullName);
  58. Assert.AreEqual ("System.Void Mono.Cecil.Tests.TypeDefinitionRocksTests/Foo::Baz()", methods [1].FullName);
  59. }
  60. enum Pan : byte {
  61. Pin,
  62. Pon,
  63. }
  64. [Test]
  65. public void GetEnumUnderlyingType ()
  66. {
  67. var pan = typeof (Pan).ToDefinition ();
  68. Assert.IsNotNull (pan);
  69. Assert.IsTrue (pan.IsEnum);
  70. var underlying_type = pan.GetEnumUnderlyingType ();
  71. Assert.IsNotNull (underlying_type);
  72. Assert.AreEqual ("System.Byte", underlying_type.FullName);
  73. }
  74. }
  75. }