CanonSharp/CanonSharp.Tests/Utils/LexicalTestBase.cs
jackfiled cf19f8197e feat: Grammar Parser (#3)
Reviewed-on: https://git.bupt-hpc.cn/jackfiled/CanonSharp/pulls/3
Co-authored-by: jackfiled <xcrenchangjun@outlook.com>
Co-committed-by: jackfiled <xcrenchangjun@outlook.com>
2024-08-18 12:01:27 +08:00

42 lines
1.6 KiB
C#

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);
}
}
}