Question

I'm new to Boost.Spirit and Boost.Test and I would like to know how you verify the correctness of your grammars. Below is a simplified version of how I do it at the moment and I'm pretty sure that there's a better way:

Each test case hase a pair of two strings containing the text to parse and the expected result delimited by semicolons.

The parse functions does the actual parsing and returns a string which should be equal to the expected result.

 std::string parse(std::string const & line) {
  std::string name;
  int hours;

  rule<> top_rule = ... ; // rule assignes values to 'name' and 'hours'

  parse_info<> info = parse(line.c_str(), top_rule);

  if(info.full) {
    std::stringstream sstr;
    sstr << name << ";" << hours;

    return sstr.str();
  }

  return "parser failed.";
}

BOOST_AUTO_TEST_SUITE( TestSuite )

BOOST_AUTO_TEST_CASE( TestCase ) {
  BOOST_CHECK_EQUAL(parse("Tom worked for 10 hours."), "Tom;10");
}

BOOST_AUTO_TEST_SUITE_END()
Was it helpful?

Solution

In general, your approach seems fine to me. I would probably group class of tests into function with descriptive names, e.g. TestInvalidGrammar, TestErrorHandling, TestNestedGrammar etc. and have those called from the main.

I am sure you have read documentation but take a look at examples if it helps.

OTHER TIPS

Here you can see how they (the boost spirit authors) test their own parsers: http://svn.boost.org/svn/boost/trunk/libs/spirit/test/qi/grammar.cpp . For each part of qi you can find a C++ file here: http://svn.boost.org/svn/boost/trunk/libs/spirit/test/qi/.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top