Changeset View
Changeset View
Standalone View
Standalone View
src/backends/julia/tests/testjulia.cpp
Show All 18 Lines | |||||
19 | */ | 19 | */ | ||
20 | #include "testjulia.h" | 20 | #include "testjulia.h" | ||
21 | 21 | | |||
22 | #include "session.h" | 22 | #include "session.h" | ||
23 | #include "backend.h" | 23 | #include "backend.h" | ||
24 | #include "expression.h" | 24 | #include "expression.h" | ||
25 | #include "result.h" | 25 | #include "result.h" | ||
26 | #include "textresult.h" | 26 | #include "textresult.h" | ||
27 | #include "imageresult.h" | ||||
28 | #include "defaultvariablemodel.h" | ||||
29 | #include "completionobject.h" | ||||
27 | 30 | | |||
28 | QString TestJulia::backendName() | 31 | QString TestJulia::backendName() | ||
29 | { | 32 | { | ||
30 | return QLatin1String("julia"); | 33 | return QLatin1String("julia"); | ||
31 | } | 34 | } | ||
32 | 35 | | |||
33 | void TestJulia::testOneLine() | 36 | void TestJulia::testOneLine() | ||
34 | { | 37 | { | ||
▲ Show 20 Lines • Show All 77 Lines • ▼ Show 20 Line(s) | 114 | QVERIFY( | |||
112 | not e->errorMessage().isEmpty() | 115 | not e->errorMessage().isEmpty() | ||
113 | and e->errorMessage().contains(QLatin1String( | 116 | and e->errorMessage().contains(QLatin1String( | ||
114 | "sqrt will only return a complex result if called with a " | 117 | "sqrt will only return a complex result if called with a " | ||
115 | "complex argument. Try sqrt(complex(x))." | 118 | "complex argument. Try sqrt(complex(x))." | ||
116 | )) | 119 | )) | ||
117 | ); | 120 | ); | ||
118 | } | 121 | } | ||
119 | 122 | | |||
123 | void TestJulia::testInlinePlot() | ||||
124 | { | ||||
125 | Cantor::Expression *e = evalExp(QLatin1String( | ||||
126 | "import GR\n" | ||||
127 | "GR.plot(linspace(-1, 1), sin(linspace(-1, 1)))\n" | ||||
128 | )); | ||||
129 | QVERIFY(e != nullptr); | ||||
130 | QCOMPARE(e->status(), Cantor::Expression::Done); | ||||
131 | QVERIFY(e->result()->type() == Cantor::ImageResult::Type); | ||||
132 | } | ||||
133 | | ||||
134 | void TestJulia::testInlinePlotWithExceptionAndPartialResult() | ||||
135 | { | ||||
136 | Cantor::Expression *e = evalExp(QLatin1String( | ||||
137 | "import GR\n" | ||||
138 | "print(\"gonna plot\")\n" | ||||
139 | "sqrt(-1)\n" | ||||
140 | "GR.plot(linspace(-1, 1), sin(linspace(-1, 1)))\n" | ||||
141 | )); | ||||
142 | QVERIFY(e != nullptr); | ||||
143 | QCOMPARE(e->status(), Cantor::Expression::Error); | ||||
144 | QVERIFY(e->result()->type() == Cantor::TextResult::Type); | ||||
145 | QCOMPARE(e->result()->data().toString(), QLatin1String("gonna plot")); | ||||
146 | QVERIFY( | ||||
147 | not e->errorMessage().isEmpty() | ||||
148 | and e->errorMessage().contains(QLatin1String( | ||||
149 | "sqrt will only return a complex result if called with a " | ||||
150 | "complex argument. Try sqrt(complex(x))." | ||||
151 | )) | ||||
152 | ); | ||||
153 | } | ||||
154 | | ||||
155 | void TestJulia::testAddVariablesFromCode() | ||||
156 | { | ||||
157 | evalExp(QLatin1String("a = 0; b = 1; c = 2; d = 3\n")); | ||||
158 | | ||||
159 | auto variableModel = session()->variableModel(); | ||||
160 | QStringList variableNames = | ||||
161 | QString::fromLatin1("a b c d").split(QLatin1String(" ")); | ||||
162 | | ||||
163 | for (int i = 0; i < variableNames.size(); i++) { | ||||
164 | QModelIndexList matchedVariables = variableModel->match( | ||||
165 | variableModel->index(0, 0), | ||||
166 | Qt::DisplayRole, | ||||
167 | QVariant::fromValue(variableNames[i]), | ||||
168 | -1, | ||||
169 | Qt::MatchExactly | ||||
170 | ); | ||||
171 | QCOMPARE(matchedVariables.size(), 1); | ||||
172 | auto match = matchedVariables[0]; | ||||
173 | auto valueIndex = | ||||
174 | variableModel->index(match.row(), match.column() + 1); | ||||
175 | QVERIFY( | ||||
176 | valueIndex.data(Qt::DisplayRole) == | ||||
177 | QVariant::fromValue(QString::number(i)) | ||||
178 | ); | ||||
179 | } | ||||
180 | } | ||||
181 | | ||||
182 | void TestJulia::testAddVariablesFromManager() | ||||
183 | { | ||||
184 | auto variableModel = dynamic_cast<Cantor::DefaultVariableModel *>( | ||||
185 | session()->variableModel() | ||||
186 | ); | ||||
187 | QStringList variableNames = | ||||
188 | QString::fromLatin1("a b c d").split(QLatin1String(" ")); | ||||
189 | | ||||
190 | for (int i = 0; i < variableNames.size(); i++) { | ||||
191 | variableModel->addVariable(variableNames[i], QString::number(i)); | ||||
192 | | ||||
193 | QModelIndexList matchedVariables = variableModel->match( | ||||
194 | variableModel->index(0, 0), | ||||
195 | Qt::DisplayRole, | ||||
196 | QVariant::fromValue(variableNames[i]), | ||||
197 | -1, | ||||
198 | Qt::MatchExactly | ||||
199 | ); | ||||
200 | QCOMPARE(matchedVariables.size(), 1); | ||||
201 | auto match = matchedVariables[0]; | ||||
202 | auto valueIndex = | ||||
203 | variableModel->index(match.row(), match.column() + 1); | ||||
204 | QVERIFY( | ||||
205 | valueIndex.data(Qt::DisplayRole) == | ||||
206 | QVariant::fromValue(QString::number(i)) | ||||
207 | ); | ||||
208 | } | ||||
209 | } | ||||
210 | | ||||
211 | void TestJulia::testRemoveVariables() | ||||
212 | { | ||||
213 | auto variableModel = dynamic_cast<Cantor::DefaultVariableModel *>( | ||||
214 | session()->variableModel() | ||||
215 | ); | ||||
216 | QStringList variableNames = | ||||
217 | QString::fromLatin1("a b c d").split(QLatin1String(" ")); | ||||
218 | | ||||
219 | for (int i = 0; i < variableNames.size(); i++) { | ||||
220 | variableModel->addVariable(variableNames[i], QString::number(i)); | ||||
221 | } | ||||
222 | for (int i = 0; i < variableNames.size(); i += 2) { | ||||
223 | variableModel->removeVariable(variableNames[i]); | ||||
224 | } | ||||
225 | | ||||
226 | for (int i = 0; i < variableNames.size(); i++) { | ||||
227 | QModelIndexList matchedVariables = variableModel->match( | ||||
228 | variableModel->index(0, 0), | ||||
229 | Qt::DisplayRole, | ||||
230 | QVariant::fromValue(variableNames[i]), | ||||
231 | -1, | ||||
232 | Qt::MatchExactly | ||||
233 | ); | ||||
234 | if (i % 2 == 0) { | ||||
235 | QVERIFY(matchedVariables.isEmpty()); | ||||
236 | } else { | ||||
237 | QCOMPARE(matchedVariables.size(), 1); | ||||
238 | auto match = matchedVariables[0]; | ||||
239 | auto valueIndex = | ||||
240 | variableModel->index(match.row(), match.column() + 1); | ||||
241 | QVERIFY( | ||||
242 | valueIndex.data(Qt::DisplayRole) == | ||||
243 | QVariant::fromValue(QString::number(i)) | ||||
244 | ); | ||||
245 | } | ||||
246 | } | ||||
247 | } | ||||
248 | | ||||
249 | void TestJulia::testAutoCompletion() | ||||
250 | { | ||||
251 | auto prefix = QLatin1String("Ba"); | ||||
252 | auto completionObject = session()->completionFor(prefix); | ||||
253 | // Give sometime for Qt's singleShot in fetch completions to trigger | ||||
254 | QTest::qWait(500); | ||||
255 | auto completions = completionObject->completions(); | ||||
256 | | ||||
257 | QStringList completionsToCheck; | ||||
258 | completionsToCheck << QLatin1String("Base"); | ||||
259 | completionsToCheck << QLatin1String("Base64DecodePipe"); | ||||
260 | completionsToCheck << QLatin1String("Base64EncodePipe"); | ||||
261 | | ||||
262 | for (auto completion : completionsToCheck) { | ||||
263 | QVERIFY(completions.contains(completion)); | ||||
264 | } | ||||
265 | | ||||
266 | for (auto completion : completionsToCheck) { | ||||
267 | QVERIFY(completion.startsWith(prefix)); | ||||
268 | } | ||||
269 | } | ||||
270 | | ||||
120 | QTEST_MAIN(TestJulia) | 271 | QTEST_MAIN(TestJulia) | ||
121 | 272 | | |||
122 | #include "testjulia.moc" | 273 | #include "testjulia.moc" |