Added strategy pattern test case

This commit is contained in:
Bartek Kryza
2021-07-25 12:39:21 +02:00
parent 6b41ccb18f
commit 4c3a662feb
9 changed files with 176 additions and 5 deletions

View File

@@ -21,6 +21,7 @@
* [t00020](./test_cases/t00020.md) - Abstract factory pattern
* [t00021](./test_cases/t00021.md) - Visitor pattern
* [t00022](./test_cases/t00022.md) - Template method pattern
* [t00023](./test_cases/t00023.md) - Strategy pattern
## Sequence diagrams
* [t20001](./test_cases/t20001.md) - Basic sequence diagram
## Configuration diagrams

View File

@@ -38,16 +38,15 @@ protected:
class A1 : public A {
protected:
void method1() override { }
void method2() override { }
void method1() override {}
void method2() override {}
};
class A2 : public A {
protected:
void method1() override { }
void method2() override { }
void method1() override {}
void method2() override {}
};
}
}

65
docs/test_cases/t00023.md Normal file
View File

@@ -0,0 +1,65 @@
# t00023 - Strategy pattern
## Config
```yaml
compilation_database_dir: ..
output_directory: puml
diagrams:
t00023_class:
type: class
glob:
- ../../tests/t00023/t00023.cc
using_namespace:
- clanguml::t00023
include:
namespaces:
- clanguml::t00023
```
## Source code
File t00023.cc
```cpp
#include <memory>
namespace clanguml {
namespace t00023 {
class Strategy {
public:
virtual ~Strategy() = default;
virtual void algorithm() = 0;
};
class StrategyA : public Strategy {
public:
void algorithm() override { }
};
class StrategyB : public Strategy {
public:
void algorithm() override { }
};
class StrategyC : public Strategy {
public:
void algorithm() override { }
};
class Context {
public:
Context(std::unique_ptr<Strategy> strategy)
: m_strategy(std::move(strategy))
{
}
void apply() { m_strategy->algorithm(); }
private:
std::unique_ptr<Strategy> m_strategy;
};
}
}
```
## Generated UML diagrams
![t00023_class](./t00023_class.png "Strategy pattern")

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

12
tests/t00023/.clanguml Normal file
View File

@@ -0,0 +1,12 @@
compilation_database_dir: ..
output_directory: puml
diagrams:
t00023_class:
type: class
glob:
- ../../tests/t00023/t00023.cc
using_namespace:
- clanguml::t00023
include:
namespaces:
- clanguml::t00023

40
tests/t00023/t00023.cc Normal file
View File

@@ -0,0 +1,40 @@
#include <memory>
namespace clanguml {
namespace t00023 {
class Strategy {
public:
virtual ~Strategy() = default;
virtual void algorithm() = 0;
};
class StrategyA : public Strategy {
public:
void algorithm() override {}
};
class StrategyB : public Strategy {
public:
void algorithm() override {}
};
class StrategyC : public Strategy {
public:
void algorithm() override {}
};
class Context {
public:
Context(std::unique_ptr<Strategy> strategy)
: m_strategy(std::move(strategy))
{
}
void apply() { m_strategy->algorithm(); }
private:
std::unique_ptr<Strategy> m_strategy;
};
}
}

50
tests/t00023/test_case.h Normal file
View File

@@ -0,0 +1,50 @@
/**
* tests/t00023/test_case.cc
*
* Copyright (c) 2021 Bartek Kryza <bkryza@gmail.com>
*
* 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("t00023", "[test-case][class]")
{
auto [config, db] = load_config("t00023");
auto diagram = config.diagrams["t00023_class"];
REQUIRE(diagram->name == "t00023_class");
REQUIRE(diagram->include.namespaces.size() == 1);
REQUIRE_THAT(diagram->include.namespaces,
VectorContains(std::string{"clanguml::t00023"}));
REQUIRE(diagram->exclude.namespaces.size() == 0);
REQUIRE(diagram->should_include("clanguml::t00023::Visitor"));
auto model = generate_class_diagram(db, diagram);
REQUIRE(model.name == "t00023_class");
auto puml = generate_class_puml(diagram, model);
AliasMatcher _A(puml);
REQUIRE_THAT(puml, StartsWith("@startuml"));
REQUIRE_THAT(puml, EndsWith("@enduml\n"));
REQUIRE_THAT(puml, IsAbstractClass(_A("Strategy")));
REQUIRE_THAT(puml, IsClass(_A("StrategyA")));
REQUIRE_THAT(puml, IsClass(_A("StrategyB")));
save_puml(
"./" + config.output_directory + "/" + diagram->name + ".puml", puml);
}

View File

@@ -126,6 +126,7 @@ using namespace clanguml::test::matchers;
#include "t00020/test_case.h"
#include "t00021/test_case.h"
#include "t00022/test_case.h"
#include "t00023/test_case.h"
//
// Sequence diagram tests

View File

@@ -63,6 +63,9 @@ test_cases:
- name: t00022
title: Template method pattern
description:
- name: t00023
title: Strategy pattern
description:
Sequence diagrams:
- name: t20001
title: Basic sequence diagram