You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

190 lines
6.2 KiB

  1. using OT.COM.Encryption;
  2. using OT.COM.LogisticsUtil;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using Xunit;
  10. namespace TestUnit.OT.COM.LogisticsUtil
  11. {
  12. public class Test_ClassHelper
  13. {
  14. [Theory]
  15. [InlineData("Util", typeof(Util))]
  16. [InlineData("Util1", null)]
  17. public void GetTypeByTypeName(string i_sTypeName, Type i_tExcept)
  18. {
  19. Type tResult = ClassHelper.GetTypeByTypeName(i_sTypeName);
  20. Assert.True(tResult == i_tExcept, $"Cannot create type '{i_sTypeName}'");
  21. }
  22. [Theory]
  23. [InlineData("OT.COM.LogisticsUtil.ValidatorHelper", typeof(ValidatorHelper))]
  24. [InlineData("Util1", null)]
  25. public void GetTypeByFullName(string i_sTypeName, Type i_tExcept)
  26. {
  27. Type tResult = ClassHelper.GetTypeByFullName(i_sTypeName);
  28. Assert.True(tResult == i_tExcept, $"Cannot create type '{i_sTypeName}'");
  29. }
  30. [Theory]
  31. [InlineData("OT.COM.Encryption.AES", typeof(AES))]
  32. [InlineData("Util1", null)]
  33. public void GetInstByFullName(string i_sTypeName, Type i_tExcept)
  34. {
  35. string sMsg = ClassHelper.GetInstByFullName(i_sTypeName, out object oInst, null);
  36. if (i_tExcept != null)
  37. {
  38. Assert.True(sMsg == null, sMsg);
  39. Assert.True(oInst != null && oInst.GetType() == i_tExcept, $"Create '{i_sTypeName}' success but type is not expect '{i_tExcept.FullName}'");
  40. }
  41. else
  42. {
  43. Assert.True(sMsg != null && oInst == null, $"Create '{i_sTypeName}' success but should not happen");
  44. }
  45. }
  46. [Theory]
  47. [InlineData("OT.COM.LogisticsUtil.ValidatorHelper", typeof(ValidatorHelper))]
  48. [InlineData("Util1", null)]
  49. public void GetTypeByFullNameEndWithTerm(string i_sTypeName, Type i_tExcept)
  50. {
  51. Type tResult = ClassHelper.GetTypeByFullNameEndWithTerm(i_sTypeName);
  52. Assert.True(tResult == i_tExcept, $"Cannot create type '{i_sTypeName}'");
  53. }
  54. [Theory]
  55. [InlineData("Encryption.AES", typeof(AES))]
  56. [InlineData("Util1", null)]
  57. public void GetInstByFullNameEndWithTerm(string i_sTypeName, Type i_tExcept)
  58. {
  59. string sMsg = ClassHelper.GetInstByFullNameEndWithTerm(i_sTypeName, out object oInst);
  60. if (i_tExcept != null)
  61. {
  62. Assert.True(sMsg == null, sMsg);
  63. Assert.True(oInst != null && oInst.GetType() == i_tExcept, $"Create '{i_sTypeName}' success but type is not expect '{i_tExcept.FullName}'");
  64. }
  65. else
  66. {
  67. Assert.True(sMsg != null && oInst == null, $"Create '{i_sTypeName}' success but should not happen");
  68. }
  69. }
  70. [Theory]
  71. [InlineData(typeof(AES), typeof(AES))]
  72. [InlineData(null, null)]
  73. public void GetInstByType(Type i_sTypeName, Type i_tExcept)
  74. {
  75. string sMsg = ClassHelper.GetInstByType(i_sTypeName, out object oInst);
  76. if (i_tExcept != null)
  77. {
  78. Assert.True(sMsg == null, sMsg);
  79. Assert.True(oInst != null && oInst.GetType() == i_tExcept, $"Create '{i_sTypeName}' success but type is not expect '{i_tExcept.FullName}'");
  80. }
  81. else
  82. {
  83. Assert.True(sMsg != null && oInst == null, $"Create '{i_sTypeName}' success but should not happen");
  84. }
  85. }
  86. [Theory]
  87. [InlineData("AES", typeof(AES))]
  88. [InlineData("Util1", null)]
  89. public void GetInstByClassName(string i_sTypeName, Type i_tExcept)
  90. {
  91. string sMsg = ClassHelper.GetInstByClassName(i_sTypeName, out object oInst);
  92. if (i_tExcept != null)
  93. {
  94. Assert.True(sMsg == null, sMsg);
  95. Assert.True(oInst != null && oInst.GetType() == i_tExcept, $"Create '{i_sTypeName}' success but type is not expect '{i_tExcept.FullName}'");
  96. }
  97. else
  98. {
  99. Assert.True(sMsg != null && oInst == null, $"Create '{i_sTypeName}' success but should not happen");
  100. }
  101. }
  102. [Theory]
  103. [InlineData(typeof(AES), typeof(AES))]
  104. [InlineData(typeof(int ?), typeof(int))]
  105. public void GetRealType(Type i_sTypeName, Type i_tExcept)
  106. {
  107. Type r = ClassHelper.GetRealType(i_sTypeName);
  108. if (i_tExcept != null)
  109. {
  110. Assert.True(r == i_tExcept, $"Create '{i_sTypeName}' success but type is not expect '{i_tExcept.FullName}'");
  111. }
  112. else
  113. {
  114. Assert.True(r == null, $"Create '{i_sTypeName}' success but should not happen");
  115. }
  116. }
  117. [Theory]
  118. [InlineData(typeof(int), "1", 1 )]
  119. [InlineData(typeof(string), 2, "2")]
  120. public void ConvertValue(Type i_sTypeName, object i_oOrigin, object i_tTarget)
  121. {
  122. object t = ClassHelper.ConvertValue(i_sTypeName, i_oOrigin);
  123. if (i_tTarget != null)
  124. {
  125. Assert.True(t != null && t.ToString() == i_tTarget.ToString(), $"Create '{i_sTypeName}' success but type is not expect '{i_tTarget.ToString()}'");
  126. }
  127. else
  128. {
  129. Assert.True(t == null, $"Create '{i_sTypeName}' success but should not happen");
  130. }
  131. }
  132. public class TestA
  133. {
  134. public int A { get; set; }
  135. }
  136. [Theory]
  137. [InlineData("3", 3)]
  138. public void ConvertValue2(object i_oOrigin, object i_tTarget)
  139. {
  140. PropertyInfo pi = typeof(TestA).GetProperties().First();
  141. object t = ClassHelper.ConvertValue(pi, i_oOrigin);
  142. if (i_tTarget != null)
  143. {
  144. Assert.True(t != null && t.ToString() == i_tTarget.ToString(), $"Create '{pi}' success but type is not expect '{i_tTarget.ToString()}'");
  145. }
  146. else
  147. {
  148. Assert.True(t == null, $"Create '{pi}' success but should not happen");
  149. }
  150. }
  151. }
  152. }