summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIvan Lakhtanov <ivan.lakhtanov@gmail.com>2016-08-20 20:24:26 (GMT)
committerIvan Lakhtanov <ivan.lakhtanov@gmail.com>2016-08-24 18:44:30 (GMT)
commit890616f6acd65956d5ccfadb140b52de4b9176c8 (patch)
tree180518edf85c49fa596d7b0ba321c1d2c014f7ee
parent7e6af394e4cbf34afd9041dfcfef80c42c1f96c7 (diff)
Added unittests
Covers command execution, multiline, exceptions, syntax errors and partial results Differential Revision: https://phabricator.kde.org/D2008
-rw-r--r--src/backends/julia/CMakeLists.txt1
-rw-r--r--src/backends/julia/tests/CMakeLists.txt4
-rw-r--r--src/backends/julia/tests/testjulia.cpp122
-rw-r--r--src/backends/julia/tests/testjulia.h37
4 files changed, 164 insertions, 0 deletions
diff --git a/src/backends/julia/CMakeLists.txt b/src/backends/julia/CMakeLists.txt
index 8739ac8..c7d4dc2 100644
--- a/src/backends/julia/CMakeLists.txt
+++ b/src/backends/julia/CMakeLists.txt
@@ -1,4 +1,5 @@
add_subdirectory(juliaserver)
+add_subdirectory(tests)
set(JuliaBackend_SRCS
juliabackend.cpp
diff --git a/src/backends/julia/tests/CMakeLists.txt b/src/backends/julia/tests/CMakeLists.txt
new file mode 100644
index 0000000..d26b7ec
--- /dev/null
+++ b/src/backends/julia/tests/CMakeLists.txt
@@ -0,0 +1,4 @@
+add_executable(testjulia testjulia.cpp)
+target_link_libraries(testjulia ${QT_QTTEST_LIBRARY} cantorlibs cantortest)
+
+add_test(NAME testjulia COMMAND testjulia)
diff --git a/src/backends/julia/tests/testjulia.cpp b/src/backends/julia/tests/testjulia.cpp
new file mode 100644
index 0000000..9032b8d
--- /dev/null
+++ b/src/backends/julia/tests/testjulia.cpp
@@ -0,0 +1,122 @@
+/*
+ * 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.
+ *
+ * ---
+ * Copyright (C) 2016 Ivan Lakhtanov <ivan.lakhtanov@gmail.com>
+ */
+#include "testjulia.h"
+
+#include "session.h"
+#include "backend.h"
+#include "expression.h"
+#include "result.h"
+#include "textresult.h"
+
+QString TestJulia::backendName()
+{
+ return QLatin1String("julia");
+}
+
+void TestJulia::testOneLine()
+{
+ Cantor::Expression *e = evalExp(QLatin1String("2 + 3"));
+ QVERIFY(e != nullptr);
+ QCOMPARE(e->status(), Cantor::Expression::Done);
+ QVERIFY(e->result()->type() == Cantor::TextResult::Type);
+ QCOMPARE(e->result()->data().toString(), QLatin1String("5"));
+ QVERIFY(e->errorMessage().isEmpty());
+}
+
+void TestJulia::testOneLineWithPrint()
+{
+ Cantor::Expression *e = evalExp(QLatin1String("print(2 + 3)"));
+ QVERIFY(e != nullptr);
+ QCOMPARE(e->status(), Cantor::Expression::Done);
+ QVERIFY(e->result()->type() == Cantor::TextResult::Type);
+ QCOMPARE(e->result()->data().toString(), QLatin1String("5"));
+ QVERIFY(e->errorMessage().isEmpty());
+}
+
+void TestJulia::testException()
+{
+ Cantor::Expression *e = evalExp(QLatin1String("sqrt(-1)"));
+ QVERIFY(e != nullptr);
+ QCOMPARE(e->status(), Cantor::Expression::Error);
+ QVERIFY(e->result()->type() == Cantor::TextResult::Type);
+ QVERIFY(
+ not e->errorMessage().isEmpty()
+ and e->errorMessage().contains(QLatin1String(
+ "sqrt will only return a complex result if called with a "
+ "complex argument. Try sqrt(complex(x))."
+ ))
+ );
+}
+
+void TestJulia::testSyntaxError()
+{
+ Cantor::Expression *e = evalExp(QLatin1String("for i = 1:10\nprint(i)"));
+ QVERIFY(e != nullptr);
+ QCOMPARE(e->status(), Cantor::Expression::Error);
+ QVERIFY(e->result()->type() == Cantor::TextResult::Type);
+ QVERIFY(
+ not e->errorMessage().isEmpty()
+ and e->errorMessage().contains(QLatin1String(
+ "syntax: incomplete: \"for\" at none:1 requires end"
+ ))
+ );
+}
+
+void TestJulia::testMultilineCode()
+{
+ Cantor::Expression *e = evalExp(QLatin1String(
+ "q = 0; # comment\n"
+ "# sdfsdf\n"
+ "for i = 1:10\n"
+ " q += i\n"
+ "end\n"
+ "print(q)"
+ ));
+ QVERIFY(e != nullptr);
+ QCOMPARE(e->status(), Cantor::Expression::Done);
+ QVERIFY(e->result()->type() == Cantor::TextResult::Type);
+ QCOMPARE(e->result()->data().toString(), QLatin1String("55"));
+ QVERIFY(e->errorMessage().isEmpty());
+}
+
+void TestJulia::testPartialResultOnException()
+{
+ Cantor::Expression *e = evalExp(QLatin1String(
+ "for i = 1:5\n"
+ " print(i)\n"
+ "end\n"
+ "sqrt(-1)\n"
+ ));
+ QVERIFY(e != nullptr);
+ QCOMPARE(e->status(), Cantor::Expression::Error);
+ QVERIFY(e->result()->type() == Cantor::TextResult::Type);
+ QCOMPARE(e->result()->data().toString(), QLatin1String("12345"));
+ QVERIFY(
+ not e->errorMessage().isEmpty()
+ and e->errorMessage().contains(QLatin1String(
+ "sqrt will only return a complex result if called with a "
+ "complex argument. Try sqrt(complex(x))."
+ ))
+ );
+}
+
+QTEST_MAIN(TestJulia)
+
+#include "testjulia.moc"
diff --git a/src/backends/julia/tests/testjulia.h b/src/backends/julia/tests/testjulia.h
new file mode 100644
index 0000000..7ed4a87
--- /dev/null
+++ b/src/backends/julia/tests/testjulia.h
@@ -0,0 +1,37 @@
+/*
+ * 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.
+ *
+ * ---
+ * Copyright (C) 2016 Ivan Lakhtanov <ivan.lakhtanov@gmail.com>
+ */
+#pragma once
+
+#include <backendtest.h>
+
+class TestJulia: public BackendTest
+{
+ Q_OBJECT
+private Q_SLOTS:
+ void testOneLine();
+ void testOneLineWithPrint();
+ void testException();
+ void testMultilineCode();
+ void testSyntaxError();
+ void testPartialResultOnException();
+
+private:
+ virtual QString backendName();
+};