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