PageRenderTime 56ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/WebEssentialsTests/TestCodeExtensions.cs

https://github.com/systembugtj/WebEssentials2013
C# | 51 lines | 44 code | 7 blank | 0 comment | 3 complexity | f3b169bf0cee2179d8c1783f53777962 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. using System;
  2. using System.Linq;
  3. using System.Text;
  4. using FluentAssertions;
  5. using FluentAssertions.Execution;
  6. using FluentAssertions.Primitives;
  7. namespace WebEssentialsTests
  8. {
  9. public static class TestCodeExtensions
  10. {
  11. private static AndConstraint<StringAssertions> BeCode(this StringAssertions assertion, string expectedCode)
  12. {
  13. var lines = TrimLines(assertion.Subject);
  14. var expectedLines = TrimLines(expectedCode);
  15. Execute.Assertion
  16. .ForCondition(lines.Length == expectedLines.Length)
  17. .FailWith(
  18. "Expected Code \r\n{2} \r\nhas {0} lines but \r\n{3}\r\n has {1} lines, empty lines don't count",
  19. expectedLines.Length,
  20. lines.Length,
  21. string.Join(Environment.NewLine, expectedLines),
  22. string.Join(Environment.NewLine, lines)
  23. );
  24. for (int i = 0; i < lines.Length; i++)
  25. {
  26. Execute.Assertion
  27. .ForCondition(lines[i] == expectedLines[i])
  28. .FailWith(
  29. "Expected code in line {0} is \r\n{1}\r\n but was \r\n{2}\r\n, whitespaces and the begining and end are not relevant",
  30. i + 1, expectedLines[i], lines[i]);
  31. }
  32. return new AndConstraint<StringAssertions>(assertion);
  33. }
  34. private static string[] TrimLines(string toString)
  35. {
  36. string[] lines = toString.Split(new[] { Environment.NewLine, "\n" }, StringSplitOptions.RemoveEmptyEntries);
  37. return lines.Select(t => t.Trim()).Where(trimmedLine => !string.IsNullOrWhiteSpace(trimmedLine)).ToArray();
  38. }
  39. public static AndConstraint<StringAssertions> ShouldBeCode(this StringBuilder sb, string expectedCode)
  40. {
  41. return sb.ToString().Should().BeCode(expectedCode);
  42. }
  43. }
  44. }