Commit 03a0e82d authored by Alexander Semke's avatar Alexander Semke
Browse files

[maxima] properly parse plot and draw commands with line breaks.

parent f5f8319b
Pipeline #207847 passed with stage
in 5 minutes and 4 seconds
......@@ -157,11 +157,19 @@ QString MaximaExpression::internalCommand()
const QString plotParameters = QLatin1String("[pdf_file, \"") + fileName + QLatin1String("\"], ") + pdfParam;
*/
//replace all newlines with spaces, as maxima isn't sensitive about
//whitespaces, and without newlines the whole command
//is executed at once, without outputting an input
//prompt after each line.
// Also, this helps to handle plot/draw commands with line breaks that are not properly handled by the regex below.
cmd.replace(QLatin1Char('\n'), QLatin1Char(' '));
if (!m_isDraw)
{
const QString plotParameters = QLatin1String("[gnuplot_png_term_command, \"set term png size 500,340\"], [png_file, \"") + m_tempFile->fileName() + QLatin1String("\"]");
cmd.replace(QRegularExpression(QStringLiteral("((plot2d|plot3d|contour_plot)\\s*\\(.*)\\)([;\n$]|$)")),
QLatin1String("\\1, ") + plotParameters + QLatin1String(");"));
}
else
{
......@@ -181,12 +189,6 @@ QString MaximaExpression::internalCommand()
cmd+=QLatin1Char(';');
}
//replace all newlines with spaces, as maxima isn't sensitive about
//whitespaces, and without newlines the whole command
//is executed at once, without outputting an input
//prompt after each line
cmd.replace(QLatin1Char('\n'), QLatin1Char(' '));
//lisp-quiet doesn't print a prompt after the command
//is completed, which causes the parsing to hang.
//replace the command with the non-quiet version
......
......@@ -70,6 +70,28 @@ void TestMaxima::testPlot()
QVERIFY( e->errorMessage().isNull() );
}
void TestMaxima::testPlotMultiline()
{
if(QStandardPaths::findExecutable(QLatin1String("gnuplot")).isNull())
QSKIP("gnuplot not found, maxima needs it for plotting", SkipSingle);
auto* e = evalExp(QLatin1String(
"plot2d (x^2-y^3+3*y=2,\n"
"[x,-2.5,2.5],\n"
"[y,-2.5,2.5])"
));
QVERIFY(e != nullptr);
QVERIFY(e->result() != nullptr);
if(!e->result())
waitForSignal(e, SIGNAL(gotResult()));
QCOMPARE(e->result()->type(), (int)Cantor::ImageResult::Type);
QVERIFY(!e->result()->data().isNull());
QVERIFY(e->errorMessage().isNull());
}
void TestMaxima::testPlotWithAnotherTextResults()
{
if(QStandardPaths::findExecutable(QLatin1String("gnuplot")).isNull())
......@@ -120,6 +142,41 @@ void TestMaxima::testDraw()
QVERIFY( e->errorMessage().isNull() );
}
void TestMaxima::testDrawMultiline()
{
if(QStandardPaths::findExecutable(QLatin1String("gnuplot")).isNull())
QSKIP("gnuplot not found, maxima needs it for plotting", SkipSingle);
auto* e = evalExp( QLatin1String(
"draw(\n"
"gr2d(\n"
"key=\"sin (x)\",grid=[2,2],\n"
"explicit(\n"
"sin(x),\n"
"x,0,2*%pi\n"
")\n"
"),\n"
"gr2d(\n"
"key=\"cos (x)\",grid=[2,2],\n"
"explicit(\n"
"cos(x),\n"
"x,0,2*%pi\n"
")\n"
"))"
));
QVERIFY(e != nullptr);
QVERIFY(e->result() != nullptr);
if(!e->result())
waitForSignal(e, SIGNAL(gotResult()));
QCOMPARE(e->result()->type(), (int)Cantor::ImageResult::Type);
QVERIFY(!e->result()->data().isNull());
QVERIFY(e->errorMessage().isNull());
}
void TestMaxima::testDrawWithAnotherTextResults()
{
if(QStandardPaths::findExecutable(QLatin1String("gnuplot")).isNull())
......@@ -148,7 +205,6 @@ void TestMaxima::testDrawWithAnotherTextResults()
QCOMPARE(e->results().at(2)->data().toString(), QLatin1String("16"));
}
void TestMaxima::testInvalidSyntax()
{
auto* e=evalExp( QLatin1String("2+2*(") );
......
......@@ -25,10 +25,13 @@ private Q_SLOTS:
void testMultilineCommand();
//tests if the command queue works correctly
void testCommandQueue();
//tests doing a plot
void testPlot();
void testPlotMultiline();
void testPlotWithAnotherTextResults();
void testDraw();
void testDrawMultiline();
void testDrawWithAnotherTextResults();
/* errors and warnings */
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment