Working with Parser Combinators: Listing 1.

Parsing a phone number.

[TestMethod]
public void ParseJustThreeNumbers()
{
  string result = threeNumberParser.Parse("123");
  Assert.AreEqual("123", result);
}
[TestMethod]
public void ParseJustThreeNumbersOutOfMore()
{
  string result = threeNumberParser.Parse("12345678");
  Assert.AreEqual("123", result);
}
[TestMethod]
public void FailToParseAThreeDigitNumberBecauseItIsTooShort(){
  var result = threeNumberParser.TryParse("10");
  Assert.IsTrue(result.ToString().StartsWith("Parsing failure"));
}

About the Author

Ted Neward is a programming language, virtual machine, and enterprise-scale architect. He has written a dozen books and hundreds of articles on .NET, Java, enterprise systems, mobile development, and programming languages. He resides in the Pacific Northwest, and can be found on the Internet at www.tedneward.com, www.itrellis.com, @tedneward on Twitter, and blogs at blogs.tedneward.com.