1
1

ReflectionUtilTests.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System;
  2. using System.Reflection;
  3. using Masuit.Tools.Reflection;
  4. using Xunit;
  5. namespace Masuit.Tools.Abstractions.Test.Refection;
  6. public class ReflectionUtilTests
  7. {
  8. private class TestClass
  9. {
  10. public string TestProperty { get; set; }
  11. public string TestField;
  12. public string TestMethod(string input) => $"Hello, {input}";
  13. }
  14. [Fact]
  15. public void InvokeMethod_ShouldInvokeMethodAndReturnResult()
  16. {
  17. var obj = new TestClass();
  18. var result = obj.InvokeMethod<string>("TestMethod", new object[] { "World" });
  19. Assert.Equal("Hello, World", result);
  20. }
  21. [Fact]
  22. public void InvokeMethod_ShouldInvokeMethodWithoutReturn()
  23. {
  24. var obj = new TestClass();
  25. obj.InvokeMethod("TestMethod", new object[] { "World" });
  26. }
  27. [Fact]
  28. public void SetField_ShouldSetFieldValue()
  29. {
  30. var obj = new TestClass();
  31. obj.SetField("TestField", "New Value");
  32. Assert.Equal("New Value", obj.TestField);
  33. }
  34. [Fact]
  35. public void GetField_ShouldGetFieldValue()
  36. {
  37. var obj = new TestClass { TestField = "Field Value" };
  38. var result = obj.GetField<string>("TestField");
  39. Assert.Equal("Field Value", result);
  40. }
  41. [Fact]
  42. public void GetFields_ShouldReturnAllFields()
  43. {
  44. var obj = new TestClass();
  45. var fields = obj.GetFields();
  46. Assert.Contains(fields, f => f.Name == "TestField");
  47. }
  48. [Fact]
  49. public void SetProperty_ShouldSetPropertyValue()
  50. {
  51. var obj = new TestClass();
  52. obj.SetProperty("TestProperty", "New Value");
  53. Assert.Equal("New Value", obj.TestProperty);
  54. }
  55. [Fact]
  56. public void GetProperty_ShouldGetPropertyValue()
  57. {
  58. var obj = new TestClass { TestProperty = "Property Value" };
  59. var result = obj.GetProperty<string>("TestProperty");
  60. Assert.Equal("Property Value", result);
  61. }
  62. [Fact]
  63. public void GetProperties_ShouldReturnAllProperties()
  64. {
  65. var obj = new TestClass();
  66. var properties = obj.GetProperties();
  67. Assert.Contains(properties, p => p.Name == "TestProperty");
  68. }
  69. [Fact]
  70. public void GetDescription_ShouldReturnDescription()
  71. {
  72. var member = typeof(TestClass).GetProperty("TestProperty");
  73. var description = member.GetDescription();
  74. Assert.Equal(string.Empty, description);
  75. }
  76. [Fact]
  77. public void GetAttribute_ShouldReturnAttribute()
  78. {
  79. var member = typeof(TestClass).GetProperty("TestProperty");
  80. var attribute = member.GetAttribute<ObsoleteAttribute>();
  81. Assert.Null(attribute);
  82. }
  83. [Fact]
  84. public void GetAttributes_ShouldReturnAttributes()
  85. {
  86. var member = typeof(TestClass).GetProperty("TestProperty");
  87. var attributes = member.GetAttributes<ObsoleteAttribute>();
  88. Assert.Empty(attributes);
  89. }
  90. [Fact]
  91. public void GetImageResource_ShouldReturnStream()
  92. {
  93. var stream = Assembly.GetExecutingAssembly().GetImageResource("resourceName");
  94. Assert.Null(stream);
  95. }
  96. [Fact]
  97. public void GetManifestString_ShouldReturnString()
  98. {
  99. var result = typeof(TestClass).GetManifestString("UTF-8", "resName");
  100. Assert.Equal(string.Empty, result);
  101. }
  102. [Fact]
  103. public void IsImplementsOf_ShouldReturnTrueForImplementedInterface()
  104. {
  105. var result = typeof(TestClass).IsImplementsOf(typeof(IDisposable));
  106. Assert.False(result);
  107. }
  108. [Fact]
  109. public void GetInstance_ShouldReturnInstance()
  110. {
  111. var instance = typeof(TestClass).GetInstance();
  112. Assert.NotNull(instance);
  113. }
  114. [Fact]
  115. public void GetLoadableTypes_ShouldReturnTypes()
  116. {
  117. var types = Assembly.GetExecutingAssembly().GetLoadableTypes();
  118. Assert.NotEmpty(types);
  119. }
  120. [Fact]
  121. public void GetLoadableExportedTypes_ShouldReturnExportedTypes()
  122. {
  123. var types = Assembly.GetExecutingAssembly().GetLoadableExportedTypes();
  124. Assert.NotEmpty(types);
  125. }
  126. }