82 lines
2.3 KiB
C#
82 lines
2.3 KiB
C#
using Antlr4.Runtime;
|
|
using Shouldly;
|
|
|
|
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.ShouldBe([
|
|
T("<-", InkBlotAntlrGrammarLexer.THREAD_ARROW),
|
|
T(" ", InkBlotAntlrGrammarLexer.WS),
|
|
T("threadName", InkBlotAntlrGrammarLexer.IDENTIFIER),
|
|
T("<EOF>", InkBlotAntlrGrammarLexer.Eof)
|
|
], _tokenEqualityComparer);
|
|
}
|
|
|
|
private static IList<IToken> GetTokens(string text)
|
|
{
|
|
var fileReader = new InMemoryFileReader([
|
|
("main.ink", text)
|
|
]);
|
|
var tokensStream = InkBlotParser.GetTokenStream(fileReader, "main.ink");
|
|
tokensStream.Fill();
|
|
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}>]";
|
|
}
|
|
}
|
|
} |