Unit tests for Boost.Spirit

I am new to Boost.Spirit and Boost.Test, and I would like to know how you check the correctness of your grammars. The following is a simplified version of how I'm doing this at the moment, and I'm sure there is a better way:

Each test case contains a pair of two lines containing text to parse, and the expected result, limited to semicolons.

The parsing functions do the actual parsing and return a string that 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()
+3
source share
2 answers

, . , , , . TestInvalidGrammar, TestErrorHandling, TestNestedGrammar .. , .

, , , .

+1

Source: https://habr.com/ru/post/1702235/


All Articles