/Tests/UnitTests/Foundation.Tests/Hydra/Modules/ErrorAnalyzer/UnitTest1.cs

https://github.com/DavidMoore/Foundation · C# · 186 lines · 140 code · 40 blank · 6 comment · 1 complexity · 000c03d94a04e5311a4a610abf405bf4 MD5 · raw file

  1. using System;
  2. using System.Globalization;
  3. using System.IO;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text.RegularExpressions;
  9. using Foundation.Windows;
  10. using Microsoft.Practices.Prism.Modularity;
  11. using Microsoft.VisualStudio.TestTools.UnitTesting;
  12. namespace Foundation.Tests.Hydra.Modules.ErrorAnalyzer
  13. {
  14. [TestClass]
  15. public class ErrorCodeParserTests
  16. {
  17. [TestMethod]
  18. public void Parse_Win32_error_code()
  19. {
  20. var parser = new ErrorCodeParser();
  21. var result = parser.Parse("2");
  22. Assert.IsNotNull(result);
  23. Assert.AreEqual(2, result.Value);
  24. Assert.IsFalse(result.IsHR);
  25. Assert.AreEqual(-2147024894, result.HR);
  26. Assert.AreEqual("The system cannot find the file specified.", result.ErrorMessage.Trim());
  27. Assert.IsTrue( result.Exception is FileNotFoundException);
  28. }
  29. [TestMethod]
  30. public void Parse_long_HRESULT()
  31. {
  32. var parser = new ErrorCodeParser();
  33. var result = parser.Parse("-2147024894");
  34. Assert.IsNotNull(result);
  35. Assert.AreEqual(-2147024894, result.Value);
  36. Assert.IsTrue(result.IsHR);
  37. Assert.AreEqual(-2147024894, result.HR);
  38. Assert.AreEqual("The system cannot find the file specified.", result.ErrorMessage.Trim());
  39. Assert.IsTrue(result.Exception is FileNotFoundException);
  40. }
  41. [TestMethod]
  42. public void Parse_short_hex_HRESULT()
  43. {
  44. var parser = new ErrorCodeParser();
  45. var result = parser.Parse("0x80070002");
  46. Assert.IsNotNull(result);
  47. Assert.AreEqual(-2147024894, result.Value);
  48. Assert.IsTrue(result.IsHR);
  49. Assert.AreEqual(-2147024894, result.HR);
  50. Assert.AreEqual("The system cannot find the file specified.", result.ErrorMessage.Trim());
  51. Assert.IsTrue(result.Exception is FileNotFoundException);
  52. }
  53. [TestMethod]
  54. public void IsHex()
  55. {
  56. Assert.IsTrue(ErrorCodeParser.IsHexString("0xABC"));
  57. Assert.IsTrue(ErrorCodeParser.IsHexString("0x000"));
  58. Assert.IsFalse(ErrorCodeParser.IsHexString("0000"));
  59. }
  60. }
  61. class ErrorCodeParser
  62. {
  63. const string hexRegexString = @"^\w*0x([a-fA-F0-9]+)\w*$";
  64. static readonly Regex hexRegex = new Regex(hexRegexString, RegexOptions.Compiled);
  65. public ErrorCode Parse(string errorString)
  66. {
  67. var errorCode = new ErrorCode();
  68. if (IsHexString(errorString))
  69. {
  70. errorCode.Value = int.Parse(errorString.Substring(2).Trim(), NumberStyles.HexNumber);
  71. }
  72. else
  73. {
  74. errorCode.Value = int.Parse(errorString);
  75. }
  76. errorCode.IsHR = Win32Error.IsHR(errorCode.Value);
  77. errorCode.ErrorMessage = Win32Error.GetMessage(errorCode.Value);
  78. errorCode.HR = Win32Error.MakeHRFromErrorCode(errorCode.Value);
  79. errorCode.Exception = Marshal.GetExceptionForHR(errorCode.HR);
  80. return errorCode;
  81. }
  82. static internal bool IsHexString(string value)
  83. {
  84. return hexRegex.IsMatch(value);
  85. }
  86. }
  87. class ErrorAnalyzerModule : IModule
  88. {
  89. public ErrorAnalyzerModule(IErrorAnalyzerModel model) {}
  90. /// <summary>
  91. /// Notifies the module that it has be initialized.
  92. /// </summary>
  93. public void Initialize()
  94. {
  95. throw new NotImplementedException();
  96. }
  97. }
  98. interface IErrorAnalyzerModel : IModel
  99. {
  100. }
  101. interface IErrorAnalyzerViewModel : IViewModel
  102. {
  103. ErrorCode ErrorCode { get; set; }
  104. }
  105. class ErrorCode
  106. {
  107. public int Value { get; set; }
  108. public ErrorCode(int intValue)
  109. {
  110. Value = intValue;
  111. IsHR = Win32Error.IsHR(Value);
  112. }
  113. public ErrorCode()
  114. {
  115. }
  116. public int HR { get; set; }
  117. public bool IsHR { get; set; }
  118. public string ErrorMessage { get; set; }
  119. public Exception Exception { get; set; }
  120. }
  121. interface IViewModel {}
  122. interface IErrorAnalyzerView : IView<IErrorAnalyzerViewModel>
  123. {
  124. }
  125. class ErrorAnalyzerView : IErrorAnalyzerView
  126. {
  127. public IErrorAnalyzerViewModel ViewModel { get; set; }
  128. }
  129. interface IView<TViewModel>
  130. {
  131. TViewModel ViewModel { get; set; }
  132. }
  133. class ErrorAnalyzerModel : IErrorAnalyzerModel
  134. {
  135. /// <summary>
  136. /// Initializes this instance.
  137. /// </summary>
  138. public void Initialize()
  139. {
  140. throw new NotImplementedException();
  141. }
  142. }
  143. interface IModel : ICanInitialize
  144. {
  145. }
  146. }