-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathParserDriver.cs
More file actions
165 lines (134 loc) · 6.24 KB
/
Copy pathParserDriver.cs
File metadata and controls
165 lines (134 loc) · 6.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using FluentAssertions;
using Gherkin;
using Gherkin.Ast;
using Reqnroll.Parser;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
namespace Reqnroll.Specs.Drivers.Parser
{
public class ParserDriver
{
readonly JsonSerializerOptions _serializerOptions = new()
{
WriteIndented = true,
TypeInfoResolver = new PolymorphicTypeResolver(),
};
/// <summary>
/// We don't want to decorate Gherkin objects with System.Text.Json Attributes, that's why poloymophy is handled manually in this class
/// </summary>
sealed class PolymorphicTypeResolver : DefaultJsonTypeInfoResolver
{
readonly Dictionary<Type, Type[]> _Inheritance = new()
{
{ typeof(IHasLocation), [typeof(Tag), typeof(Comment), typeof(ReqnrollFeature), typeof(Background), typeof(Scenario), typeof(ScenarioOutline), typeof(Examples), typeof(ReqnrollStep), typeof(TableCell)] },
{ typeof(StepsContainer), [typeof(Background), typeof(Scenario), typeof(ScenarioOutline)] },
{ typeof(Step), [typeof(ReqnrollStep)] },
{ typeof(Feature), [typeof(ReqnrollFeature)] },
{ typeof(StepArgument), [typeof(Gherkin.Ast.DataTable), typeof(DocString)] },
};
public override JsonTypeInfo GetTypeInfo(Type type, JsonSerializerOptions options)
{
JsonTypeInfo jsonTypeInfo = base.GetTypeInfo(type, options);
if (!_Inheritance.TryGetValue(type, out var derivedTypes))
return jsonTypeInfo;
var polymorphismOptions = new JsonPolymorphismOptions
{
UnknownDerivedTypeHandling = JsonUnknownDerivedTypeHandling.FailSerialization,
};
foreach (var derivedType in derivedTypes)
polymorphismOptions.DerivedTypes.Add(new JsonDerivedType(derivedType));
jsonTypeInfo.PolymorphismOptions = polymorphismOptions;
return jsonTypeInfo;
}
}
private readonly ReqnrollGherkinParser _parser = new ReqnrollGherkinParser(new CultureInfo("en-US"));
public string FileContent { get; set; }
public ReqnrollDocument ParsedDocument { get; private set; }
public ParserException[] ParsingErrors { get; private set; }
public void ParseFile()
{
var contentReader = new StringReader(FileContent);
ParsedDocument = null;
ParsingErrors = new ParserException[0];
try
{
ParsedDocument = _parser.Parse(contentReader, new ReqnrollDocumentLocation("sample.feature"));
ParsedDocument.Should().NotBeNull();
}
catch (ParserException ex)
{
ParsingErrors = ex.GetParserExceptions();
Console.WriteLine("-> parsing errors");
foreach (var error in ParsingErrors)
{
Console.WriteLine("-> {0}:{1} {2}", error.Location?.Line ?? 0, error.Location?.Column ?? 0, error.Message);
}
}
}
public void AssertParsedFeatureEqualTo(string expected)
{
static string Normalize(string value)
=> value.Replace("\r", "").Replace(@"\r", "");
string got = SerializeDocument(ParsedDocument);
got = Normalize(got);
var expectedNormalized = Normalize(expected);
got.Should().Be(expectedNormalized);
}
public void AssertErrors(List<ExpectedError> expectedErrors)
{
expectedErrors.Should().NotBeEmpty("please specify expected errors");
ParsingErrors.Should().NotBeEmpty("The parsing was successful");
foreach (var expectedError in expectedErrors)
{
string message = expectedError.Error.ToLower();
var errorDetail =
ParsingErrors.FirstOrDefault(ed => ed.Location != null && ed.Location?.Line == expectedError.Line &&
ed.Message.ToLower().Contains(message));
errorDetail.Should().NotBeNull("no such error: {0}", message);
}
}
public void AssertTableHasColumns(params string[] expectedColumns)
{
ParsedDocument.Should().NotBeNull("The parsing was not successful");
var scenario = ParsedDocument.ReqnrollFeature.Children.OfType<Scenario>().FirstOrDefault();
scenario.Should().NotBeNull("No scenario found in the parsed document");
var step = scenario.Steps.FirstOrDefault();
step.Should().NotBeNull("No step found in the scenario");
var table = step.Argument as Gherkin.Ast.DataTable;
table.Should().NotBeNull("No table argument found in the step");
var headerRow = table.Rows.FirstOrDefault();
headerRow.Should().NotBeNull("Table has no rows");
var actualColumns = headerRow.Cells.Select(c => c.Value).ToList();
actualColumns.Should().BeEquivalentTo(expectedColumns,
"the parsed table should have the expected columns");
}
public void SaveSerializedFeatureTo(string fileName)
{
ParsedDocument.Should().NotBeNull("The parsing was not successful");
SerializeDocument(ParsedDocument, fileName);
}
private void SerializeDocument(ReqnrollDocument feature, string fileName)
{
using (var writer = new StreamWriter(fileName, false, Encoding.UTF8))
{
writer.Write(SerializeDocument(feature));
}
}
private string SerializeDocument(ReqnrollDocument feature)
{
return JsonSerializer.Serialize(feature, _serializerOptions);
}
}
public class ExpectedError
{
public int? Line { get; set; }
public string Error { get; set; }
}
}