From 7b13b1206270cf93dc6f63919c88fc96ce2598f6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ond=C5=99ej=20=C5=A0torc?= <storcond@fit.cvut.cz>
Date: Sat, 15 Apr 2023 15:36:23 +0200
Subject: [PATCH] cli: Improve CLI tests compilation

Tests for parser are now under one test_case and are dynamicly loaded
at runtime. Thanks to that we have only one compilation unit.

It also simplifies CMake for alib2cli
---
 alib2cli/CMake/AqlParserTest.cmake      | 39 ----------------------
 alib2cli/CMakeLists.txt                 | 20 ++++++++++--
 alib2cli/test-src/aql/AqlParserTest.cpp | 43 +++++++++++++++++++++++++
 alib2cli/test-src/aql/aql.cpp.in        | 41 -----------------------
 4 files changed, 60 insertions(+), 83 deletions(-)
 delete mode 100644 alib2cli/CMake/AqlParserTest.cmake
 create mode 100644 alib2cli/test-src/aql/AqlParserTest.cpp
 delete mode 100644 alib2cli/test-src/aql/aql.cpp.in

diff --git a/alib2cli/CMake/AqlParserTest.cmake b/alib2cli/CMake/AqlParserTest.cmake
deleted file mode 100644
index 6a3f44053..000000000
--- a/alib2cli/CMake/AqlParserTest.cmake
+++ /dev/null
@@ -1,39 +0,0 @@
-function(aql_parser_test ENABLE_COVERAGE)
-
-    # Enable coverage report
-    if (ENABLE_COVERAGE)
-        target_compile_options(alib2cli PUBLIC -fprofile-arcs -ftest-coverage)
-        target_link_options(alib2cli PUBLIC -fprofile-arcs -lgcov)
-        target_compile_options(test_alib2cli PUBLIC -fprofile-arcs -ftest-coverage)
-        target_link_options(test_alib2cli PUBLIC -fprofile-arcs -lgcov)
-    endif ()
-
-    configure_file(
-            ${CMAKE_CURRENT_SOURCE_DIR}/test-src/aql/configure_tests.hpp.in
-            ${CMAKE_CURRENT_BINARY_DIR}/aql-parser/configure_tests.hpp
-    )
-
-    # list all aql files
-    file(GLOB TEST_FILES_AQL
-            LIST_DIRECTORIES FALSE
-            RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
-            CONFIGURE_DEPENDS
-            "test-src/aql/*/*.aql"
-            )
-
-    foreach(AQL_FILEPATH IN LISTS TEST_FILES_AQL)
-        get_filename_component(AQL_FILENAME "${AQL_FILEPATH}" NAME_WE)
-        get_filename_component(AQL_DIRECTORY "${AQL_FILEPATH}" DIRECTORY)
-        string(REPLACE "/" "_" AQL_DIRECTORY ${AQL_DIRECTORY})
-        string(CONCAT AQL_FILENAME ${AQL_DIRECTORY} "_" ${AQL_FILENAME})
-        configure_file(
-                ${CMAKE_CURRENT_SOURCE_DIR}/test-src/aql/aql.cpp.in
-                ${CMAKE_CURRENT_BINARY_DIR}/aql-parser/${AQL_FILENAME}.cpp
-                @ONLY
-        )
-        set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/aql-parser/${AQL_FILENAME}.cpp PROPERTIES GENERATED 1)
-        target_sources(test_alib2cli PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/aql-parser/${AQL_FILENAME}.cpp)
-    endforeach()
-    target_sources(test_alib2cli PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/test-src/aql/)
-    target_include_directories(test_alib2cli PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/test-src/aql/)
-endfunction()
diff --git a/alib2cli/CMakeLists.txt b/alib2cli/CMakeLists.txt
index dad04a310..efba55fbf 100644
--- a/alib2cli/CMakeLists.txt
+++ b/alib2cli/CMakeLists.txt
@@ -1,5 +1,8 @@
 project(alt-libcli VERSION ${CMAKE_PROJECT_VERSION})
 
+option(ALIB2CLI_COVERAGE "Collects coverage data for alib2cli" OFF)
+
+
 find_package(LibXml2 REQUIRED)
 find_package(antlr4-generator 4.11 REQUIRED)
 find_package(antlr4-runtime 4.11 REQUIRED)
@@ -44,6 +47,17 @@ target_include_directories(alib2cli PUBLIC ${ANTLR4_INCLUDE_DIR_AltCliGrammarLex
 target_include_directories(alib2cli SYSTEM PUBLIC ${ANTLR4_INCLUDE_DIR})
 target_link_libraries(alib2cli INTERFACE antlr4_shared)
 
-list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/CMake)
-include(AqlParserTest)
-aql_parser_test(TRUE)
+# Enable coverage report
+if (ALIB2CLI_COVERAGE)
+	target_compile_options(alib2cli PUBLIC -fprofile-arcs -ftest-coverage)
+	target_link_options(alib2cli PUBLIC -fprofile-arcs -lgcov)
+	target_compile_options(test_alib2cli PUBLIC -fprofile-arcs -ftest-coverage)
+	target_link_options(test_alib2cli PUBLIC -fprofile-arcs -lgcov)
+endif ()
+
+configure_file(
+		${CMAKE_CURRENT_SOURCE_DIR}/test-src/aql/configure_tests.hpp.in
+		${CMAKE_CURRENT_BINARY_DIR}/test-src/aql/configure_tests.hpp
+)
+
+target_include_directories(test_alib2cli PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/test-src/aql/)
diff --git a/alib2cli/test-src/aql/AqlParserTest.cpp b/alib2cli/test-src/aql/AqlParserTest.cpp
new file mode 100644
index 000000000..7b9be1a48
--- /dev/null
+++ b/alib2cli/test-src/aql/AqlParserTest.cpp
@@ -0,0 +1,43 @@
+#include <catch2/catch.hpp>
+#include <configure_tests.hpp>
+#include <filesystem>
+#include <fstream>
+#include <iostream>
+#include <unordered_set>
+#include "AntlrParserTest.hpp"
+using namespace std::literals;
+
+void testFolder(const std::filesystem::path& folder, const std::set<std::string>& skipFiles = {})
+{
+    for (const auto& entry : std::filesystem::directory_iterator(folder)) {
+        if (entry.is_regular_file()) {
+            std::string fileName = entry.path().filename().string();
+
+            if (skipFiles.contains(fileName)) {
+                continue;
+            }
+
+            DYNAMIC_SECTION("Old Parse - " << fileName)
+            {
+                parseOld(entry.path());
+            }
+
+            DYNAMIC_SECTION("New Parse - " << fileName)
+            {
+                parseNew(entry.path());
+            }
+
+            DYNAMIC_SECTION("Compare AST - " << fileName)
+            {
+                compareAst(entry.path(), !skipFiles.contains(fileName));
+            }
+        }
+    }
+}
+
+
+TEST_CASE("AQL parser tests", "[unit][cli][parser]")
+{
+    testFolder(std::filesystem::path(CMAKE_CURRENT_SOURCE_DIR) / "test-src/aql/basic"s);
+    testFolder(std::filesystem::path(CMAKE_CURRENT_SOURCE_DIR) / "test-src/aql/tests"s, {"eof.aql", "ArbologyMPAlreadyMatchedPatternPrefixTest.aql", "functions.aql"});
+}
diff --git a/alib2cli/test-src/aql/aql.cpp.in b/alib2cli/test-src/aql/aql.cpp.in
deleted file mode 100644
index 2c65ce021..000000000
--- a/alib2cli/test-src/aql/aql.cpp.in
+++ /dev/null
@@ -1,41 +0,0 @@
-#include <catch2/catch.hpp>
-#include <filesystem>
-#include <unordered_set>
-#include <iostream>
-#include "AntlrParserTest.hpp"
-#include "configure_tests.hpp"
-using namespace std::literals;
-
-TEST_CASE("@AQL_FILEPATH@", "[unit][cli] ")
-{
-    SECTION("Old Parse")
-    {
-        parseOld(std::filesystem::path(CMAKE_CURRENT_SOURCE_DIR) / "@AQL_FILEPATH@"s);
-    }
-
-    SECTION("New Parse")
-    {
-        parseNew(std::filesystem::path(CMAKE_CURRENT_SOURCE_DIR) / "@AQL_FILEPATH@"s);
-    }
-
-    SECTION("Compare AST")
-    {
-        // Due to different approach to top level commands parsing we need to only match
-        // first top level statement.
-        const std::unordered_set<std::string> severalTopLevelStatements = {
-            "test-src/aql/tests/eof.aql",
-            "test-src/aql/tests/ArbologyMPAlreadyMatchedPatternPrefixTest.aql",
-            "test-src/aql/tests/functions.aql"
-        };
-
-        std::cout << "@AQL_FILEPATH@"s << std::endl;
-        if (severalTopLevelStatements.contains("@AQL_FILEPATH@"s))
-        {
-            compareAst(std::filesystem::path(CMAKE_CURRENT_SOURCE_DIR) / "@AQL_FILEPATH@"s, false);
-        }
-        else
-        {
-        compareAst(std::filesystem::path(CMAKE_CURRENT_SOURCE_DIR) / "@AQL_FILEPATH@"s);
-        }
-    }
-}
-- 
GitLab