diff --git a/apps/Makefile b/apps/Makefile index 6c2bb1e4e..ff6c78f78 100644 --- a/apps/Makefile +++ b/apps/Makefile @@ -87,5 +87,6 @@ $(call object_for,$(apps_src) $(tests_src)): $(BUILD_DIR)/python/port/genhdr/qst apps_tests_src = $(app_calculation_test_src) $(app_code_test_src) $(app_probability_test_src) $(app_regression_test_src) $(app_sequence_test_src) $(app_shared_test_src) $(app_statistics_test_src) $(app_settings_test_src) $(app_solver_test_src) apps_tests_src += $(addprefix apps/,\ + alternate_empty_nested_menu_controller.cpp \ global_preferences.cpp \ ) diff --git a/apps/code/Makefile b/apps/code/Makefile index 7a4772d2c..de11dcb4c 100644 --- a/apps/code/Makefile +++ b/apps/code/Makefile @@ -1,10 +1,6 @@ apps += Code::App app_headers += apps/code/app.h -app_code_test_src = $(addprefix apps/code/,\ - script_template.cpp \ -) - app_code_src = $(addprefix apps/code/,\ app.cpp \ console_controller.cpp \ @@ -15,16 +11,24 @@ app_code_src = $(addprefix apps/code/,\ editor_view.cpp \ helpers.cpp \ menu_controller.cpp \ - python_toolbox.cpp \ python_text_area.cpp \ sandbox_controller.cpp \ - script.cpp \ script_name_cell.cpp \ - script_node_cell.cpp \ script_parameter_controller.cpp \ +) + +app_code_test_src = $(addprefix apps/code/,\ + python_toolbox.cpp \ + script.cpp \ + script_node_cell.cpp \ script_store.cpp \ - variable_box_controller.cpp \ + script_template.cpp \ variable_box_empty_controller.cpp \ + variable_box_controller.cpp \ +) + +tests_src += $(addprefix apps/code/test/,\ + variable_box_controller.cpp\ ) app_code_src += $(app_code_test_src) diff --git a/apps/code/test/variable_box_controller.cpp b/apps/code/test/variable_box_controller.cpp new file mode 100644 index 000000000..7ddda610b --- /dev/null +++ b/apps/code/test/variable_box_controller.cpp @@ -0,0 +1,70 @@ +#include +#include "../script_store.h" +#include "../variable_box_controller.h" +#include + +using namespace Code; + +void assert_variables_are(const char * script, const char * nameToComplete, const char * * expectedVariables, int expectedVariablesCount) { + // Clean the store + ScriptStore store; + store.deleteAllScripts(); + + // Add the script + store.addNewScript(); + constexpr int dataBufferSize = 500; + char dataBuffer[dataBufferSize]; + Ion::Storage::Record::Data data = { + .buffer = &dataBuffer, + .size = dataBufferSize + }; + strcpy(dataBuffer, script); + constexpr int scriptIndex = 0; + store.scriptAtIndex(scriptIndex).setValue(data); + + // Load the variable box + VariableBoxController varBox(&store); + + const size_t nameToCompleteLength = strlen(nameToComplete); + varBox.loadFunctionsAndVariables(scriptIndex, nameToComplete, nameToCompleteLength); + + // Compare the variables + int index = 0; // Index to make sure we are not cycling through the results + int textToInsertLength; + bool addParentheses; + for (int i = 0; i < expectedVariablesCount; i++) { + quiz_assert(i == index); + const char * autocompletionI = varBox.autocompletionAlternativeAtIndex( + nameToCompleteLength, + &textToInsertLength, + &addParentheses, + i, + &index); + quiz_assert(i == index); // If false, the autompletion has cycled: there are not as many results as expected + quiz_assert(strncmp(*(expectedVariables + i), autocompletionI - nameToCompleteLength, textToInsertLength + nameToCompleteLength) == 0); + index++; + } + varBox.autocompletionAlternativeAtIndex( + strlen(nameToComplete), + &textToInsertLength, + &addParentheses, + index, + &index); + /* Assert the autocompletion has cycles: otherwise, there are more results + * than expected. */ + quiz_assert(index == 0); +} + +QUIZ_CASE(variable_box_controller) { + const char * expectedVariables[] = { + "froo", + "from", + "frozenset()" + }; + // FIXME This test does not load imported variables for now + assert_variables_are( + "from math import *\nfroo=3", + "fr", + expectedVariables, + sizeof(expectedVariables) / sizeof(const char *)); +} diff --git a/apps/shared/Makefile b/apps/shared/Makefile index 4056c9dbd..4ffcd50df 100644 --- a/apps/shared/Makefile +++ b/apps/shared/Makefile @@ -14,6 +14,7 @@ app_shared_test_src = $(addprefix apps/shared/,\ labeled_curve_view.cpp \ memoized_curve_view_range.cpp \ range_1D.cpp \ + toolbox_helpers.cpp \ zoom_and_pan_curve_view_controller.cpp \ zoom_curve_view_controller.cpp \ ) @@ -74,7 +75,6 @@ app_shared_src = $(addprefix apps/shared/,\ text_field_delegate_app.cpp \ text_field_with_extension.cpp \ text_helpers.cpp \ - toolbox_helpers.cpp \ values_controller.cpp \ values_function_parameter_controller.cpp \ values_parameter_controller.cpp \