Added initial json generator test cases

This commit is contained in:
Bartek Kryza
2023-03-20 01:33:37 +01:00
parent 44cd7b0484
commit b412f46fb2
10 changed files with 188 additions and 2271 deletions

View File

@@ -113,6 +113,9 @@ generator::generator(diagram_config &config, diagram_model &model)
void generator::generate(std::ostream &ostr) const
{
json_["elements"] = std::vector<nlohmann::json>{};
json_["relationships"] = std::vector<nlohmann::json>{};
generate_top_level_elements(json_);
generate_relationships(json_);

View File

@@ -58,13 +58,6 @@ generator::generator(
{
}
std::string generator::render_name(std::string name) const
{
util::replace_all(name, "##", "::");
return name;
}
void generator::generate_call(const message &m, nlohmann::json &parent) const
{
const auto &from = m_model.get_participant<model::participant>(m.from());

View File

@@ -67,19 +67,8 @@ public:
private:
bool is_participant_generated(common::id_t id) const;
std::string render_name(std::string name) const;
mutable std::set<common::id_t> generated_participants_;
mutable nlohmann::json json_;
mutable std::vector<std::reference_wrapper<nlohmann::json>>
block_statements_stack_;
void process_call_message(const model::message &m,
std::vector<common::model::diagram_element::id_t> &visited) const;
void process_if_message(const model::message &m) const;
void process_else_if_message() const;
void process_end_if_message() const;
@@ -98,6 +87,13 @@ private:
void process_for_message(const model::message &m) const;
void process_end_while_message() const;
void process_while_message(const model::message &m) const;
mutable std::set<common::id_t> generated_participants_;
mutable nlohmann::json json_;
mutable std::vector<std::reference_wrapper<nlohmann::json>>
block_statements_stack_;
};
} // namespace clanguml::sequence_diagram::generators::json

View File

@@ -75,444 +75,20 @@ TEST_CASE("t00002", "[test-case][class]")
save_puml(config.output_directory() + "/" + diagram->name + ".puml", puml);
const std::string expected_json = R"##(
{
"elements": [
{
"bases": [],
"comment": {
"brief": [
" This is class A\n"
],
"formatted": "\\brief This is class A",
"paragraph": [
" \n"
],
"raw": "/// \\brief This is class A",
"text": "\n \n"
},
"display_name": "clanguml::t00002::A",
"id": "987634239855407298",
"is_abstract": true,
"is_nested": false,
"is_struct": false,
"is_template": false,
"is_union": false,
"members": [],
"methods": [
{
"access": "public",
"comment": {
"formatted": "Abstract foo_a",
"paragraph": [
" Abstract foo_a\n"
],
"raw": "/// Abstract foo_a",
"text": "\n Abstract foo_a\n"
},
"is_const": false,
"is_defaulted": false,
"is_implicit": false,
"is_pure_virtual": true,
"is_static": false,
"is_virtual": true,
"name": "foo_a",
"parameters": [],
"type": "void"
},
{
"access": "public",
"comment": {
"formatted": "Abstract foo_c",
"paragraph": [
" Abstract foo_c\n"
],
"raw": "/// Abstract foo_c",
"text": "\n Abstract foo_c\n"
},
"is_const": false,
"is_defaulted": false,
"is_implicit": false,
"is_pure_virtual": true,
"is_static": false,
"is_virtual": true,
"name": "foo_c",
"parameters": [],
"type": "void"
}
],
"name": "A",
"namespace": "clanguml::t00002",
"source_location": {
"file": "../../tests/t00002/t00002.cc",
"line": 7
},
"template_parameters": [],
"type": "class"
},
{
"bases": [
{
"access": "public",
"id": "987634239855407298",
"is_virtual": false,
"name": "clanguml::t00002::A"
}
],
"comment": {
"brief": [
" This is class B\n"
],
"formatted": "\\brief This is class B",
"paragraph": [
" \n"
],
"raw": "/// \\brief This is class B",
"text": "\n \n"
},
"display_name": "clanguml::t00002::B",
"id": "594234458687375950",
"is_abstract": false,
"is_nested": false,
"is_struct": false,
"is_template": false,
"is_union": false,
"members": [],
"methods": [
{
"access": "public",
"is_const": false,
"is_defaulted": false,
"is_implicit": false,
"is_pure_virtual": false,
"is_static": false,
"is_virtual": true,
"name": "foo_a",
"parameters": [],
"type": "void"
}
],
"name": "B",
"namespace": "clanguml::t00002",
"source_location": {
"file": "../../tests/t00002/t00002.cc",
"line": 16
},
"template_parameters": [],
"type": "class"
},
{
"bases": [
{
"access": "public",
"id": "987634239855407298",
"is_virtual": false,
"name": "clanguml::t00002::A"
}
],
"comment": {
"brief": [
" This is class C - class C has a long comment\n"
],
"formatted": "@brief This is class C - class C has a long comment\n\nVivamus integer non suscipit taciti mus etiam at primis tempor sagittis sit,\neuismod libero facilisi aptent elementum felis blandit cursus gravida sociis\nerat ante, eleifend lectus nullam dapibus netus feugiat curae curabitur est\nad.",
"paragraph": [
" \n",
" Vivamus integer non suscipit taciti mus etiam at primis tempor sagittis sit,\n euismod libero facilisi aptent elementum felis blandit cursus gravida sociis\n erat ante, eleifend lectus nullam dapibus netus feugiat curae curabitur est\n ad.\n"
],
"raw": "/// @brief This is class C - class C has a long comment\n///\n/// Vivamus integer non suscipit taciti mus etiam at primis tempor sagittis sit,\n/// euismod libero facilisi aptent elementum felis blandit cursus gravida sociis\n/// erat ante, eleifend lectus nullam dapibus netus feugiat curae curabitur est\n/// ad.",
"text": "\n \n\n Vivamus integer non suscipit taciti mus etiam at primis tempor sagittis sit,\n euismod libero facilisi aptent elementum felis blandit cursus gravida sociis\n erat ante, eleifend lectus nullam dapibus netus feugiat curae curabitur est\n ad.\n"
},
"display_name": "clanguml::t00002::C",
"id": "1142499429598587507",
"is_abstract": false,
"is_nested": false,
"is_struct": false,
"is_template": false,
"is_union": false,
"members": [],
"methods": [
{
"access": "public",
"comment": {
"formatted": "Do nothing unless override is provided",
"paragraph": [
" Do nothing unless override is provided\n"
],
"raw": "/// Do nothing unless override is provided",
"text": "\n Do nothing unless override is provided\n"
},
"is_const": false,
"is_defaulted": false,
"is_implicit": false,
"is_pure_virtual": false,
"is_static": false,
"is_virtual": true,
"name": "foo_c",
"parameters": [],
"type": "void"
}
],
"name": "C",
"namespace": "clanguml::t00002",
"source_location": {
"file": "../../tests/t00002/t00002.cc",
"line": 27
},
"template_parameters": [],
"type": "class"
},
{
"bases": [
{
"access": "public",
"id": "594234458687375950",
"is_virtual": false,
"name": "clanguml::t00002::B"
},
{
"access": "public",
"id": "1142499429598587507",
"is_virtual": false,
"name": "clanguml::t00002::C"
}
],
"comment": {
"formatted": "This is class D\nwhich is a little like B\nand a little like C",
"paragraph": [
" This is class D\n which is a little like B\n and a little like C\n"
],
"raw": "/// This is class D\n/// which is a little like B\n/// and a little like C",
"text": "\n This is class D\n which is a little like B\n and a little like C\n"
},
"display_name": "clanguml::t00002::D",
"id": "60950494980414724",
"is_abstract": false,
"is_nested": false,
"is_struct": false,
"is_template": false,
"is_union": false,
"members": [
{
"access": "private",
"comment": {
"formatted": "All the A pointers",
"paragraph": [
" All the A pointers\n"
],
"raw": "/// All the A pointers",
"text": "\n All the A pointers\n"
},
"is_static": false,
"name": "as",
"source_location": {
"file": "../../tests/t00002/t00002.cc",
"line": 58
},
"type": "std::vector<A *>"
}
],
"methods": [
{
"access": "public",
"comment": {
"formatted": "\n Forward foo_a\n ",
"paragraph": [
" Forward foo_a\n"
],
"raw": "/**\n * Forward foo_a\n */",
"text": "\n Forward foo_a\n"
},
"is_const": false,
"is_defaulted": false,
"is_implicit": false,
"is_pure_virtual": false,
"is_static": false,
"is_virtual": true,
"name": "foo_a",
"parameters": [],
"type": "void"
},
{
"access": "public",
"comment": {
"formatted": "\n Forward foo_c\n ",
"paragraph": [
" Forward foo_c\n"
],
"raw": "/**\n * Forward foo_c\n */",
"text": "\n Forward foo_c\n"
},
"is_const": false,
"is_defaulted": false,
"is_implicit": false,
"is_pure_virtual": false,
"is_static": false,
"is_virtual": true,
"name": "foo_c",
"parameters": [],
"type": "void"
}
],
"name": "D",
"namespace": "clanguml::t00002",
"source_location": {
"file": "../../tests/t00002/t00002.cc",
"line": 36
},
"template_parameters": [],
"type": "class"
},
{
"bases": [
{
"access": "public",
"id": "594234458687375950",
"is_virtual": true,
"name": "clanguml::t00002::B"
},
{
"access": "public",
"id": "1142499429598587507",
"is_virtual": true,
"name": "clanguml::t00002::C"
}
],
"display_name": "clanguml::t00002::E",
"id": "2237886670308966220",
"is_abstract": false,
"is_nested": false,
"is_struct": false,
"is_template": false,
"is_union": false,
"members": [
{
"access": "private",
"comment": {
"formatted": "All the A pointers",
"paragraph": [
" All the A pointers\n"
],
"raw": "/// All the A pointers",
"text": "\n All the A pointers\n"
},
"is_static": false,
"name": "as",
"source_location": {
"file": "../../tests/t00002/t00002.cc",
"line": 83
},
"type": "std::vector<A *>"
}
],
"methods": [
{
"access": "public",
"comment": {
"formatted": "\n Forward foo_a",
"paragraph": [
" Forward foo_a\n"
],
"raw": "///\n /// Forward foo_a\n ///",
"text": "\n Forward foo_a\n"
},
"is_const": false,
"is_defaulted": false,
"is_implicit": false,
"is_pure_virtual": false,
"is_static": false,
"is_virtual": true,
"name": "foo_a",
"parameters": [],
"type": "void"
},
{
"access": "public",
"comment": {
"formatted": "\n Forward foo_c",
"paragraph": [
" Forward foo_c\n"
],
"raw": "///\n /// Forward foo_c\n ///",
"text": "\n Forward foo_c\n"
},
"is_const": false,
"is_defaulted": false,
"is_implicit": false,
"is_pure_virtual": false,
"is_static": false,
"is_virtual": true,
"name": "foo_c",
"parameters": [],
"type": "void"
}
],
"name": "E",
"namespace": "clanguml::t00002",
"source_location": {
"file": "../../tests/t00002/t00002.cc",
"line": 61
},
"template_parameters": [],
"type": "class"
}
],
"relationships": [
{
"access": "public",
"destination": "987634239855407298",
"source": "594234458687375950",
"type": "extension"
},
{
"access": "public",
"destination": "987634239855407298",
"source": "1142499429598587507",
"type": "extension"
},
{
"access": "private",
"destination": "987634239855407298",
"label": "as",
"source": "60950494980414724",
"type": "association"
},
{
"access": "public",
"destination": "594234458687375950",
"source": "60950494980414724",
"type": "extension"
},
{
"access": "public",
"destination": "1142499429598587507",
"source": "60950494980414724",
"type": "extension"
},
{
"access": "private",
"destination": "987634239855407298",
"label": "as",
"source": "2237886670308966220",
"type": "association"
},
{
"access": "public",
"destination": "594234458687375950",
"source": "2237886670308966220",
"type": "extension"
},
{
"access": "public",
"destination": "1142499429598587507",
"source": "2237886670308966220",
"type": "extension"
}
]
}
)##";
auto j = generate_class_json(diagram, *model);
REQUIRE(j == nlohmann::json::parse(expected_json));
REQUIRE(json::IsClass(j, "clanguml::t00002::A"));
REQUIRE(json::IsClass(j, "clanguml::t00002::B"));
REQUIRE(json::IsClass(j, "clanguml::t00002::C"));
REQUIRE(json::IsBaseClass(j, "clanguml::t00002::A", "clanguml::t00002::B"));
REQUIRE(json::IsBaseClass(j, "clanguml::t00002::A", "clanguml::t00002::C"));
REQUIRE(json::IsBaseClass(j, "clanguml::t00002::B", "clanguml::t00002::D"));
REQUIRE(json::IsBaseClass(j, "clanguml::t00002::C", "clanguml::t00002::D"));
REQUIRE(json::IsMethod(j, "clanguml::t00002::A", "foo_a"));
REQUIRE(json::IsMethod(j, "clanguml::t00002::C", "foo_c"));
REQUIRE(json::IsAssociation(
j, "clanguml::t00002::D", "clanguml::t00002::A", "as"));
save_json(config.output_directory() + "/" + diagram->name + ".json", j);
}

View File

@@ -66,4 +66,20 @@ TEST_CASE("t00003", "[test-case][class]")
REQUIRE_THAT(puml, (IsField<Private>("c_", "int")));
save_puml(config.output_directory() + "/" + diagram->name + ".puml", puml);
auto j = generate_class_json(diagram, *model);
REQUIRE(json::IsClass(j, "clanguml::t00003::A"));
REQUIRE(json::IsMethod(j, "clanguml::t00003::A", "A"));
REQUIRE(json::IsMethod(j, "clanguml::t00003::A", "~A"));
REQUIRE(json::IsMethod(j, "clanguml::t00003::A", "basic_method"));
REQUIRE(json::IsMethod(j, "clanguml::t00003::A", "static_method"));
REQUIRE(json::IsMethod(j, "clanguml::t00003::A", "const_method"));
REQUIRE(json::IsMethod(j, "clanguml::t00003::A", "default_int"));
REQUIRE(json::IsMethod(j, "clanguml::t00003::A", "default_string"));
REQUIRE(
!json::IsDependency(j, "clanguml::t00002::A", "clanguml::t00002::A"));
save_json(config.output_directory() + "/" + diagram->name + ".json", j);
}

File diff suppressed because it is too large Load Diff

View File

@@ -47,192 +47,12 @@ TEST_CASE("t00036", "[test-case][class]")
save_puml(config.output_directory() + "/" + diagram->name + ".puml", puml);
const std::string expected_json = R"##(
{
"elements": [
{
"display_name": "clanguml::t00036::ns1",
"elements": [
{
"constants": [
"blue",
"yellow"
],
"display_name": "clanguml::t00036::ns1::E",
"id": "2144761953049158478",
"is_nested": false,
"name": "E",
"namespace": "clanguml::t00036::ns1",
"source_location": {
"file": "../../tests/t00036/t00036.cc",
"line": 6
},
"type": "enum"
},
{
"display_name": "clanguml::t00036::ns1::ns11",
"elements": [
{
"bases": [],
"display_name": "clanguml::t00036::ns1::ns11::A<T>",
"id": "571573305652194946",
"is_abstract": false,
"is_nested": false,
"is_struct": true,
"is_template": false,
"is_union": false,
"members": [
{
"access": "public",
"is_static": false,
"name": "a",
"source_location": {
"file": "../../tests/t00036/t00036.cc",
"line": 11
},
"type": "T"
}
],
"methods": [],
"name": "A",
"namespace": "clanguml::t00036::ns1::ns11",
"source_location": {
"file": "../../tests/t00036/t00036.cc",
"line": 10
},
"template_parameters": [
{
"is_variadic": false,
"kind": "template_type",
"name": "T"
}
],
"type": "class"
},
{
"display_name": "clanguml::t00036::ns1::ns11::ns111",
"elements": [
{
"bases": [],
"display_name": "clanguml::t00036::ns1::ns11::ns111::B",
"id": "1964031933563607376",
"is_abstract": false,
"is_nested": false,
"is_struct": true,
"is_template": false,
"is_union": false,
"members": [
{
"access": "public",
"is_static": false,
"name": "a_int",
"source_location": {
"file": "../../tests/t00036/t00036.cc",
"line": 17
},
"type": "A<int>"
}
],
"methods": [],
"name": "B",
"namespace": "clanguml::t00036::ns1::ns11::ns111",
"source_location": {
"file": "../../tests/t00036/t00036.cc",
"line": 16
},
"template_parameters": [],
"type": "class"
}
],
"name": "ns111",
"type": "namespace"
},
{
"bases": [],
"display_name": "clanguml::t00036::ns1::ns11::A<int>",
"id": "1832710427462319797",
"is_abstract": false,
"is_nested": false,
"is_struct": false,
"is_template": false,
"is_union": false,
"members": [],
"methods": [],
"name": "A",
"namespace": "clanguml::t00036::ns1::ns11",
"template_parameters": [
{
"is_variadic": false,
"kind": "argument",
"type": "int"
}
],
"type": "class"
}
],
"name": "ns11",
"type": "namespace"
}
],
"name": "ns1",
"type": "namespace"
},
{
"display_name": "clanguml::t00036::ns2",
"elements": [
{
"display_name": "clanguml::t00036::ns2::ns22",
"elements": [
{
"bases": [],
"display_name": "clanguml::t00036::ns2::ns22::C",
"id": "2038956882066165590",
"is_abstract": false,
"is_nested": false,
"is_struct": true,
"is_template": false,
"is_union": false,
"members": [],
"methods": [],
"name": "C",
"namespace": "clanguml::t00036::ns2::ns22",
"source_location": {
"file": "../../tests/t00036/t00036.cc",
"line": 28
},
"template_parameters": [],
"type": "class"
}
],
"name": "ns22",
"type": "namespace"
}
],
"name": "ns2",
"type": "namespace"
}
],
"relationships": [
{
"access": "public",
"destination": "1832710427462319797",
"label": "a_int",
"source": "1964031933563607376",
"type": "aggregation"
},
{
"access": "public",
"destination": "571573305652194946",
"source": "1832710427462319797",
"type": "instantiation"
}
]
}
)##";
auto j = generate_class_json(diagram, *model);
REQUIRE(j == nlohmann::json::parse(expected_json));
// REQUIRE(json::IsClass(j, "clanguml::t00036::A<T>"));
// REQUIRE(json::IsClass(j, "clanguml::t00036::A<int>"));
// REQUIRE(json::IsClass(j, "clanguml::t00036::B"));
// REQUIRE(json::IsClass(j, "clanguml::t00036::C"));
save_json(config.output_directory() + "/" + diagram->name + ".json", j);
}

File diff suppressed because it is too large Load Diff

View File

@@ -58,230 +58,7 @@ TEST_CASE("t20029", "[test-case][sequence]")
save_puml(config.output_directory() + "/" + diagram->name + ".puml", puml);
const std::string expected_json = R"###(
{
"diagram_type": "sequence",
"name": "t20029_sequence",
"participants": [
{
"id": "2091374738808319642",
"name": "clanguml::t20029::tmain()",
"source_location": {
"file": "../../tests/t20029/t20029.cc",
"line": 55
},
"type": "function"
},
{
"id": "1673261195873192383",
"name": "clanguml::t20029::Encoder<clanguml::t20029::Retrier<clanguml::t20029::ConnectionPool>>",
"source_location": {
"file": "../../tests/t20029/t20029.cc",
"line": 11
},
"type": "class"
},
{
"id": "658058855590948094",
"name": "clanguml::t20029::Retrier<clanguml::t20029::ConnectionPool>",
"source_location": {
"file": "../../tests/t20029/t20029.cc",
"line": 22
},
"type": "class"
},
{
"id": "1896406205097618937",
"name": "clanguml::t20029::ConnectionPool",
"source_location": {
"file": "../../tests/t20029/t20029.cc",
"line": 39
},
"type": "class"
},
{
"id": "1362646431260879440",
"name": "clanguml::t20029::encode_b64(std::string &&)",
"source_location": {
"file": "../../tests/t20029/t20029.cc",
"line": 9
},
"type": "function"
}
],
"sequences": [
{
"messages": [
{
"from": {
"id": "2091374738808319642",
"name": "clanguml::t20029::tmain()"
},
"name": "connect()",
"return_type": "void",
"scope": "normal",
"source_location": {
"file": "../../tests/t20029/t20029.cc",
"line": 59
},
"to": {
"activity_id": "940428568182104530",
"activity_name": "clanguml::t20029::ConnectionPool::connect()",
"participant_id": "1896406205097618937",
"participant_name": "clanguml::t20029::ConnectionPool"
},
"type": "message"
},
{
"activity_id": "2091374738808319642",
"messages": [
{
"activity_id": "2091374738808319642",
"branches": [
{
"messages": [
{
"from": {
"id": "2091374738808319642",
"name": "clanguml::t20029::tmain()"
},
"name": "send(std::string &&)",
"return_type": "_Bool",
"scope": "condition",
"source_location": {
"file": "../../tests/t20029/t20029.cc",
"line": 62
},
"to": {
"activity_id": "2026763864005979273",
"activity_name": "clanguml::t20029::Encoder<clanguml::t20029::Retrier<clanguml::t20029::ConnectionPool>>::send(std::string &&)",
"participant_id": "1673261195873192383",
"participant_name": "clanguml::t20029::Encoder<clanguml::t20029::Retrier<clanguml::t20029::ConnectionPool>>"
},
"type": "message"
},
{
"from": {
"id": "2026763864005979273",
"name": "clanguml::t20029::Encoder<clanguml::t20029::Retrier<clanguml::t20029::ConnectionPool>>::send(std::string &&)"
},
"name": "encode(std::string &&)",
"return_type": "std::string",
"scope": "normal",
"source_location": {
"file": "../../tests/t20029/t20029.cc",
"line": 15
},
"to": {
"activity_id": "1468258269466480773",
"activity_name": "clanguml::t20029::Encoder<clanguml::t20029::Retrier<clanguml::t20029::ConnectionPool>>::encode(std::string &&)",
"participant_id": "1673261195873192383",
"participant_name": "clanguml::t20029::Encoder<clanguml::t20029::Retrier<clanguml::t20029::ConnectionPool>>"
},
"type": "message"
},
{
"from": {
"id": "1468258269466480773",
"name": "clanguml::t20029::Encoder<clanguml::t20029::Retrier<clanguml::t20029::ConnectionPool>>::encode(std::string &&)"
},
"name": "",
"return_type": "",
"scope": "normal",
"source_location": {
"file": "../../tests/t20029/t20029.cc",
"line": 19
},
"to": {
"activity_id": "1362646431260879440",
"activity_name": "clanguml::t20029::encode_b64(std::string &&)"
},
"type": "message"
},
{
"from": {
"id": "2026763864005979273",
"name": "clanguml::t20029::Encoder<clanguml::t20029::Retrier<clanguml::t20029::ConnectionPool>>::send(std::string &&)"
},
"name": "send(std::string &&)",
"return_type": "_Bool",
"scope": "normal",
"source_location": {
"file": "../../tests/t20029/t20029.cc",
"line": 15
},
"to": {
"activity_id": "30515971485361302",
"activity_name": "clanguml::t20029::Retrier<clanguml::t20029::ConnectionPool>::send(std::string &&)",
"participant_id": "658058855590948094",
"participant_name": "clanguml::t20029::Retrier<clanguml::t20029::ConnectionPool>"
},
"type": "message"
},
{
"activity_id": "30515971485361302",
"messages": [
{
"activity_id": "30515971485361302",
"branches": [
{
"messages": [
{
"from": {
"id": "30515971485361302",
"name": "clanguml::t20029::Retrier<clanguml::t20029::ConnectionPool>::send(std::string &&)"
},
"name": "send(const std::string &)",
"return_type": "_Bool",
"scope": "condition",
"source_location": {
"file": "../../tests/t20029/t20029.cc",
"line": 31
},
"to": {
"activity_id": "972625940114169157",
"activity_name": "clanguml::t20029::ConnectionPool::send(const std::string &)",
"participant_id": "1896406205097618937",
"participant_name": "clanguml::t20029::ConnectionPool"
},
"type": "message"
}
],
"type": "consequent"
}
],
"name": "if",
"type": "alt"
}
],
"name": "while",
"type": "loop"
}
],
"type": "consequent"
}
],
"name": "if",
"type": "alt"
}
],
"name": "for",
"type": "loop"
}
],
"start_from": {
"id": 2091374738808319700,
"location": "clanguml::t20029::tmain()"
}
}
],
"using_namespace": "clanguml::t20029"
}
)###";
auto j = generate_sequence_json(diagram, *model);
REQUIRE(j == nlohmann::json::parse(expected_json));
save_json(config.output_directory() + "/" + diagram->name + ".json", j);
}

View File

@@ -77,6 +77,15 @@ using Catch::Matchers::StdString::CasedString;
using Catch::Matchers::StdString::ContainsMatcher;
using Catch::Matchers::StdString::RegexMatcher;
struct JsonMatcherBase : Catch::MatcherBase<nlohmann::json> {
JsonMatcherBase(
std::string const &operation, CasedString const &comparator);
std::string describe() const override;
CasedString m_comparator;
std::string m_operation;
};
template <typename T, typename... Ts> constexpr bool has_type() noexcept
{
return (std::is_same_v<T, Ts> || ... || false);
@@ -589,6 +598,125 @@ ContainsMatcher IsDeprecated(std::string const &str,
return ContainsMatcher(
CasedString(str + " <<deprecated>> ", caseSensitivity));
}
namespace json {
namespace detail {
auto get_element(const nlohmann::json &j, const std::string &name)
{
return std::find_if(j["elements"].begin(), j["elements"].end(),
[&](const auto &it) { return it["display_name"] == name; });
}
auto get_relationship(const nlohmann::json &j, const nlohmann::json &from,
const nlohmann::json &to, const std::string &type)
{
return std::find_if(j["relationships"].begin(), j["relationships"].end(),
[&](const auto &it) {
return (it["source"] == from) && (it["destination"] == to) &&
(it["type"] == type);
});
}
auto get_relationship(const nlohmann::json &j, const std::string &from,
const std::string &to, const std::string &type)
{
auto from_it = detail::get_element(j, from);
auto to_it = detail::get_element(j, to);
if (from_it == j["elements"].end() || to_it == j["elements"].end())
return j["relationships"].end();
return detail::get_relationship(
j, from_it->at("id"), to_it->at("id"), type);
}
} // namespace detail
bool IsClass(const nlohmann::json &j, const std::string &name)
{
return detail::get_element(j, name) != j["elements"].end();
}
bool IsBaseClass(const nlohmann::json &j, const std::string &base,
const std::string &subclass)
{
auto sc = detail::get_element(j, subclass);
if (sc == j["elements"].end())
return false;
const nlohmann::json &bases = (*sc)["bases"];
return std::find_if(bases.begin(), bases.end(), [&](const auto &it) {
return it["name"] == base;
}) != bases.end();
}
bool IsMethod(
const nlohmann::json &j, const std::string &cls, const std::string &name)
{
auto sc = detail::get_element(j, cls);
if (sc == j["elements"].end())
return false;
const nlohmann::json &methods = (*sc)["methods"];
return std::find_if(methods.begin(), methods.end(), [&](const auto &it) {
return it["name"] == name;
}) != methods.end();
}
bool IsAssociation(nlohmann::json j, const std::string &from,
const std::string &to, const std::string &label = "")
{
auto rel = detail::get_relationship(j, from, to, "association");
if (rel == j["relationships"].end())
return false;
if (!label.empty() && rel->at("label") != label)
return false;
return true;
}
bool IsComposition(nlohmann::json j, const std::string &from,
const std::string &to, const std::string &label = "")
{
auto rel = detail::get_relationship(j, from, to, "composition");
if (rel == j["relationships"].end())
return false;
if (!label.empty() && rel->at("label") != label)
return false;
return true;
}
bool IsAggregation(nlohmann::json j, const std::string &from,
const std::string &to, const std::string &label = "")
{
auto rel = detail::get_relationship(j, from, to, "aggregation");
if (rel == j["relationships"].end())
return false;
if (!label.empty() && rel->at("label") != label)
return false;
return true;
}
bool IsDependency(
nlohmann::json j, const std::string &from, const std::string &to)
{
auto rel = detail::get_relationship(j, from, to, "aggregation");
return rel != j["relationships"].end();
}
} // namespace json
}
}
}