Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract parse() function, and add unittest for Diceparser #15

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "webserver/qhttp"]
path = webserver/qhttp
url = https://github.com/azadkuh/qhttp.git
[submodule "Catch2"]
path = Catch2
url = [email protected]:catchorg/Catch2.git
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ add_subdirectory(irc)
add_subdirectory(cli)
add_subdirectory(mobile)
#add_subdirectory(webserver)
add_subdirectory(Catch2)
add_subdirectory(unittest)


#qt5_use_modules()
Expand Down
1 change: 1 addition & 0 deletions Catch2
Submodule Catch2 added at 7abd7d
1 change: 1 addition & 0 deletions cli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ set(MODE "cli")

ADD_DEFINITIONS(
-std=c++11 # Or -std=c++0x
-g
# Other flags
)

Expand Down
69 changes: 69 additions & 0 deletions cli/displaytoolbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,74 @@ class DisplayToolBox
static QString diceResultToString(QJsonObject val);
};

struct Parsed {
int isValid;
QList<ExportedDiceResult> diceResult;
QString result;
QString comment;
QString error;
QString scalarText;
};

inline
Parsed parse(DiceParser& parser, QString cmd)
{
Parsed parsed;

parsed.isValid = parser.parseLine(cmd);
if (!parsed.isValid) {
return parsed;
}

parser.start();

bool homogeneous = true;
parser.getLastDiceResult(parsed.diceResult,homogeneous);

QString lastScalarText;
QStringList strLst;

if(parser.hasIntegerResultNotInFirst())
{
auto values = parser.getLastIntegerResults();
for(auto val : values )
{
strLst << QString::number(val);
}
parsed.scalarText = QString("%1").arg(strLst.join(','));
lastScalarText = strLst.last();
}
else if(!parsed.diceResult.isEmpty())
{
auto values = parser.getSumOfDiceResult();
for(auto val : values )
{
strLst << QString::number(val);
}
parsed.scalarText = QString("%1").arg(strLst.join(','));
}

if(parser.hasStringResult())
{
bool ok; // FIXME: use me
parsed.result = parser.getAllStringResult(ok)
.join(" ; ")
.replace("%1",parsed.scalarText)
.replace("%3",lastScalarText);

int i = 1;
for(auto value : strLst)
{
parsed.result.replace(QStringLiteral("$%1").arg(i),value);
++i;
}
}

parsed.comment = parser.getComment();
parsed.error = parser.humanReadableError();

return parsed;
}


#endif // GENERATEIMAGE_H
123 changes: 37 additions & 86 deletions cli/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
QTextStream out(stdout, QIODevice::WriteOnly);
bool markdown = false;
enum EXPORTFORMAT {TERMINAL, SVG, IMAGE, MARKDOWN, JSON, BOT};
int returnValue = 0;


QString diceToMarkdown(QJsonArray array,bool withColor,bool allSameColor,bool allSameFaceCount )
Expand Down Expand Up @@ -208,103 +207,56 @@ int startDiceParsing(QStringList& cmds,QString& treeFile,bool withColor, EXPORTF
int rt=0;
for(QString cmd : cmds)
{
if(parser.parseLine(cmd))
{
parser.start();
QList<ExportedDiceResult> list;
bool homogeneous = true;
parser.getLastDiceResult(list,homogeneous);
bool allSameFaceCount, allSameColor;
auto array = DisplayToolBox::diceToJson(list,allSameFaceCount,allSameColor);
QString resultStr;
QString scalarText;
QString lastScalarText;
QString comment = parser.getComment();
QString error = parser.humanReadableError();
QStringList strLst;
Parsed parsed = parse(parser, cmd);

if(parser.hasIntegerResultNotInFirst())
{
auto values = parser.getLastIntegerResults();
for(auto val : values )
{
strLst << QString::number(val);
}
scalarText = QString("%1").arg(strLst.join(','));
lastScalarText = strLst.last();
}
else if(!list.isEmpty())
if(!parsed.isValid) {
rt = parsed.isValid; // FIXME: a new error can override a previous error
continue;
}

bool allSameFaceCount, allSameColor;
auto array = DisplayToolBox::diceToJson(parsed.diceResult,allSameFaceCount,allSameColor);

if(format == BOT)
{
if(allSameColor)
{
auto values = parser.getSumOfDiceResult();
for(auto val : values )
{
strLst << QString::number(val);
}
scalarText = QString("%1").arg(strLst.join(','));
format = MARKDOWN;
}

if(parser.hasStringResult())
else
{
bool ok;
QStringList allStringlist = parser.getAllStringResult(ok);
QString stringResult = allStringlist.join(" ; ");
stringResult.replace("%1",scalarText);
stringResult.replace("%3",lastScalarText);

int i = 1;
for(auto value : strLst)
{
stringResult.replace(QStringLiteral("$%1").arg(i),value);
++i;
}

resultStr = stringResult;
format = IMAGE;
}
if(format == BOT)
if(!parsed.error.isEmpty())
{
if(allSameColor)
{
format = MARKDOWN;
}
else
{
format = IMAGE;
}
if(!error.isEmpty())
{
format = MARKDOWN;
}
format = MARKDOWN;
}
}

switch(format)
{
case TERMINAL:
displayCommandResult(scalarText, resultStr, array, withColor, cmd, error, comment, allSameFaceCount, allSameColor);
switch(format)
{
case TERMINAL:
displayCommandResult(parsed.scalarText, parsed.result, array, withColor, cmd, parsed.error, parsed.comment, allSameFaceCount, allSameColor);
break;
case SVG:
displaySVG(scalarText, resultStr, array, withColor, cmd, error, comment, allSameFaceCount, allSameColor);
case SVG:
displaySVG(parsed.scalarText, parsed.result, array, withColor, cmd, parsed.error, parsed.comment, allSameFaceCount, allSameColor);
break;
case MARKDOWN:
displayMarkdown(scalarText, resultStr, array, withColor, cmd, error, comment, allSameFaceCount, allSameColor);
case MARKDOWN:
displayMarkdown(parsed.scalarText, parsed.result, array, withColor, cmd, parsed.error, parsed.comment, allSameFaceCount, allSameColor);
break;
case JSON:
displayJSon(scalarText, resultStr, array, withColor, cmd, error, comment, allSameFaceCount, allSameColor);
case JSON:
displayJSon(parsed.scalarText, parsed.result, array, withColor, cmd, parsed.error, parsed.comment, allSameFaceCount, allSameColor);
break;
case IMAGE:
displayImage(scalarText, resultStr, array, withColor, cmd, comment, allSameFaceCount, allSameColor);
case IMAGE:
displayImage(parsed.scalarText, parsed.result, array, withColor, cmd, parsed.comment, allSameFaceCount, allSameColor);
break;
}
if(!treeFile.isEmpty())
{
parser.writeDownDotTree(treeFile);
}

if(!error.isEmpty())
{
rt = 1;
}
}
else
if(!treeFile.isEmpty())
{
parser.writeDownDotTree(treeFile);
}

if(!parsed.error.isEmpty())
{
rt = 1;
}
Expand Down Expand Up @@ -404,10 +356,9 @@ int main(int argc, char *argv[])
{
aliasstr = optionParser.value(alias);
}
returnValue = startDiceParsing(cmdList,dotFileStr,colorb,format);
if(optionParser.isSet(help))
{
out << optionParser.helpText();
}
return returnValue;
return startDiceParsing(cmdList,dotFileStr,colorb,format);
}
67 changes: 19 additions & 48 deletions die.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,18 @@
#include <QDebug>
#include <chrono>

Die::Die()
: m_hasValue(false),m_displayStatus(false),m_highlighted(true),m_base(1),m_color(""),m_op(Die::PLUS)//,m_mt(m_randomDevice)
{
// uint seed = quintptr(this) + QDateTime::currentDateTime().toMSecsSinceEpoch();

// qsrand(seed);

auto seed = std::chrono::high_resolution_clock::now().time_since_epoch().count();
m_rng = std::mt19937(quintptr(this)+seed);
std::mt19937 DefaultRandomGenerator::m_rng = std::mt19937(std::chrono::high_resolution_clock::now().time_since_epoch().count());
std::shared_ptr<RandomGenerator> Die::m_rng = std::make_shared<DefaultRandomGenerator>();

void Die::setRandomGenerator(std::shared_ptr<RandomGenerator>&& r)
{
Die::m_rng = r;
}
Die::Die(const Die& die)

Die::Die(qint64 min, qint64 max)
: m_hasValue(false),m_displayStatus(false),m_highlighted(true),m_min(min),m_max(max),m_color(""),m_op(Die::PLUS)
{
m_value = die.m_value;
m_rollResult = die.m_rollResult;
m_selected = die.m_selected;
m_hasValue = die.m_hasValue;
m_displayStatus = die.m_displayStatus;
m_maxValue = die.m_maxValue;
m_highlighted = die.m_highlighted;
m_base = die.m_base;
m_color = die.getColor();
m_op = die.getOp();
assert(min<=max);
}

void Die::setValue(qint64 r)
Expand Down Expand Up @@ -134,26 +123,21 @@ void Die::replaceLastValue(qint64 value)

void Die::roll(bool adding)
{
if(m_maxValue!=0)
std::uniform_int_distribution<qint64> dist(m_min,m_max);
qint64 value = dist(*m_rng);
if((adding)||(m_rollResult.isEmpty()))
{
//quint64 value=(qrand()%m_faces)+m_base;

std::uniform_int_distribution<qint64> dist(m_base,m_maxValue);
qint64 value = dist(m_rng);
if((adding)||(m_rollResult.isEmpty()))
{
insertRollValue(value);
}
else
{
replaceLastValue(value);
}
insertRollValue(value);
}
else
{
replaceLastValue(value);
}
}

quint64 Die::getFaces() const
{
return abs(m_maxValue-m_base)+1;
return abs(m_max-m_min)+1;
}
qint64 Die::getLastRolledValue()
{
Expand Down Expand Up @@ -181,14 +165,6 @@ bool Die::isHighlighted() const
{
return m_highlighted;
}
void Die::setBase(qint64 base)
{
m_base = base;
}
qint64 Die::getBase()
{
return m_base;
}
QString Die::getColor() const
{
return m_color;
Expand All @@ -201,12 +177,7 @@ void Die::setColor(const QString &color)

qint64 Die::getMaxValue() const
{
return m_maxValue;
}

void Die::setMaxValue(const qint64 &maxValue)
{
m_maxValue = maxValue;
return m_max;
}

Die::ArithmeticOperator Die::getOp() const
Expand Down
Loading