0

我正在使用 '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
}
4

1 回答 1

2

Parse.AnyCharToken在修饰符起作用之前消耗尾随空格。

要修复解析器,请像这样排除空格:

[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
}
于 2018-01-05T01:46:28.747 回答