Canon/Canon.Tests/GeneratedParserTests/GenerateParserTests.cs
Ichirinko dbbab1c761 feat-visualization (#33)
尽管里面有点粪,但还是生成了漂亮的树,就当给树施肥了
剩下一个小问题:未考虑节点的宽度(因为名称不一样长),但目前未发现对图生成的明显影响

Co-authored-by: jackfiled <xcrenchangjun@outlook.com>
Reviewed-on: PostGuard/Canon#33
Co-authored-by: Ichirinko <1621543655@qq.com>
Co-committed-by: Ichirinko <1621543655@qq.com>
2024-04-19 14:59:45 +08:00

75 lines
2.6 KiB
C#

using Canon.Core.Abstractions;
using Canon.Core.Enums;
using Canon.Core.GrammarParser;
namespace Canon.Tests.GeneratedParserTests;
public class GenerateParserTests
{
private readonly GrammarBuilder _builder = new()
{
Generators = PascalGrammar.Grammar, Begin = new NonTerminator(NonTerminatorType.StartNonTerminator)
};
private readonly IGrammarParser _parser;
private readonly Grammar _grammar;
public GenerateParserTests()
{
_grammar = _builder.Build();
_parser = _grammar.ToGrammarParser();
}
[Fact]
public void ConsistencyTests()
{
GeneratedGrammarParser generatedGrammarParser = GeneratedGrammarParser.Instance;
ITransformer originTransformer = _parser.BeginTransformer;
ITransformer generatedTransformer = generatedGrammarParser.BeginTransformer;
Queue<(ITransformer, ITransformer)> transformerQueue = [];
transformerQueue.Enqueue((originTransformer, generatedTransformer));
HashSet<string> visited = [];
while (transformerQueue.Count != 0)
{
(originTransformer, generatedTransformer) = transformerQueue.Dequeue();
if (visited.Contains(generatedTransformer.Name))
{
continue;
}
visited.Add(generatedTransformer.Name);
foreach (KeyValuePair<TerminatorBase, ITransformer> pair in originTransformer.ShiftTable)
{
Assert.True(generatedTransformer.ShiftTable.TryGetValue(pair.Key, out ITransformer? nextTransformer));
Assert.NotNull(nextTransformer);
transformerQueue.Enqueue((pair.Value, nextTransformer));
}
foreach (KeyValuePair<Terminator, ReduceInformation> pair in originTransformer.ReduceTable)
{
Assert.True(generatedTransformer.ReduceTable.TryGetValue(pair.Key, out ReduceInformation? information));
Assert.NotNull(information);
Assert.Equal(pair.Value.Length, information.Length);
Assert.Equal(pair.Value.Left, information.Left);
}
}
}
[Fact]
public void SubprogramDeclarationsFirstSetTest()
{
Assert.True(_builder.FirstSet.TryGetValue(
new NonTerminator(NonTerminatorType.SubprogramDeclarations), out HashSet<Terminator>? firstSet));
Assert.NotNull(firstSet);
Assert.Contains(Terminator.EmptyTerminator, firstSet);
Assert.Contains(new Terminator(KeywordType.Procedure), firstSet);
Assert.Contains(new Terminator(KeywordType.Function), firstSet);
}
}