Added include diagram JSON model generator

This commit is contained in:
Bartek Kryza
2023-03-25 18:18:19 +01:00
parent aa2d3099de
commit 344549ac03
12 changed files with 438 additions and 91 deletions

View File

@@ -28,27 +28,56 @@ TEST_CASE("t40001", "[test-case][include]")
REQUIRE(model->name() == "t40001_include");
auto puml = generate_include_puml(diagram, *model);
{
auto puml = generate_include_puml(diagram, *model);
AliasMatcher _A(puml);
AliasMatcher _A(puml);
REQUIRE_THAT(puml, StartsWith("@startuml"));
REQUIRE_THAT(puml, EndsWith("@enduml\n"));
REQUIRE_THAT(puml, StartsWith("@startuml"));
REQUIRE_THAT(puml, EndsWith("@enduml\n"));
REQUIRE_THAT(puml, IsFolder("lib1"));
REQUIRE_THAT(puml, IsFile("lib1.h"));
REQUIRE_THAT(puml, IsFile("t40001.cc"));
REQUIRE_THAT(puml, IsFile("t40001_include1.h"));
REQUIRE_THAT(puml, IsFolder("lib1"));
REQUIRE_THAT(puml, IsFile("lib1.h"));
REQUIRE_THAT(puml, IsFile("t40001.cc"));
REQUIRE_THAT(puml, IsFile("t40001_include1.h"));
REQUIRE_THAT(puml, IsFile("string"));
REQUIRE_THAT(puml, IsFile("yaml-cpp/yaml.h"));
REQUIRE_THAT(puml, IsFile("string"));
REQUIRE_THAT(puml, IsFile("yaml-cpp/yaml.h"));
REQUIRE_THAT(puml, IsAssociation(_A("t40001.cc"), _A("t40001_include1.h")));
REQUIRE_THAT(puml, IsAssociation(_A("t40001_include1.h"), _A("lib1.h")));
REQUIRE_THAT(
puml, IsAssociation(_A("t40001.cc"), _A("t40001_include1.h")));
REQUIRE_THAT(
puml, IsAssociation(_A("t40001_include1.h"), _A("lib1.h")));
REQUIRE_THAT(puml, IsDependency(_A("t40001_include1.h"), _A("string")));
REQUIRE_THAT(puml, IsDependency(_A("t40001_include1.h"), _A("string")));
REQUIRE_THAT(puml, HasComment("t40001 test diagram of type include"));
REQUIRE_THAT(puml, HasComment("t40001 test diagram of type include"));
save_puml(config.output_directory() + "/" + diagram->name + ".puml", puml);
save_puml(
config.output_directory() + "/" + diagram->name + ".puml", puml);
}
{
auto j = generate_include_json(diagram, *model);
using namespace json;
REQUIRE(IsFolder(j, "include"));
REQUIRE(IsFolder(j, "include/lib1"));
REQUIRE(IsFolder(j, "src"));
REQUIRE(IsFile(j, "include/lib1/lib1.h"));
REQUIRE(IsFile(j, "include/t40001_include1.h"));
REQUIRE(IsFile(j, "src/t40001.cc"));
REQUIRE(IsFile(j, "yaml-cpp/yaml.h"));
REQUIRE(IsFile(j, "string"));
REQUIRE(IsAssociation(j, "src/t40001.cc", "include/t40001_include1.h"));
REQUIRE(IsAssociation(
j, "include/t40001_include1.h", "include/lib1/lib1.h"));
REQUIRE(IsDependency(j, "include/t40001_include1.h", "string"));
save_json(config.output_directory() + "/" + diagram->name + ".json", j);
}
}

View File

@@ -28,56 +28,87 @@ TEST_CASE("t40002", "[test-case][include]")
REQUIRE(model->name() == "t40002_include");
auto puml = generate_include_puml(diagram, *model);
{
auto puml = generate_include_puml(diagram, *model);
AliasMatcher _A(puml);
AliasMatcher _A(puml);
REQUIRE_THAT(puml, StartsWith("@startuml"));
REQUIRE_THAT(puml, EndsWith("@enduml\n"));
REQUIRE_THAT(puml, StartsWith("@startuml"));
REQUIRE_THAT(puml, EndsWith("@enduml\n"));
REQUIRE_THAT(puml, IsFolder("lib1"));
REQUIRE_THAT(puml, IsFolder("lib2"));
REQUIRE_THAT(puml, IsFile("lib1.h"));
REQUIRE_THAT(puml, IsFile("lib2.h"));
REQUIRE_THAT(puml, !IsFile("lib2_detail.h"));
REQUIRE_THAT(puml, IsFile("t40002.cc"));
REQUIRE_THAT(puml, IsFile("lib1.cc"));
REQUIRE_THAT(puml, IsFile("lib2.cc"));
REQUIRE_THAT(puml, IsFolder("lib1"));
REQUIRE_THAT(puml, IsFolder("lib2"));
REQUIRE_THAT(puml, IsFile("lib1.h"));
REQUIRE_THAT(puml, IsFile("lib2.h"));
REQUIRE_THAT(puml, !IsFile("lib2_detail.h"));
REQUIRE_THAT(puml, IsFile("t40002.cc"));
REQUIRE_THAT(puml, IsFile("lib1.cc"));
REQUIRE_THAT(puml, IsFile("lib2.cc"));
REQUIRE_THAT(puml, !IsFile("string"));
REQUIRE_THAT(puml, !IsFile("string"));
REQUIRE_THAT(puml, IsAssociation(_A("t40002.cc"), _A("lib1.h")));
REQUIRE_THAT(puml, IsAssociation(_A("lib1.h"), _A("lib2.h")));
REQUIRE_THAT(puml, IsAssociation(_A("lib1.cc"), _A("lib1.h")));
REQUIRE_THAT(puml, IsAssociation(_A("lib2.cc"), _A("lib2.h")));
REQUIRE_THAT(puml, IsAssociation(_A("t40002.cc"), _A("lib1.h")));
REQUIRE_THAT(puml, IsAssociation(_A("lib1.h"), _A("lib2.h")));
REQUIRE_THAT(puml, IsAssociation(_A("lib1.cc"), _A("lib1.h")));
REQUIRE_THAT(puml, IsAssociation(_A("lib2.cc"), _A("lib2.h")));
REQUIRE_THAT(puml,
HasLink(_A("t40002.cc"),
fmt::format("https://github.com/bkryza/clang-uml/blob/{}/tests/"
"t40002/src/t40002.cc#L0",
clanguml::util::get_git_commit()),
"t40002.cc"));
REQUIRE_THAT(puml,
HasLink(_A("t40002.cc"),
fmt::format("https://github.com/bkryza/clang-uml/blob/{}/tests/"
"t40002/src/t40002.cc#L0",
clanguml::util::get_git_commit()),
"t40002.cc"));
REQUIRE_THAT(puml,
HasLink(_A("lib1.cc"),
fmt::format("https://github.com/bkryza/clang-uml/blob/{}/tests/"
"t40002/src/lib1/lib1.cc#L0",
clanguml::util::get_git_commit()),
"lib1.cc"));
REQUIRE_THAT(puml,
HasLink(_A("lib1.cc"),
fmt::format("https://github.com/bkryza/clang-uml/blob/{}/tests/"
"t40002/src/lib1/lib1.cc#L0",
clanguml::util::get_git_commit()),
"lib1.cc"));
REQUIRE_THAT(puml,
HasLink(_A("lib1.h"),
fmt::format("https://github.com/bkryza/clang-uml/blob/{}/tests/"
"t40002/include/lib1/lib1.h#L0",
clanguml::util::get_git_commit()),
"lib1.h"));
REQUIRE_THAT(puml,
HasLink(_A("lib1.h"),
fmt::format("https://github.com/bkryza/clang-uml/blob/{}/tests/"
"t40002/include/lib1/lib1.h#L0",
clanguml::util::get_git_commit()),
"lib1.h"));
REQUIRE_THAT(puml,
HasLink(_A("lib2.h"),
fmt::format("https://github.com/bkryza/clang-uml/blob/{}/tests/"
"t40002/include/lib2/lib2.h#L0",
clanguml::util::get_git_commit()),
"lib2.h"));
REQUIRE_THAT(puml,
HasLink(_A("lib2.h"),
fmt::format("https://github.com/bkryza/clang-uml/blob/{}/tests/"
"t40002/include/lib2/lib2.h#L0",
clanguml::util::get_git_commit()),
"lib2.h"));
save_puml(config.output_directory() + "/" + diagram->name + ".puml", puml);
save_puml(
config.output_directory() + "/" + diagram->name + ".puml", puml);
}
{
auto j = generate_include_json(diagram, *model);
using namespace json;
REQUIRE(IsFolder(j, "include"));
REQUIRE(IsFolder(j, "include/lib1"));
REQUIRE(IsFolder(j, "include/lib2"));
REQUIRE(IsFolder(j, "src"));
REQUIRE(IsFolder(j, "src/lib1"));
REQUIRE(IsFolder(j, "src/lib2"));
REQUIRE(IsFile(j, "include/lib1/lib1.h"));
REQUIRE(IsFile(j, "include/lib2/lib2.h"));
REQUIRE(!IsFile(j, "include/lib2/lib2_detail.h"));
REQUIRE(IsFile(j, "src/lib1/lib1.cc"));
REQUIRE(IsFile(j, "src/lib2/lib2.cc"));
REQUIRE(IsFile(j, "src/t40002.cc"));
REQUIRE(!IsFile(j, "string"));
REQUIRE(IsAssociation(j, "src/t40002.cc", "include/lib1/lib1.h"));
REQUIRE(IsAssociation(j, "include/lib1/lib1.h", "include/lib2/lib2.h"));
REQUIRE(IsAssociation(j, "src/lib1/lib1.cc", "include/lib1/lib1.h"));
REQUIRE(IsAssociation(j, "src/lib2/lib2.cc", "include/lib2/lib2.h"));
save_json(config.output_directory() + "/" + diagram->name + ".json", j);
}
}

View File

@@ -28,23 +28,51 @@ TEST_CASE("t40003", "[test-case][include]")
REQUIRE(model->name() == "t40003_include");
auto puml = generate_include_puml(diagram, *model);
{
auto puml = generate_include_puml(diagram, *model);
AliasMatcher _A(puml);
AliasMatcher _A(puml);
REQUIRE_THAT(puml, StartsWith("@startuml"));
REQUIRE_THAT(puml, EndsWith("@enduml\n"));
REQUIRE_THAT(puml, StartsWith("@startuml"));
REQUIRE_THAT(puml, EndsWith("@enduml\n"));
REQUIRE_THAT(puml, IsFolder("dependants"));
REQUIRE_THAT(puml, IsFolder("dependencies"));
REQUIRE_THAT(puml, IsFolder("dependants"));
REQUIRE_THAT(puml, IsFolder("dependencies"));
REQUIRE_THAT(puml, IsFile("t1.h"));
REQUIRE_THAT(puml, IsFile("t2.h"));
REQUIRE_THAT(puml, IsFile("t3.h"));
REQUIRE_THAT(puml, IsFile("t1.h"));
REQUIRE_THAT(puml, IsFile("t2.h"));
REQUIRE_THAT(puml, IsFile("t3.h"));
REQUIRE_THAT(puml, !IsFile("t4.h"));
REQUIRE_THAT(puml, IsFile("t5.h"));
REQUIRE_THAT(puml, !IsFile("t6.h"));
REQUIRE_THAT(puml, !IsFile("t4.h"));
REQUIRE_THAT(puml, IsFile("t5.h"));
REQUIRE_THAT(puml, !IsFile("t6.h"));
save_puml(config.output_directory() + "/" + diagram->name + ".puml", puml);
save_puml(
config.output_directory() + "/" + diagram->name + ".puml", puml);
}
{
auto j = generate_include_json(diagram, *model);
using namespace json;
REQUIRE(IsFolder(j, "include/dependants"));
REQUIRE(IsFolder(j, "include/dependencies"));
REQUIRE(IsFolder(j, "src/dependants"));
REQUIRE(IsFolder(j, "src/dependencies"));
REQUIRE(IsFile(j, "include/dependants/t1.h"));
REQUIRE(IsFile(j, "include/dependants/t2.h"));
REQUIRE(IsFile(j, "include/dependants/t3.h"));
REQUIRE(!IsFile(j, "include/dependants/t4.h"));
REQUIRE(IsFile(j, "src/dependants/t1.cc"));
REQUIRE(IsFile(j, "include/dependencies/t1.h"));
REQUIRE(IsFile(j, "include/dependencies/t2.h"));
REQUIRE(IsFile(j, "include/dependencies/t3.h"));
REQUIRE(!IsFile(j, "include/dependencies/t4.h"));
REQUIRE(IsFile(j, "src/dependencies/t2.cc"));
save_json(config.output_directory() + "/" + diagram->name + ".json", j);
}
}

View File

@@ -232,6 +232,22 @@ std::string generate_include_puml(
return ss.str();
}
nlohmann::json generate_include_json(
std::shared_ptr<clanguml::config::diagram> config,
clanguml::include_diagram::model::diagram &model)
{
using namespace clanguml::include_diagram::generators::json;
std::stringstream ss;
assert(config.get() != nullptr);
ss << generator(
dynamic_cast<clanguml::config::include_diagram &>(*config), model);
return nlohmann::json::parse(ss.str());
}
void save_puml(const std::string &path, const std::string &puml)
{
std::filesystem::path p{path};

View File

@@ -611,6 +611,15 @@ ContainsMatcher IsDeprecated(std::string const &str,
}
namespace json {
struct File {
explicit File(const std::string &f)
: file{f}
{
}
const std::string file;
};
std::optional<nlohmann::json> get_element(
const nlohmann::json &j, const std::string &name)
{
@@ -621,7 +630,7 @@ std::optional<nlohmann::json> get_element(
if (e["display_name"] == name)
return {e};
if (e["type"] == "namespace") {
if (e["type"] == "namespace" || e["type"] == "folder") {
auto maybe_e = get_element(e, name);
if (maybe_e)
return maybe_e;
@@ -714,6 +723,18 @@ bool IsPackage(const nlohmann::json &j, const std::string &name)
return e && e->at("type") == "namespace";
}
bool IsFolder(const nlohmann::json &j, const std::string &name)
{
auto e = get_element(j, name);
return e && e->at("type") == "folder";
}
bool IsFile(const nlohmann::json &j, const std::string &name)
{
auto e = get_element(j, name);
return e && e->at("type") == "file";
}
bool IsDeprecated(const nlohmann::json &j, const std::string &name)
{
auto e = get_element(j, expand_name(j, name));
@@ -810,13 +831,27 @@ bool IsAggregation(nlohmann::json j, const std::string &from,
return true;
}
namespace detail {
bool is_dependency_impl(
nlohmann::json j, const std::string &from, const std::string &to)
{
auto rel = get_relationship(j, from, to, "dependency");
return rel != j["relationships"].end();
}
} // namespace detail
bool IsDependency(
nlohmann::json j, const std::string &from, const std::string &to)
{
auto rel = get_relationship(
j, expand_name(j, from), expand_name(j, to), "dependency");
return detail::is_dependency_impl(
j, expand_name(j, from), expand_name(j, to));
}
return rel != j["relationships"].end();
bool IsDependency(nlohmann::json j, const File &from, const File &to)
{
return detail::is_dependency_impl(j, from.file, to.file);
}
bool IsInstantiation(
@@ -930,15 +965,6 @@ int find_message_impl(const nlohmann::json &j, const std::string &from,
} // namespace detail
struct File {
explicit File(const std::string &f)
: file{f}
{
}
const std::string file;
};
int FindMessage(const nlohmann::json &j, const File &from, const File &to,
const std::string &msg)
{