How to run hierarhy of SECTION()s on several objects?

47 views
Skip to first unread message

Lev Serebryakov

unread,
Apr 11, 2017, 6:28:31 PM4/11/17
to CATCH
Hello!

  I have test case which looks like (really, I'm testing plain C library, but it is object-oriented by design):

TEST_CASE("Implementation of some object") {
  some_t *obj = lib_create_obj("arg");
  REQUIRE(obj != NULL);
  SECTION("Basic sanity check") {
    CHECK(lib_api_1(obj) == 42);
    CHECK(strcmp("arg", lib_get_obj_name(obj)) == 0);

    SECTION("More advanced check") {
      other_t retval = lib_api_2(obj, 1, 2, 3);
      CHECK(retval == -42);
      CHECK(9000 = lib_api_getter_1(obj);
      CHECK(lib_api_setter_2(obj, 37) == 1);

      SECTION("These checks are meaningful only if previous are Ok") {
         CHECK(lib_api_getter_2(obj) == 37);
         CHECK(lib_api_3(obj) == 18);
      }
    }
    lib_free_obj(obj);
  }
}

  Now I need to repeat all checks for objects, created with different arguments to lib_create_obj(). What is idiomatic Catch way to do this? Copy'n'pasting TEST_CASE()s works, sure, but looks ugly. I want to keep nested structure of SECTION()s, which in reality is more complex that in this synthetic example.

--
// Lev Serebryakov

Phil Nash

unread,
Jul 13, 2017, 4:35:27 AM7/13/17
to CATCH
(sorry for delayed response).

At the moment the only way to do it is to put the common sections in a function that you can call with the different arguments.
Note that you'll need to disambiguate the top level section. By that I mean make sure it's name is different for each set of arguments (so, somehow, produce a string from the arguments and attach that to the section name).

In the future there will be support for generators/ parameterised tests - but for now this is the workaround.
Reply all
Reply to author
Forward
0 new messages