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

/Tests/TechTalk.SpecFlow.Specs/Drivers/Parser/ParserDriver.cs

http://github.com/techtalk/SpecFlow
C# | 108 lines | 91 code | 17 blank | 0 comment | 3 complexity | f05aedb02cbe16111373f94faed8b6ee MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using FluentAssertions;
  8. using Gherkin;
  9. using Newtonsoft.Json;
  10. using TechTalk.SpecFlow.Parser;
  11. namespace TechTalk.SpecFlow.Specs.Drivers.Parser
  12. {
  13. public class ParserDriver
  14. {
  15. private readonly JsonSerializer _serializer = new JsonSerializer();
  16. private readonly SpecFlowGherkinParser _parser = new SpecFlowGherkinParser(new CultureInfo("en-US"));
  17. public string FileContent { get; set; }
  18. public SpecFlowDocument ParsedDocument { get; private set; }
  19. public ParserException[] ParsingErrors { get; private set; }
  20. public void ParseFile()
  21. {
  22. var contentReader = new StringReader(FileContent);
  23. ParsedDocument = null;
  24. ParsingErrors = new ParserException[0];
  25. try
  26. {
  27. ParsedDocument = _parser.Parse(contentReader, "sample.feature");
  28. ParsedDocument.Should().NotBeNull();
  29. }
  30. catch (ParserException ex)
  31. {
  32. ParsingErrors = ex.GetParserExceptions();
  33. Console.WriteLine("-> parsing errors");
  34. foreach (var error in ParsingErrors)
  35. {
  36. Console.WriteLine("-> {0}:{1} {2}", error.Location?.Line ?? 0, error.Location?.Column ?? 0, error.Message);
  37. }
  38. }
  39. }
  40. public void AssertParsedFeatureEqualTo(string parsedFeatureXml)
  41. {
  42. const string ns1 = "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"";
  43. const string ns2 = "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"";
  44. string expected = parsedFeatureXml.Replace("\r", "").Replace(ns1, "").Replace(ns2, "");
  45. string got = SerializeDocument(ParsedDocument).Replace("\r", "").Replace(ns1, "").Replace(ns2, "");
  46. got.Should().Be(expected);
  47. }
  48. public void AssertErrors(List<ExpectedError> expectedErrors)
  49. {
  50. expectedErrors.Should().NotBeEmpty("please specify expected errors");
  51. ParsingErrors.Should().NotBeEmpty("The parsing was successful");
  52. foreach (var expectedError in expectedErrors)
  53. {
  54. string message = expectedError.Error.ToLower();
  55. var errorDetail =
  56. ParsingErrors.FirstOrDefault(ed => ed.Location != null && ed.Location.Line == expectedError.Line &&
  57. ed.Message.ToLower().Contains(message));
  58. errorDetail.Should().NotBeNull("no such error: {0}", message);
  59. }
  60. }
  61. public void SaveSerializedFeatureTo(string fileName)
  62. {
  63. ParsedDocument.Should().NotBeNull("The parsing was not successful");
  64. SerializeDocument(ParsedDocument, fileName);
  65. }
  66. private void SerializeDocument(SpecFlowDocument feature, string fileName)
  67. {
  68. using (var writer = new StreamWriter(fileName, false, Encoding.UTF8))
  69. {
  70. SerializeDocument(feature, writer);
  71. }
  72. }
  73. private string SerializeDocument(SpecFlowDocument feature)
  74. {
  75. using (var writer = new Utf8StringWriter())
  76. {
  77. SerializeDocument(feature, writer);
  78. return writer.ToString();
  79. }
  80. }
  81. private void SerializeDocument(SpecFlowDocument feature, TextWriter writer)
  82. {
  83. _serializer.Serialize(writer, feature);
  84. }
  85. }
  86. public class ExpectedError
  87. {
  88. public int? Line { get; set; }
  89. public string Error { get; set; }
  90. }
  91. }