CanonSharp/CanonSharp.Tests/Utils/ParserTestsBase.cs
jackfiled 3ed8bf5d36
All checks were successful
Run unit test / Unit-Test (push) Successful in 41s
feat: Parser Combinator库和词法分析器 (#2)
Reviewed-on: https://git.bupt-hpc.cn/jackfiled/CanonSharp/pulls/2
Co-authored-by: jackfiled <xcrenchangjun@outlook.com>
Co-committed-by: jackfiled <xcrenchangjun@outlook.com>
2024-08-13 14:46:11 +08:00

44 lines
1.2 KiB
C#

using CanonSharp.Combinator;
using CanonSharp.Combinator.Abstractions;
using CanonSharp.Common.Scanner;
namespace CanonSharp.Tests.Utils;
public abstract class ParserTestsBase
{
protected static void ValidateSuccessfulResult<T>(Parser<char, T> parser, T value, string source)
{
StringReadState state = new(source);
ParseResult<char, T> result = parser.Parse(state);
Assert.Equal(value, result.Value);
}
protected static void ValidateSuccessfulResult<T>(
Parser<char, IEnumerable<T>> parser,
IEnumerable<T> values, string source)
{
StringReadState state = new(source);
ParseResult<char, IEnumerable<T>> result = parser.Parse(state);
foreach ((T actual, T except) in result.Value.Zip(values))
{
Assert.Equal(except, actual);
}
}
protected static FailedResult<char, T> ValidateFailedResult<T>(Parser<char, T> parser, string source)
{
StringReadState state = new(source);
ParseResult<char, T> result = parser.Parse(state);
Assert.ThrowsAny<ParseException>(() =>
{
_ = result.Value;
});
return (FailedResult<char, T>)result;
}
}