VisibilityTest.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // VisibilityTest.cs
  2. //
  3. //
  4. // Author:
  5. // Marius Ungureanu <[email protected]>
  6. //
  7. // Copyright (c) 2015 Xamarin, Inc (http://www.xamarin.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. #if !MONOTOUCH && !FULL_AOT_RUNTIME
  27. using System;
  28. using System.Linq;
  29. using System.Reflection;
  30. using NUnit.Framework;
  31. using System.Collections;
  32. namespace MonoTests.System.Reflection.VisibilityTypes
  33. {
  34. [TestFixture]
  35. public class VisibilityTests
  36. {
  37. static void DoesNotContain (IEnumerable collection, object val)
  38. {
  39. Assert.That(collection, Has.No.Member(val));
  40. }
  41. static void Contains (IEnumerable collection, object val)
  42. {
  43. Assert.That(collection, Has.Member(val));
  44. }
  45. [Test]
  46. public void TestsExportedTypes ()
  47. {
  48. var types = typeof (VisibilityTests).Assembly.GetExportedTypes ();
  49. // Test visibility means that the class is public by applying and on the 'public' visibility of the nested items.
  50. DoesNotContain (types, typeof (InternalClass));
  51. Contains (types, typeof (PublicClass));
  52. DoesNotContain (types, Type.GetType ("MonoTests.System.Reflection.VisibilityTypes.VisibilityTests+InternalClass+InternalNested", true));
  53. DoesNotContain (types, Type.GetType ("MonoTests.System.Reflection.VisibilityTypes.VisibilityTests+InternalClass+PrivateNested", true));
  54. DoesNotContain (types, Type.GetType ("MonoTests.System.Reflection.VisibilityTypes.VisibilityTests+InternalClass+ProtectedNested", true));
  55. DoesNotContain (types, Type.GetType ("MonoTests.System.Reflection.VisibilityTypes.VisibilityTests+InternalClass+PublicNested", true));
  56. DoesNotContain (types, Type.GetType ("MonoTests.System.Reflection.VisibilityTypes.VisibilityTests+PublicClass+InternalNested", true));
  57. DoesNotContain (types, Type.GetType ("MonoTests.System.Reflection.VisibilityTypes.VisibilityTests+PublicClass+PrivateNested", true));
  58. DoesNotContain (types, Type.GetType ("MonoTests.System.Reflection.VisibilityTypes.VisibilityTests+PublicClass+ProtectedNested", true));
  59. Contains (types, Type.GetType ("MonoTests.System.Reflection.VisibilityTypes.VisibilityTests+PublicClass+PublicNested", true));
  60. }
  61. [Test]
  62. public void TestsModuleTypes ()
  63. {
  64. var types = typeof (VisibilityTests).Module.GetTypes ();
  65. // Test that all the types defined exist.
  66. Contains (types, typeof (InternalClass));
  67. Contains (types, typeof (PublicClass));
  68. Contains (types, Type.GetType ("MonoTests.System.Reflection.VisibilityTypes.VisibilityTests+InternalClass+InternalNested", true));
  69. Contains (types, Type.GetType ("MonoTests.System.Reflection.VisibilityTypes.VisibilityTests+InternalClass+PrivateNested", true));
  70. Contains (types, Type.GetType ("MonoTests.System.Reflection.VisibilityTypes.VisibilityTests+InternalClass+ProtectedNested", true));
  71. Contains (types, Type.GetType ("MonoTests.System.Reflection.VisibilityTypes.VisibilityTests+InternalClass+PublicNested", true));
  72. Contains (types, Type.GetType ("MonoTests.System.Reflection.VisibilityTypes.VisibilityTests+PublicClass+InternalNested", true));
  73. Contains (types, Type.GetType ("MonoTests.System.Reflection.VisibilityTypes.VisibilityTests+PublicClass+PrivateNested", true));
  74. Contains (types, Type.GetType ("MonoTests.System.Reflection.VisibilityTypes.VisibilityTests+PublicClass+ProtectedNested", true));
  75. Contains (types, Type.GetType ("MonoTests.System.Reflection.VisibilityTypes.VisibilityTests+PublicClass+PublicNested", true));
  76. }
  77. class InternalClass
  78. {
  79. internal class InternalNested {}
  80. private class PrivateNested {}
  81. protected class ProtectedNested {}
  82. public class PublicNested {}
  83. }
  84. public class PublicClass
  85. {
  86. internal class InternalNested {}
  87. private class PrivateNested {}
  88. protected class ProtectedNested {}
  89. public class PublicNested {}
  90. }
  91. }
  92. }
  93. #endif