From e014454593009dda63f956486d0bdd31be80cd86 Mon Sep 17 00:00:00 2001 From: Bartek Kryza Date: Sat, 27 Feb 2021 00:46:59 +0100 Subject: [PATCH] Refactored test case structure --- tests/t00001/test_case.h | 52 +++++++++++ tests/t00002/test_case.h | 53 +++++++++++ tests/t00003/test_case.h | 65 ++++++++++++++ tests/t00004/test_case.h | 56 ++++++++++++ tests/test_cases.cc | 190 +++------------------------------------ tests/test_cases.h | 59 ++++++++++++ 6 files changed, 296 insertions(+), 179 deletions(-) create mode 100644 tests/t00001/test_case.h create mode 100644 tests/t00002/test_case.h create mode 100644 tests/t00003/test_case.h create mode 100644 tests/t00004/test_case.h create mode 100644 tests/test_cases.h diff --git a/tests/t00001/test_case.h b/tests/t00001/test_case.h new file mode 100644 index 00000000..4c2b3c22 --- /dev/null +++ b/tests/t00001/test_case.h @@ -0,0 +1,52 @@ +/** + * tests/t00001/test_case.cc + * + * Copyright (c) 2021 Bartek Kryza + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +TEST_CASE("Test t00001", "[unit-test]") +{ + spdlog::set_level(spdlog::level::debug); + + auto [config, db] = load_config("t00001"); + + auto diagram = config.diagrams["t00001_sequence"]; + + REQUIRE(diagram->include.namespaces.size() == 1); + REQUIRE_THAT(diagram->include.namespaces, + VectorContains(std::string{"clanguml::t00001"})); + + REQUIRE(diagram->exclude.namespaces.size() == 1); + REQUIRE_THAT(diagram->exclude.namespaces, + VectorContains(std::string{"clanguml::t00001::detail"})); + + REQUIRE(diagram->should_include("clanguml::t00001::A")); + REQUIRE(!diagram->should_include("clanguml::t00001::detail::C")); + REQUIRE(!diagram->should_include("std::vector")); + + REQUIRE(diagram->name == "t00001_sequence"); + + auto model = generate_sequence_diagram(db, diagram); + + REQUIRE(model.name == "t00001_sequence"); + + auto puml = generate_sequence_puml(diagram, model); + + REQUIRE_THAT(puml, StartsWith("@startuml")); + REQUIRE_THAT(puml, EndsWith("@enduml\n")); + + save_puml( + "./" + config.output_directory + "/" + diagram->name + ".puml", puml); +} diff --git a/tests/t00002/test_case.h b/tests/t00002/test_case.h new file mode 100644 index 00000000..0aabc108 --- /dev/null +++ b/tests/t00002/test_case.h @@ -0,0 +1,53 @@ +/** + * tests/t00002/test_case.cc + * + * Copyright (c) 2021 Bartek Kryza + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +TEST_CASE("Test t00002", "[unit-test]") +{ + spdlog::set_level(spdlog::level::debug); + + auto [config, db] = load_config("t00002"); + + auto diagram = config.diagrams["t00002_class"]; + + REQUIRE(diagram->name == "t00002_class"); + + REQUIRE(diagram->include.namespaces.size() == 1); + REQUIRE_THAT(diagram->include.namespaces, + VectorContains(std::string{"clanguml::t00002"})); + + REQUIRE(diagram->exclude.namespaces.size() == 0); + + REQUIRE(diagram->should_include("clanguml::t00002::A")); + REQUIRE(!diagram->should_include("std::vector")); + + auto model = generate_class_diagram(db, diagram); + + REQUIRE(model.name == "t00002_class"); + + auto puml = generate_class_puml(diagram, model); + + REQUIRE_THAT(puml, StartsWith("@startuml")); + REQUIRE_THAT(puml, EndsWith("@enduml\n")); + REQUIRE_THAT(puml, Contains("abstract A")); + REQUIRE_THAT(puml, Contains("class B")); + REQUIRE_THAT(puml, Contains("class C")); + REQUIRE_THAT(puml, Contains("class D")); + + save_puml( + "./" + config.output_directory + "/" + diagram->name + ".puml", puml); +} diff --git a/tests/t00003/test_case.h b/tests/t00003/test_case.h new file mode 100644 index 00000000..bf521f93 --- /dev/null +++ b/tests/t00003/test_case.h @@ -0,0 +1,65 @@ +/** + * tests/t00003/test_case.cc + * + * Copyright (c) 2021 Bartek Kryza + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +TEST_CASE("Test t00003", "[unit-test]") +{ + spdlog::set_level(spdlog::level::debug); + + auto [config, db] = load_config("t00003"); + + auto diagram = config.diagrams["t00003_class"]; + + REQUIRE(diagram->name == "t00003_class"); + + REQUIRE(diagram->include.namespaces.size() == 1); + REQUIRE_THAT(diagram->include.namespaces, + VectorContains(std::string{"clanguml::t00003"})); + + REQUIRE(diagram->exclude.namespaces.size() == 0); + + REQUIRE(diagram->should_include("clanguml::t00003::A")); + + auto model = generate_class_diagram(db, diagram); + + REQUIRE(model.name == "t00003_class"); + + auto puml = generate_class_puml(diagram, model); + + REQUIRE_THAT(puml, StartsWith("@startuml")); + REQUIRE_THAT(puml, EndsWith("@enduml\n")); + REQUIRE_THAT(puml, Contains("class A")); + + REQUIRE_THAT(puml, Contains("+ A() = default")); + REQUIRE_THAT(puml, Contains("+ A() = default")); + REQUIRE_THAT(puml, Contains("+ A() = default")); + REQUIRE_THAT(puml, Contains("+ ~A() = default")); + REQUIRE_THAT(puml, Contains("+ basic_method()")); + REQUIRE_THAT(puml, Contains("{static} +int static_method()")); + REQUIRE_THAT(puml, Contains("+ const_method() const")); + REQUIRE_THAT(puml, Contains("# protected_method()")); + REQUIRE_THAT(puml, Contains("- private_method()")); + REQUIRE_THAT(puml, Contains("+int public_member")); + REQUIRE_THAT(puml, Contains("#int protected_member")); + REQUIRE_THAT(puml, Contains("-int private_member")); + REQUIRE_THAT(puml, Contains("-int a")); + REQUIRE_THAT(puml, Contains("-int b")); + REQUIRE_THAT(puml, Contains("-int c")); + + save_puml( + "./" + config.output_directory + "/" + diagram->name + ".puml", puml); +} diff --git a/tests/t00004/test_case.h b/tests/t00004/test_case.h new file mode 100644 index 00000000..3d193d5d --- /dev/null +++ b/tests/t00004/test_case.h @@ -0,0 +1,56 @@ +/** + * tests/t00004/test_case.cc + * + * Copyright (c) 2021 Bartek Kryza + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +TEST_CASE("Test t00004", "[unit-test]") +{ + spdlog::set_level(spdlog::level::debug); + + auto [config, db] = load_config("t00004"); + + auto diagram = config.diagrams["t00004_class"]; + + REQUIRE(diagram->name == "t00004_class"); + + REQUIRE(diagram->include.namespaces.size() == 1); + REQUIRE_THAT(diagram->include.namespaces, + VectorContains(std::string{"clanguml::t00004"})); + + REQUIRE(diagram->exclude.namespaces.size() == 0); + + REQUIRE(diagram->should_include("clanguml::t00004::A")); + REQUIRE(diagram->should_include("clanguml::t00004::A::AA")); + REQUIRE(diagram->should_include("clanguml::t00004::A:::AAA")); + + auto model = generate_class_diagram(db, diagram); + + REQUIRE(model.name == "t00004_class"); + + auto puml = generate_class_puml(diagram, model); + + REQUIRE_THAT(puml, StartsWith("@startuml")); + REQUIRE_THAT(puml, EndsWith("@enduml\n")); + REQUIRE_THAT(puml, Contains("class A")); + REQUIRE_THAT(puml, Contains("A +-- AA")); + REQUIRE_THAT(puml, Contains("AA +-- AAA")); + REQUIRE_THAT(puml, Contains("AA +-- Lights")); + REQUIRE_THAT(puml, Contains("+ foo() const")); + REQUIRE_THAT(puml, Contains("+ foo2() const")); + + save_puml( + "./" + config.output_directory + "/" + diagram->name + ".puml", puml); +} diff --git a/tests/test_cases.cc b/tests/test_cases.cc index bba9dc6f..ff8df601 100644 --- a/tests/test_cases.cc +++ b/tests/test_cases.cc @@ -17,27 +17,10 @@ */ #define CATCH_CONFIG_MAIN -#include "config/config.h" -#include "cx/compilation_database.h" -#include "puml/class_diagram_generator.h" -#include "puml/sequence_diagram_generator.h" -#include "uml/class_diagram_model.h" -#include "uml/class_diagram_visitor.h" -#include "uml/sequence_diagram_visitor.h" +#include "test_cases.h" -#include "catch.h" - -#include -#include -#include - -using Catch::Matchers::Contains; -using Catch::Matchers::EndsWith; -using Catch::Matchers::StartsWith; -using Catch::Matchers::VectorContains; -using clanguml::cx::compilation_database; - -auto load_config(const std::string &test_name) +std::pair load_config( + const std::string &test_name) { auto config = clanguml::config::load(test_name + "/.clanguml"); @@ -47,7 +30,8 @@ auto load_config(const std::string &test_name) return std::make_pair(std::move(config), std::move(db)); } -auto generate_sequence_diagram(compilation_database &db, +clanguml::model::sequence_diagram::diagram generate_sequence_diagram( + compilation_database &db, std::shared_ptr diagram) { auto diagram_model = @@ -57,7 +41,8 @@ auto generate_sequence_diagram(compilation_database &db, return diagram_model; } -auto generate_class_diagram(compilation_database &db, +clanguml::model::class_diagram::diagram generate_class_diagram( + compilation_database &db, std::shared_ptr diagram) { auto diagram_model = @@ -107,160 +92,7 @@ void save_puml(const std::string &path, const std::string &puml) ofs.close(); } -TEST_CASE("Test t00001", "[unit-test]") -{ - spdlog::set_level(spdlog::level::debug); - - auto [config, db] = load_config("t00001"); - - auto diagram = config.diagrams["t00001_sequence"]; - - REQUIRE(diagram->include.namespaces.size() == 1); - REQUIRE_THAT(diagram->include.namespaces, - VectorContains(std::string{"clanguml::t00001"})); - - REQUIRE(diagram->exclude.namespaces.size() == 1); - REQUIRE_THAT(diagram->exclude.namespaces, - VectorContains(std::string{"clanguml::t00001::detail"})); - - REQUIRE(diagram->should_include("clanguml::t00001::A")); - REQUIRE(!diagram->should_include("clanguml::t00001::detail::C")); - REQUIRE(!diagram->should_include("std::vector")); - - REQUIRE(diagram->name == "t00001_sequence"); - - auto model = generate_sequence_diagram(db, diagram); - - REQUIRE(model.name == "t00001_sequence"); - - auto puml = generate_sequence_puml(diagram, model); - - REQUIRE_THAT(puml, StartsWith("@startuml")); - REQUIRE_THAT(puml, EndsWith("@enduml\n")); - - save_puml( - "./" + config.output_directory + "/" + diagram->name + ".puml", puml); -} - -TEST_CASE("Test t00002", "[unit-test]") -{ - spdlog::set_level(spdlog::level::debug); - - auto [config, db] = load_config("t00002"); - - auto diagram = config.diagrams["t00002_class"]; - - REQUIRE(diagram->name == "t00002_class"); - - REQUIRE(diagram->include.namespaces.size() == 1); - REQUIRE_THAT(diagram->include.namespaces, - VectorContains(std::string{"clanguml::t00002"})); - - REQUIRE(diagram->exclude.namespaces.size() == 0); - - REQUIRE(diagram->should_include("clanguml::t00002::A")); - REQUIRE(!diagram->should_include("std::vector")); - - auto model = generate_class_diagram(db, diagram); - - REQUIRE(model.name == "t00002_class"); - - auto puml = generate_class_puml(diagram, model); - - REQUIRE_THAT(puml, StartsWith("@startuml")); - REQUIRE_THAT(puml, EndsWith("@enduml\n")); - REQUIRE_THAT(puml, Contains("abstract A")); - REQUIRE_THAT(puml, Contains("class B")); - REQUIRE_THAT(puml, Contains("class C")); - REQUIRE_THAT(puml, Contains("class D")); - - save_puml( - "./" + config.output_directory + "/" + diagram->name + ".puml", puml); -} - -TEST_CASE("Test t00003", "[unit-test]") -{ - spdlog::set_level(spdlog::level::debug); - - auto [config, db] = load_config("t00003"); - - auto diagram = config.diagrams["t00003_class"]; - - REQUIRE(diagram->name == "t00003_class"); - - REQUIRE(diagram->include.namespaces.size() == 1); - REQUIRE_THAT(diagram->include.namespaces, - VectorContains(std::string{"clanguml::t00003"})); - - REQUIRE(diagram->exclude.namespaces.size() == 0); - - REQUIRE(diagram->should_include("clanguml::t00003::A")); - - auto model = generate_class_diagram(db, diagram); - - REQUIRE(model.name == "t00003_class"); - - auto puml = generate_class_puml(diagram, model); - - REQUIRE_THAT(puml, StartsWith("@startuml")); - REQUIRE_THAT(puml, EndsWith("@enduml\n")); - REQUIRE_THAT(puml, Contains("class A")); - - REQUIRE_THAT(puml, Contains("+ A() = default")); - REQUIRE_THAT(puml, Contains("+ A() = default")); - REQUIRE_THAT(puml, Contains("+ A() = default")); - REQUIRE_THAT(puml, Contains("+ ~A() = default")); - REQUIRE_THAT(puml, Contains("+ basic_method()")); - REQUIRE_THAT(puml, Contains("{static} +int static_method()")); - REQUIRE_THAT(puml, Contains("+ const_method() const")); - REQUIRE_THAT(puml, Contains("# protected_method()")); - REQUIRE_THAT(puml, Contains("- private_method()")); - REQUIRE_THAT(puml, Contains("+int public_member")); - REQUIRE_THAT(puml, Contains("#int protected_member")); - REQUIRE_THAT(puml, Contains("-int private_member")); - REQUIRE_THAT(puml, Contains("-int a")); - REQUIRE_THAT(puml, Contains("-int b")); - REQUIRE_THAT(puml, Contains("-int c")); - - save_puml( - "./" + config.output_directory + "/" + diagram->name + ".puml", puml); -} - -TEST_CASE("Test t00004", "[unit-test]") -{ - spdlog::set_level(spdlog::level::debug); - - auto [config, db] = load_config("t00004"); - - auto diagram = config.diagrams["t00004_class"]; - - REQUIRE(diagram->name == "t00004_class"); - - REQUIRE(diagram->include.namespaces.size() == 1); - REQUIRE_THAT(diagram->include.namespaces, - VectorContains(std::string{"clanguml::t00004"})); - - REQUIRE(diagram->exclude.namespaces.size() == 0); - - REQUIRE(diagram->should_include("clanguml::t00004::A")); - REQUIRE(diagram->should_include("clanguml::t00004::A::AA")); - REQUIRE(diagram->should_include("clanguml::t00004::A:::AAA")); - - auto model = generate_class_diagram(db, diagram); - - REQUIRE(model.name == "t00004_class"); - - auto puml = generate_class_puml(diagram, model); - - REQUIRE_THAT(puml, StartsWith("@startuml")); - REQUIRE_THAT(puml, EndsWith("@enduml\n")); - REQUIRE_THAT(puml, Contains("class A")); - REQUIRE_THAT(puml, Contains("A +-- AA")); - REQUIRE_THAT(puml, Contains("AA +-- AAA")); - REQUIRE_THAT(puml, Contains("AA +-- Lights")); - REQUIRE_THAT(puml, Contains("+ foo() const")); - REQUIRE_THAT(puml, Contains("+ foo2() const")); - - save_puml( - "./" + config.output_directory + "/" + diagram->name + ".puml", puml); -} +#include "t00001/test_case.h" +#include "t00002/test_case.h" +#include "t00003/test_case.h" +#include "t00004/test_case.h" diff --git a/tests/test_cases.h b/tests/test_cases.h new file mode 100644 index 00000000..c651b743 --- /dev/null +++ b/tests/test_cases.h @@ -0,0 +1,59 @@ +/** + * tests/test_cases.h + * + * Copyright (c) 2021 Bartek Kryza + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#include "config/config.h" +#include "cx/compilation_database.h" +#include "puml/class_diagram_generator.h" +#include "puml/sequence_diagram_generator.h" +#include "uml/class_diagram_model.h" +#include "uml/class_diagram_visitor.h" +#include "uml/sequence_diagram_visitor.h" + +#include "catch.h" + +#include +#include +#include + +using Catch::Matchers::Contains; +using Catch::Matchers::EndsWith; +using Catch::Matchers::StartsWith; +using Catch::Matchers::VectorContains; +using clanguml::cx::compilation_database; + +std::pair load_config( + const std::string &test_name); + +clanguml::model::sequence_diagram::diagram generate_sequence_diagram( + compilation_database &db, + std::shared_ptr diagram); + +clanguml::model::class_diagram::diagram generate_class_diagram( + compilation_database &db, + std::shared_ptr diagram); + +std::string generate_sequence_puml( + std::shared_ptr config, + clanguml::model::sequence_diagram::diagram &model); + +std::string generate_class_puml( + std::shared_ptr config, + clanguml::model::class_diagram::diagram &model); + +void save_puml(const std::string &path, const std::string &puml);