mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-03-18 21:30:38 +01:00
[python] Improve test API
This commit is contained in:
committed by
LeaNumworks
parent
ffb8f8f953
commit
d0a77bda2b
@@ -2,7 +2,9 @@
|
|||||||
#include "execution_environment.h"
|
#include "execution_environment.h"
|
||||||
|
|
||||||
QUIZ_CASE(python_basics) {
|
QUIZ_CASE(python_basics) {
|
||||||
assert_command_execution_succeeds("2+3","5\n");
|
TestExecutionEnvironment env = init_environement();
|
||||||
|
assert_command_execution_succeeds(env, "2+3","5\n");
|
||||||
|
deinit_environment();
|
||||||
}
|
}
|
||||||
|
|
||||||
QUIZ_CASE(python_template) {
|
QUIZ_CASE(python_template) {
|
||||||
|
|||||||
@@ -36,31 +36,51 @@ void inlineToBeSingleInput(char * buffer, size_t bufferSize, const char * script
|
|||||||
*bufferChar = 0;
|
*bufferChar = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool execute_input(const char * input, bool singleCommandLine, const char * outputText = nullptr) {
|
bool execute_input(TestExecutionEnvironment env, bool singleCommandLine, const char * input, const char * outputText = nullptr) {
|
||||||
constexpr size_t bufferSize = 500;
|
constexpr size_t bufferSize = 1000;
|
||||||
char buffer[bufferSize];
|
char buffer[bufferSize];
|
||||||
if (!singleCommandLine) {
|
if (!singleCommandLine) {
|
||||||
inlineToBeSingleInput(buffer, bufferSize, input);
|
inlineToBeSingleInput(buffer, bufferSize, input);
|
||||||
input = buffer;
|
input = buffer;
|
||||||
}
|
}
|
||||||
MicroPython::init(TestExecutionEnvironment::s_pythonHeap, TestExecutionEnvironment::s_pythonHeap + TestExecutionEnvironment::s_pythonHeapSize);
|
|
||||||
TestExecutionEnvironment env;
|
|
||||||
bool executionResult = env.runCode(input);
|
bool executionResult = env.runCode(input);
|
||||||
MicroPython::deinit();
|
|
||||||
if (outputText) {
|
if (outputText) {
|
||||||
quiz_assert(strcmp(outputText, env.lastPrintedText()) == 0);
|
quiz_assert(strcmp(outputText, env.lastPrintedText()) == 0);
|
||||||
}
|
}
|
||||||
return executionResult;
|
return executionResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TestExecutionEnvironment init_environement() {
|
||||||
|
MicroPython::init(TestExecutionEnvironment::s_pythonHeap, TestExecutionEnvironment::s_pythonHeap + TestExecutionEnvironment::s_pythonHeapSize);
|
||||||
|
return TestExecutionEnvironment();
|
||||||
|
}
|
||||||
|
|
||||||
|
void deinit_environment() {
|
||||||
|
MicroPython::deinit();
|
||||||
|
}
|
||||||
|
|
||||||
|
void assert_script_execution_result(bool expectedResult, const char * script, const char * outputText = nullptr) {
|
||||||
|
TestExecutionEnvironment env = init_environement();
|
||||||
|
quiz_assert(expectedResult == execute_input(env, false, script, outputText));
|
||||||
|
deinit_environment();
|
||||||
|
}
|
||||||
|
|
||||||
void assert_script_execution_succeeds(const char * script, const char * outputText) {
|
void assert_script_execution_succeeds(const char * script, const char * outputText) {
|
||||||
quiz_assert(execute_input(script, false, outputText));
|
assert_script_execution_result(true, script, outputText);
|
||||||
}
|
}
|
||||||
|
|
||||||
void assert_script_execution_fails(const char * script) {
|
void assert_script_execution_fails(const char * script) {
|
||||||
quiz_assert(!execute_input(script, false));
|
assert_script_execution_result(false, script);
|
||||||
}
|
}
|
||||||
|
|
||||||
void assert_command_execution_succeeds(const char * line, const char * outputText) {
|
void assert_command_execution_result(bool expectedResult, TestExecutionEnvironment env, const char * line, const char * outputText = nullptr) {
|
||||||
quiz_assert(execute_input(line, true, outputText));
|
quiz_assert(execute_input(env, true,line, outputText) == expectedResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
void assert_command_execution_succeeds(TestExecutionEnvironment env, const char * line, const char * outputText) {
|
||||||
|
assert_command_execution_result(true, env, line, outputText);
|
||||||
|
}
|
||||||
|
|
||||||
|
void assert_command_execution_fails(TestExecutionEnvironment env, const char * line) {
|
||||||
|
assert_command_execution_result(false, env, line);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,10 +15,10 @@ private:
|
|||||||
size_t m_printTextIndex;
|
size_t m_printTextIndex;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
TestExecutionEnvironment init_environement();
|
||||||
// TODO: this will be obsolete when runCode will take a parameter to choose the input type
|
void deinit_environment();
|
||||||
void inlineToBeSingleInput(char * buffer, size_t bufferSize, const char * script);
|
|
||||||
|
|
||||||
void assert_script_execution_succeeds(const char * script, const char * outputText = nullptr);
|
void assert_script_execution_succeeds(const char * script, const char * outputText = nullptr);
|
||||||
void assert_script_execution_fails(const char * script);
|
void assert_script_execution_fails(const char * script);
|
||||||
void assert_command_execution_succeeds(const char * line, const char * outputText = nullptr);
|
void assert_command_execution_succeeds(TestExecutionEnvironment env, const char * line, const char * outputText = nullptr);
|
||||||
|
void assert_command_execution_fails(TestExecutionEnvironment env, const char * line);
|
||||||
|
|||||||
@@ -1,120 +1,113 @@
|
|||||||
#include <quiz.h>
|
#include <quiz.h>
|
||||||
#include "execution_environment.h"
|
#include "execution_environment.h"
|
||||||
|
|
||||||
static const char * s_pyplotArrowScript = R"(#
|
QUIZ_CASE(python_matplotlib_pyplot_import) {
|
||||||
from matplotlib.pyplot import *
|
// Test "from matplotlib.pyplot import *"
|
||||||
arrow(2,3,4,5)
|
TestExecutionEnvironment env = init_environement();
|
||||||
show()
|
assert_command_execution_fails(env, "arrow(2,3,4,5)");
|
||||||
)";
|
assert_command_execution_succeeds(env, "from matplotlib.pyplot import *");
|
||||||
|
assert_command_execution_succeeds(env, "arrow(2,3,4,5)");
|
||||||
|
deinit_environment();
|
||||||
|
|
||||||
|
// "from matplotlib import *"
|
||||||
|
env = init_environement();
|
||||||
|
assert_command_execution_fails(env, "pyplot.arrow(2,3,4,5)");
|
||||||
|
assert_command_execution_succeeds(env, "from matplotlib import *");
|
||||||
|
assert_command_execution_succeeds(env, "pyplot.arrow(2,3,4,5)");
|
||||||
|
deinit_environment();
|
||||||
|
|
||||||
|
// "import matplotlib"
|
||||||
|
env = init_environement();
|
||||||
|
assert_command_execution_fails(env, "matplotlib.pyplot.arrow(2,3,4,5)");
|
||||||
|
assert_command_execution_succeeds(env, "import matplotlib");
|
||||||
|
assert_command_execution_succeeds(env, "matplotlib.pyplot.arrow(2,3,4,5)");
|
||||||
|
deinit_environment();
|
||||||
|
|
||||||
|
// "import matplotlib.pyplot"
|
||||||
|
env = init_environement();
|
||||||
|
assert_command_execution_fails(env, "matplotlib.pyplot.arrow(2,3,4,5)");
|
||||||
|
assert_command_execution_succeeds(env, "import matplotlib.pyplot");
|
||||||
|
assert_command_execution_succeeds(env, "matplotlib.pyplot.arrow(2,3,4,5)");
|
||||||
|
deinit_environment();
|
||||||
|
}
|
||||||
|
|
||||||
QUIZ_CASE(python_matplotlib_pyplot_arrow) {
|
QUIZ_CASE(python_matplotlib_pyplot_arrow) {
|
||||||
assert_script_execution_succeeds(s_pyplotArrowScript);
|
TestExecutionEnvironment env = init_environement();
|
||||||
|
assert_command_execution_succeeds(env, "from matplotlib.pyplot import *");
|
||||||
|
assert_command_execution_succeeds(env, "arrow(2,3,4,5)");
|
||||||
|
assert_command_execution_succeeds(env, "show()");
|
||||||
|
deinit_environment();
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char * s_pyplotAxisScript = R"(#
|
|
||||||
from matplotlib.pyplot import *
|
|
||||||
axis((2,3,4,5))
|
|
||||||
axis([2,3,4,5])
|
|
||||||
print(axis())
|
|
||||||
scatter(0,1)
|
|
||||||
show()
|
|
||||||
)";
|
|
||||||
|
|
||||||
static const char * s_pyplotAxisErrorScript = R"(#
|
|
||||||
from matplotlib.pyplot import *
|
|
||||||
axis(2,3,4,5)
|
|
||||||
)";
|
|
||||||
|
|
||||||
QUIZ_CASE(python_matplotlib_pyplot_axis) {
|
QUIZ_CASE(python_matplotlib_pyplot_axis) {
|
||||||
assert_script_execution_succeeds(s_pyplotAxisScript, "(2.0, 3.0, 4.0, 5.0)\n");
|
TestExecutionEnvironment env = init_environement();
|
||||||
assert_script_execution_fails(s_pyplotAxisErrorScript);
|
assert_command_execution_succeeds(env, "from matplotlib.pyplot import *");
|
||||||
|
assert_command_execution_succeeds(env, "axis((2,3,4,5))");
|
||||||
|
assert_command_execution_succeeds(env, "axis([2,3,4,5])");
|
||||||
|
assert_command_execution_succeeds(env, "axis()","(2.0, 3.0, 4.0, 5.0)\n");
|
||||||
|
assert_command_execution_succeeds(env, "show()");
|
||||||
|
assert_command_execution_fails(env, "axis(2,3,4,5)");
|
||||||
|
deinit_environment();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char * s_pyplotBarScript = R"(#
|
|
||||||
from matplotlib.pyplot import *
|
|
||||||
bar([0,2,3],[10,12,23])
|
|
||||||
bar([0,2,3],10)
|
|
||||||
bar([],[])
|
|
||||||
bar([1,2,3],[1,2,3],2,3)
|
|
||||||
bar([1,2,3],[1,2,3],[1,2,3],[1,2,3])
|
|
||||||
show()
|
|
||||||
)";
|
|
||||||
|
|
||||||
static const char * s_pyplotBarErrorScript = R"(#
|
|
||||||
from matplotlib.pyplot import *
|
|
||||||
bar([1,2,3],[1,2,3,4],[1,2,3],[1,2,3])
|
|
||||||
show()
|
|
||||||
)";
|
|
||||||
|
|
||||||
QUIZ_CASE(python_matplotlib_pyplot_bar) {
|
QUIZ_CASE(python_matplotlib_pyplot_bar) {
|
||||||
assert_script_execution_succeeds(s_pyplotBarScript);
|
TestExecutionEnvironment env = init_environement();
|
||||||
assert_script_execution_fails(s_pyplotBarErrorScript);
|
assert_command_execution_succeeds(env, "from matplotlib.pyplot import *");
|
||||||
|
assert_command_execution_succeeds(env, "bar([0,2,3],[10,12,23])");
|
||||||
|
assert_command_execution_succeeds(env, "bar([0,2,3],10)");
|
||||||
|
assert_command_execution_succeeds(env, "bar([],[])");
|
||||||
|
assert_command_execution_succeeds(env, "bar([1,2,3],[1,2,3],2,3)");
|
||||||
|
assert_command_execution_succeeds(env, "bar([1,2,3],[1,2,3],[1,2,3],[1,2,3])");
|
||||||
|
assert_command_execution_succeeds(env, "show()");
|
||||||
|
assert_command_execution_fails(env, "bar([1,2,3],[1,2,3,4],[1,2,3],[1,2,3])");
|
||||||
|
deinit_environment();
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char * s_pyplotGridScript = R"(#
|
|
||||||
from matplotlib.pyplot import *
|
|
||||||
grid(True)
|
|
||||||
grid()
|
|
||||||
show()
|
|
||||||
)";
|
|
||||||
|
|
||||||
QUIZ_CASE(python_matplotlib_pyplot_grid) {
|
QUIZ_CASE(python_matplotlib_pyplot_grid) {
|
||||||
assert_script_execution_succeeds(s_pyplotGridScript);
|
TestExecutionEnvironment env = init_environement();
|
||||||
|
assert_command_execution_succeeds(env, "from matplotlib.pyplot import *");
|
||||||
|
assert_command_execution_succeeds(env, "grid(True)");
|
||||||
|
assert_command_execution_succeeds(env, "grid()");
|
||||||
|
deinit_environment();
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char * s_pyplotHistScript = R"(#
|
|
||||||
from matplotlib.pyplot import *
|
|
||||||
hist([2,3,4,5,6])
|
|
||||||
hist([2,3,4,5,6],23)
|
|
||||||
hist([2,3,4,5,6],[0,2,3])
|
|
||||||
hist([2,3,4,5,6],[0,2,3, 4,5,6,7])
|
|
||||||
show()
|
|
||||||
)";
|
|
||||||
|
|
||||||
QUIZ_CASE(python_matplotlib_pyplot_hist) {
|
QUIZ_CASE(python_matplotlib_pyplot_hist) {
|
||||||
assert_script_execution_succeeds(s_pyplotHistScript);
|
TestExecutionEnvironment env = init_environement();
|
||||||
|
assert_command_execution_succeeds(env, "from matplotlib.pyplot import *");
|
||||||
|
assert_command_execution_succeeds(env, "hist([2,3,4,5,6])");
|
||||||
|
assert_command_execution_succeeds(env, "hist([2,3,4,5,6],23)");
|
||||||
|
assert_command_execution_succeeds(env, "hist([2,3,4,5,6],[0,2,3])");
|
||||||
|
assert_command_execution_succeeds(env, "hist([2,3,4,5,6],[0,2,3, 4,5,6,7])");
|
||||||
|
assert_command_execution_succeeds(env, "show()");
|
||||||
|
deinit_environment();
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char * s_pyplotPlotScript = R"(#
|
|
||||||
from matplotlib.pyplot import *
|
|
||||||
plot([2,3,4,5,6])
|
|
||||||
plot([2,3,4,5,6],[3,4,5,6,7])
|
|
||||||
show()
|
|
||||||
)";
|
|
||||||
|
|
||||||
static const char * s_pyplotPlotErrorScript = R"(#
|
|
||||||
from matplotlib.pyplot import *
|
|
||||||
plot([2,3,4,5,6],2)
|
|
||||||
)";
|
|
||||||
|
|
||||||
QUIZ_CASE(python_matplotlib_pyplot_plot) {
|
QUIZ_CASE(python_matplotlib_pyplot_plot) {
|
||||||
assert_script_execution_succeeds(s_pyplotPlotScript);
|
TestExecutionEnvironment env = init_environement();
|
||||||
assert_script_execution_fails(s_pyplotPlotErrorScript);
|
assert_command_execution_succeeds(env, "from matplotlib.pyplot import *");
|
||||||
|
assert_command_execution_succeeds(env, "plot([2,3,4,5,6])");
|
||||||
|
assert_command_execution_succeeds(env, "plot(2,3)");
|
||||||
|
assert_command_execution_succeeds(env, "plot([2,3,4,5,6],[3,4,5,6,7])");
|
||||||
|
assert_command_execution_succeeds(env, "show()");
|
||||||
|
assert_command_execution_fails(env, "plot([2,3,4,5,6],2)");
|
||||||
|
deinit_environment();
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char * s_pyplotScatterScript = R"(#
|
|
||||||
from matplotlib.pyplot import *
|
|
||||||
scatter(2,3)
|
|
||||||
scatter([2,3,4,5,6],[3,4,5,6,7])
|
|
||||||
show()
|
|
||||||
)";
|
|
||||||
|
|
||||||
static const char * s_pyplotScatterErrorScript = R"(#
|
|
||||||
from matplotlib.pyplot import *
|
|
||||||
scatter([2,3,4,5,6],2)
|
|
||||||
)";
|
|
||||||
|
|
||||||
QUIZ_CASE(python_matplotlib_pyplot_scatter) {
|
QUIZ_CASE(python_matplotlib_pyplot_scatter) {
|
||||||
assert_script_execution_succeeds(s_pyplotScatterScript);
|
TestExecutionEnvironment env = init_environement();
|
||||||
assert_script_execution_fails(s_pyplotScatterErrorScript);
|
assert_command_execution_succeeds(env, "from matplotlib.pyplot import *");
|
||||||
|
assert_command_execution_succeeds(env, "scatter(2,3)");
|
||||||
|
assert_command_execution_succeeds(env, "scatter([2,3,4,5,6],[3,4,5,6,7])");
|
||||||
|
assert_command_execution_succeeds(env, "show()");
|
||||||
|
assert_command_execution_fails(env, "scatter([2,3,4,5,6],2)");
|
||||||
|
deinit_environment();
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char * s_pyplotTextScript = R"(#
|
|
||||||
from matplotlib.pyplot import *
|
|
||||||
text(2,3,'hello')
|
|
||||||
show()
|
|
||||||
)";
|
|
||||||
|
|
||||||
QUIZ_CASE(python_matplotlib_pyplot_text) {
|
QUIZ_CASE(python_matplotlib_pyplot_text) {
|
||||||
assert_script_execution_succeeds(s_pyplotTextScript);
|
TestExecutionEnvironment env = init_environement();
|
||||||
|
assert_command_execution_succeeds(env, "from matplotlib.pyplot import *");
|
||||||
|
assert_command_execution_succeeds(env, "text(2,3,'hello')");
|
||||||
|
assert_command_execution_succeeds(env, "show()");
|
||||||
|
deinit_environment();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user