test: implement tests for policyfile

This commit is contained in:
august-alt 2021-03-23 15:28:05 +04:00
parent 8f461aad80
commit ff0a3fd763
7 changed files with 141 additions and 7 deletions

View File

@ -49,6 +49,11 @@ PolicyFile::PolicyFile()
}
PolicyFile::~PolicyFile()
{
delete d;
}
/*!
* \brief PolicyFile::addPolicy Adds policy to the file.
* \param policy Policy to add.
@ -64,11 +69,11 @@ void PolicyFile::addPolicy(std::shared_ptr<Policy> policy)
*/
void PolicyFile::removePolicy(std::shared_ptr<Policy> policy)
{
std::remove_if(d->policies.begin(), d->policies.end(),
[policy](std::shared_ptr<Policy> currentPolicy)
{
return currentPolicy == policy;
});
d->policies.erase(std::remove_if(d->policies.begin(), d->policies.end(),
[policy](std::shared_ptr<Policy> currentPolicy)
{
return currentPolicy == policy;
}));
}
/*!

View File

@ -42,7 +42,7 @@ namespace io {
public:
PolicyFile();
~PolicyFile() = default;
~PolicyFile();
void addPolicy(std::shared_ptr<model::admx::Policy> policy);
@ -59,7 +59,7 @@ namespace io {
bool contains(const std::string& name);
private:
std::unique_ptr<PolicyFilePrivate> d;
PolicyFilePrivate* const d;
};
}

View File

@ -1 +1,2 @@
add_subdirectory(gui)
add_subdirectory(io)

View File

@ -0,0 +1,7 @@
find_package(GPUI COMPONENTS io model REQUIRED)
include_directories(${GPUI_INCLUDE_DIRS})
find_package(Qt5 COMPONENTS Core Test REQUIRED)
set(QT_USE_QTTEST TRUE)
add_subdirectory(policyfile)

View File

@ -0,0 +1,4 @@
qt5_wrap_cpp(MOC_SOURCES policyfiletest.h)
add_executable(policyfiletest policyfiletest.cpp ${MOC_SOURCES})
target_link_libraries(policyfiletest gpui-model gpui-io Qt5::Core Qt5::Test)
add_gpui_test(io.policyfiletest policyfiletest)

View File

@ -0,0 +1,80 @@
/***********************************************************************************************************************
**
** Copyright (C) 2021 BaseALT Ltd.
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** as published by the Free Software Foundation; either version 2
** of the License, or (at your option) any later version.
**
** This program 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 General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
***********************************************************************************************************************/
#include "policyfiletest.h"
#include "../../../../src/io/policyfile.h"
#include "../../../../src/model/admx/policy.h"
#include "../../../../src/model/admx/policyelement.h"
using namespace io;
using namespace model::admx;
namespace tests {
void PolciyFileTest::getPolicy()
{
PolicyFile file;
auto policyA = std::make_shared<Policy>();
auto policyB = std::make_shared<Policy>();
auto policyC = std::make_shared<Policy>();
file.addPolicy(policyA);
file.addPolicy(policyB);
file.addPolicy(policyC);
QVERIFY(file.getPolicy(0) == policyA);
QVERIFY(file.getPolicy(1) == policyB);
QVERIFY(file.getPolicy(2) == policyC);
policyA->name = "policyA";
policyB->name = "policyB";
policyC->name = "policyC";
QVERIFY(file.getPolicy("policyC") == policyC);
QVERIFY(file.getPolicy("policyA") == policyA);
QVERIFY(file.getPolicy("policyB") == policyB);
QVERIFY(file.getPolicy("no-policy") == nullptr);
}
void PolciyFileTest::contains()
{
PolicyFile file;
QVERIFY(file.policyCount() == 0);
auto policy = std::make_shared<Policy>();
policy->name = "policy";
file.addPolicy(policy);
QVERIFY(file.policyCount() == 1);
QVERIFY(file.contains("policy"));
file.removePolicy(policy);
QVERIFY(file.policyCount() == 0);
QVERIFY(file.contains("policy") == false);
}
}
QTEST_MAIN(tests::PolciyFileTest)

View File

@ -0,0 +1,37 @@
/***********************************************************************************************************************
**
** Copyright (C) 2021 BaseALT Ltd.
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** as published by the Free Software Foundation; either version 2
** of the License, or (at your option) any later version.
**
** This program 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 General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
***********************************************************************************************************************/
#ifndef GPUI_POLICYFILE_TEST_H
#define GPUI_POLICYFILE_TEST_H
#include <QtTest>
namespace tests {
class PolciyFileTest : public QObject
{
Q_OBJECT
private slots:
void getPolicy();
void contains();
};
}
#endif // GPUI_POLICYFILE_TEST_H