-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathParserTests.cs
65 lines (53 loc) · 2.09 KB
/
ParserTests.cs
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
using System.Collections.Generic;
using System.Linq;
using Dust.Compiler;
using Dust.Compiler.Diagnostics;
using Dust.Compiler.Lexer;
using Dust.Compiler.Parser;
using Dust.Compiler.Parser.SyntaxTree;
using JetBrains.Annotations;
using KellermanSoftware.CompareNetObjects;
using Xunit;
namespace Dust.UnitTests.Parser
{
public class ParserTests : ExpectTests<SyntaxParseResult, ParserTests.ExceptToFunctions>
{
protected CodeBlockNode root;
protected SourceRange range;
protected List<SyntaxToken> tokens;
private readonly SyntaxLexer lexer = new SyntaxLexer();
private readonly SyntaxParser parser = new SyntaxParser();
private static readonly CompareLogic comparer = new CompareLogic();
protected virtual void Setup(string code)
{
range = SourceRange.FromText(code);
root = new CodeBlockNode(range: range);
tokens = lexer.Lex(code);
}
protected SyntaxParseResult Parse()
{
return parser.Parse(tokens);
}
[UsedImplicitly(ImplicitUseKindFlags.InstantiatedNoFixedConstructorSignature)]
public class ExceptToFunctions
{
private readonly SyntaxParseResult result;
public ExceptToFunctions(SyntaxParseResult result)
{
this.result = result;
}
public ExpectFunctions ParseSuccessfully()
{
Assert.True(result.Diagnostics.Any((diagnostic) => diagnostic.Severity == DiagnosticSeverity.Error) == false, $"Failed to parse\n{string.Join('\n', result.Diagnostics)}");
return new ExpectFunctions(result);
}
public ExpectFunctions SyntaxTreeOf(SyntaxNode node)
{
ComparisonResult comparisonResult = comparer.Compare(result.Node, node);
string[] differences = comparisonResult.Differences.Select((difference) => $"{difference.ExpectedName}.{difference.PropertyName} ({difference.Object2Value}) != {difference.ActualName}.{difference.PropertyName} ({(difference.Object1)})").ToArray();
Assert.True(comparisonResult.AreEqual, $"Syntax tree mismatch\n{string.Join("\n", differences.ToArray())}");
return new ExpectFunctions(result);
}
}
}
}