Skip to content

Commit

Permalink
Adds extension test
Browse files Browse the repository at this point in the history
  • Loading branch information
tinganho committed Jun 24, 2017
1 parent c241e2d commit bd5332e
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 17 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"KeyExtractions/CallExpressionIdentifier.js" :
[
{
"column" : 1,
"line" : 1,
"name" : "KEY",
"params" : []
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"KeyExtractions/EmptyFile.js" : []
}
10 changes: 9 additions & 1 deletion src/Program/CommandController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "Configurations.h"
#include "Utils.cpp"
#include "json.hpp"
#include "Extension.cpp"
#include "ExtensionTestRunner.cpp"

using namespace std;
Expand Down Expand Up @@ -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;
}
Expand Down
11 changes: 11 additions & 0 deletions src/Program/Extension.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@

#ifndef EXTENSION_H_
#define EXTENSION_H_

#include <stdio.h>
#include <unistd.h>
#include <iostream>
Expand Down Expand Up @@ -75,13 +78,18 @@ 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<vector<string>>();
extension->function_names = function_names.get<vector<string>>();
extension->capabilities = capabilities.get<vector<string>>();
extension->dependency_test = dependency_test;
extension->command = command;
extension->test_dir = test_dir;
extension->session = session;

return extension;
Expand All @@ -91,6 +99,7 @@ class Extension {
vector<string> file_extensions;
vector<string> function_names;
vector<string> capabilities;
string test_dir;
string dependency_test;
string command;
Session* session;
Expand Down Expand Up @@ -142,3 +151,5 @@ class Extension {

}
};

#endif //EXTENSION_H_
8 changes: 5 additions & 3 deletions src/Program/ExtensionTestRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
17 changes: 4 additions & 13 deletions src/Program/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit bd5332e

Please sign in to comment.