Refactored command to decorators
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* src/uml/command_parser.cc
|
* src/uml/decorators.cc
|
||||||
*
|
*
|
||||||
* Copyright (c) 2021 Bartek Kryza <bkryza@gmail.com>
|
* Copyright (c) 2021 Bartek Kryza <bkryza@gmail.com>
|
||||||
*
|
*
|
||||||
@@ -15,7 +15,7 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
#include "command_parser.h"
|
#include "decorators.h"
|
||||||
|
|
||||||
#include "util/util.h"
|
#include "util/util.h"
|
||||||
|
|
||||||
@@ -23,7 +23,7 @@
|
|||||||
#include <string_view>
|
#include <string_view>
|
||||||
|
|
||||||
namespace clanguml {
|
namespace clanguml {
|
||||||
namespace command_parser {
|
namespace decorators {
|
||||||
|
|
||||||
const std::string note::label = "note";
|
const std::string note::label = "note";
|
||||||
const std::string skip::label = "skip";
|
const std::string skip::label = "skip";
|
||||||
@@ -31,7 +31,7 @@ const std::string skip_relationship::label = "skiprelationship";
|
|||||||
const std::string style::label = "style";
|
const std::string style::label = "style";
|
||||||
const std::string aggregation::label = "aggregation";
|
const std::string aggregation::label = "aggregation";
|
||||||
|
|
||||||
std::shared_ptr<command> command::from_string(std::string_view c)
|
std::shared_ptr<decorator> decorator::from_string(std::string_view c)
|
||||||
{
|
{
|
||||||
if (c.find(note::label) == 0) {
|
if (c.find(note::label) == 0) {
|
||||||
return note::from_string(c);
|
return note::from_string(c);
|
||||||
@@ -52,7 +52,7 @@ std::shared_ptr<command> command::from_string(std::string_view c)
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<command> note::from_string(std::string_view c)
|
std::shared_ptr<decorator> note::from_string(std::string_view c)
|
||||||
{
|
{
|
||||||
auto res = std::make_shared<note>();
|
auto res = std::make_shared<note>();
|
||||||
auto it = c.begin();
|
auto it = c.begin();
|
||||||
@@ -77,19 +77,19 @@ std::shared_ptr<command> note::from_string(std::string_view c)
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<command> skip::from_string(std::string_view c)
|
std::shared_ptr<decorator> skip::from_string(std::string_view c)
|
||||||
{
|
{
|
||||||
auto res = std::make_shared<skip>();
|
auto res = std::make_shared<skip>();
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<command> skip_relationship::from_string(std::string_view c)
|
std::shared_ptr<decorator> skip_relationship::from_string(std::string_view c)
|
||||||
{
|
{
|
||||||
auto res = std::make_shared<skip_relationship>();
|
auto res = std::make_shared<skip_relationship>();
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<command> style::from_string(std::string_view c)
|
std::shared_ptr<decorator> style::from_string(std::string_view c)
|
||||||
{
|
{
|
||||||
auto res = std::make_shared<style>();
|
auto res = std::make_shared<style>();
|
||||||
auto it = c.begin();
|
auto it = c.begin();
|
||||||
@@ -108,7 +108,7 @@ std::shared_ptr<command> style::from_string(std::string_view c)
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<command> aggregation::from_string(std::string_view c)
|
std::shared_ptr<decorator> aggregation::from_string(std::string_view c)
|
||||||
{
|
{
|
||||||
auto res = std::make_shared<aggregation>();
|
auto res = std::make_shared<aggregation>();
|
||||||
auto it = c.begin();
|
auto it = c.begin();
|
||||||
@@ -127,9 +127,9 @@ std::shared_ptr<command> aggregation::from_string(std::string_view c)
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::shared_ptr<command>> parse(std::string documentation_block)
|
std::vector<std::shared_ptr<decorator>> parse(std::string documentation_block)
|
||||||
{
|
{
|
||||||
std::vector<std::shared_ptr<command>> res;
|
std::vector<std::shared_ptr<decorator>> res;
|
||||||
const std::string begin_tag{"@clanguml"};
|
const std::string begin_tag{"@clanguml"};
|
||||||
const auto begin_tag_size = begin_tag.size();
|
const auto begin_tag_size = begin_tag.size();
|
||||||
|
|
||||||
@@ -148,7 +148,7 @@ std::vector<std::shared_ptr<command>> parse(std::string documentation_block)
|
|||||||
return res;
|
return res;
|
||||||
|
|
||||||
auto com =
|
auto com =
|
||||||
command::from_string(block_view.substr(c_begin + 1, c_end - 2));
|
decorator::from_string(block_view.substr(c_begin + 1, c_end - 2));
|
||||||
|
|
||||||
if (com)
|
if (com)
|
||||||
res.emplace_back(std::move(com));
|
res.emplace_back(std::move(com));
|
||||||
@@ -159,6 +159,6 @@ std::vector<std::shared_ptr<command>> parse(std::string documentation_block)
|
|||||||
return res;
|
return res;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace command_parser
|
} // namespace decorators
|
||||||
} // namespace clanguml
|
} // namespace clanguml
|
||||||
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* src/uml/command_parser.h
|
* src/uml/decorators.h
|
||||||
*
|
*
|
||||||
* Copyright (c) 2021 Bartek Kryza <bkryza@gmail.com>
|
* Copyright (c) 2021 Bartek Kryza <bkryza@gmail.com>
|
||||||
*
|
*
|
||||||
@@ -24,50 +24,50 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace clanguml {
|
namespace clanguml {
|
||||||
namespace command_parser {
|
namespace decorators {
|
||||||
|
|
||||||
struct command {
|
struct decorator {
|
||||||
virtual ~command() = default;
|
virtual ~decorator() = default;
|
||||||
static std::shared_ptr<command> from_string(std::string_view c);
|
static std::shared_ptr<decorator> from_string(std::string_view c);
|
||||||
};
|
};
|
||||||
|
|
||||||
struct note : public command {
|
struct note : public decorator {
|
||||||
static const std::string label;
|
static const std::string label;
|
||||||
|
|
||||||
std::string position;
|
std::string position;
|
||||||
std::string text;
|
std::string text;
|
||||||
|
|
||||||
static std::shared_ptr<command> from_string(std::string_view c);
|
static std::shared_ptr<decorator> from_string(std::string_view c);
|
||||||
};
|
};
|
||||||
|
|
||||||
struct skip : public command {
|
struct skip : public decorator {
|
||||||
static const std::string label;
|
static const std::string label;
|
||||||
|
|
||||||
static std::shared_ptr<command> from_string(std::string_view c);
|
static std::shared_ptr<decorator> from_string(std::string_view c);
|
||||||
};
|
};
|
||||||
|
|
||||||
struct skip_relationship : public command {
|
struct skip_relationship : public decorator {
|
||||||
static const std::string label;
|
static const std::string label;
|
||||||
|
|
||||||
static std::shared_ptr<command> from_string(std::string_view c);
|
static std::shared_ptr<decorator> from_string(std::string_view c);
|
||||||
};
|
};
|
||||||
|
|
||||||
struct style : public command {
|
struct style : public decorator {
|
||||||
static const std::string label;
|
static const std::string label;
|
||||||
|
|
||||||
std::string spec;
|
std::string spec;
|
||||||
static std::shared_ptr<command> from_string(std::string_view c);
|
static std::shared_ptr<decorator> from_string(std::string_view c);
|
||||||
};
|
};
|
||||||
|
|
||||||
struct aggregation : public command {
|
struct aggregation : public decorator {
|
||||||
static const std::string label;
|
static const std::string label;
|
||||||
|
|
||||||
std::string multiplicity;
|
std::string multiplicity;
|
||||||
static std::shared_ptr<command> from_string(std::string_view c);
|
static std::shared_ptr<decorator> from_string(std::string_view c);
|
||||||
};
|
};
|
||||||
|
|
||||||
std::vector<std::shared_ptr<command>> parse(std::string documentation_block);
|
std::vector<std::shared_ptr<decorator>> parse(std::string documentation_block);
|
||||||
|
|
||||||
} // namespace command_parser
|
} // namespace decorators
|
||||||
} // namespace clanguml
|
} // namespace clanguml
|
||||||
|
|
||||||
@@ -23,11 +23,11 @@ set(CLANG_UML_TEST_CASES_HEADER
|
|||||||
catch.h
|
catch.h
|
||||||
)
|
)
|
||||||
|
|
||||||
set(CLANG_UML_TEST_COMMAND_PARSER_SRC
|
set(CLANG_UML_TEST_DECORATOR_PARSER_SRC
|
||||||
test_command_parser.cc
|
test_decorator_parser.cc
|
||||||
${TEST_UTIL_SOURCES}
|
${TEST_UTIL_SOURCES}
|
||||||
)
|
)
|
||||||
set(CLANG_UML_TEST_COMMAND_PARSER_HEADER
|
set(CLANG_UML_TEST_DECORATOR_PARSER_HEADER
|
||||||
catch.h
|
catch.h
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -41,11 +41,11 @@ target_link_libraries(test_util
|
|||||||
${YAML_CPP_LIBRARIES}
|
${YAML_CPP_LIBRARIES}
|
||||||
spdlog::spdlog clang-umllib cppast)
|
spdlog::spdlog clang-umllib cppast)
|
||||||
|
|
||||||
add_executable(test_command_parser
|
add_executable(test_decorator_parser
|
||||||
${CLANG_UML_TEST_COMMAND_PARSER_SRC}
|
${CLANG_UML_TEST_DECORATOR_PARSER_SRC}
|
||||||
${CLANG_UML_TEST_COMMAND_PARSER_HEADER})
|
${CLANG_UML_TEST_DECORATOR_PARSER_HEADER})
|
||||||
|
|
||||||
target_link_libraries(test_command_parser
|
target_link_libraries(test_decorator_parser
|
||||||
PRIVATE
|
PRIVATE
|
||||||
${LIBCLANG_LIBRARIES}
|
${LIBCLANG_LIBRARIES}
|
||||||
${YAML_CPP_LIBRARIES}
|
${YAML_CPP_LIBRARIES}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* tests/test_command_parser.cc
|
* tests/test_decorator_parser.cc
|
||||||
*
|
*
|
||||||
* Copyright (c) 2021 Bartek Kryza <bkryza@gmail.com>
|
* Copyright (c) 2021 Bartek Kryza <bkryza@gmail.com>
|
||||||
*
|
*
|
||||||
@@ -17,11 +17,11 @@
|
|||||||
*/
|
*/
|
||||||
#define CATCH_CONFIG_MAIN
|
#define CATCH_CONFIG_MAIN
|
||||||
|
|
||||||
#include "uml/command_parser.h"
|
#include "uml/decorators.h"
|
||||||
|
|
||||||
#include "catch.h"
|
#include "catch.h"
|
||||||
|
|
||||||
TEST_CASE("Test command parser on regular comment", "[unit-test]")
|
TEST_CASE("Test decorator parser on regular comment", "[unit-test]")
|
||||||
{
|
{
|
||||||
std::string comment = R"(
|
std::string comment = R"(
|
||||||
\brief This is a comment.
|
\brief This is a comment.
|
||||||
@@ -33,14 +33,14 @@ TEST_CASE("Test command parser on regular comment", "[unit-test]")
|
|||||||
|
|
||||||
)";
|
)";
|
||||||
|
|
||||||
using namespace clanguml::command_parser;
|
using namespace clanguml::decorators;
|
||||||
|
|
||||||
auto commands = parse(comment);
|
auto decorators = parse(comment);
|
||||||
|
|
||||||
CHECK(commands.empty());
|
CHECK(decorators.empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Test command parser on note", "[unit-test]")
|
TEST_CASE("Test decorator parser on note", "[unit-test]")
|
||||||
{
|
{
|
||||||
std::string comment = R"(
|
std::string comment = R"(
|
||||||
\brief This is a comment.
|
\brief This is a comment.
|
||||||
@@ -58,14 +58,14 @@ TEST_CASE("Test command parser on note", "[unit-test]")
|
|||||||
This is a comment }
|
This is a comment }
|
||||||
)";
|
)";
|
||||||
|
|
||||||
using namespace clanguml::command_parser;
|
using namespace clanguml::decorators;
|
||||||
|
|
||||||
auto commands = parse(comment);
|
auto decorators = parse(comment);
|
||||||
|
|
||||||
CHECK(commands.size() == 2);
|
CHECK(decorators.size() == 2);
|
||||||
|
|
||||||
auto n1 = std::dynamic_pointer_cast<note>(commands.at(0));
|
auto n1 = std::dynamic_pointer_cast<note>(decorators.at(0));
|
||||||
auto n2 = std::dynamic_pointer_cast<note>(commands.at(1));
|
auto n2 = std::dynamic_pointer_cast<note>(decorators.at(1));
|
||||||
|
|
||||||
CHECK(n1);
|
CHECK(n1);
|
||||||
CHECK(n1->position == "left");
|
CHECK(n1->position == "left");
|
||||||
@@ -76,72 +76,72 @@ TEST_CASE("Test command parser on note", "[unit-test]")
|
|||||||
CHECK(n2->text == "This is a comment");
|
CHECK(n2->text == "This is a comment");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Test command parser on style", "[unit-test]")
|
TEST_CASE("Test decorator parser on style", "[unit-test]")
|
||||||
{
|
{
|
||||||
std::string comment = R"(
|
std::string comment = R"(
|
||||||
\clanguml{style[#green,dashed,thickness=4]}
|
\clanguml{style[#green,dashed,thickness=4]}
|
||||||
)";
|
)";
|
||||||
|
|
||||||
using namespace clanguml::command_parser;
|
using namespace clanguml::decorators;
|
||||||
|
|
||||||
auto commands = parse(comment);
|
auto decorators = parse(comment);
|
||||||
|
|
||||||
CHECK(commands.size() == 1);
|
CHECK(decorators.size() == 1);
|
||||||
|
|
||||||
auto n1 = std::dynamic_pointer_cast<style>(commands.at(0));
|
auto n1 = std::dynamic_pointer_cast<style>(decorators.at(0));
|
||||||
|
|
||||||
CHECK(n1);
|
CHECK(n1);
|
||||||
CHECK(n1->spec == "#green,dashed,thickness=4");
|
CHECK(n1->spec == "#green,dashed,thickness=4");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Test command parser on aggregation", "[unit-test]")
|
TEST_CASE("Test decorator parser on aggregation", "[unit-test]")
|
||||||
{
|
{
|
||||||
std::string comment = R"(
|
std::string comment = R"(
|
||||||
\clanguml{aggregation[0..1:0..*]}
|
\clanguml{aggregation[0..1:0..*]}
|
||||||
)";
|
)";
|
||||||
|
|
||||||
using namespace clanguml::command_parser;
|
using namespace clanguml::decorators;
|
||||||
|
|
||||||
auto commands = parse(comment);
|
auto decorators = parse(comment);
|
||||||
|
|
||||||
CHECK(commands.size() == 1);
|
CHECK(decorators.size() == 1);
|
||||||
|
|
||||||
auto n1 = std::dynamic_pointer_cast<aggregation>(commands.at(0));
|
auto n1 = std::dynamic_pointer_cast<aggregation>(decorators.at(0));
|
||||||
|
|
||||||
CHECK(n1);
|
CHECK(n1);
|
||||||
CHECK(n1->multiplicity == "0..1:0..*");
|
CHECK(n1->multiplicity == "0..1:0..*");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Test command parser on skip", "[unit-test]")
|
TEST_CASE("Test decorator parser on skip", "[unit-test]")
|
||||||
{
|
{
|
||||||
std::string comment = R"(
|
std::string comment = R"(
|
||||||
\clanguml{skip}
|
\clanguml{skip}
|
||||||
)";
|
)";
|
||||||
|
|
||||||
using namespace clanguml::command_parser;
|
using namespace clanguml::decorators;
|
||||||
|
|
||||||
auto commands = parse(comment);
|
auto decorators = parse(comment);
|
||||||
|
|
||||||
CHECK(commands.size() == 1);
|
CHECK(decorators.size() == 1);
|
||||||
|
|
||||||
auto n1 = std::dynamic_pointer_cast<skip>(commands.at(0));
|
auto n1 = std::dynamic_pointer_cast<skip>(decorators.at(0));
|
||||||
|
|
||||||
CHECK(n1);
|
CHECK(n1);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Test command parser on skiprelationship", "[unit-test]")
|
TEST_CASE("Test decorator parser on skiprelationship", "[unit-test]")
|
||||||
{
|
{
|
||||||
std::string comment = R"(
|
std::string comment = R"(
|
||||||
\clanguml{skiprelationship}
|
\clanguml{skiprelationship}
|
||||||
)";
|
)";
|
||||||
|
|
||||||
using namespace clanguml::command_parser;
|
using namespace clanguml::decorators;
|
||||||
|
|
||||||
auto commands = parse(comment);
|
auto decorators = parse(comment);
|
||||||
|
|
||||||
CHECK(commands.size() == 1);
|
CHECK(decorators.size() == 1);
|
||||||
|
|
||||||
auto n1 = std::dynamic_pointer_cast<skip_relationship>(commands.at(0));
|
auto n1 = std::dynamic_pointer_cast<skip_relationship>(decorators.at(0));
|
||||||
|
|
||||||
CHECK(n1);
|
CHECK(n1);
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user