formatter: add tree-sitter dependency and commit early draft work on a proper code formatter (#2536)

This commit is contained in:
Tyler Wilding
2023-04-24 22:46:55 -05:00
committed by GitHub
parent 83f43b7153
commit 0ffb912a04
385 changed files with 71427 additions and 28 deletions
+4 -2
View File
@@ -35,10 +35,12 @@ add_executable(goalc-test
${CMAKE_CURRENT_LIST_DIR}/decompiler/test_DisasmVifDecompile.cpp
${CMAKE_CURRENT_LIST_DIR}/decompiler/test_VuDisasm.cpp
${CMAKE_CURRENT_LIST_DIR}/game/test_newpad.cpp
${CMAKE_CURRENT_LIST_DIR}/common/formatter/test_formatter.cpp
${GOALC_TEST_FRAMEWORK_SOURCES}
${GOALC_TEST_CASES})
${GOALC_TEST_CASES}
)
target_link_libraries(goalc-test common runtime compiler gtest decomp Zydis libzstd_static)
target_link_libraries(goalc-test common runtime compiler gtest decomp Zydis libzstd_static tree-sitter)
if(WIN32)
target_link_libraries(goalc-test mman)
@@ -0,0 +1,34 @@
===
Basic Function
===
(defun test-function ((hello string))
"world hello"
(+ 1 1))
---
(defun test-function ((hello string))
"world hello"
(+ 1 1))
===
Two Functions
===
(defun test-function ((hello string))
"world hello"
(+ 1 1))
(defun test-function ((hello string))
"world hello"
(+ 1 1))
---
(defun test-function ((hello string))
"world hello"
(+ 1 1))
(defun test-function ((hello string))
"world hello"
(+ 1 1))
+105
View File
@@ -0,0 +1,105 @@
// TODO - eventually replace our `goalc` tests with this setup
// A simple test runner framework for debugging / iterating on the formatter
// Tests are defined in files as such:
/*
===
TEST NAME
===
INPUT
---
EXPECTED OUTPUT
*/
// Test files can contain multiple tests, upon running we will recurse a directory
// looking for any `.test` files and run them through the framework
//
// Any differences will be diff'd and displayed
#include "common/formatter/formatter.h"
#include "common/util/FileUtil.h"
#include "common/util/string_util.h"
#include "gtest/gtest.h"
#include "third-party/fmt/core.h"
struct TestDefinition {
std::string name;
std::string input;
std::string output;
};
bool run_tests(fs::path file_path) {
// Read in the file, and run the test
const auto contents = str_util::split(file_util::read_text_file(file_path));
std::vector<TestDefinition> tests;
TestDefinition curr_test;
int i = 0;
while (i < contents.size()) {
const auto& line = contents.at(i);
if (line == "===") {
curr_test = TestDefinition();
curr_test.name = contents.at(i + 1);
i += 3;
continue;
}
// Parse the input and output
if (!curr_test.name.empty() && line.empty()) {
i++;
while (true) {
if (contents.at(i) == "---") {
i++;
curr_test.input = str_util::trim(curr_test.input);
break;
}
curr_test.input += contents.at(i) + "\n";
i++;
}
i++;
while (true) {
if (i == contents.size() || contents.at(i) == "===") {
curr_test.output = str_util::trim(curr_test.output);
tests.push_back(curr_test);
break;
}
curr_test.output += contents.at(i) + "\n";
i++;
}
continue;
}
}
// Run the tests, report successes and failures
bool test_failed = false;
fmt::print("{}:\n", file_util::base_name(file_path.string()));
for (const auto& test : tests) {
const auto formatted_result = formatter::format_code(test.input);
if (formatted_result != test.output) {
fmt::print(" ❌ - {}\n", test.name);
fmt::print("{}\n", str_util::diff(test.output, formatted_result));
test_failed = true;
} else {
fmt::print(" ✅ - {}\n", test.name);
}
}
return test_failed;
}
bool find_and_run_tests() {
// Enumerate test files
const auto test_files = file_util::find_files_recursively(
file_util::get_file_path({"test/common/formatter/corpus"}), std::regex("^.*\.test$"));
bool failed = false;
for (const auto& file : test_files) {
failed = run_tests(file);
}
return !failed;
}
TEST(Formatter, FormatterTests) {
EXPECT_TRUE(find_and_run_tests());
}