File indexing completed on 2023-10-03 06:50:42
0001 /* 0002 SPDX-License-Identifier: GPL-2.0-or-later 0003 SPDX-FileCopyrightText: 2018 Nikita Sirgienko <warquark@gmail.com> 0004 SPDX-FileCopyrightText: 2023 Alexander Semke <alexander.semke@web.de> 0005 */ 0006 0007 #include "testlua.h" 0008 0009 #include "session.h" 0010 #include "result.h" 0011 #include "luaexpression.h" 0012 0013 QString TestLua::backendName() 0014 { 0015 return QLatin1String("lua"); 0016 } 0017 0018 void TestLua::testSimpleCommand() 0019 { 0020 auto* e = evalExp( QLatin1String("print(2+2)\n") ); 0021 0022 QVERIFY(e != nullptr); 0023 QVERIFY(e->result() != nullptr); 0024 0025 QCOMPARE(cleanOutput(e->result()->data().toString() ), QLatin1String("4")); 0026 } 0027 0028 void TestLua::testMultilineCommand01() 0029 { 0030 auto* e = evalExp(QLatin1String("print(4+4); print(2-1)")); 0031 0032 QVERIFY(e != nullptr); 0033 QVERIFY(e->errorMessage().isNull()); 0034 QCOMPARE(e->results().size(), 2); 0035 0036 QCOMPARE(e->results().at(0)->data().toString(), QLatin1String("8")); 0037 QCOMPARE(e->results().at(1)->data().toString(), QLatin1String("1")); 0038 } 0039 0040 /*! 0041 * test multiple assignments with comments and with multi-line strings 0042 */ 0043 void TestLua::testMultilineCommand02() 0044 { 0045 QSKIP("Works in Cantor, doesn't work in the test"); 0046 auto* e = evalExp(QLatin1String( 0047 "s = 'walternate' -- Immutable strings like Python.\n" 0048 "t = \"double-quotes are also fine\"\n" 0049 "u = [[ Double brackets\n" 0050 " start and end\n" 0051 " multi-line strings.]]\n" 0052 "print(u)\n" 0053 )); 0054 0055 QVERIFY(e != nullptr); 0056 QVERIFY(e->errorMessage().isNull()); 0057 QCOMPARE(e->results().size(), 1); 0058 0059 QCOMPARE(e->results().at(0)->data().toString(), 0060 QLatin1String( 0061 "Double brackets" 0062 " start and end" 0063 " multi-line strings." 0064 )); 0065 } 0066 0067 void TestLua::testVariableDefinition() 0068 { 0069 auto* e = evalExp( QLatin1String("num = 42; print(num)") ); 0070 0071 QVERIFY(e != nullptr); 0072 QVERIFY(e->result() != nullptr); 0073 0074 QCOMPARE(cleanOutput(e->result()->data().toString()), QLatin1String("42")); 0075 } 0076 0077 void TestLua::testInvalidSyntax() 0078 { 0079 QSKIP("Works in Cantor, doesn't work in the test"); 0080 auto* e = evalExp( QLatin1String("2+2*+.") ); 0081 0082 QVERIFY(e != nullptr); 0083 0084 waitForSignal(e, SIGNAL(statusChanged(Cantor::Expression::Status))); 0085 QCOMPARE(e->status(), Cantor::Expression::Done); 0086 0087 if (e->status() != Cantor::Expression::Error) 0088 waitForSignal(e, SIGNAL(statusChanged(Cantor::Expression::Status))); 0089 0090 QCOMPARE(e->status(), Cantor::Expression::Error); 0091 } 0092 0093 void TestLua::testIfElseCondition() 0094 { 0095 QLatin1String cmd( 0096 "if 12 > 50 then" 0097 " print('true')" 0098 "else" 0099 " print('false')" 0100 "end"); 0101 0102 auto* e = evalExp(cmd); 0103 0104 QVERIFY(e != nullptr); 0105 QVERIFY(e->result() != nullptr); 0106 0107 QCOMPARE(cleanOutput(e->result()->data().toString()), QLatin1String("false")); 0108 } 0109 0110 void TestLua::testForLoop() 0111 { 0112 QSKIP("Works in Cantor, doesn't work in the test"); 0113 QLatin1String cmd( 0114 "karlSum = 0""\n" 0115 "for i = 1, 100 do""\n" 0116 " karlSum = karlSum + i""\n" 0117 "end""\n" 0118 "print(karlSum)"); 0119 0120 auto* e = evalExp(cmd); 0121 0122 QVERIFY( e!=nullptr ); 0123 QVERIFY( e->result()!=nullptr ); 0124 0125 QCOMPARE( cleanOutput(e->result()->data().toString()), QLatin1String("5050") ); 0126 } 0127 0128 void TestLua::testWhileLoop() 0129 { 0130 QSKIP("Works in Cantor, doesn't work in the test"); 0131 auto* e = evalExp(QLatin1String( 0132 "num = 42\n" 0133 "print(num)\n" 0134 "while num < 50 do\n" 0135 " num = num + 1 -- No ++ or += type operators.\n" 0136 "end\n" 0137 "print(num)\n" 0138 )); 0139 0140 if (e->status() != Cantor::Expression::Done) 0141 waitForSignal(e, SIGNAL(statusChanged(Cantor::Expression::Status))); 0142 0143 QVERIFY(e != nullptr); 0144 QVERIFY(e->errorMessage().isNull()); 0145 QCOMPARE(e->results().size(), 2); 0146 0147 QCOMPARE(e->results().at(0)->data().toString(), QLatin1String("42")); 0148 QCOMPARE(e->results().at(1)->data().toString(), QLatin1String("50")); 0149 } 0150 0151 void TestLua::testFunction() 0152 { 0153 QSKIP("Works in Cantor, doesn't work in the test"); 0154 QLatin1String cmd( 0155 "function max(num1, num2)\n" 0156 "if (num1 > num2) then\n" 0157 "result = num1;\n" 0158 "else\n" 0159 "result = num2;\n" 0160 "end\n" 0161 "return result;\n" 0162 "end"); 0163 0164 auto* e = evalExp(cmd); 0165 0166 if (e->status() != Cantor::Expression::Done) 0167 waitForSignal(e, SIGNAL(statusChanged(Cantor::Expression::Status))); 0168 0169 QVERIFY(e!=nullptr); 0170 QVERIFY(e->result() == nullptr); 0171 0172 cmd = QLatin1String("print(max(5,10));"); 0173 auto* e2 = evalExp(cmd); 0174 0175 if (e2->status() != Cantor::Expression::Done) 0176 waitForSignal(e2, SIGNAL(statusChanged(Cantor::Expression::Status))); 0177 0178 QVERIFY( e2!=nullptr ); 0179 QVERIFY( e2->result()!=nullptr ); 0180 QCOMPARE( cleanOutput(e2->result()->data().toString()), QLatin1String("10") ); 0181 } 0182 0183 QTEST_MAIN( TestLua )