CanonSharp/CanonSharp.Tests/Utils/LexicalTestBase.cs

42 lines
1.6 KiB
C#
Raw Permalink Normal View History

using CanonSharp.Combinator;
using CanonSharp.Combinator.Abstractions;
using CanonSharp.Pascal.Scanner;
namespace CanonSharp.Tests.Utils;
public abstract class LexicalTestBase
{
protected static void ValidateSuccessfulParser(IParser<char, LexicalToken> parser, LexicalTokenType exceptedType,
string literalValue, string input)
{
StringReadState state = new(input);
IParseResult<char, LexicalToken> result = parser.Parse(state);
Assert.Equal(exceptedType, result.Value.TokenType);
Assert.Equal(literalValue, result.Value.LiteralValue);
}
protected static void ValidateFailedParser(IParser<char, LexicalToken> parser, string input)
{
StringReadState state = new(input);
IParseResult<char, LexicalToken> result = parser.Parse(state);
Assert.ThrowsAny<ParseException>(() => result.Value);
}
protected static void ValidateLexicalTokens(IParser<char, IEnumerable<LexicalToken>> parser, string input,
List<(LexicalTokenType, string)> exceptedResult)
{
StringReadState state = new(input);
IParseResult<char, IEnumerable<LexicalToken>> result = parser.Parse(state);
List<LexicalToken> actualResult = result.Value.ToList();
Assert.Equal(exceptedResult.Count, actualResult.Count);
foreach (((LexicalTokenType exceptedType, string exceptedValue), LexicalToken token) in exceptedResult.Zip(
actualResult))
{
Assert.Equal(exceptedType, token.TokenType);
Assert.Equal(exceptedValue, token.LiteralValue);
}
}
}