Skip to content

Commit

Permalink
added test for extendeded type. Added range find_if (all std::find_if…
Browse files Browse the repository at this point in the history
… changed to range version)
  • Loading branch information
vt4a2h committed Jul 5, 2015
1 parent ffcb33d commit 842ff6e
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 58 deletions.
17 changes: 7 additions & 10 deletions entity/class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,7 @@ namespace entity {
*/
bool Class::containsParent(const QString &typeId)
{
auto it = std::find_if(m_Parents.begin(), m_Parents.end(),
[&](const Parent &p) { return p.first == typeId; });
return (it != m_Parents.end());
return utility::find_if(m_Parents, [&](const Parent &p) { return p.first == typeId; }) != m_Parents.end();
}

/**
Expand All @@ -176,9 +174,9 @@ namespace entity {
*/
void Class::removeParent(const QString &typeId)
{
auto it = std::find_if(m_Parents.begin(), m_Parents.end(),
[&](const Parent &p) { return p.first == typeId; });
if (it != m_Parents.end()) m_Parents.erase(it);
auto it = utility::find_if(m_Parents, [&](const Parent &p) { return p.first == typeId; });
if (it != m_Parents.end())
m_Parents.erase(it);
}

/**
Expand Down Expand Up @@ -354,9 +352,8 @@ namespace entity {
*/
SharedField Class::getField(const QString &name) const
{
auto it = std::find_if(m_Fields.begin(), m_Fields.end(),
[&name](const SharedField &f){ return f->name() == name; });
return (it != m_Fields.end() ? *it : nullptr);
auto it = utility::find_if(m_Fields, [&name](const SharedField &f){ return f->name() == name; });
return it != m_Fields.end() ? *it : SharedField();
}

/**
Expand Down Expand Up @@ -462,7 +459,7 @@ namespace entity {
*/
ConstSharedProperty Class::property(const QString &name) const
{
auto it = std::find_if(m_Properties.cbegin(), m_Properties.cend(), [&](auto &&prop){ return prop->name() == name; });
auto it = utility::find_if(m_Properties, [&](auto &&prop){ return prop->name() == name; });
return it != m_Properties.end() ? *it : SharedProperty();
}

Expand Down
6 changes: 2 additions & 4 deletions entity/classmethod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,8 @@ namespace entity {
*/
SharedField ClassMethod::getParameter(const QString &name)
{
auto it = std::find_if(m_Parameters.begin(), m_Parameters.end(),
[&name](const SharedField &f){ return f->name() == name; });

return (it != m_Parameters.end() ? *it : nullptr);
auto it = utility::find_if(m_Parameters, [&name](const SharedField &f){ return f->name() == name; });
return it != m_Parameters.end() ? *it : SharedField();
}

/**
Expand Down
12 changes: 4 additions & 8 deletions entity/enum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,8 @@ namespace entity {
*/
SharedElement Enum::element(const QString &name) const
{
auto it = std::find_if(m_Elements.cbegin(), m_Elements.cend(),
[&name](const SharedElement &v){ return v->first == name; });
return it != m_Elements.cend() ? *it : std::make_shared<Element>(DEFAULT_NAME, -1);
auto it = utility::find_if(m_Elements, [&name](const SharedElement &v){ return v->first == name; });
return it != m_Elements.cend() ? *it : SharedElement();
}

/**
Expand All @@ -164,8 +163,7 @@ namespace entity {
*/
void Enum::removeElement(const QString &name)
{
auto it = std::find_if(m_Elements.begin(), m_Elements.end(),
[&name](const SharedElement &v){ return v->first == name; });
auto it = utility::find_if(m_Elements, [&name](const SharedElement &v){ return v->first == name; });
if (it != m_Elements.end())
m_Elements.erase(it);
}
Expand All @@ -177,9 +175,7 @@ namespace entity {
*/
bool Enum::containsElement(const QString &name) const
{
return std::find_if(m_Elements.cbegin(),
m_Elements.cend(),
[&name](const SharedElement &v){ return v->first == name; }) != m_Elements.cend();
return utility::find_if(m_Elements, [&name](const SharedElement &v){ return v->first == name; }) != m_Elements.cend();
}

/**
Expand Down
14 changes: 8 additions & 6 deletions entity/extendedtype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ namespace entity {
*/
bool ExtendedType::isLink() const
{
return !m_PointersAndLinks.isEmpty() && m_PointersAndLinks.last().first == "&";
return utility::find_if(m_PointersAndLinks, [](const Pl &pl){ return pl.first == "&"; }) != m_PointersAndLinks.end();
}

/**
Expand All @@ -83,15 +83,16 @@ namespace entity {
*/
void ExtendedType::addPointerStatus(bool pointerToConst)
{
m_PointersAndLinks.append(std::make_pair("*", pointerToConst));
m_PointersAndLinks.append({"*", pointerToConst});
}

/**
* @brief ExtendedType::removePointerStatus
*/
void ExtendedType::removePointerStatus()
{
if (isPointer()) m_PointersAndLinks.removeLast();
if (!m_PointersAndLinks.isEmpty() && m_PointersAndLinks.last().first == "*")
m_PointersAndLinks.removeLast();
}

/**
Expand All @@ -100,23 +101,24 @@ namespace entity {
*/
bool ExtendedType::isPointer() const
{
return !m_PointersAndLinks.isEmpty() && m_PointersAndLinks.last().first == "*";
return utility::find_if(m_PointersAndLinks, [](const Pl &pl){ return pl.first == "*"; }) != m_PointersAndLinks.end();
}

/**
* @brief ExtendedType::addLinkStatus
*/
void ExtendedType::addLinkStatus()
{
m_PointersAndLinks.append(std::make_pair("&", false));
m_PointersAndLinks.append({"&", false});
}

/**
* @brief ExtendedType::removeLinkStatus
*/
void ExtendedType::removeLinkStatus()
{
if (isLink()) m_PointersAndLinks.removeLast();
if (!m_PointersAndLinks.isEmpty() && m_PointersAndLinks.last().first == "&")
m_PointersAndLinks.removeLast();
}

/**
Expand Down
4 changes: 2 additions & 2 deletions entity/extendedtype.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ namespace entity {

friend bool operator ==(const ExtendedType &lhs, const ExtendedType &rhs);

bool isLink() const;
bool isPointer() const;
void addPointerStatus(bool pointerToConst = false);
void removePointerStatus();

bool isPointer() const;
bool isLink() const;
void addLinkStatus();
void removeLinkStatus();

Expand Down
14 changes: 6 additions & 8 deletions entity/template.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
/*****************************************************************************
**
**
** Copyright (C) 2014 Fanaskov Vitaly (vt4a2h@gmail.com)
**
** Created 29/10/2014.
**
** This file is part of Q-UML (UML tool for Qt).
**
**
** Q-UML is free software: you can redistribute it and/or modify
** it under the terms of the GNU Lesser General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
**
** Q-UML is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU Lesser General Public License for more details.
** You should have received a copy of the GNU Lesser General Public License
** along with Q-UML. If not, see <http://www.gnu.org/licenses/>.
** along with Q-UML. If not, see <http://www.gnu.org/licenses/>.
**
*****************************************************************************/

Expand Down Expand Up @@ -65,10 +65,8 @@ namespace entity {
*/
TemplateParameter Template::getTemplateParameter(const QString &typeId) const
{
auto it = std::find_if(m_TemplateParameters.begin(), m_TemplateParameters.end(),
[&typeId](const TemplateParameter &p) { return p.first == typeId; });

return (it != m_TemplateParameters.end() ? *it : TemplateParameter(STUB_ID, STUB_ID));
auto it = utility::find_if(m_TemplateParameters, [&typeId](auto &&p) { return p.first == typeId; });
return it != m_TemplateParameters.cend() ? *it : TemplateParameter(STUB_ID, STUB_ID);
}

/**
Expand Down
5 changes: 2 additions & 3 deletions entity/union.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,9 @@ namespace entity {
*/
SharedField Union::getField(const QString &name) const
{
auto it = std::find_if(m_Fields.begin(), m_Fields.end(),
[&](const SharedField &f) {return f->name() == name;});
auto it = utility::find_if(m_Fields, [&](auto &&f) {return f->name() == name;});

return (it != m_Fields.end() ? *it : nullptr);
return it != m_Fields.cend() ? *it : SharedField();
}

/**
Expand Down
3 changes: 1 addition & 2 deletions gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,7 @@ namespace gui {
{
if (obj == m_MainView) {
if (ev->type() == QEvent::MouseButtonPress) {
auto it = std::find_if(m_AddActions.begin(), m_AddActions.end(),
[](const ActionMaker &am) { return am.first->isChecked(); });
auto it = utility::find_if(m_AddActions, [](auto &&am) { return am.first->isChecked(); });
if (it != m_AddActions.end()) {
if (m_ApplicationModel && m_ApplicationModel->currentProject()) {
auto event = static_cast<QMouseEvent* >(ev);
Expand Down
8 changes: 3 additions & 5 deletions models/projecttreemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@

#include <db/projectdatabase.h>

#include <utility/helpfunctions.h>

namespace models {

namespace {
Expand Down Expand Up @@ -379,11 +381,7 @@ namespace models {
*/
const BasicTreeItem *ProjectTreeModel::find(const QString &id) const
{
auto projectIt = std::find_if(m_Items.cbegin(), m_Items.cend(),
[&](const BasicTreeItem &item){
return item.id() == id;
});

auto projectIt = utility::find_if(m_Items, [&](auto &&item){ return item.id() == id; });
return projectIt != m_Items.cend() ? &*projectIt : nullptr;
}

Expand Down
46 changes: 44 additions & 2 deletions tests/test/cases/entitiestestcases.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,54 @@ TEST_F(Enteties, SimpleType)
_type->setId("some id");
_type->setScopeId("global scope id");

test_copy_move(Type, _type)
test_copy_move(Type, _type);
}

TEST_F(Enteties, ExtendedType)
{
test_copy_move(ExtendedType, _extendedType)
ASSERT_STREQ(_extendedType->name().toStdString().c_str(), BASE_TYPE_NAME);
ASSERT_STREQ(_extendedType->typeId().toStdString().c_str(), STUB_ID);
ASSERT_FALSE(_extendedType->isConst());

ASSERT_FALSE(_extendedType->isLink());
_extendedType->addLinkStatus();
ASSERT_TRUE(_extendedType->isLink());
_extendedType->removeLinkStatus();
ASSERT_FALSE(_extendedType->isLink());

ASSERT_FALSE(_extendedType->isPointer());
_extendedType->addPointerStatus();
ASSERT_TRUE(_extendedType->isPointer());
_extendedType->removePointerStatus();
ASSERT_FALSE(_extendedType->isPointer());

_extendedType->addPointerStatus();
_extendedType->addLinkStatus();
ASSERT_TRUE(_extendedType->isLink());
ASSERT_TRUE(_extendedType->isPointer());

_extendedType->setConstStatus(true);
ASSERT_TRUE(_extendedType->isConst());
_extendedType->setConstStatus(false);
ASSERT_FALSE(_extendedType->isConst());

const QString par1 = "par1";
const QString par2 = "par2";

_extendedType->addTemplateParameter(par1);
ASSERT_TRUE(_extendedType->containsTemplateParameter(par1));
_extendedType->addTemplateParameter(par2);
ASSERT_TRUE(_extendedType->containsTemplateParameter(par2));

_extendedType->removeTemplateParameters(par1);
_extendedType->removeTemplateParameters(par2);

ASSERT_TRUE(_extendedType->templateParameters().isEmpty());

// Re-add one parameter to test copy and move
_extendedType->addTemplateParameter(par1);

test_copy_move(ExtendedType, _extendedType);
}

TEST_F(Enteties, Class)
Expand Down
10 changes: 2 additions & 8 deletions translator/projecttranslator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,8 @@ namespace translator {
return false;

entity::FieldsList fields(m->parameters());
auto it = std::find_if(fields.begin(), fields.end(), [&](const entity::SharedField &f) {
return classDatabase->depthTypeSearch(f->typeId()) /*!= nullptr*/;
});

if (it != fields.end() || classDatabase->depthTypeSearch(m->returnTypeId()) /*!= nullptr*/)
return true;

return false;
auto it = utility::find_if(fields, [&](auto &&f) { return classDatabase->depthTypeSearch(f->typeId()); });
return it != fields.end() || classDatabase->depthTypeSearch(m->returnTypeId());
}

/**
Expand Down
12 changes: 12 additions & 0 deletions utility/helpfunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,18 @@ namespace utility {
return false;
}

template <class Sequence, class UnaryPredicate>
inline decltype(auto) find_if( const Sequence& seq, UnaryPredicate pred)
{
return std::find_if(std::begin(seq), std::end(seq), pred);
}

template <class Sequence, class UnaryPredicate>
inline decltype(auto) find_if( Sequence& seq, UnaryPredicate pred)
{
return std::find_if(std::begin(seq), std::end(seq), pred);
}

QString fieldKeywordToString(entity::FieldKeyword keyword);
QString methodLhsIdToString(entity::LhsIdentificator id);
QString methodRhsIdToString(entity::RhsIdentificator id);
Expand Down

0 comments on commit 842ff6e

Please sign in to comment.