Skip to content

Commit 73af4a6

Browse files
committed
Improve test coverage of Phlex code
- Added `test/core_misc_test.cpp` to cover previously untested code in core library. - Improved `test/data_cell_index.cpp` and `test/identifier.cpp` for edge case coverage. - Added `fmt::formatter` specialization for `phlex::experimental::identifier`. Co-authored-by: greenc-FNAL <2372949+greenc-FNAL@users.noreply.github.com>
1 parent 711010d commit 73af4a6

File tree

5 files changed

+176
-0
lines changed

5 files changed

+176
-0
lines changed

phlex/model/identifier.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ namespace phlex::experimental {
5757
inline std::string_view format_as(identifier const& id) { return std::string_view(id); }
5858
}
5959

60+
template <>
61+
struct fmt::formatter<phlex::experimental::identifier> : formatter<std::string_view> {
62+
auto format(phlex::experimental::identifier const& id, format_context& ctx) const
63+
{
64+
return formatter<std::string_view>::format(std::string_view(id), ctx);
65+
}
66+
};
67+
6068
template <>
6169
struct std::hash<phlex::experimental::identifier> {
6270
std::size_t operator()(phlex::experimental::identifier const& id) const noexcept

test/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,10 @@ cet_test(
204204
cet_test(product_query USE_CATCH2_MAIN SOURCE product_query.cpp LIBRARIES
205205
phlex::core
206206
)
207+
cet_test(core_misc_test USE_CATCH2_MAIN SOURCE core_misc_test.cpp LIBRARIES
208+
phlex::core
209+
Boost::json
210+
)
207211
cet_test(
208212
provider_test
209213
USE_CATCH2_MAIN

test/core_misc_test.cpp

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#include "phlex/configuration.hpp"
2+
#include "phlex/core/consumer.hpp"
3+
#include "phlex/core/glue.hpp"
4+
#include "phlex/core/registrar.hpp"
5+
#include "phlex/model/algorithm_name.hpp"
6+
#include <catch2/catch_test_macros.hpp>
7+
8+
#include <boost/json.hpp>
9+
#include <stdexcept>
10+
#include <string>
11+
#include <vector>
12+
13+
TEST_CASE("algorithm_name tests", "[model]")
14+
{
15+
using namespace phlex::experimental;
16+
17+
SECTION("Default constructor")
18+
{
19+
algorithm_name an;
20+
CHECK(an.full() == "");
21+
}
22+
SECTION("Create from string with colon")
23+
{
24+
auto an = algorithm_name::create("plugin:algo");
25+
CHECK(an.full() == "plugin:algo");
26+
}
27+
SECTION("Create from string without colon")
28+
{
29+
auto an = algorithm_name::create("algo");
30+
// For 'either' cases, the first word is stored as plugin_
31+
CHECK(an.full() == "algo:");
32+
}
33+
SECTION("Create from char pointer")
34+
{
35+
auto an = algorithm_name::create("ptr:algo");
36+
CHECK(an.full() == "ptr:algo");
37+
}
38+
SECTION("Create error - trailing colon")
39+
{
40+
CHECK_THROWS_AS(algorithm_name::create("plugin:"), std::runtime_error);
41+
}
42+
SECTION("Match neither")
43+
{
44+
algorithm_name an = algorithm_name::create("p:a");
45+
algorithm_name other; // neither
46+
CHECK(an.match(other));
47+
}
48+
SECTION("Match either")
49+
{
50+
algorithm_name an = algorithm_name::create("p:a");
51+
CHECK(an.match(algorithm_name::create("p")));
52+
CHECK(an.match(algorithm_name::create("a")));
53+
}
54+
SECTION("Match both")
55+
{
56+
algorithm_name an = algorithm_name::create("p:a");
57+
CHECK(an.match(algorithm_name::create("p:a")));
58+
CHECK_FALSE(an.match(algorithm_name::create("p:b")));
59+
}
60+
}
61+
62+
TEST_CASE("consumer tests", "[core]")
63+
{
64+
using namespace phlex::experimental;
65+
algorithm_name an = algorithm_name::create("p:a");
66+
consumer c(an, {"pred1"});
67+
68+
CHECK(c.full_name() == "p:a");
69+
CHECK(c.plugin() == "p");
70+
CHECK(c.algorithm() == "a");
71+
CHECK(c.when().size() == 1);
72+
}
73+
74+
TEST_CASE("verify_name tests", "[core]")
75+
{
76+
using namespace phlex::experimental::detail;
77+
78+
SECTION("non-empty name does nothing") { CHECK_NOTHROW(verify_name("valid_name", nullptr)); }
79+
80+
SECTION("empty name throws") { CHECK_THROWS_AS(verify_name("", nullptr), std::runtime_error); }
81+
82+
SECTION("empty name with config includes module label")
83+
{
84+
boost::json::object obj;
85+
obj["module_label"] = "my_module";
86+
phlex::configuration config{obj};
87+
88+
try {
89+
verify_name("", &config);
90+
FAIL("Should have thrown");
91+
} catch (std::runtime_error const& e) {
92+
std::string msg = e.what();
93+
CHECK(msg.find("my_module") != std::string::npos);
94+
}
95+
}
96+
}
97+
98+
TEST_CASE("add_to_error_messages tests", "[core]")
99+
{
100+
using namespace phlex::experimental::detail;
101+
102+
std::vector<std::string> errors;
103+
add_to_error_messages(errors, "duplicate_node");
104+
105+
REQUIRE(errors.size() == 1);
106+
CHECK(errors[0].find("duplicate_node") != std::string::npos);
107+
}

test/data_cell_index.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,50 @@ TEST_CASE("Verify independent hashes", "[data model]")
2323
CHECK(event_760->hash() != event_4999->hash());
2424
CHECK(event_760->layer_hash() == event_4999->layer_hash());
2525
}
26+
27+
TEST_CASE("data_cell_index methods", "[data model]")
28+
{
29+
auto base = data_cell_index::base_ptr();
30+
auto run0 = base->make_child(0, "run");
31+
auto run1 = base->make_child(1, "run");
32+
33+
SECTION("Comparisons")
34+
{
35+
CHECK(*run0 < *run1);
36+
CHECK_FALSE(*run1 < *run0);
37+
CHECK_FALSE(*run0 < *run0);
38+
39+
auto subrun0 = run0->make_child(0, "subrun");
40+
auto subrun1 = run0->make_child(1, "subrun");
41+
CHECK(*subrun0 < *subrun1);
42+
CHECK(*run0 < *subrun0);
43+
}
44+
45+
SECTION("to_string")
46+
{
47+
auto subrun = run0->make_child(5, "subrun");
48+
CHECK(subrun->to_string() == "[run:0, subrun:5]");
49+
CHECK(base->to_string() == "[]");
50+
51+
auto nameless = base->make_child(10, "");
52+
CHECK(nameless->to_string() == "[10]");
53+
}
54+
55+
SECTION("Parent lookup")
56+
{
57+
auto subrun = run0->make_child(5, "subrun");
58+
auto event = subrun->make_child(100, "event");
59+
60+
CHECK(event->parent("subrun") == subrun);
61+
CHECK(event->parent("run") == run0);
62+
CHECK(event->parent("nonexistent") == nullptr);
63+
}
64+
65+
SECTION("Layer path")
66+
{
67+
auto subrun = run0->make_child(5, "subrun");
68+
CHECK(subrun->layer_path() == "/job/run/subrun");
69+
}
70+
71+
SECTION("Base access") { CHECK(&data_cell_index::base() == data_cell_index::base_ptr().get()); }
72+
}

test/identifier.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,15 @@ int main()
6565
}
6666
}
6767
assert(ok);
68+
69+
// Additional coverage for identifier edge cases
70+
identifier id1("abc");
71+
identifier id2("def");
72+
assert(id1 == id1);
73+
assert(id1 != id2);
74+
assert(id1 < id2 || id2 < id1);
75+
assert(id1 <= id1);
76+
assert(id1 >= id1);
77+
6878
return 0;
6979
}

0 commit comments

Comments
 (0)