From bd5332e3b10f46b69cdae840c6f882e5d4e81137 Mon Sep 17 00:00:00 2001 From: Tingan Ho Date: Sat, 24 Jun 2017 16:25:19 +0200 Subject: [PATCH] Adds extension test --- .travis.yml | 2 ++ .../CallExpressionIdentifier.json | 11 +++++++++++ .../References/KeyExtractions/EmptyFile.json | 3 +++ src/Program/CommandController.cpp | 10 +++++++++- src/Program/Extension.cpp | 11 +++++++++++ src/Program/ExtensionTestRunner.cpp | 8 +++++--- src/Program/Utils.cpp | 17 ++++------------- 7 files changed, 45 insertions(+), 17 deletions(-) create mode 100644 src/Extensions/JavaScript/Tests/References/KeyExtractions/CallExpressionIdentifier.json create mode 100644 src/Extensions/JavaScript/Tests/References/KeyExtractions/EmptyFile.json diff --git a/.travis.yml b/.travis.yml index 3be89619..f638df03 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,6 +10,8 @@ script: - mkdir debug && cd debug - cmake -DCMAKE_CXX_COMPILER=$COMPILER .. && make - ../bin/run-tests + - cd src/Extensions/JavaScript + - ../../../bin/l10ns extension-run-tests # Handle git submodules by ourselves. git: diff --git a/src/Extensions/JavaScript/Tests/References/KeyExtractions/CallExpressionIdentifier.json b/src/Extensions/JavaScript/Tests/References/KeyExtractions/CallExpressionIdentifier.json new file mode 100644 index 00000000..5a10a51c --- /dev/null +++ b/src/Extensions/JavaScript/Tests/References/KeyExtractions/CallExpressionIdentifier.json @@ -0,0 +1,11 @@ +{ + "KeyExtractions/CallExpressionIdentifier.js" : + [ + { + "column" : 1, + "line" : 1, + "name" : "KEY", + "params" : [] + } + ] +} \ No newline at end of file diff --git a/src/Extensions/JavaScript/Tests/References/KeyExtractions/EmptyFile.json b/src/Extensions/JavaScript/Tests/References/KeyExtractions/EmptyFile.json new file mode 100644 index 00000000..45ca357c --- /dev/null +++ b/src/Extensions/JavaScript/Tests/References/KeyExtractions/EmptyFile.json @@ -0,0 +1,3 @@ +{ + "KeyExtractions/EmptyFile.js" : [] +} \ No newline at end of file diff --git a/src/Program/CommandController.cpp b/src/Program/CommandController.cpp index 328bc073..4dad5e43 100644 --- a/src/Program/CommandController.cpp +++ b/src/Program/CommandController.cpp @@ -5,6 +5,7 @@ #include "Configurations.h" #include "Utils.cpp" #include "json.hpp" +#include "Extension.cpp" #include "ExtensionTestRunner.cpp" using namespace std; @@ -120,8 +121,15 @@ int init(int argc, char* argv[]) { case CommandKind::Extension_RunTests: run_extension_tests(session); break; - case CommandKind::Extension_AcceptBaselines: + case CommandKind::Extension_AcceptBaselines: { + string extension_file = join_paths(*session->root_dir, "Extension.json"); + Extension* extension = Extension::create(session, extension_file); + string currents_dir = join_paths(*session->root_dir, extension->test_dir + "/Currents"); + string references_dir = replace_string(currents_dir, "Currents", "References"); + remove_all(references_dir); + copy_folder(currents_dir, references_dir); break; + } default: break; } diff --git a/src/Program/Extension.cpp b/src/Program/Extension.cpp index 8737d465..e29b0d1e 100644 --- a/src/Program/Extension.cpp +++ b/src/Program/Extension.cpp @@ -1,4 +1,7 @@ +#ifndef EXTENSION_H_ +#define EXTENSION_H_ + #include #include #include @@ -75,6 +78,10 @@ class Extension { if (command.is_null()) { add_diagnostic(session, D::Missing_field_in_your_extension_file, "Command", extension_file); } + auto test_dir = manifest["TestDirectory"]; + if (test_dir.is_null()) { + add_diagnostic(session, D::Missing_field_in_your_extension_file, "TestDirectory", extension_file); + } extension->programming_language = programming_language; extension->file_extensions = file_extensions.get>(); @@ -82,6 +89,7 @@ class Extension { extension->capabilities = capabilities.get>(); extension->dependency_test = dependency_test; extension->command = command; + extension->test_dir = test_dir; extension->session = session; return extension; @@ -91,6 +99,7 @@ class Extension { vector file_extensions; vector function_names; vector capabilities; + string test_dir; string dependency_test; string command; Session* session; @@ -142,3 +151,5 @@ class Extension { } }; + +#endif //EXTENSION_H_ diff --git a/src/Program/ExtensionTestRunner.cpp b/src/Program/ExtensionTestRunner.cpp index c477a0c9..f027e2cf 100644 --- a/src/Program/ExtensionTestRunner.cpp +++ b/src/Program/ExtensionTestRunner.cpp @@ -84,20 +84,22 @@ void run_extension_tests(Session* session) { string currents_dir = currents_file_path.substr(0, currents_file_path.find_last_of("/")); recursively_create_dir(currents_dir); write_file(replace_string(currents_file_path, "Cases", "Currents"), result_string); - string reference_file_path = replace_string(test_file, "Cases", "References"); - reference_file_path = replace_string(reference_file_path, ".js", ".json"); + string reference_file_path = replace_string(currents_file_path, "Cases", "References"); string reference_string = ""; if (file_exists(reference_file_path)) { reference_string = read_file(reference_file_path); } string test_name = currents_file_path.substr(currents_file_path.find_last_of("/") + 1); test_name = replace_string(test_name, *session->root_dir, ""); - test(test_name, [&](Test* t) { + test(test_name, [reference_string, result_string](Test* t) { if (result_string != reference_string) { + cout << result_string << endl; + cout << reference_string << endl; throw runtime_error("Assertion Error!"); } }); }); + runTests(); printResult(); kill_all_processes(SIGTERM); } diff --git a/src/Program/Utils.cpp b/src/Program/Utils.cpp index 5b914eb8..534ceb6b 100644 --- a/src/Program/Utils.cpp +++ b/src/Program/Utils.cpp @@ -183,19 +183,10 @@ bool file_exists(const string filename) { } string read_file(string filename) { - string line; - string result; - ifstream f(filename); - if (f.is_open()) { - while (getline(f, line)) { - result += line + '\n'; - } - f.close(); - return result; - } - else { - throw invalid_argument("Utils::readFile: Could not open file '" + filename + "'."); - } + std::ifstream t(filename); + std::stringstream buffer; + buffer << t.rdbuf(); + return buffer.str(); } void write_file(string filename, string content) {