PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Tests/TechTalk.SpecFlow.Specs/Drivers/ReportInfo.cs

http://github.com/techtalk/SpecFlow
C# | 77 lines | 67 code | 10 blank | 0 comment | 4 complexity | c3ae16a9e8613a0a089d2a5fd9ea622e MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0
  1. using System.IO;
  2. using System.Text.RegularExpressions;
  3. using FluentAssertions;
  4. using NUnit.Framework;
  5. namespace TechTalk.SpecFlow.Specs.Drivers
  6. {
  7. public class ReportInfo
  8. {
  9. private string content;
  10. public string Content
  11. {
  12. get
  13. {
  14. if (content == null)
  15. {
  16. AssertExists();
  17. content = File.ReadAllText(FilePath);
  18. }
  19. return content;
  20. }
  21. set { content = value; }
  22. }
  23. public string FilePath { get; set; }
  24. public void AssertExists()
  25. {
  26. File.Exists(FilePath).Should().BeTrue("no result is generated");
  27. }
  28. public void AssertEqualIgnoringWhitespace(string expectedValue)
  29. {
  30. StringAssert.AreEqualIgnoringCase(
  31. NormalizeWhitespace(CleanHtml(expectedValue)),
  32. NormalizeWhitespace(CleanHtml(Content)));
  33. }
  34. public void AssertContainsIgnoringWhitespace(string expectedValue)
  35. {
  36. StringAssert.Contains(
  37. NormalizeWhitespace(HtmlEncode(expectedValue)).ToLowerInvariant(),
  38. NormalizeWhitespace(CleanHtml(Content)).ToLowerInvariant());
  39. }
  40. private string NormalizeWhitespace(string value)
  41. {
  42. var whitespaceRe = new Regex(@"\s+");
  43. return whitespaceRe.Replace(value.Trim(), "");
  44. }
  45. private string HtmlEncode(string value)
  46. {
  47. return value.Replace("<", "&lt;").Replace(">", "&gt;");
  48. }
  49. private string CleanHtml(string value)
  50. {
  51. var bodyRe = new Regex(@"\<\/?\s*body\s*\>");
  52. var bodyMatch = bodyRe.Match(value);
  53. if (bodyMatch.Success)
  54. {
  55. value = value.Substring(bodyMatch.Index + bodyMatch.Value.Length);
  56. bodyMatch = bodyRe.Match(value);
  57. if (bodyMatch.Success)
  58. value = value.Substring(0, bodyMatch.Index);
  59. }
  60. var htmlTagRe = new Regex(@"\<.*?\>");
  61. value = htmlTagRe.Replace(value.Trim(), " ");
  62. var nbspRe = new Regex(@"\&nbsp;", RegexOptions.IgnoreCase);
  63. value = nbspRe.Replace(value, " ");
  64. return value;
  65. }
  66. }
  67. }