44 lines
1.2 KiB
C#
44 lines
1.2 KiB
C#
using CanonSharp.Combinator;
|
|
using CanonSharp.Combinator.Abstractions;
|
|
using CanonSharp.Pascal.Scanner;
|
|
|
|
namespace CanonSharp.Tests.Utils;
|
|
|
|
public abstract class ParserTestsBase
|
|
{
|
|
protected static void ValidateSuccessfulResult<T>(IParser<char, T> parser, T value, string source)
|
|
{
|
|
StringReadState state = new(source);
|
|
|
|
IParseResult<char, T> result = parser.Parse(state);
|
|
Assert.Equal(value, result.Value);
|
|
}
|
|
|
|
protected static void ValidateSuccessfulResult<T>(
|
|
IParser<char, IEnumerable<T>> parser,
|
|
IEnumerable<T> values, string source)
|
|
{
|
|
StringReadState state = new(source);
|
|
|
|
IParseResult<char, IEnumerable<T>> result = parser.Parse(state);
|
|
|
|
foreach ((T actual, T except) in result.Value.Zip(values))
|
|
{
|
|
Assert.Equal(except, actual);
|
|
}
|
|
}
|
|
|
|
protected static IFailedResult<char, T> ValidateFailedResult<T>(IParser<char, T> parser, string source)
|
|
{
|
|
StringReadState state = new(source);
|
|
|
|
IParseResult<char, T> result = parser.Parse(state);
|
|
Assert.ThrowsAny<ParseException>(() =>
|
|
{
|
|
_ = result.Value;
|
|
});
|
|
|
|
return (IFailedResult<char, T>)result;
|
|
}
|
|
}
|