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.

169 lines
4.4 KiB

  1. using OT.COM.LogisticsUtil;
  2. using System;
  3. using System.Collections.Concurrent;
  4. using Xunit;
  5. namespace TestUnit.OT.COM.LogisticsUtil
  6. {
  7. public class Test_Util
  8. {
  9. public class A
  10. {
  11. public string S { get; set; } = null;
  12. public int? N { get; set; } = null;
  13. }
  14. [Fact]
  15. public async void DeepClone()
  16. {
  17. A a1 = new () { N = 9, S = "AAAA" };
  18. A a2 = await Util.DeepClone(a1);
  19. a2.S = "888";
  20. Assert.True(a1.S != a2.S, "Clone value fail");
  21. Assert.True(a1 != a2, "Clone fail");
  22. }
  23. [Fact]
  24. public void GetDefaultTempPath()
  25. {
  26. Assert.NotNull(Util.GetDefaultTempPath());
  27. }
  28. [Fact]
  29. public void GetProcessName()
  30. {
  31. Assert.NotNull(Util.GetProcessName());
  32. }
  33. [Theory]
  34. [InlineData("KEY_EXIST", "AAA", "9")]
  35. [InlineData("KEY_NOTEXOST", null, null)]
  36. [InlineData("KEY_NOTEXOST", "3", "3")]
  37. public string GetSettingString(string i_sKey, string i_nDefault, string i_nRealResult)
  38. {
  39. string sMsg = null;
  40. string sResult = null;
  41. try
  42. {
  43. do
  44. {
  45. ConcurrentDictionary<string, string> cd = new ConcurrentDictionary<string, string>();
  46. cd.TryAdd("KEY_EXIST", "9");
  47. Util.SetSetting(cd);
  48. sMsg = Util.GetSettingString(out sResult, i_sKey, i_nDefault);
  49. }
  50. while (false);
  51. }
  52. catch (Exception ex)
  53. {
  54. sMsg = $"{nameof(GetSettingString)} exception.";
  55. #if DEBUG
  56. sMsg += Util.GetLastExceptionMsg(ex);
  57. #endif
  58. }
  59. #if DEBUG
  60. if (sMsg != null)
  61. {
  62. System.Diagnostics.Debug.WriteLine(sMsg);
  63. }
  64. #endif
  65. Assert.True(sMsg == null, sMsg);
  66. Assert.True(i_nRealResult == sResult, sMsg);
  67. return sMsg;
  68. }
  69. [Theory]
  70. [InlineData("KEY_EXIST", 1, 9)]
  71. [InlineData("KEY_NOTEXOST", 2, 2)]
  72. [InlineData("KEY_NOTEXOST", 3, 3)]
  73. public string GetSettingInt(string i_sKey, int i_nDefault, int i_nRealResult)
  74. {
  75. string sMsg = null;
  76. int nResult = -10;
  77. try
  78. {
  79. do
  80. {
  81. ConcurrentDictionary<string, string> cd = new ConcurrentDictionary<string, string>();
  82. cd.TryAdd("KEY_EXIST", "9");
  83. Util.SetSetting(cd);
  84. sMsg = Util.GetSettingInt(out nResult, i_sKey, i_nDefault);
  85. }
  86. while (false);
  87. }
  88. catch (Exception ex)
  89. {
  90. sMsg = $"{nameof(GetSettingInt)} exception.";
  91. #if DEBUG
  92. sMsg += Util.GetLastExceptionMsg(ex);
  93. #endif
  94. }
  95. #if DEBUG
  96. if (sMsg != null)
  97. {
  98. System.Diagnostics.Debug.WriteLine(sMsg);
  99. }
  100. #endif
  101. Assert.True(sMsg == null, sMsg);
  102. Assert.True(i_nRealResult == nResult, sMsg);
  103. return sMsg;
  104. }
  105. [Theory]
  106. [InlineData("KEY_EXIST", true, true)]
  107. [InlineData("KEY_NOTEXOST", true, true)]
  108. [InlineData("KEY_NOTEXOST", false, false)]
  109. public string GetSettingBoolean(string i_sKey, bool i_bDefault, bool i_bRealResult)
  110. {
  111. string sMsg = null;
  112. bool bResult = false;
  113. try
  114. {
  115. do
  116. {
  117. ConcurrentDictionary<string, string> cd = new ConcurrentDictionary<string, string>();
  118. cd.TryAdd("KEY_EXIST", "True");
  119. Util.SetSetting(cd);
  120. sMsg = Util.GetSettingBoolean(out bResult, i_sKey, i_bDefault);
  121. }
  122. while (false);
  123. }
  124. catch (Exception ex)
  125. {
  126. sMsg = $"{nameof(GetSettingBoolean)} exception.";
  127. #if DEBUG
  128. sMsg += Util.GetLastExceptionMsg(ex);
  129. #endif
  130. }
  131. #if DEBUG
  132. if (sMsg != null)
  133. {
  134. System.Diagnostics.Debug.WriteLine(sMsg);
  135. }
  136. #endif
  137. Assert.True(sMsg == null, sMsg);
  138. Assert.True(i_bRealResult == bResult, sMsg);
  139. return sMsg;
  140. }
  141. }
  142. }