feat: finally able to overcome the base of the base of this f*cked up mess

This commit is contained in:
mattia
2025-03-01 17:06:56 +01:00
parent eed28168ad
commit 6ee8051004
20 changed files with 518 additions and 2306 deletions

View File

@@ -20,12 +20,15 @@ public class ContentTextTest
var fileReader = new InMemoryFileReader([
("main.ink", inkInput)
]);
var parser = new InkBlotParser();
using var loggerFactory = Helpers.GetLoggerFactory();
var (story, diagnostics) = InkBlotParser.Parse(fileReader, loggerFactory, "main.ink");
var (story, diagnosticsEnumerable) = InkBlotParser.Parse(fileReader, loggerFactory, "main.ink");
var diagnostics = diagnosticsEnumerable.ToArray();
// check the diagnostic counts match
diagnostics.Count().ShouldBe(numErrors);
if (numErrors == 0)
diagnostics.ShouldBe([]);
else
diagnostics.Count().ShouldBe(numErrors);
// check the contents match (only if there was no diagnostic)
if (numErrors != 0) return;

View File

@@ -5,23 +5,36 @@ namespace InkBlot.Tests;
public class LexerTest
{
private readonly TokenEqualityComparer _tokenEqualityComparer = new();
private IToken T(string text, int type)
{
return new SimpleToken(text, type);
}
[Fact]
public void TestSimpleTokens()
{
var tokens = GetTokens("testTest15");
tokens.ShouldBe([
T("testTest15", InkBlotAntlrGrammarLexer.IDENTIFIER),
T("<EOF>", InkBlotAntlrGrammarLexer.Eof)
], _tokenEqualityComparer);
}
[Fact]
public void TestDivertForLexer()
{
var divertTokens = GetTokens("<- threadName");
divertTokens.ShouldSatisfyAllConditions(
() => divertTokens[0].Text.ShouldBe("<-"),
() => divertTokens[0].Type.ShouldBe(InkBlotAntlrGrammarLexer.THREAD_ARROW),
() => divertTokens[1].Text.ShouldBe(" "),
() => divertTokens[1].Type.ShouldBe(InkBlotAntlrGrammarLexer.WS),
() => divertTokens[2].Text.ShouldBe("threadName"),
() => divertTokens[2].Type.ShouldBe(InkBlotAntlrGrammarLexer.IDENTIFIER),
() => divertTokens[3].Text.ShouldBe("<EOF>"),
() => divertTokens[3].Type.ShouldBe(InkBlotAntlrGrammarLexer.Eof)
);
divertTokens.ShouldBe([
T("<-", InkBlotAntlrGrammarLexer.THREAD_ARROW),
T(" ", InkBlotAntlrGrammarLexer.WS),
T("threadName", InkBlotAntlrGrammarLexer.IDENTIFIER),
T("<EOF>", InkBlotAntlrGrammarLexer.Eof)
], _tokenEqualityComparer);
}
private IList<IToken> GetTokens(string text)
private static IList<IToken> GetTokens(string text)
{
var fileReader = new InMemoryFileReader([
("main.ink", text)
@@ -31,4 +44,39 @@ public class LexerTest
var tokens = tokensStream.GetTokens();
return tokens;
}
private class TokenEqualityComparer : IEqualityComparer<IToken>
{
public bool Equals(IToken? x, IToken? y)
{
if (ReferenceEquals(x, y)) return true;
if (x is null) return false;
if (y is null) return false;
return x.Text == y.Text && x.Type == y.Type;
}
public int GetHashCode(IToken obj)
{
return HashCode.Combine(obj.Text, obj.Type);
}
}
private class SimpleToken(string text, int type) : IToken
{
public string Text { get; } = text;
public int Type { get; } = type;
public int Line => 0;
public int Column => 0;
public int Channel => 0;
public int TokenIndex => 0;
public int StartIndex => 0;
public int StopIndex => 0;
public ITokenSource TokenSource => null!;
public ICharStream InputStream => null!;
public override string ToString()
{
return $"['{Text}',<{Type}>]";
}
}
}