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.

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