/Tests/TechTalk.SpecFlow.Specs/Drivers/ReportInfo.cs
C# | 77 lines | 67 code | 10 blank | 0 comment | 4 complexity | c3ae16a9e8613a0a089d2a5fd9ea622e MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0
- using System.IO;
- using System.Text.RegularExpressions;
- using FluentAssertions;
- using NUnit.Framework;
- namespace TechTalk.SpecFlow.Specs.Drivers
- {
- public class ReportInfo
- {
- private string content;
- public string Content
- {
- get
- {
- if (content == null)
- {
- AssertExists();
- content = File.ReadAllText(FilePath);
- }
- return content;
- }
- set { content = value; }
- }
- public string FilePath { get; set; }
- public void AssertExists()
- {
- File.Exists(FilePath).Should().BeTrue("no result is generated");
- }
- public void AssertEqualIgnoringWhitespace(string expectedValue)
- {
- StringAssert.AreEqualIgnoringCase(
- NormalizeWhitespace(CleanHtml(expectedValue)),
- NormalizeWhitespace(CleanHtml(Content)));
- }
- public void AssertContainsIgnoringWhitespace(string expectedValue)
- {
- StringAssert.Contains(
- NormalizeWhitespace(HtmlEncode(expectedValue)).ToLowerInvariant(),
- NormalizeWhitespace(CleanHtml(Content)).ToLowerInvariant());
- }
- private string NormalizeWhitespace(string value)
- {
- var whitespaceRe = new Regex(@"\s+");
- return whitespaceRe.Replace(value.Trim(), "");
- }
- private string HtmlEncode(string value)
- {
- return value.Replace("<", "<").Replace(">", ">");
- }
- private string CleanHtml(string value)
- {
- var bodyRe = new Regex(@"\<\/?\s*body\s*\>");
- var bodyMatch = bodyRe.Match(value);
- if (bodyMatch.Success)
- {
- value = value.Substring(bodyMatch.Index + bodyMatch.Value.Length);
- bodyMatch = bodyRe.Match(value);
- if (bodyMatch.Success)
- value = value.Substring(0, bodyMatch.Index);
- }
- var htmlTagRe = new Regex(@"\<.*?\>");
- value = htmlTagRe.Replace(value.Trim(), " ");
- var nbspRe = new Regex(@"\ ", RegexOptions.IgnoreCase);
- value = nbspRe.Replace(value, " ");
- return value;
- }
- }
- }