我正在使用'Token()'方法丢弃前导和尾随空格,但它不会,此测试失败,消息Expected string to be "token", but it has unexpected whitespace at the end.
我试图在方法Token()
之前调用方法Text()
,但它也没有帮助。 Parse.AnyChar.Many().Token().Text()
如何以正确的方式使用方法Token()
?
[Test]
public void Test()
{
Parser<string> parser = Parse.AnyChar.Many().Text().Token();
var actual = parser.Parse(" token ");
actual.Should().Be("token"); // without leading and trailing whitespaces
}
在Parse.AnyChar
修饰符发挥作用之前,Token
消耗了尾随空格。
要修复解析器,请排除这样的空格:
[Test]
public void Test()
{
var parser = Parse.AnyChar.Except(Parse.WhiteSpace).Many().Text().Token();
var actual = parser.Parse(" token ");
actual.Should().Be("token"); // without leading and trailing whitespaces
}