From 6f499de159eeac37fe473f945042e13359dc2d40 Mon Sep 17 00:00:00 2001 From: Renaud G Date: Thu, 25 Jul 2019 09:59:03 +0200 Subject: move files --- tests/dice/CMakeLists.txt | 18 +- tests/dice/Dice.pro | 25 -- tests/dice/testnode.cpp | 49 +++ tests/dice/testnode.h | 44 ++ tests/dice/tst_dice.cpp | 1069 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 1167 insertions(+), 38 deletions(-) delete mode 100644 tests/dice/Dice.pro create mode 100644 tests/dice/testnode.cpp create mode 100644 tests/dice/testnode.h create mode 100644 tests/dice/tst_dice.cpp (limited to 'tests/dice') diff --git a/tests/dice/CMakeLists.txt b/tests/dice/CMakeLists.txt index a0f27fd..4065cc5 100644 --- a/tests/dice/CMakeLists.txt +++ b/tests/dice/CMakeLists.txt @@ -1,17 +1,9 @@ -cmake_minimum_required(VERSION 3.5) -project(tst_diceParser) - -enable_testing(true) - -# Tell CMake to run moc when necessary: +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fno-permissive -pedantic -Wall -Wextra") set(CMAKE_AUTOMOC ON) - -# As moc files are generated in the binary dir, tell CMake -# to always look for includes there: -set(CMAKE_INCLUDE_CURRENT_DIR ON) - find_package(Qt5Test REQUIRED) -add_test(tst_diceParser tst_dice.cpp) +set(test_source testnode.cpp tst_dice.cpp) -target_link_libraries(foo Qt5::Test) +add_executable(test_dice ${test_source} ) +target_link_libraries(test_dice diceparser Qt5::Test) +add_test(tst_diceparser test_dice) diff --git a/tests/dice/Dice.pro b/tests/dice/Dice.pro deleted file mode 100644 index 9d24f91..0000000 --- a/tests/dice/Dice.pro +++ /dev/null @@ -1,25 +0,0 @@ -include(../tests.pri) - -SOURCES += \ - tst_dice.cpp - -TEMPLATE = app -TARGET= tst_diceParser -MOC_DIR=trash -OBJECTS_DIR=trash - -CONFIG+=testcase - -QT += testlib quick qml widgets - -INCLUDEPATH += $$ROLISTEAMSRC/diceparser/ -INCLUDEPATH += $$ROLISTEAMSRC/diceparser/node -INCLUDEPATH += $$ROLISTEAMSRC/diceparser/result - - -include($$ROLISTEAMSRC/diceparser/diceparser.pri) - -HEADERS -= ../../../rolisteam/client/diceparser/qmltypesregister.h -SOURCES -= ../../../rolisteam/client/diceparser/qmltypesregister.cpp - - diff --git a/tests/dice/testnode.cpp b/tests/dice/testnode.cpp new file mode 100644 index 0000000..de9fc05 --- /dev/null +++ b/tests/dice/testnode.cpp @@ -0,0 +1,49 @@ +#include "testnode.h" +#include "die.h" + +TestNode::TestNode() {} + +TestNode::~TestNode() +{ + m_nextNode= nullptr; + m_result= nullptr; +} +void TestNode::run(ExecutionNode* previous) +{ + Q_UNUSED(previous) + if(nullptr != m_nextNode) + { + m_nextNode->run(this); + } +} + +QString TestNode::toString(bool wl) const +{ + if(wl) + { + return QStringLiteral("%1 [label=\"TestNode \"]").arg(m_id); + } + else + { + return m_id; + } +} +qint64 TestNode::getPriority() const +{ + qint64 priority= 4; + return priority; +} +ExecutionNode* TestNode::getCopy() const +{ + TestNode* node= new TestNode(); + if(nullptr != m_nextNode) + { + node->setNextNode(m_nextNode->getCopy()); + } + return node; +} + +void TestNode::setResult(Result* result) +{ + m_result= result; +} diff --git a/tests/dice/testnode.h b/tests/dice/testnode.h new file mode 100644 index 0000000..5c918ee --- /dev/null +++ b/tests/dice/testnode.h @@ -0,0 +1,44 @@ +#ifndef TESTNODE_H +#define TESTNODE_H + +#include + +#include "executionnode.h" +#include "result/diceresult.h" +#include +/** + * @brief The TestNode class replaces any kind of node for test purpose. + */ +class TestNode : public ExecutionNode +{ +public: + /** + * @brief TestNode builds an instance + * @param faces, number of faces of dices + * @param offset, first value of dice. + */ + TestNode(); + virtual ~TestNode() override; + + /** + * @brief run - starts to roll dice. + */ + virtual void run(ExecutionNode*) override; + /** + * @brief toString + * @param wl + * @return use to generate dot tree; + */ + virtual QString toString(bool wl) const override; + /** + * @brief getPriority + * @return priority for dice roll: 4 (higher) + */ + virtual qint64 getPriority() const override; + + virtual ExecutionNode* getCopy() const override; + + void setResult(Result* result); +}; + +#endif // TESTNODE_H diff --git a/tests/dice/tst_dice.cpp b/tests/dice/tst_dice.cpp new file mode 100644 index 0000000..1fe2641 --- /dev/null +++ b/tests/dice/tst_dice.cpp @@ -0,0 +1,1069 @@ +/*************************************************************************** + * Copyright (C) 2011 by Renaud Guezennec * + * http://renaudguezennec.homelinux.org/accueil,3.html * + * * + * Rolisteam 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., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +#include +#include +#include + +#include "dicealias.h" +#include "diceparser.h" +#include "die.h" + +// node +#include "booleancondition.h" +#include "node/bind.h" +#include "node/countexecutenode.h" +#include "node/explodedicenode.h" +#include "node/filternode.h" +#include "node/groupnode.h" +#include "node/ifnode.h" +#include "node/jumpbackwardnode.h" +#include "node/keepdiceexecnode.h" +#include "node/numbernode.h" +#include "node/occurencecountnode.h" +#include "node/rerolldicenode.h" +#include "node/sortresult.h" +#include "node/splitnode.h" +#include "node/stringnode.h" +#include "node/uniquenode.h" +#include "result/stringresult.h" +#include "testnode.h" + +class TestDice : public QObject +{ + Q_OBJECT + +public: + TestDice(); + +private slots: + void init(); + void getAndSetTest(); + void diceRollD10Test(); + void diceRollD20Test(); + void commandEndlessLoop(); + + void mathPriority(); + void mathPriority_data(); + + void commandsTest(); + void commandsTest_data(); + + void dangerousCommandsTest(); + void dangerousCommandsTest_data(); + + void wrongCommandsTest(); + void wrongCommandsTest_data(); + + void wrongCommandsExecutionTimeTest(); + void scopeDF(); + void scopeDF_data(); + + void severalInstruction(); + void testAlias(); + void cleanupTestCase(); + + void keepTest(); + void keepTest_data(); + + void sortTest(); + void sortTest_data(); + + void countTest(); + void countTest_data(); + + void rerollTest(); + void rerollTest_data(); + + void explodeTest(); + void explodeTest_data(); + + void rerollUntilTest(); + void rerollUntilTest_data(); + + void rerollAddTest(); + void rerollAddTest_data(); + + void mergeTest(); + void mergeTest_data(); + + void ifTest(); + void ifTest_data(); + + void ifCommandTest(); + void ifCommandTest_data(); + + void paintTest(); + void paintTest_data(); + + void filterTest(); + void filterTest_data(); + + void uniqueTest(); + void uniqueTest_data(); + + void spreadTest(); + void spreadTest_data(); + + void groupTest(); + void groupTest_data(); + + void bindTest(); + void bindTest_data(); + + void occurenceTest(); + void occurenceTest_data(); + +private: + std::unique_ptr m_die; + std::unique_ptr m_diceParser; +}; + +TestDice::TestDice() {} + +void TestDice::init() +{ + m_die.reset(new Die()); + m_diceParser.reset(new DiceParser()); +} + +void TestDice::getAndSetTest() +{ + for(unsigned int i= 0; i < 2000; i++) + { + m_die->setMaxValue(i); + QVERIFY(m_die->getMaxValue() == i); + } + + m_die->setSelected(true); + QVERIFY(m_die->isSelected() == true); + + m_die->setSelected(false); + QVERIFY(m_die->isSelected() == false); +} + +void TestDice::diceRollD10Test() +{ + m_die->setMaxValue(10); + for(int i= 0; i < 2000; i++) + { + m_die->roll(false); + QVERIFY(m_die->getValue() > 0); + QVERIFY(m_die->getValue() < 11); + } +} +void TestDice::diceRollD20Test() +{ + m_die->setMaxValue(20); + for(int i= 0; i < 2000; i++) + { + m_die->roll(false); + QVERIFY(m_die->getValue() > 0); + QVERIFY(m_die->getValue() < 21); + } +} +void TestDice::commandEndlessLoop() +{ + bool a= m_diceParser->parseLine("1D10e[>0]"); + QVERIFY(!a); +} + +void TestDice::commandsTest() +{ + QFETCH(QString, cmd); + + bool a= m_diceParser->parseLine(cmd); + QVERIFY2(a, "parsing"); + + m_diceParser->start(); + QVERIFY2(m_diceParser->humanReadableError().isEmpty(), "no error"); + QVERIFY2(m_diceParser->humanReadableWarning().isEmpty(), "no warning"); +} + +void TestDice::commandsTest_data() +{ + QTest::addColumn("cmd"); + + QTest::addRow("cmd1") << "1L[cheminée,chocolat,épée,arc,chute de pierre]"; + QTest::addRow("cmd2") << "10d10c[>=6]-@c[=1]"; + QTest::addRow("cmd3") << "10d10c[>=6]-@c[=1]-@c[=1]"; + QTest::addRow("cmd4") << "10d10c[>6]+@c[=10]"; + QTest::addRow("cmd5") << "1+1D10"; + QTest::addRow("cmd6") << "3d10c[>=5]"; + QTest::addRow("cmd7") << "1+(4*3)D10"; + QTest::addRow("cmd8") << "2+4/4"; + QTest::addRow("cmd9") << "2D10*2D20*8"; + QTest::addRow("cmd10") << "1+(4*3)D10"; + QTest::addRow("cmd11") << "(4D6)D10"; + QTest::addRow("cmd12") << "1D100a[>=95]a[>=96]a[>=97]a[>=98]a[>=99]e[>=100]"; + QTest::addRow("cmd13") << "3D100"; + QTest::addRow("cmd14") << "4k3"; + QTest::addRow("cmd15") << "10D10e[>=6]sc[>=6]"; + QTest::addRow("cmd16") << "10D10e10s"; + QTest::addRow("cmd17") << "10D10s"; + QTest::addRow("cmd18") << "15D10e10c[8-10]"; + QTest::addRow("cmd19") << "10d10e10"; + QTest::addRow("cmd30") << "(4+4)^4"; + QTest::addRow("cmd31") << "(1d20+20)*7/10"; + QTest::addRow("cmd32") << "20*7/10"; + QTest::addRow("cmd33") << "1D8+2D6+7"; + QTest::addRow("cmd34") << "D25"; + QTest::addRow("cmd35") << "1L[tete[10],ventre[50],jambe[40]]"; + QTest::addRow("cmd36") << "2d6c[%2=0]"; + QTest::addRow("cmd37") << "D25+D10"; + QTest::addRow("cmd38") << "D25;D10"; + QTest::addRow("cmd39") << "8+8+8"; + QTest::addRow("cmd40") << "1D20-88"; + QTest::addRow("cmd41") << "100*1D20*2D6"; + QTest::addRow("cmd42") << "2D6 # two 6sided dice"; + QTest::addRow("cmd43") << "100/28*3"; + QTest::addRow("cmd44") << "100/8"; + QTest::addRow("cmd45") << "100*3*8"; + QTest::addRow("cmd46") << "help"; + QTest::addRow("cmd47") << "la"; + QTest::addRow("cmd48") << "10D10c[<2|>7]"; + QTest::addRow("cmd49") << "10D6c[=2|=4|=6]"; + QTest::addRow("cmd50") << "10D10e[=1|=10]k4"; + QTest::addRow("cmd51") << "1L[tete,bras droit,bras gauche,jambe droite,jambe gauche,ventre[6-7],buste[8-10]]"; + QTest::addRow("cmd52") << "10+10s"; + QTest::addRow("cmd53") << "1d6e6;1d4e4mk1"; + QTest::addRow("cmd54") << "1d6e6;1d4e4mk1"; + QTest::addRow("cmd55") << "400D20/400000"; + QTest::addRow("cmd56") << "1d100e[>=95]i[<5]{-1d100e95}"; + QTest::addRow("cmd57") << "100*3*8"; + QTest::addRow("cmd58") << "1d100i[<70]{1d10i[=10]{1d100i[<70]{1d10e10}}}"; + QTest::addRow("cmd59") << "10d6c[<2|>5]"; + QTest::addRow("cmd60") << "5-5*5+5"; + QTest::addRow("cmd61") << "((3+4)*2)d6"; + QTest::addRow("cmd62") << "4d6i[=6]{+1d6}"; + QTest::addRow("cmd63") << "10d[-8--1]"; + QTest::addRow("cmd64") << "4d6e6i[=4]{-4}+2"; + QTest::addRow("cmd65") << "4d6e6f[!=4]+2"; + QTest::addRow("cmd66") << "5d10g10"; + QTest::addRow("cmd67") << "4d6p[4:blue]c[>=4];1d6p[1:#FFFFFF]c6-@c1;1d6p[1:#FF0000]c[>=4]+@c6-@c1"; + QTest::addRow("cmd68") << "10d[0-9]"; + QTest::addRow("cmd69") << "1d8e8;1d6e6mk1+2"; + QTest::addRow("cmd70") << "3d100g50"; + QTest::addRow("cmd71") << "3d100g33"; + QTest::addRow("cmd72") << "3d100g5"; + QTest::addRow("cmd73") << "3d100g40"; + QTest::addRow("cmd74") << "2d10k1+2d10k1+2d10k1"; + QTest::addRow("cmd75") << "2d10k1-2d10k1-2d10k1"; + QTest::addRow("cmd76") << "(2d10k1)-2d10k1-2d10k1"; + QTest::addRow("cmd77") << "2d10k1-(2d10k1)-2d10k1"; + QTest::addRow("cmd78") << "2d10k1-2d10k1-(2d10k1)"; + QTest::addRow("cmd79") << "1d6-2d6e6"; + QTest::addRow("cmd80") << "(1)-1d6e6"; + QTest::addRow("cmd81") << "(1)-(1d6e6)"; + QTest::addRow("cmd82") << "8d10o"; + QTest::addRow("cmd83") << "8d10o2,4"; + QTest::addRow("cmd84") << "8d10o2[>2]"; + QTest::addRow("cmd85") << "8d10ok2"; + QTest::addRow("cmd86") << "[100,200,300]k2"; + QTest::addRow("cmd87") << "100;200;300;[$1,$2,$3]k2"; +} + +void TestDice::wrongCommandsTest() +{ + // << "pajaejlbnmàw"; + QFETCH(QString, cmd); + QFETCH(bool, valid); + bool a= m_diceParser->parseLine(cmd); + if(a) + { + m_diceParser->start(); + auto map= m_diceParser->getErrorMap(); + a= map.isEmpty(); + } + QCOMPARE(a, valid); +} + +void TestDice::wrongCommandsTest_data() +{ + QTest::addColumn("cmd"); + QTest::addColumn("valid"); + + QTest::newRow("test1") << "1L[cheminée,chocolat,épée,arc,chute de pierre" << false; + QTest::newRow("test2") << "10d10c" << false; + QTest::newRow("test3") << "10d10a" << false; + QTest::newRow("test4") << "10d0a[>7]" << false; + QTest::newRow("test5") << "aiteanetauearuteurn" << false; + QTest::newRow("test6") << "meregue" << false; + QTest::newRow("test7") << "p i follow rivers" << false; + QTest::newRow("test8") << "manga violet evergarden" << false; + QTest::newRow("test9") << "((1d8e8+2);(1d6e6+2))" << true; +} + +void TestDice::wrongCommandsExecutionTimeTest() +{ + QStringList commands; + + //<< "8D20+10*@c[=20]" + commands << "1/0" + << "" + << "0d10" + << "10d10k11" + << "!!!!"; + + for(QString cmd : commands) + { + bool test= m_diceParser->parseLine(cmd); + m_diceParser->start(); + + QVERIFY2(m_diceParser->getErrorMap().isEmpty() == false || !test, cmd.toStdString().c_str()); + } +} +void TestDice::scopeDF() +{ + QFETCH(QString, cmd); + QFETCH(int, min); + QFETCH(int, max); + QFETCH(bool, valid); + + bool test= m_diceParser->parseLine(cmd); + QVERIFY2(test == valid, cmd.toStdString().c_str()); + m_diceParser->start(); + auto results= m_diceParser->getLastIntegerResults(); + + for(auto result : results) + QVERIFY(result >= min && result <= max); +} + +void TestDice::scopeDF_data() +{ + QTest::addColumn("cmd"); + QTest::addColumn("min"); + QTest::addColumn("max"); + QTest::addColumn("valid"); + + QTest::newRow("test1") << "1D[-1-1]" << -1 << 1 << true; + QTest::newRow("test2") << "1D[-10--5]" << -10 << -5 << true; + QTest::newRow("test3") << "1D[-100-100]" << -100 << 100 << true; + QTest::newRow("test4") << "1D[-1-0]" << -1 << 0 << true; + QTest::newRow("test5") << "1D[10-20]" << 10 << 20 << true; + QTest::newRow("test6") << "1D[30-100]" << 30 << 100 << true; + QTest::newRow("test7") << "1D[0-99]" << 0 << 99 << true; + QTest::newRow("test8") << "5-5*5+5" << -15 << -15 << true; + QTest::newRow("test9") << "2d20c[<=13]+@c[<=3]" << 0 << 4 << true; + QTest::newRow("test10") << "6d10c[>=6]-@c1" << -6 << 6 << true; + QTest::newRow("test11") << "2d6k1+2d8k1+2d10k1" << 3 << 30 << true; + QTest::newRow("test12") << "1D[-2-50]" << -2 << 50 << true; +} +void TestDice::testAlias() +{ + m_diceParser->insertAlias(new DiceAlias("!", "3d6c"), 0); + m_diceParser->insertAlias(new DiceAlias("g", "d10k"), 1); + m_diceParser->insertAlias(new DiceAlias("(.*)C(.*)", QStringLiteral("\\1d10e10c[>=\\2]"), false), 2); + + QStringList cmds; + cmds << "!2" + << "${rang}g4" + << "${rang}g4 # gerald" + << "5C3" + << "1d100i:[<101]{\"great!\"}{\"try again\"}"; + + QStringList expected; + expected << "3d6c2" + << "${rang}d10k4" + << "${rang}d10k4 # gerald" + << "5d10e10c[>=3]" + << "1d100i:[<101]{\"great!\"}{\"try again\"}"; + + int i= 0; + for(auto cmd : cmds) + { + auto result= m_diceParser->convertAlias(cmd); + QVERIFY2(result == expected[i], result.toLatin1()); + ++i; + } +} +void TestDice::severalInstruction() +{ + QStringList commands; + + commands << "1d10;2d20;$1+$2"; + + QList results; + results << 3; + + int i= 0; + for(auto cmd : commands) + { + auto test= m_diceParser->parseLine(cmd); + QVERIFY2(test, cmd.toStdString().c_str()); + QVERIFY2(m_diceParser->getStartNodeCount() == results[i], "Wrong number of instruction"); + } +} +void TestDice::mathPriority() +{ + QFETCH(QString, cmd); + QFETCH(int, expected); + + bool test= m_diceParser->parseLine(cmd); + QVERIFY(test); + m_diceParser->start(); + auto resultList= m_diceParser->getLastIntegerResults(); + QCOMPARE(resultList.size(), 1); + + auto value= resultList.first(); + QVERIFY(qFuzzyCompare(value, expected) == 1); +} + +void TestDice::mathPriority_data() +{ + QTest::addColumn("cmd"); + QTest::addColumn("expected"); + + QTest::addRow("cmd1") << "10+2" << 12; + QTest::addRow("cmd2") << "2-10" << -8; + QTest::addRow("cmd3") << "5+2*3" << 11; + QTest::addRow("cmd4") << "5-5*5+5" << -15; + QTest::addRow("cmd5") << "5-5/5+5" << 9; + QTest::addRow("cmd6") << "10*(3*2)" << 60; + QTest::addRow("cmd7") << "60/(3*2)" << 10; + QTest::addRow("cmd8") << "5-(5*5+5)" << -25; +} + +void TestDice::dangerousCommandsTest() +{ + QFETCH(QString, cmd); + + for(int i= 0; i < 1000; ++i) + { + auto b= m_diceParser->parseLine(cmd); + QVERIFY(b); + m_diceParser->start(); + } +} +void TestDice::dangerousCommandsTest_data() +{ + QTest::addColumn("cmd"); + + QTest::addRow("cmd1") << "10d6g10"; + QTest::addRow("cmd2") << "10d2g10"; + QTest::addRow("cmd3") << "10d10g10"; + // QTest::addRow("cmd4") << "10d10g10"; + // QTest::addRow("cmd5") << "10d10g10"; +} + +void makeResult(DiceResult& result, const QVector& values) +{ + for(int val : values) + { + auto die= new Die(); + die->setBase(1); + die->setMaxValue(10); + die->insertRollValue(val); + result.insertResult(die); + } +} + +void makeResultExplode(DiceResult& result, const QVector& values) +{ + auto die= new Die(); + die->setBase(1); + die->setMaxValue(10); + for(int val : values) + { + die->insertRollValue(val); + } + result.insertResult(die); +} + +Validator* makeValidator(int number, BooleanCondition::LogicOperator op) +{ + BooleanCondition* validator= new BooleanCondition(); + NumberNode* node= new NumberNode(); + node->setNumber(number); + validator->setValueNode(node); + validator->setOperator(op); + return validator; +} + +void TestDice::keepTest() +{ + QFETCH(QVector, values); + QFETCH(int, keep); + QFETCH(int, score); + QFETCH(bool, error); + + TestNode node; + KeepDiceExecNode keepN; + keepN.setDiceKeepNumber(keep); + + DiceResult result; + + makeResult(result, values); + + node.setResult(&result); + node.setNextNode(&keepN); + + node.run(nullptr); + + bool isErrorEmpty= !keepN.getExecutionErrorMap().isEmpty(); + + QCOMPARE(isErrorEmpty, error); + + if(error) + return; + + auto resultScore= keepN.getResult()->getResult(Result::SCALAR).toInt(); + + QCOMPARE(score, resultScore); +} + +void TestDice::keepTest_data() +{ + QTest::addColumn>("values"); + QTest::addColumn("keep"); + QTest::addColumn("score"); + QTest::addColumn("error"); + + QTest::addRow("cmd1") << QVector({10, 9, 2}) << 1 << 10 << false; + QTest::addRow("cmd2") << QVector({10, 9, 2}) << 2 << 19 << false; + QTest::addRow("cmd3") << QVector({10, 9, 2}) << 3 << 21 << false; + QTest::addRow("cmd4") << QVector({10, 9, 2}) << 4 << 0 << true; +} + +void TestDice::sortTest() +{ + QFETCH(QVector, values); + QFETCH(bool, ascending); + QFETCH(QVector, scores); + + TestNode node; + SortResultNode sortN; + sortN.setSortAscending(ascending); + + DiceResult result; + + makeResult(result, values); + + node.setResult(&result); + node.setNextNode(&sortN); + + DiceResult expectedScore; + makeResult(expectedScore, scores); + + node.run(nullptr); + + auto list= dynamic_cast(sortN.getResult())->getResultList(); + + int i= 0; + auto expected= expectedScore.getResultList(); + for(auto sortedDie : list) + { + QCOMPARE(expected[i]->getValue(), sortedDie->getValue()); + ++i; + } +} + +void TestDice::sortTest_data() +{ + QTest::addColumn>("values"); + QTest::addColumn("ascending"); + QTest::addColumn>("scores"); + + QTest::addRow("cmd1") << QVector({10, 9, 2}) << true << QVector({2, 9, 10}); + QTest::addRow("cmd2") << QVector({1, 2, 3}) << false << QVector({3, 2, 1}); + QTest::addRow("cmd3") << QVector({10, 9, 2}) << false << QVector({10, 9, 2}); + QTest::addRow("cmd4") << QVector({2, 9, 10}) << true << QVector({2, 9, 10}); + QTest::addRow("cmd5") << QVector({1, 25, 10}) << false << QVector({25, 10, 1}); + QTest::addRow("cmd6") << QVector({10, 2, 100}) << false << QVector({100, 10, 2}); +} + +void TestDice::countTest() +{ + QFETCH(QVector, values); + QFETCH(int, condition); + QFETCH(int, score); + + TestNode node; + CountExecuteNode countN; + + auto validator= makeValidator(condition, BooleanCondition::GreaterThan); + + countN.setValidator(validator); + DiceResult result; + node.setResult(&result); + node.setNextNode(&countN); + + makeResult(result, values); + + node.run(nullptr); + + QCOMPARE(score, countN.getResult()->getResult(Result::SCALAR).toInt()); + + countN.setValidator(nullptr); +} + +void TestDice::countTest_data() +{ + QTest::addColumn>("values"); + QTest::addColumn("condition"); + QTest::addColumn("score"); + + QTest::addRow("cmd1") << QVector({10, 9, 2}) << 3 << 2; + QTest::addRow("cmd2") << QVector({1, 2, 3}) << 3 << 0; +} + +void TestDice::rerollTest() +{ + QFETCH(QVector, values); + QFETCH(int, condition); + QFETCH(bool, different); + + TestNode node; + RerollDiceNode reroll(true, false); + + DiceResult result; + makeResult(result, values); + node.setResult(&result); + + auto validator= makeValidator(condition, BooleanCondition::GreaterThan); + reroll.setValidator(validator); + node.setNextNode(&reroll); + + node.run(nullptr); + + auto list= dynamic_cast(reroll.getResult())->getResultList(); + + int i= 0; + auto expected= result.getResultList(); + bool resultDiff= false; + for(auto rerolled : list) + { + if(!resultDiff && rerolled->getValue() != expected[i]->getValue()) + resultDiff= true; + ++i; + } + + QCOMPARE(different, resultDiff); +} + +void TestDice::rerollTest_data() +{ + QTest::addColumn>("values"); + QTest::addColumn("condition"); + QTest::addColumn("different"); + + QTest::addRow("cmd1") << QVector({8, 9, 2}) << 10 << false; + QTest::addRow("cmd2") << QVector({0, 0, 0}) << -1 << true; +} + +void TestDice::explodeTest() +{ + QFETCH(QVector, values); + QFETCH(int, condition); + QFETCH(bool, different); + + TestNode node; + ExplodeDiceNode explode; + + DiceResult result; + makeResult(result, values); + node.setResult(&result); + + auto validator= makeValidator(condition, BooleanCondition::Equal); + explode.setValidator(validator); + node.setNextNode(&explode); + + node.run(nullptr); + + auto list= dynamic_cast(explode.getResult())->getResultList(); + + int i= 0; + auto expected= result.getResultList(); + bool resultDiff= false; + for(auto rerolled : list) + { + if(!resultDiff && rerolled->getValue() != expected[i]->getValue()) + resultDiff= true; + ++i; + } + + QCOMPARE(different, resultDiff); +} + +void TestDice::explodeTest_data() +{ + QTest::addColumn>("values"); + QTest::addColumn("condition"); + QTest::addColumn("different"); + + QTest::addRow("cmd1") << QVector({8, 9, 2}) << 10 << false; + QTest::addRow("cmd2") << QVector({0, 0, 0}) << 0 << true; +} + +void TestDice::rerollUntilTest() +{ + QFETCH(QVector, values); + QFETCH(int, condition); + QFETCH(bool, different); + + TestNode node; + RerollDiceNode reroll(false, false); + + DiceResult result; + makeResult(result, values); + node.setResult(&result); + + auto validator= makeValidator(condition, BooleanCondition::Equal); + reroll.setValidator(validator); + node.setNextNode(&reroll); + + node.run(nullptr); + + auto list= dynamic_cast(reroll.getResult())->getResultList(); + + int i= 0; + auto expected= result.getResultList(); + bool resultDiff= false; + for(auto rerolled : list) + { + if(!resultDiff && rerolled->getValue() != expected[i]->getValue()) + resultDiff= true; + ++i; + } + + QCOMPARE(different, resultDiff); +} +void TestDice::rerollUntilTest_data() +{ + QTest::addColumn>("values"); + QTest::addColumn("condition"); + QTest::addColumn("different"); + + QTest::addRow("cmd1") << QVector({8, 9, 2}) << 10 << false; + QTest::addRow("cmd2") << QVector({0, 0, 0}) << 0 << true; +} + +void TestDice::rerollAddTest() +{ + QFETCH(QVector, values); + QFETCH(int, condition); + QFETCH(bool, different); + + TestNode node; + RerollDiceNode reroll(true, true); + + DiceResult result; + makeResult(result, values); + node.setResult(&result); + + auto validator= makeValidator(condition, BooleanCondition::Equal); + reroll.setValidator(validator); + node.setNextNode(&reroll); + + node.run(nullptr); + + auto list= dynamic_cast(reroll.getResult())->getResultList(); + + int i= 0; + auto expected= result.getResultList(); + bool resultDiff= false; + for(auto rerolled : list) + { + if(!resultDiff && rerolled->getValue() != expected[i]->getValue()) + resultDiff= true; + ++i; + } + + QCOMPARE(different, resultDiff); +} +void TestDice::rerollAddTest_data() +{ + QTest::addColumn>("values"); + QTest::addColumn("condition"); + QTest::addColumn("different"); + + QTest::addRow("cmd1") << QVector({8, 9, 2}) << 10 << false; + QTest::addRow("cmd2") << QVector({0, 0, 0}) << 0 << true; +} + +void TestDice::mergeTest() {} +void TestDice::mergeTest_data() {} + +void TestDice::ifTest() +{ + QFETCH(QVector, values); + QFETCH(int, condition); + QFETCH(int, valCondition); + QFETCH(QString, expectedResult); + + IfNode::ConditionType conditionType= static_cast(condition); + + TestNode node; + IfNode ifNode; + ifNode.setConditionType(conditionType); + + DiceResult result; + makeResult(result, values); + node.setResult(&result); + + StringNode trueNode; + trueNode.setString(QStringLiteral("True")); + StringNode falseNode; + falseNode.setString(QStringLiteral("False")); + + ifNode.setInstructionTrue(&trueNode); + ifNode.setInstructionFalse(&falseNode); + + auto validator= makeValidator(valCondition, BooleanCondition::Equal); + ifNode.setValidator(validator); + node.setNextNode(&ifNode); + + node.run(nullptr); + + auto text= dynamic_cast(ifNode.getNextNode()->getResult())->getText(); + + QCOMPARE(expectedResult, text); + + ifNode.setNextNode(nullptr); +} +void TestDice::ifTest_data() +{ + QTest::addColumn>("values"); + QTest::addColumn("condition"); + QTest::addColumn("valCondition"); + QTest::addColumn("expectedResult"); + + int onEach= 0; + int oneOfThem= 1; + int allOfThem= 2; + int onScalar= 3; + + QTest::addRow("cmd1") << QVector({8, 9, 2}) << onEach << 0 << "False"; + QTest::addRow("cmd2") << QVector({2, 2, 2}) << onEach << 2 << "True"; + + QTest::addRow("cmd3") << QVector({0, 0, 0}) << oneOfThem << 10 << "False"; + QTest::addRow("cmd4") << QVector({10, 9, 5}) << oneOfThem << 10 << "True"; + QTest::addRow("cmd5") << QVector({9, 9, 9}) << oneOfThem << 9 << "True"; + + QTest::addRow("cmd6") << QVector({8, 9, 2}) << allOfThem << 1 << "False"; + QTest::addRow("cmd7") << QVector({8, 9, 2}) << allOfThem << 9 << "False"; + QTest::addRow("cmd8") << QVector({8, 8, 8}) << allOfThem << 8 << "True"; + + QTest::addRow("cmd9") << QVector({25, 8, 14}) << onScalar << 1 << "False"; + QTest::addRow("cmd10") << QVector({25, 8, 14}) << onScalar << 47 << "True"; +} + +void TestDice::paintTest() {} +void TestDice::paintTest_data() {} + +void TestDice::filterTest() +{ + QFETCH(QVector, values); + QFETCH(int, condition); + QFETCH(bool, different); + + TestNode node; + FilterNode filter; + + DiceResult result; + makeResult(result, values); + node.setResult(&result); + + auto validator= makeValidator(condition, BooleanCondition::Different); + filter.setValidator(validator); + node.setNextNode(&filter); + + node.run(nullptr); + + auto list= dynamic_cast(filter.getResult())->getResultList(); + + auto expected= result.getResultList(); + bool resultDiff= (list.size() != expected.size()); + + QCOMPARE(different, resultDiff); +} + +void TestDice::filterTest_data() +{ + QTest::addColumn>("values"); + QTest::addColumn("condition"); + QTest::addColumn("different"); + + QTest::addRow("cmd1") << QVector({8, 4, 2}) << 4 << true; + QTest::addRow("cmd2") << QVector({0, 0, 0}) << 1 << false; +} + +void TestDice::uniqueTest() +{ + QFETCH(QVector, values); + QFETCH(QVector, expected); + + TestNode node; + UniqueNode unique; + + DiceResult result; + makeResult(result, values); + node.setResult(&result); + + node.setNextNode(&unique); + node.run(nullptr); + + auto list= dynamic_cast(unique.getResult())->getResultList(); + QVector resultVal; + + std::transform(list.begin(), list.end(), std::back_inserter(resultVal), [](Die* die) { return die->getValue(); }); + + QVERIFY(resultVal == expected); +} + +void TestDice::uniqueTest_data() +{ + QTest::addColumn>("values"); + QTest::addColumn>("expected"); + + QTest::addRow("cmd1") << QVector({8, 4, 2, 8}) << QVector({8, 4, 2}); + QTest::addRow("cmd2") << QVector({8, 4, 2}) << QVector({8, 4, 2}); +} + +void TestDice::spreadTest() +{ + QFETCH(QVector, values); + + TestNode node; + SplitNode split; + + DiceResult result; + makeResultExplode(result, values); + node.setResult(&result); + + node.setNextNode(&split); + + node.run(nullptr); + + auto list= dynamic_cast(split.getResult())->getResultList(); + + auto expected= result.getResultList(); + QVERIFY(list.size() == values.size()); +} + +void TestDice::spreadTest_data() +{ + QTest::addColumn>("values"); + + QTest::addRow("cmd1") << QVector({8, 4, 2}); +} + +void TestDice::groupTest() {} +void TestDice::groupTest_data() {} + +void TestDice::bindTest() {} +void TestDice::bindTest_data() {} + +void TestDice::occurenceTest() +{ + QFETCH(QVector, values); + QFETCH(int, condition); + QFETCH(QString, expected); + + TestNode node; + OccurenceCountNode count; + + DiceResult result; + makeResult(result, values); + node.setResult(&result); + + auto validator= makeValidator(condition, BooleanCondition::GreaterThan); + count.setValidator(validator); + node.setNextNode(&count); + + node.run(nullptr); + + auto text= dynamic_cast(count.getResult())->getText(); + + QVERIFY(text.startsWith(expected)); +} +void TestDice::occurenceTest_data() +{ + QTest::addColumn>("values"); + QTest::addColumn("condition"); + QTest::addColumn("expected"); + + QTest::addRow("cmd1") << QVector({8, 8, 2}) << 7 << "2x8"; + QTest::addRow("cmd2") << QVector({0, 0, 0}) << 1 << "No matching result"; +} + +void TestDice::ifCommandTest() +{ + QFETCH(QString, cmd); + QFETCH(BooleanCondition::LogicOperator, compare); + QFETCH(QList, level); + QFETCH(QStringList, startExperted); + + bool test= m_diceParser->parseLine(cmd); + QVERIFY2(test, cmd.toStdString().c_str()); + m_diceParser->start(); + auto results= m_diceParser->getLastIntegerResults(); + auto strResult= m_diceParser->getStringResult(); + + QCOMPARE(results.size(), 1); + QCOMPARE(strResult.size(), 1); + + auto result= results.first(); + auto it= std::find_if(level.begin(), level.end(), [compare, result](int level) { + if(compare == BooleanCondition::GreaterOrEqual) + return result >= level; + else if(compare == BooleanCondition::GreaterThan) + return result > level; + else if(compare == BooleanCondition::LesserThan) + return result < level; + else if(compare == BooleanCondition::LesserOrEqual) + return result <= level; + else if(compare == BooleanCondition::Equal) + return qFuzzyCompare(result, level); + else // if(compare == BooleanCondition::Different) + return !qFuzzyCompare(result, level); + }); + + auto index= std::distance(level.begin(), it); + + auto strResultExpected= startExperted[index]; + auto resultText= strResult.first(); + + QVERIFY2(resultText.startsWith(strResultExpected), "string result does not fit the expectation"); +} + +void TestDice::ifCommandTest_data() +{ + QTest::addColumn("cmd"); + QTest::addColumn("compare"); + QTest::addColumn>("level"); + QTest::addColumn("startExperted"); + + QTest::addRow("cmd1") << "2d10i:[>=15]{\"Complete Success: %1 [%2]\"}{i:[>=10]{\"Success with Complications: %1 " + "[%2]\"}{\"Failure: %1 [%2]\"}}" + << BooleanCondition::GreaterOrEqual << QList({15, 10, 1}) + << QStringList({"Complete Success:", "Success with Complications:", "Failure:"}); + QTest::addRow("cmd2") + << "2d10;$1i:[>=15]{\"Complete Success: %1 [%2]\"}{$1i:[>=10]{\"Success with Complications: %1 " + "[%2]\"}{\"Failure: %1 [%2]\"}}" + << BooleanCondition::GreaterOrEqual << QList({15, 10, 1}) + << QStringList({"Complete Success:", "Success with Complications:", "Failure:"}); +} + +void TestDice::cleanupTestCase() {} + +QTEST_MAIN(TestDice) + +#include "tst_dice.moc" -- cgit v1.2.3-70-g09d2 From bb6b87a2685c0d71b5c38be33c100f85ac0b9cee Mon Sep 17 00:00:00 2001 From: Renaud G Date: Thu, 25 Jul 2019 09:59:29 +0200 Subject: Rework of the component to be a proper lib --- CMakeLists.txt | 37 +++++++++++++++++++++++-- booleancondition.cpp | 2 +- cli/CMakeLists.txt | 2 ++ cli/main.cpp | 48 ++++++++++++++++---------------- diceparser.cpp | 57 +++++++++++++++++++------------------- highlightdice.cpp | 2 +- include/diceparser.h | 67 +++++++++++++++++++++------------------------ include/diceparserhelper.h | 30 ++++++++++++++++++++ node/dicerollernode.cpp | 8 +++--- node/executionnode.cpp | 6 ++-- node/executionnode.h | 21 ++------------ node/forloopnode.cpp | 2 +- node/ifnode.cpp | 2 +- node/jumpbackwardnode.cpp | 5 ++-- node/keepdiceexecnode.cpp | 7 +++-- node/listsetrollnode.cpp | 6 ++-- node/mergenode.cpp | 4 +-- node/occurencecountnode.cpp | 1 + node/paintnode.cpp | 2 +- node/rerolldicenode.cpp | 3 +- node/scalaroperatornode.cpp | 33 +++++++++++----------- node/scalaroperatornode.h | 2 +- node/valueslistnode.cpp | 2 +- node/variablenode.cpp | 2 +- operationcondition.cpp | 2 +- result/diceresult.cpp | 8 +++--- result/diceresult.h | 2 +- result/result.cpp | 9 ++++-- result/result.h | 16 ++--------- result/scalarresult.cpp | 9 ++---- result/scalarresult.h | 2 +- result/stringresult.cpp | 15 +++++----- result/stringresult.h | 4 +-- tests/dice/CMakeLists.txt | 3 ++ tests/dice/tst_dice.cpp | 4 +-- 35 files changed, 235 insertions(+), 190 deletions(-) (limited to 'tests/dice') diff --git a/CMakeLists.txt b/CMakeLists.txt index 9a9a340..e06bdac 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.5) -project(diceparser) +project(diceparser VERSION 1.9.0 DESCRIPTION "Parser of dice command") set(QT_REQUIRED_VERSION "5.12.0") find_package(Qt5 ${QT_REQUIRED_VERSION} CONFIG REQUIRED COMPONENTS Core Test Gui Svg) @@ -53,11 +53,42 @@ SET( dice_sources node/valueslistnode.cpp ) -add_library(diceparser SHARED ${dice_sources} ) -target_link_libraries(diceparser PUBLIC Qt5::Core Qt5::Gui Qt5::Svg) +add_library(diceparser_shared SHARED ${dice_sources} ) +add_library(diceparser_static STATIC ${dice_sources} ) + +target_include_directories(diceparser_shared PRIVATE include) +target_include_directories(diceparser_static PRIVATE include) + +SET_TARGET_PROPERTIES(diceparser_static PROPERTIES OUTPUT_NAME diceparser CLEAN_DIRECT_OUTPUT 1) +SET_TARGET_PROPERTIES(diceparser_shared PROPERTIES OUTPUT_NAME diceparser CLEAN_DIRECT_OUTPUT 1) + +target_link_libraries(diceparser_shared PUBLIC Qt5::Core Qt5::Gui Qt5::Svg) +target_link_libraries(diceparser_static PUBLIC Qt5::Core Qt5::Gui Qt5::Svg) + +set_target_properties(diceparser_shared PROPERTIES VERSION ${PROJECT_VERSION}) +set_target_properties(diceparser_shared PROPERTIES SOVERSION 1) +#target_link_libraries(diceparsersta PROPERTIES VERSION ${PROJECT_VERSION}) + +set_target_properties(diceparser_shared PROPERTIES PUBLIC_HEADER "include/diceparser.h;include/highlightdice.h;include/parsingtoolbox.h;include/dicealias.h;include/diceparserhelper.h") +#set_target_properties(diceparsersta PROPERTIES PUBLIC_HEADER "include/diceparser.h;include/highlightdice.h;include/parsingtoolbox.h;include/dicealias.h;include/diceparserhelper.h") add_subdirectory(cli) add_subdirectory( tests ) #add_subdirectory(irc) #add_subdirectory(mobile) #add_subdirectory(webserver) + + +include(GNUInstallDirs) + +install(TARGETS diceparser_shared + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) + +install(TARGETS diceparser_static + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) + +configure_file(diceparser.pc.in diceparser.pc @ONLY) + +install(FILES ${CMAKE_BINARY_DIR}/diceparser.pc DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig) + diff --git a/booleancondition.cpp b/booleancondition.cpp index f62c900..c81f5ab 100644 --- a/booleancondition.cpp +++ b/booleancondition.cpp @@ -159,5 +159,5 @@ qint64 BooleanCondition::valueToScalar() const m_value->run(nullptr); auto result= m_value->getResult(); - return result->getResult(Result::SCALAR).toInt(); + return result->getResult(Dice::RESULT_TYPE::SCALAR).toInt(); } diff --git a/cli/CMakeLists.txt b/cli/CMakeLists.txt index 215287d..5d79b3c 100644 --- a/cli/CMakeLists.txt +++ b/cli/CMakeLists.txt @@ -74,6 +74,8 @@ SET( cli_sources set(documentation_files ../HelpMe.md ../README.md) add_executable( dice ${cli_sources} ${dice_QM} ${documentation_files}) +target_include_directories(dice PRIVATE ../include) + target_link_libraries(dice diceparser ${Qt5Core_LIBRARIES} ${Qt5Gui_LIBRARIES} ${Qt5Svg_LIBRARIES}) INSTALL_TARGETS(/bin dice) diff --git a/cli/main.cpp b/cli/main.cpp index d6f6398..958d96f 100644 --- a/cli/main.cpp +++ b/cli/main.cpp @@ -35,6 +35,7 @@ #include #endif +#include "dicealias.h" #include "diceparser.h" #include "displaytoolbox.h" #include "highlightdice.h" @@ -115,7 +116,7 @@ QString diceToMarkdown(QJsonArray array, bool withColor, bool allSameColor, bool } void displayJSon(QString scalarText, QString resultStr, QJsonArray array, bool withColor, QString cmd, QString error, - QString warning, QString comment, bool allSameFaceCount, bool allSameColor) + QString warning, QString comment, bool allSameFaceCount, bool allSameColor) { Q_UNUSED(withColor); QJsonDocument doc; @@ -133,7 +134,7 @@ void displayJSon(QString scalarText, QString resultStr, QJsonArray array, bool w out << doc.toJson() << "\n"; } void displayMarkdown(QString scalarText, QString resultStr, QJsonArray array, bool withColor, QString cmd, - QString error, QString warning, QString comment, bool allSameFaceCount, bool allSameColor) + QString error, QString warning, QString comment, bool allSameFaceCount, bool allSameColor) { Q_UNUSED(withColor); QString str("```Markdown\n"); @@ -165,7 +166,7 @@ void displayMarkdown(QString scalarText, QString resultStr, QJsonArray array, bo out << str; } QString displaySVG(QString scalarText, QString resultStr, QJsonArray array, bool withColor, QString cmd, QString error, - QString warning, QString comment, bool allSameFaceCount, bool allSameColor) + QString warning, QString comment, bool allSameFaceCount, bool allSameColor) { QString str( "\n", "aliasfile"); + "path to alias json files: ", "aliasfile"); QCommandLineOption character(QStringList() << "s" << "charactersheet", - "set Parameters to simulate character sheet: ", "sheetfile"); + "set Parameters to simulate character sheet: ", "sheetfile"); QCommandLineOption markdown(QStringList() << "m" << "markdown", - "The output is formatted in markdown."); + "The output is formatted in markdown."); QCommandLineOption bot(QStringList() << "b" << "bot", - "Discord bot."); + "Discord bot."); QCommandLineOption svg(QStringList() << "g" << "svg", - "The output is formatted in svg."); + "The output is formatted in svg."); QCommandLineOption json(QStringList() << "j" << "json", - "The output is formatted in json."); + "The output is formatted in json."); QCommandLineOption dotFile(QStringList() << "d" << "dot-file", - "Instead of rolling dice, generate the execution tree and write it in ", "dotfile"); + "Instead of rolling dice, generate the execution tree and write it in ", + "dotfile"); QCommandLineOption translation(QStringList() << "t" << "translation", - "path to the translation file: ", "translationfile"); + "path to the translation file: ", "translationfile"); QCommandLineOption help(QStringList() << "h" << "help", - "Display this help"); + "Display this help"); optionParser.addOption(color); optionParser.addOption(version); diff --git a/diceparser.cpp b/diceparser.cpp index 4ec44ef..da838f1 100644 --- a/diceparser.cpp +++ b/diceparser.cpp @@ -29,6 +29,7 @@ #include "node/bind.h" #include "node/countexecutenode.h" #include "node/dicerollernode.h" +#include "node/executionnode.h" #include "node/explodedicenode.h" #include "node/filternode.h" #include "node/groupnode.h" @@ -180,7 +181,7 @@ bool DiceParser::parseLine(QString str, bool allowAlias) if(!hasInstruction) { m_errorMap.insert( - ExecutionNode::NOTHING_UNDERSTOOD, + Dice::ERROR_CODE::NOTHING_UNDERSTOOD, QObject::tr("Nothing was understood. To roll dice: !1d6 - full documation: " "https://github.com/" @@ -190,7 +191,7 @@ bool DiceParser::parseLine(QString str, bool allowAlias) { auto i= m_command.size() - str.size(); m_warningMap.insert( - ExecutionNode::UNEXPECTED_CHARACTER, + Dice::ERROR_CODE::UNEXPECTED_CHARACTER, QObject::tr("Unexpected character at %1 - end of command was ignored \"%2\"").arg(i).arg(str)); } if(!m_errorMap.isEmpty()) @@ -226,7 +227,7 @@ bool DiceParser::readExpression(QString& str, ExecutionNode*& node) } else { - m_warningMap.insert(ExecutionNode::BAD_SYNTAXE, + m_warningMap.insert(Dice::ERROR_CODE::BAD_SYNTAXE, QObject::tr("Expected closing parenthesis - can't validate the inside.")); } } @@ -392,11 +393,11 @@ QList DiceParser::getLastIntegerResults() bool scalarDone= false; while((result != nullptr) && (!scalarDone)) { - if(result->hasResultOfType(Result::SCALAR)) + if(result->hasResultOfType(Dice::RESULT_TYPE::SCALAR)) { if(!alreadyVisitedNode.contains(result->getId())) { - resultValues << result->getResult(Result::SCALAR).toReal(); + resultValues << result->getResult(Dice::RESULT_TYPE::SCALAR).toReal(); alreadyVisitedNode << result->getId(); } scalarDone= true; @@ -417,9 +418,9 @@ QStringList DiceParser::getStringResult() bool found= false; while((nullptr != result) && (!found)) { - if(result->hasResultOfType(Result::STRING)) + if(result->hasResultOfType(Dice::RESULT_TYPE::STRING)) { - str= result->getResult(Result::STRING).toString(); + str= result->getResult(Dice::RESULT_TYPE::STRING).toString(); found= true; } result= result->getPrevious(); @@ -440,7 +441,7 @@ QStringList DiceParser::getAllStringResult(bool& hasAlias) while(nullptr != result) { - if(result->hasResultOfType(Result::STRING)) + if(result->hasResultOfType(Dice::RESULT_TYPE::STRING)) { StringResult* stringResult= dynamic_cast(result); if(nullptr != stringResult) @@ -465,7 +466,7 @@ QStringList DiceParser::getAllDiceResult(bool& hasAlias) while(nullptr != result) { - if(result->hasResultOfType(Result::DICE_LIST)) + if(result->hasResultOfType(Dice::RESULT_TYPE::DICE_LIST)) { DiceResult* stringResult= dynamic_cast(result); if(nullptr != stringResult) @@ -508,7 +509,7 @@ void DiceParser::getDiceResultFromAllInstruction(QList& resu ExportedDiceResult nodeResult; while(nullptr != result) { - if(result->hasResultOfType(Result::DICE_LIST)) + if(result->hasResultOfType(Dice::RESULT_TYPE::DICE_LIST)) { DiceResult* diceResult= dynamic_cast(result); QList list; @@ -540,7 +541,7 @@ void DiceParser::getLastDiceResult(QList& diceValuesList, bo Result* result= next->getResult(); while(nullptr != result) { - if(result->hasResultOfType(Result::DICE_LIST)) + if(result->hasResultOfType(Dice::RESULT_TYPE::DICE_LIST)) { DiceResult* diceResult= dynamic_cast(result); if(nullptr != diceResult) @@ -604,7 +605,7 @@ bool DiceParser::hasIntegerResultNotInFirst() bool result= false; for(auto node : m_startNodes) { - result|= hasResultOfType(Result::SCALAR, node); + result|= hasResultOfType(Dice::RESULT_TYPE::SCALAR, node); } return result; } @@ -614,7 +615,7 @@ bool DiceParser::hasDiceResult() bool result= false; for(auto node : m_startNodes) { - result|= hasResultOfType(Result::DICE_LIST, node); + result|= hasResultOfType(Dice::RESULT_TYPE::DICE_LIST, node); } return result; } @@ -623,11 +624,11 @@ bool DiceParser::hasStringResult() bool result= false; for(auto node : m_startNodes) { - result|= hasResultOfType(Result::STRING, node); + result|= hasResultOfType(Dice::RESULT_TYPE::STRING, node); } return result; } -bool DiceParser::hasResultOfType(Result::RESULT_TYPE type, ExecutionNode* node, bool notthelast) +bool DiceParser::hasResultOfType(Dice::RESULT_TYPE type, ExecutionNode* node, bool notthelast) { bool scalarDone= false; ExecutionNode* next= getLeafNode(node); @@ -653,7 +654,7 @@ QList DiceParser::getSumOfDiceResult() bool found= false; while((nullptr != result) && (!found)) { - if(result->hasResultOfType(Result::DICE_LIST)) + if(result->hasResultOfType(Dice::RESULT_TYPE::DICE_LIST)) { DiceResult* myDiceResult= dynamic_cast(result); if(nullptr != myDiceResult) @@ -704,7 +705,7 @@ bool DiceParser::readDice(QString& str, ExecutionNode*& node) if(max < 1) { m_errorMap.insert( - ExecutionNode::BAD_SYNTAXE, + Dice::ERROR_CODE::BAD_SYNTAXE, QObject::tr("Dice with %1 face(s) does not exist. Please, put a value higher than 0").arg(max)); return false; } @@ -759,7 +760,7 @@ bool DiceParser::readDice(QString& str, ExecutionNode*& node) else { m_errorMap.insert( - ExecutionNode::BAD_SYNTAXE, + Dice::ERROR_CODE::BAD_SYNTAXE, QObject::tr( "List is missing after the L operator. Please, add it (e.g : 1L[sword,spear,gun,arrow])")); } @@ -1087,7 +1088,7 @@ bool DiceParser::readOption(QString& str, ExecutionNode* previous) //, } else { - m_errorMap.insert(ExecutionNode::BAD_SYNTAXE, + m_errorMap.insert(Dice::ERROR_CODE::BAD_SYNTAXE, QObject::tr("Validator is missing after the c operator. Please, change it")); } } @@ -1103,7 +1104,7 @@ bool DiceParser::readOption(QString& str, ExecutionNode* previous) //, if(!m_parsingToolbox->isValidValidator(previous, validator)) { m_errorMap.insert( - ExecutionNode::BAD_SYNTAXE, + Dice::ERROR_CODE::BAD_SYNTAXE, QObject::tr("Validator is missing after the %1 operator. Please, change it") .arg(operatorName == Reroll ? "r" : "a")); } @@ -1123,7 +1124,7 @@ bool DiceParser::readOption(QString& str, ExecutionNode* previous) //, } else { - m_errorMap.insert(ExecutionNode::BAD_SYNTAXE, + m_errorMap.insert(Dice::ERROR_CODE::BAD_SYNTAXE, QObject::tr("Validator is missing after the %1 operator. Please, change it") .arg(operatorName == Reroll ? QStringLiteral("r") : @@ -1140,7 +1141,7 @@ bool DiceParser::readOption(QString& str, ExecutionNode* previous) //, { if(!m_parsingToolbox->isValidValidator(previous, validator)) { - m_errorMap.insert(ExecutionNode::ENDLESS_LOOP_ERROR, + m_errorMap.insert(Dice::ERROR_CODE::ENDLESS_LOOP_ERROR, QObject::tr("This condition %1 introduces an endless loop. Please, change it") .arg(validator->toString())); } @@ -1152,7 +1153,7 @@ bool DiceParser::readOption(QString& str, ExecutionNode* previous) //, } else { - m_errorMap.insert(ExecutionNode::BAD_SYNTAXE, + m_errorMap.insert(Dice::ERROR_CODE::BAD_SYNTAXE, QObject::tr("Validator is missing after the e operator. Please, change it")); } } @@ -1345,9 +1346,9 @@ void DiceParser::setComment(const QString& comment) m_comment= comment; } -QMap DiceParser::getErrorMap() +QMap DiceParser::getErrorMap() { - QMap map; + QMap map; for(auto start : m_startNodes) { @@ -1362,7 +1363,7 @@ QMap DiceParser::getErrorMap() } QString DiceParser::humanReadableError() { - QMapIterator i(m_errorMap); + QMapIterator i(m_errorMap); QString str(""); while(i.hasNext()) { @@ -1372,7 +1373,7 @@ QString DiceParser::humanReadableError() } /// list - QMapIterator j(getErrorMap()); + QMapIterator j(getErrorMap()); while(j.hasNext()) { j.next(); @@ -1384,7 +1385,7 @@ QString DiceParser::humanReadableError() QString DiceParser::humanReadableWarning() { - QMapIterator i(m_warningMap); + QMapIterator i(m_warningMap); QString str(""); while(i.hasNext()) { diff --git a/highlightdice.cpp b/highlightdice.cpp index 02941d5..c2abdb6 100644 --- a/highlightdice.cpp +++ b/highlightdice.cpp @@ -17,7 +17,7 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ -#include "highlightdice.h" +#include "include/highlightdice.h" HighLightDice::HighLightDice(QList result, bool isHighlighted, QString color, bool displayed, quint64 faces) : m_result(result), m_hasHighlight(isHighlighted), m_color(color), m_displayed(displayed), m_faces(faces) diff --git a/include/diceparser.h b/include/diceparser.h index 8ecfb50..230d571 100644 --- a/include/diceparser.h +++ b/include/diceparser.h @@ -24,10 +24,11 @@ #include #include +#include -#include "dicealias.h" #include "highlightdice.h" -#include "node/executionnode.h" +#include "diceparserhelper.h" +//#include "node/executionnode.h" typedef QList ListDiceResult; typedef QMap ExportedDiceResult; @@ -35,6 +36,8 @@ typedef QMap ExportedDiceResult; class ExplodeDiceNode; class ParsingToolBox; class DiceRollerNode; +class DiceAlias; +class ExecutionNode; /** * @page DiceParser Dice Parser * @@ -123,18 +126,6 @@ public: * */ void start(); - - /** - * @brief displayResult - */ - QString displayResult(); - /** - * @brief readExpression - * @param str - * @param node - * @return - */ - bool readExpression(QString& str, ExecutionNode*& node); /** * @brief displayDotTree - Write the execution tree into file using dot format. * @param filepath absolute or relative path to the tree file. @@ -204,7 +195,7 @@ public: * @brief getErrorList * @return */ - QMap getErrorMap(); + QMap getErrorMap(); /** * @brief setPathToHelp set the path to the documentation, this path must be adatped to the lang of application etc… * @param l the path. @@ -226,15 +217,7 @@ public: * @return true when the command has separator, false otherwise. */ bool hasSeparator() const; - /** - * @brief readIfInstruction reads the current command to build if node with proper parameters. - * @param str is the command string, if IF istruction is found, the str will be changed, in other case the string is - * unmodified - * @param trueNode is the branch's beginning to be executed if the IfNode is true. - * @param falseNode is the branch's beginning to be executed if the IfNode is false. - * @return true, ifNode has been found, false otherwise - */ - bool readIfInstruction(QString& str, ExecutionNode*& trueNode, ExecutionNode*& falseNode); + /** * @brief setVariableDictionary * @param variables @@ -243,19 +226,32 @@ public: QString getComment() const; void setComment(const QString& comment); - bool readOptionFromNull(QString& str, ExecutionNode*& node); - bool readOperatorFromNull(QString& str, ExecutionNode*& node); - - bool readInstructionList(QString& str); void getDiceResultFromAllInstruction(QList& resultList); QString humanReadableWarning(); bool readValuesList(QString& str, ExecutionNode*& node); -protected: - bool readParameterNode(QString& str, ExecutionNode*& node); - private: + /** + * @brief readIfInstruction reads the current command to build if node with proper parameters. + * @param str is the command string, if IF istruction is found, the str will be changed, in other case the string is + * unmodified + * @param trueNode is the branch's beginning to be executed if the IfNode is true. + * @param falseNode is the branch's beginning to be executed if the IfNode is false. + * @return true, ifNode has been found, false otherwise + */ + bool readIfInstruction(QString& str, ExecutionNode*& trueNode, ExecutionNode*& falseNode); + bool readInstructionList(QString& str); + bool readOptionFromNull(QString& str, ExecutionNode*& node); + bool readOperatorFromNull(QString& str, ExecutionNode*& node); + bool readParameterNode(QString& str, ExecutionNode*& node); + /** + * @brief readExpression + * @param str + * @param node + * @return + */ + bool readExpression(QString& str, ExecutionNode*& node); /** * @brief readDice * @param str @@ -336,7 +332,8 @@ private: * @param notthelast * @return */ - bool hasResultOfType(Result::RESULT_TYPE, ExecutionNode* node, bool notthelast= false); + bool hasResultOfType(Dice::RESULT_TYPE, ExecutionNode* node, bool notthelast= false); + bool readBlocInstruction(QString& str, ExecutionNode*& resultnode); private: QMap* m_mapDiceOp; @@ -345,17 +342,15 @@ private: QList* m_aliasList; QStringList* m_commandList; - QMap m_errorMap; - QMap m_warningMap; + QMap m_errorMap; + QMap m_warningMap; ExecutionNode* m_start= nullptr; std::vector m_startNodes; - // ExecutionNode* m_current; QString m_command; ParsingToolBox* m_parsingToolbox; QString m_helpPath; bool m_currentTreeHasSeparator; - bool readBlocInstruction(QString& str, ExecutionNode*& resultnode); QString m_comment; }; diff --git a/include/diceparserhelper.h b/include/diceparserhelper.h index 4f53293..3ca4f15 100644 --- a/include/diceparserhelper.h +++ b/include/diceparserhelper.h @@ -1,4 +1,34 @@ #ifndef DICEPARSERHELPER_H #define DICEPARSERHELPER_H +namespace Dice +{ + +enum class ERROR_CODE : int +{ + NO_DICE_ERROR, + DIE_RESULT_EXPECTED, + BAD_SYNTAXE, + ENDLESS_LOOP_ERROR, + DIVIDE_BY_ZERO, + NOTHING_UNDERSTOOD, + NO_DICE_TO_ROLL, + TOO_MANY_DICE, + NO_VARIBALE, + INVALID_INDEX, + UNEXPECTED_CHARACTER, + NO_PREVIOUS_ERROR +}; + +/** + * @brief The RESULT_TYPE enum or combinaison + */ +enum class RESULT_TYPE : int +{ + NONE= 0, + SCALAR= 1, + STRING= 2, + DICE_LIST= 4 +}; +} #endif // DICEPARSERHELPER_H diff --git a/node/dicerollernode.cpp b/node/dicerollernode.cpp index f57d3e3..f87908f 100644 --- a/node/dicerollernode.cpp +++ b/node/dicerollernode.cpp @@ -19,10 +19,10 @@ void DiceRollerNode::run(ExecutionNode* previous) Result* result= previous->getResult(); if(nullptr != result) { - auto num= result->getResult(Result::SCALAR).toReal(); + auto num= result->getResult(Dice::RESULT_TYPE::SCALAR).toReal(); if(num <= 0) { - m_errors.insert(NO_DICE_TO_ROLL, QObject::tr("No dice to roll")); + m_errors.insert(Dice::ERROR_CODE::NO_DICE_TO_ROLL, QObject::tr("No dice to roll")); } m_diceCount= num > 0 ? static_cast(num) : 0; m_result->setPrevious(result); @@ -30,7 +30,7 @@ void DiceRollerNode::run(ExecutionNode* previous) auto possibleValue= static_cast(std::abs((m_max - m_min) + 1)); if(possibleValue < m_diceCount && m_unique) { - m_errors.insert(TOO_MANY_DICE, + m_errors.insert(Dice::ERROR_CODE::TOO_MANY_DICE, QObject::tr("More unique values asked than possible values (D operator)")); return; } @@ -62,7 +62,7 @@ void DiceRollerNode::run(ExecutionNode* previous) quint64 DiceRollerNode::getFaces() const { - return std::abs(m_max - m_min) + 1; + return static_cast(std::abs(m_max - m_min) + 1); } std::pair DiceRollerNode::getRange() const diff --git a/node/executionnode.cpp b/node/executionnode.cpp index 5bee091..4545934 100644 --- a/node/executionnode.cpp +++ b/node/executionnode.cpp @@ -6,7 +6,7 @@ ExecutionNode::ExecutionNode() : m_previousNode(nullptr) , m_result(nullptr) , m_nextNode(nullptr) - , m_errors(QMap()) + , m_errors(QMap()) , m_id(QString("\"%1\"").arg(QUuid::createUuid().toString())) { } @@ -40,7 +40,7 @@ ExecutionNode* ExecutionNode::getNextNode() { return m_nextNode; } -QMap ExecutionNode::getExecutionErrorMap() +QMap ExecutionNode::getExecutionErrorMap() { if(nullptr != m_nextNode) { @@ -97,5 +97,5 @@ qint64 ExecutionNode::getScalarResult() { if(m_result == nullptr) return 0; - return m_result->getResult(Result::SCALAR).toInt(); + return m_result->getResult(Dice::RESULT_TYPE::SCALAR).toInt(); } diff --git a/node/executionnode.h b/node/executionnode.h index e8eca1f..62bc5a5 100644 --- a/node/executionnode.h +++ b/node/executionnode.h @@ -1,8 +1,8 @@ #ifndef EXECUTIONNODE_H #define EXECUTIONNODE_H +#include "diceparserhelper.h" #include "result/result.h" -#include /** * @brief The ExecutionNode class @@ -10,21 +10,6 @@ class ExecutionNode { public: - enum DICE_ERROR_CODE - { - NO_DICE_ERROR, - DIE_RESULT_EXPECTED, - BAD_SYNTAXE, - ENDLESS_LOOP_ERROR, - DIVIDE_BY_ZERO, - NOTHING_UNDERSTOOD, - NO_DICE_TO_ROLL, - TOO_MANY_DICE, - NO_VARIBALE, - INVALID_INDEX, - UNEXPECTED_CHARACTER, - NO_PREVIOUS_ERROR - }; /** * @brief ExecutionNode */ @@ -72,7 +57,7 @@ public: * @brief getErrorList * @return */ - virtual QMap getExecutionErrorMap(); + virtual QMap getExecutionErrorMap(); /** * @brief generateDotTree @@ -109,7 +94,7 @@ protected: /** * @brief m_errors */ - QMap m_errors; + QMap m_errors; QString m_id; }; diff --git a/node/forloopnode.cpp b/node/forloopnode.cpp index f65a389..aba6ff4 100644 --- a/node/forloopnode.cpp +++ b/node/forloopnode.cpp @@ -58,7 +58,7 @@ void ForLoopNode::run(ExecutionNode* previous) tmp= tmp->getNextNode(); } Result* internalResult= tmp->getResult(); - auto value= internalResult->getResult(Result::SCALAR).toInt(); + auto value= internalResult->getResult(Dice::RESULT_TYPE::SCALAR).toInt(); Die* neodie= new Die(); *neodie= *dice; diff --git a/node/ifnode.cpp b/node/ifnode.cpp index 5470e15..10b2f79 100644 --- a/node/ifnode.cpp +++ b/node/ifnode.cpp @@ -45,7 +45,7 @@ void IfNode::run(ExecutionNode* previous) if(nullptr != m_result) { - qreal value= previousResult->getResult(Result::SCALAR).toReal(); + qreal value= previousResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal(); if(nullptr != m_validator) { diff --git a/node/jumpbackwardnode.cpp b/node/jumpbackwardnode.cpp index 15c7063..2870854 100644 --- a/node/jumpbackwardnode.cpp +++ b/node/jumpbackwardnode.cpp @@ -96,7 +96,7 @@ void JumpBackwardNode::run(ExecutionNode* previous) if(nullptr != result) { //--i; - if(/*(i==0)&&*/ (result->hasResultOfType(Result::DICE_LIST))) + if(/*(i==0)&&*/ (result->hasResultOfType(Dice::RESULT_TYPE::DICE_LIST))) { found= true; m_backwardNode= parent; @@ -118,7 +118,8 @@ void JumpBackwardNode::run(ExecutionNode* previous) } if(nullptr == result) { - m_errors.insert(DIE_RESULT_EXPECTED, + m_errors.insert( + Dice::ERROR_CODE::DIE_RESULT_EXPECTED, QObject::tr(" The @ operator expects dice result. Please check the documentation to fix your command.")); } else diff --git a/node/keepdiceexecnode.cpp b/node/keepdiceexecnode.cpp index 42b4c40..688c27b 100644 --- a/node/keepdiceexecnode.cpp +++ b/node/keepdiceexecnode.cpp @@ -58,9 +58,10 @@ void KeepDiceExecNode::run(ExecutionNode* previous) if(m_numberOfDice > static_cast(diceList.size())) { - m_errors.insert(TOO_MANY_DICE, QObject::tr(" You ask to keep %1 dice but the result only has %2") - .arg(m_numberOfDice) - .arg(diceList.size())); + m_errors.insert(Dice::ERROR_CODE::TOO_MANY_DICE, + QObject::tr(" You ask to keep %1 dice but the result only has %2") + .arg(m_numberOfDice) + .arg(diceList.size())); } for(auto& tmp : diceList.mid(static_cast(m_numberOfDice), -1)) diff --git a/node/listsetrollnode.cpp b/node/listsetrollnode.cpp index ffa0f03..565a609 100644 --- a/node/listsetrollnode.cpp +++ b/node/listsetrollnode.cpp @@ -62,11 +62,11 @@ void ListSetRollNode::run(ExecutionNode* previous) Result* result= previous->getResult(); if(nullptr != result) { - quint64 diceCount= result->getResult(Result::SCALAR).toReal(); + quint64 diceCount= result->getResult(Dice::RESULT_TYPE::SCALAR).toReal(); if(diceCount > static_cast(m_values.size()) && m_unique) { - m_errors.insert( - TOO_MANY_DICE, QObject::tr("More unique values asked than possible values (L operator)")); + m_errors.insert(Dice::ERROR_CODE::TOO_MANY_DICE, + QObject::tr("More unique values asked than possible values (L operator)")); } else { diff --git a/node/mergenode.cpp b/node/mergenode.cpp index 4a11a76..c4c1543 100644 --- a/node/mergenode.cpp +++ b/node/mergenode.cpp @@ -29,7 +29,7 @@ void MergeNode::run(ExecutionNode* previous) { if(nullptr == previous) { - m_errors.insert(ExecutionNode::NO_PREVIOUS_ERROR, QObject::tr("No previous node before Merge operator")); + m_errors.insert(Dice::ERROR_CODE::NO_PREVIOUS_ERROR, QObject::tr("No previous node before Merge operator")); return; } @@ -69,7 +69,7 @@ void MergeNode::run(ExecutionNode* previous) } } auto it= std::find_if(pastResult.begin(), pastResult.end(), - [tmpResult](const Result* a) { return (a == tmpResult->getPrevious()); }); + [tmpResult](const Result* a) { return (a == tmpResult->getPrevious()); }); if(it == pastResult.end()) { pastResult.push_back(previousLast->getResult()); diff --git a/node/occurencecountnode.cpp b/node/occurencecountnode.cpp index e0117ef..0d69d2a 100644 --- a/node/occurencecountnode.cpp +++ b/node/occurencecountnode.cpp @@ -20,6 +20,7 @@ #include "occurencecountnode.h" #include "result/diceresult.h" #include "result/stringresult.h" +#include OccurenceCountNode::OccurenceCountNode() : ExecutionNode() {} diff --git a/node/paintnode.cpp b/node/paintnode.cpp index 02c7230..fe5a324 100644 --- a/node/paintnode.cpp +++ b/node/paintnode.cpp @@ -61,7 +61,7 @@ void PainterNode::run(ExecutionNode* previous) m_previousNode= previous; if(nullptr == previous) { - m_errors.insert(ExecutionNode::NO_PREVIOUS_ERROR, QObject::tr("No previous node before Paint operator")); + m_errors.insert(Dice::ERROR_CODE::NO_PREVIOUS_ERROR, QObject::tr("No previous node before Paint operator")); return; } Result* previousResult= previous->getResult(); diff --git a/node/rerolldicenode.cpp b/node/rerolldicenode.cpp index 56ee6d9..18e8bba 100644 --- a/node/rerolldicenode.cpp +++ b/node/rerolldicenode.cpp @@ -79,7 +79,8 @@ void RerollDiceNode::run(ExecutionNode* previous) } else { - m_errors.insert(ExecutionNode::DIE_RESULT_EXPECTED, + m_errors.insert( + Dice::ERROR_CODE::DIE_RESULT_EXPECTED, QObject::tr( " The a operator expects dice result. Please check the documentation and fix your command.")); } diff --git a/node/scalaroperatornode.cpp b/node/scalaroperatornode.cpp index 3a26fc7..9edbcdc 100644 --- a/node/scalaroperatornode.cpp +++ b/node/scalaroperatornode.cpp @@ -69,28 +69,29 @@ void ScalarOperatorNode::run(ExecutionNode* previous) switch(m_arithmeticOperator) { case Die::PLUS: - m_scalarResult->setValue(add(previousResult->getResult(Result::SCALAR).toReal(), - internalResult->getResult(Result::SCALAR).toReal())); + m_scalarResult->setValue(add(previousResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal(), + internalResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal())); break; case Die::MINUS: - m_scalarResult->setValue(substract(previousResult->getResult(Result::SCALAR).toReal(), - internalResult->getResult(Result::SCALAR).toReal())); + m_scalarResult->setValue(substract(previousResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal(), + internalResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal())); break; case Die::MULTIPLICATION: - m_scalarResult->setValue(multiple(previousResult->getResult(Result::SCALAR).toReal(), - internalResult->getResult(Result::SCALAR).toReal())); + m_scalarResult->setValue(multiple(previousResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal(), + internalResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal())); break; case Die::DIVIDE: - m_scalarResult->setValue(divide(previousResult->getResult(Result::SCALAR).toReal(), - internalResult->getResult(Result::SCALAR).toReal())); + m_scalarResult->setValue(divide(previousResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal(), + internalResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal())); break; case Die::INTEGER_DIVIDE: - m_scalarResult->setValue(static_cast(divide(previousResult->getResult(Result::SCALAR).toReal(), - internalResult->getResult(Result::SCALAR).toReal()))); + m_scalarResult->setValue( + static_cast(divide(previousResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal(), + internalResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal()))); break; case Die::POW: - m_scalarResult->setValue(pow(previousResult->getResult(Result::SCALAR).toReal(), - internalResult->getResult(Result::SCALAR).toReal())); + m_scalarResult->setValue(pow(previousResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal(), + internalResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal())); break; } } @@ -128,7 +129,7 @@ qreal ScalarOperatorNode::divide(qreal a, qreal b) { if(qFuzzyCompare(b, 0)) { - m_errors.insert(DIVIDE_BY_ZERO, QObject::tr("Division by zero")); + m_errors.insert(Dice::ERROR_CODE::DIVIDE_BY_ZERO, QObject::tr("Division by zero")); return 0; } return static_cast(a / b); @@ -240,12 +241,12 @@ void ScalarOperatorNode::generateDotTree(QString& s) } s.append(str); } -QMap ScalarOperatorNode::getExecutionErrorMap() +QMap ScalarOperatorNode::getExecutionErrorMap() { if(nullptr != m_internalNode) { auto keys= m_internalNode->getExecutionErrorMap().keys(); - for(ExecutionNode::DICE_ERROR_CODE& key : keys) + for(const auto& key : keys) { m_errors.insert(key, m_internalNode->getExecutionErrorMap().value(key)); } @@ -253,7 +254,7 @@ QMap ScalarOperatorNode::getExecutionEr if(nullptr != m_nextNode) { auto keys= m_nextNode->getExecutionErrorMap().keys(); - for(ExecutionNode::DICE_ERROR_CODE& key : keys) + for(auto const& key : keys) { m_errors.insert(key, m_nextNode->getExecutionErrorMap().value(key)); } diff --git a/node/scalaroperatornode.h b/node/scalaroperatornode.h index 2ac89b0..a58a8d3 100644 --- a/node/scalaroperatornode.h +++ b/node/scalaroperatornode.h @@ -76,7 +76,7 @@ public: * @brief getErrorList * @return */ - virtual QMap getExecutionErrorMap(); + virtual QMap getExecutionErrorMap(); /** * @brief getArithmeticOperator * @return diff --git a/node/valueslistnode.cpp b/node/valueslistnode.cpp index b31ee84..d378350 100644 --- a/node/valueslistnode.cpp +++ b/node/valueslistnode.cpp @@ -16,7 +16,7 @@ void ValuesListNode::run(ExecutionNode* previous) auto result= node->getResult(); if(!result) continue; - auto val= result->getResult(Result::SCALAR).toInt(); + auto val= result->getResult(Dice::RESULT_TYPE::SCALAR).toInt(); Die* die= new Die(); auto dyna= dynamic_cast(node); if(nullptr != dyna) diff --git a/node/variablenode.cpp b/node/variablenode.cpp index 1d13a2c..e45214d 100644 --- a/node/variablenode.cpp +++ b/node/variablenode.cpp @@ -24,7 +24,7 @@ void VariableNode::run(ExecutionNode* previous) } else { - m_errors.insert(NO_VARIBALE, QObject::tr("No variable at index:%1").arg(m_index + 1)); + m_errors.insert(Dice::ERROR_CODE::NO_VARIBALE, QObject::tr("No variable at index:%1").arg(m_index + 1)); } } diff --git a/operationcondition.cpp b/operationcondition.cpp index 53a994d..67b10fc 100644 --- a/operationcondition.cpp +++ b/operationcondition.cpp @@ -131,5 +131,5 @@ qint64 OperationCondition::valueToScalar() const m_value->run(nullptr); auto result= m_value->getResult(); - return result->getResult(Result::SCALAR).toInt(); + return result->getResult(Dice::RESULT_TYPE::SCALAR).toInt(); } diff --git a/result/diceresult.cpp b/result/diceresult.cpp index e66fc4f..23b925e 100644 --- a/result/diceresult.cpp +++ b/result/diceresult.cpp @@ -25,7 +25,7 @@ DiceResult::DiceResult() : m_operator(Die::PLUS) { - m_resultTypes= (DICE_LIST | SCALAR); + m_resultTypes= (static_cast(Dice::RESULT_TYPE::DICE_LIST) | static_cast(Dice::RESULT_TYPE::SCALAR)); m_homogeneous= true; } void DiceResult::insertResult(Die* die) @@ -63,15 +63,15 @@ DiceResult::~DiceResult() m_diceValues.clear(); } } -QVariant DiceResult::getResult(RESULT_TYPE type) +QVariant DiceResult::getResult(Dice::RESULT_TYPE type) { switch(type) { - case SCALAR: + case Dice::RESULT_TYPE::SCALAR: { return getScalarResult(); } - case DICE_LIST: + case Dice::RESULT_TYPE::DICE_LIST: { return QVariant(); } diff --git a/result/diceresult.h b/result/diceresult.h index 54e0b57..ce8ffb7 100644 --- a/result/diceresult.h +++ b/result/diceresult.h @@ -61,7 +61,7 @@ public: * @brief getScalar * @return */ - virtual QVariant getResult(RESULT_TYPE); + virtual QVariant getResult(Dice::RESULT_TYPE); /** * @brief toString * @return diff --git a/result/result.cpp b/result/result.cpp index edd00e4..983f1f4 100644 --- a/result/result.cpp +++ b/result/result.cpp @@ -22,7 +22,10 @@ #include "result.h" #include -Result::Result() : m_resultTypes(NONE), m_id(QString("\"%1\"").arg(QUuid::createUuid().toString())), m_previous(nullptr) +Result::Result() + : m_resultTypes(static_cast(Dice::RESULT_TYPE::NONE)) + , m_id(QString("\"%1\"").arg(QUuid::createUuid().toString())) + , m_previous(nullptr) { } Result::~Result() {} @@ -43,9 +46,9 @@ bool Result::isStringResult() const return false; } void Result::clear() {} -bool Result::hasResultOfType(RESULT_TYPE type) const +bool Result::hasResultOfType(Dice::RESULT_TYPE type) const { - return (m_resultTypes & type); + return (m_resultTypes & static_cast(type)); } void Result::generateDotTree(QString& s) { diff --git a/result/result.h b/result/result.h index 8f31c05..a34c74f 100644 --- a/result/result.h +++ b/result/result.h @@ -22,7 +22,7 @@ #ifndef RESULT_H #define RESULT_H -//#include +#include "diceparserhelper.h" #include #include /** @@ -31,16 +31,6 @@ class Result { public: - /** - * @brief The RESULT_TYPE enum or combinaison - */ - enum RESULT_TYPE - { - NONE= 0, - SCALAR= 1, - STRING= 2, - DICE_LIST= 4 - }; /** * @brief Result */ @@ -54,12 +44,12 @@ public: * @brief isScalar * @return */ - virtual bool hasResultOfType(RESULT_TYPE) const; + virtual bool hasResultOfType(Dice::RESULT_TYPE) const; /** * @brief getScalar * @return */ - virtual QVariant getResult(RESULT_TYPE)= 0; + virtual QVariant getResult(Dice::RESULT_TYPE)= 0; /** * @brief getPrevious * @return diff --git a/result/scalarresult.cpp b/result/scalarresult.cpp index 089c4b8..ec2b5f0 100644 --- a/result/scalarresult.cpp +++ b/result/scalarresult.cpp @@ -21,18 +21,15 @@ ***************************************************************************/ #include "scalarresult.h" -ScalarResult::ScalarResult() -{ - m_resultTypes= Result::SCALAR; -} +ScalarResult::ScalarResult() {} void ScalarResult::setValue(qreal i) { m_value= i; } -QVariant ScalarResult::getResult(Result::RESULT_TYPE type) +QVariant ScalarResult::getResult(Dice::RESULT_TYPE type) { - if(SCALAR == type) + if(Dice::RESULT_TYPE::SCALAR == type) { return m_value; } diff --git a/result/scalarresult.h b/result/scalarresult.h index c20de0f..1eac73c 100644 --- a/result/scalarresult.h +++ b/result/scalarresult.h @@ -39,7 +39,7 @@ public: * @brief getResult * @return */ - virtual QVariant getResult(Result::RESULT_TYPE); + virtual QVariant getResult(Dice::RESULT_TYPE); /** * @brief setValue * @param i diff --git a/result/stringresult.cpp b/result/stringresult.cpp index a44e24e..d5a7880 100644 --- a/result/stringresult.cpp +++ b/result/stringresult.cpp @@ -3,20 +3,21 @@ StringResult::StringResult() { m_highlight= true; - m_resultTypes= Result::STRING; + m_resultTypes= static_cast(Dice::RESULT_TYPE::STRING); } void StringResult::setText(QString text) { m_value= text; } StringResult::~StringResult() {} -bool StringResult::hasResultOfType(RESULT_TYPE resultType) const +bool StringResult::hasResultOfType(Dice::RESULT_TYPE resultType) const { - if(resultType & Result::STRING) + + if(resultType == Dice::RESULT_TYPE::STRING) { return true; } - else if(resultType & Result::SCALAR) + else if(resultType == Dice::RESULT_TYPE::SCALAR) { bool ok= false; getText().toInt(&ok); @@ -28,13 +29,13 @@ QString StringResult::getText() const { return m_value; } -QVariant StringResult::getResult(RESULT_TYPE type) +QVariant StringResult::getResult(Dice::RESULT_TYPE type) { switch(type) { - case STRING: + case Dice::RESULT_TYPE::STRING: return getText(); - case SCALAR: + case Dice::RESULT_TYPE::SCALAR: return getText().toInt(); default: return QVariant(); diff --git a/result/stringresult.h b/result/stringresult.h index ec6c3ce..0e469fe 100644 --- a/result/stringresult.h +++ b/result/stringresult.h @@ -31,7 +31,7 @@ public: * @brief getScalar * @return */ - virtual QVariant getResult(RESULT_TYPE); + virtual QVariant getResult(Dice::RESULT_TYPE); /** * @brief toString * @return @@ -40,7 +40,7 @@ public: virtual void setHighLight(bool); virtual bool hasHighLight() const; - virtual bool hasResultOfType(RESULT_TYPE resultType) const; + virtual bool hasResultOfType(Dice::RESULT_TYPE resultType) const; virtual Result* getCopy() const; private: diff --git a/tests/dice/CMakeLists.txt b/tests/dice/CMakeLists.txt index 4065cc5..fef1a7b 100644 --- a/tests/dice/CMakeLists.txt +++ b/tests/dice/CMakeLists.txt @@ -5,5 +5,8 @@ find_package(Qt5Test REQUIRED) set(test_source testnode.cpp tst_dice.cpp) add_executable(test_dice ${test_source} ) + +target_include_directories(test_dice PRIVATE ../../include) + target_link_libraries(test_dice diceparser Qt5::Test) add_test(tst_diceparser test_dice) diff --git a/tests/dice/tst_dice.cpp b/tests/dice/tst_dice.cpp index 1fe2641..4da3474 100644 --- a/tests/dice/tst_dice.cpp +++ b/tests/dice/tst_dice.cpp @@ -524,7 +524,7 @@ void TestDice::keepTest() if(error) return; - auto resultScore= keepN.getResult()->getResult(Result::SCALAR).toInt(); + auto resultScore= keepN.getResult()->getResult(Dice::RESULT_TYPE::SCALAR).toInt(); QCOMPARE(score, resultScore); } @@ -609,7 +609,7 @@ void TestDice::countTest() node.run(nullptr); - QCOMPARE(score, countN.getResult()->getResult(Result::SCALAR).toInt()); + QCOMPARE(score, countN.getResult()->getResult(Dice::RESULT_TYPE::SCALAR).toInt()); countN.setValidator(nullptr); } -- cgit v1.2.3-70-g09d2 From 61497d06b2ec5ba576dad92d008de3f4835000d5 Mon Sep 17 00:00:00 2001 From: Renaud G Date: Sun, 28 Jul 2019 02:49:16 +0200 Subject: add tests --- tests/dice/tst_dice.cpp | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) (limited to 'tests/dice') diff --git a/tests/dice/tst_dice.cpp b/tests/dice/tst_dice.cpp index 4da3474..a35f918 100644 --- a/tests/dice/tst_dice.cpp +++ b/tests/dice/tst_dice.cpp @@ -43,6 +43,7 @@ #include "node/splitnode.h" #include "node/stringnode.h" #include "node/uniquenode.h" +#include "operationcondition.h" #include "result/stringresult.h" #include "testnode.h" @@ -131,6 +132,8 @@ private slots: void occurenceTest(); void occurenceTest_data(); + void operatoionConditionValidatorTest(); + private: std::unique_ptr m_die; std::unique_ptr m_diceParser; @@ -463,13 +466,13 @@ void TestDice::dangerousCommandsTest_data() // QTest::addRow("cmd5") << "10d10g10"; } -void makeResult(DiceResult& result, const QVector& values) +void makeResult(DiceResult& result, const QVector& values, int base= 1, int max= 10) { for(int val : values) { auto die= new Die(); - die->setBase(1); - die->setMaxValue(10); + die->setBase(base); + die->setMaxValue(max); die->insertRollValue(val); result.insertResult(die); } @@ -722,7 +725,7 @@ void TestDice::rerollUntilTest() RerollDiceNode reroll(false, false); DiceResult result; - makeResult(result, values); + makeResult(result, values, 0); node.setResult(&result); auto validator= makeValidator(condition, BooleanCondition::Equal); @@ -742,7 +745,6 @@ void TestDice::rerollUntilTest() resultDiff= true; ++i; } - QCOMPARE(different, resultDiff); } void TestDice::rerollUntilTest_data() @@ -1062,6 +1064,31 @@ void TestDice::ifCommandTest_data() << QStringList({"Complete Success:", "Success with Complications:", "Failure:"}); } +void TestDice::operatoionConditionValidatorTest() +{ + OperationCondition validator; + NumberNode number; + number.setNumber(2); + validator.setValueNode(&number); + + BooleanCondition subValidator; + subValidator.setOperator(BooleanCondition::Equal); + NumberNode subnumber; + subnumber.setNumber(0); + subValidator.setValueNode(&subnumber); + + validator.setBoolean(&subValidator); + + std::set data= {2, 4, 6, 8, 10}; + auto value= validator.getPossibleValues(std::make_pair(1, 10)); + + subValidator.setValueNode(nullptr); + validator.setValueNode(nullptr); + validator.setBoolean(nullptr); + + QCOMPARE(value, data); +} + void TestDice::cleanupTestCase() {} QTEST_MAIN(TestDice) -- cgit v1.2.3-70-g09d2 From f1a5dd3a90c8a363be9489f8f582a94d15bebc8f Mon Sep 17 00:00:00 2001 From: Renaud G Date: Sun, 28 Jul 2019 02:49:48 +0200 Subject: Compile source instead of against lib --- tests/dice/CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests/dice') diff --git a/tests/dice/CMakeLists.txt b/tests/dice/CMakeLists.txt index fef1a7b..d254624 100644 --- a/tests/dice/CMakeLists.txt +++ b/tests/dice/CMakeLists.txt @@ -4,9 +4,9 @@ find_package(Qt5Test REQUIRED) set(test_source testnode.cpp tst_dice.cpp) -add_executable(test_dice ${test_source} ) +add_executable(test_dice ${test_source} ${dice_sources}) target_include_directories(test_dice PRIVATE ../../include) -target_link_libraries(test_dice diceparser Qt5::Test) +target_link_libraries(test_dice Qt5::Test) add_test(tst_diceparser test_dice) -- cgit v1.2.3-70-g09d2