From a3329faa464682f9fa02c3b788a861efd7f88242 Mon Sep 17 00:00:00 2001 From: klingbolt Date: Sun, 8 Oct 2023 23:22:23 -0400 Subject: [PATCH 1/9] First set of attribute tests. Contains one case of a value being copied over and one case of a value being changed from zero to false. --- .../can_fade_tests/can_fade_is_false.xml | 8 +++ .../can_fade_tests/can_fade_is_zero.xml | 8 +++ .../test_output/can_fade_corrected_zero.xml | 8 +++ .../test_output/can_fade_is_false.xml | 8 +++ xml_converter/tests/test_bool.cpp | 70 +++++++++++++++++++ 5 files changed, 102 insertions(+) create mode 100644 xml_converter/test_cases/can_fade_tests/can_fade_is_false.xml create mode 100644 xml_converter/test_cases/can_fade_tests/can_fade_is_zero.xml create mode 100644 xml_converter/test_cases/test_output/can_fade_corrected_zero.xml create mode 100644 xml_converter/test_cases/test_output/can_fade_is_false.xml create mode 100644 xml_converter/tests/test_bool.cpp diff --git a/xml_converter/test_cases/can_fade_tests/can_fade_is_false.xml b/xml_converter/test_cases/can_fade_tests/can_fade_is_false.xml new file mode 100644 index 00000000..f02cfbea --- /dev/null +++ b/xml_converter/test_cases/can_fade_tests/can_fade_is_false.xml @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/xml_converter/test_cases/can_fade_tests/can_fade_is_zero.xml b/xml_converter/test_cases/can_fade_tests/can_fade_is_zero.xml new file mode 100644 index 00000000..44d3c264 --- /dev/null +++ b/xml_converter/test_cases/can_fade_tests/can_fade_is_zero.xml @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/xml_converter/test_cases/test_output/can_fade_corrected_zero.xml b/xml_converter/test_cases/test_output/can_fade_corrected_zero.xml new file mode 100644 index 00000000..c4975868 --- /dev/null +++ b/xml_converter/test_cases/test_output/can_fade_corrected_zero.xml @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/xml_converter/test_cases/test_output/can_fade_is_false.xml b/xml_converter/test_cases/test_output/can_fade_is_false.xml new file mode 100644 index 00000000..f02cfbea --- /dev/null +++ b/xml_converter/test_cases/test_output/can_fade_is_false.xml @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/xml_converter/tests/test_bool.cpp b/xml_converter/tests/test_bool.cpp new file mode 100644 index 00000000..013ed236 --- /dev/null +++ b/xml_converter/tests/test_bool.cpp @@ -0,0 +1,70 @@ +#include "../src/attribute/bool.hpp" +#include "../src/packaging_xml.hpp" +#include +#include + +class BoolTest : public ::testing::Test { + protected: + void SetUp() override { + } + void TearDown() override { + } +}; + +bool compare_files(const std::string& file1_path, const std::string& file2_path) { + std::ifstream file1(file1_path); + std::ifstream file2(file2_path); + + if (!file1.is_open() || !file2.is_open()) { + std::cerr << "Error: Could not open one or both of the files." << std::endl; + return false; + } + + char char1, char2; + bool files_are_equal = true; + + while (true) { + char1 = file1.get(); + char2 = file2.get(); + + if (char1 != char2) { + files_are_equal = false; + break; + } + + if (file1.eof() && file2.eof()) { + break; // Reached the end of both files + } + + // If one file reaches the end before the other, they are not equal + if (file1.eof() || file2.eof()) { + files_are_equal = false; + break; + } + } + + file1.close(); + file2.close(); + + return files_are_equal; +} + +TEST_F(BoolTest, ValueIsFalse) { + std::map marker_categories; + std::vector parsed_pois; + std::string xml_input = "../test_cases/can_fade_tests/can_fade_is_false.xml"; + std::string xml_output = "../test_cases/test_output/can_fade_is_false.xml"; + parse_xml_file(xml_input, &marker_categories, &parsed_pois); + write_xml_file(xml_output, &marker_categories, &parsed_pois); + EXPECT_TRUE(compare_files(xml_input, xml_output)); +} + +TEST_F(BoolTest, ValueIsZero) { + std::map marker_categories; + std::vector parsed_pois; + std::string xml_input = "../test_cases/can_fade_tests/can_fade_is_zero.xml"; + std::string xml_output = "../test_cases/test_output/can_fade_corrected_zero.xml"; + parse_xml_file(xml_input, &marker_categories, &parsed_pois); + write_xml_file(xml_output, &marker_categories, &parsed_pois); + EXPECT_FALSE(compare_files(xml_input, xml_output)); +} From 8fc2227268135c862ec0977ba35e4d23214d7af2 Mon Sep 17 00:00:00 2001 From: klingbolt Date: Sun, 8 Oct 2023 23:54:51 -0400 Subject: [PATCH 2/9] Renamed test to XMLtoXML --- .../test_cases/can_fade_tests/can_fade_is_false.xml | 4 ++-- .../test_cases/can_fade_tests/can_fade_is_zero.xml | 4 ++-- .../test_cases/test_output/can_fade_corrected_zero.xml | 4 ++-- .../test_cases/test_output/can_fade_is_false.xml | 2 +- .../tests/{test_bool.cpp => test_xml_to_xml.cpp} | 8 ++++---- 5 files changed, 11 insertions(+), 11 deletions(-) rename xml_converter/tests/{test_bool.cpp => test_xml_to_xml.cpp} (93%) diff --git a/xml_converter/test_cases/can_fade_tests/can_fade_is_false.xml b/xml_converter/test_cases/can_fade_tests/can_fade_is_false.xml index f02cfbea..180562d2 100644 --- a/xml_converter/test_cases/can_fade_tests/can_fade_is_false.xml +++ b/xml_converter/test_cases/can_fade_tests/can_fade_is_false.xml @@ -1,8 +1,8 @@ - + - + diff --git a/xml_converter/test_cases/can_fade_tests/can_fade_is_zero.xml b/xml_converter/test_cases/can_fade_tests/can_fade_is_zero.xml index 44d3c264..0fb5af9f 100644 --- a/xml_converter/test_cases/can_fade_tests/can_fade_is_zero.xml +++ b/xml_converter/test_cases/can_fade_tests/can_fade_is_zero.xml @@ -1,8 +1,8 @@ - + - + diff --git a/xml_converter/test_cases/test_output/can_fade_corrected_zero.xml b/xml_converter/test_cases/test_output/can_fade_corrected_zero.xml index c4975868..9b8ef76b 100644 --- a/xml_converter/test_cases/test_output/can_fade_corrected_zero.xml +++ b/xml_converter/test_cases/test_output/can_fade_corrected_zero.xml @@ -1,8 +1,8 @@ - + - + diff --git a/xml_converter/test_cases/test_output/can_fade_is_false.xml b/xml_converter/test_cases/test_output/can_fade_is_false.xml index f02cfbea..0fbac02c 100644 --- a/xml_converter/test_cases/test_output/can_fade_is_false.xml +++ b/xml_converter/test_cases/test_output/can_fade_is_false.xml @@ -3,6 +3,6 @@ - + diff --git a/xml_converter/tests/test_bool.cpp b/xml_converter/tests/test_xml_to_xml.cpp similarity index 93% rename from xml_converter/tests/test_bool.cpp rename to xml_converter/tests/test_xml_to_xml.cpp index 013ed236..7addcdaf 100644 --- a/xml_converter/tests/test_bool.cpp +++ b/xml_converter/tests/test_xml_to_xml.cpp @@ -1,9 +1,9 @@ #include "../src/attribute/bool.hpp" -#include "../src/packaging_xml.hpp" #include +#include "../src/packaging_xml.hpp" #include -class BoolTest : public ::testing::Test { +class XMLtoXMLTest : public ::testing::Test { protected: void SetUp() override { } @@ -49,7 +49,7 @@ bool compare_files(const std::string& file1_path, const std::string& file2_path) return files_are_equal; } -TEST_F(BoolTest, ValueIsFalse) { +TEST_F(XMLtoXMLTest, ValueIsValid) { std::map marker_categories; std::vector parsed_pois; std::string xml_input = "../test_cases/can_fade_tests/can_fade_is_false.xml"; @@ -59,7 +59,7 @@ TEST_F(BoolTest, ValueIsFalse) { EXPECT_TRUE(compare_files(xml_input, xml_output)); } -TEST_F(BoolTest, ValueIsZero) { +TEST_F(XMLtoXMLTest, ValueIsValidButCorrected) { std::map marker_categories; std::vector parsed_pois; std::string xml_input = "../test_cases/can_fade_tests/can_fade_is_zero.xml"; From 4284f2c6c66ca40da730cd006a459317e40f2776 Mon Sep 17 00:00:00 2001 From: klingbolt Date: Sun, 8 Oct 2023 23:56:36 -0400 Subject: [PATCH 3/9] Alphabetized the includes --- xml_converter/tests/test_xml_to_xml.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xml_converter/tests/test_xml_to_xml.cpp b/xml_converter/tests/test_xml_to_xml.cpp index 7addcdaf..9d76fc60 100644 --- a/xml_converter/tests/test_xml_to_xml.cpp +++ b/xml_converter/tests/test_xml_to_xml.cpp @@ -1,7 +1,7 @@ #include "../src/attribute/bool.hpp" -#include #include "../src/packaging_xml.hpp" #include +#include class XMLtoXMLTest : public ::testing::Test { protected: From 66ab387bdf6748780c4963dc4448e5711125e49a Mon Sep 17 00:00:00 2001 From: klingbolt Date: Mon, 9 Oct 2023 20:19:49 -0400 Subject: [PATCH 4/9] New shell script for testing --- xml_converter/attribute_test_script.sh | 30 +++++++++++++++++++ .../test_output/can_fade_corrected_zero.xml | 8 ----- .../test_output/can_fade_is_false.xml | 8 ----- .../{test_xml_to_xml.cpp => test_bool.cpp} | 25 +++++----------- 4 files changed, 38 insertions(+), 33 deletions(-) create mode 100755 xml_converter/attribute_test_script.sh delete mode 100644 xml_converter/test_cases/test_output/can_fade_corrected_zero.xml delete mode 100644 xml_converter/test_cases/test_output/can_fade_is_false.xml rename xml_converter/tests/{test_xml_to_xml.cpp => test_bool.cpp} (55%) diff --git a/xml_converter/attribute_test_script.sh b/xml_converter/attribute_test_script.sh new file mode 100755 index 00000000..d463524d --- /dev/null +++ b/xml_converter/attribute_test_script.sh @@ -0,0 +1,30 @@ +#!/bin/bash + +ATTRIBUTE_TEST_CASES_FOLDER_PATHS=("./test_cases/can_fade_tests") + +ATTRIBUTE_TEST_CASES=() + +for ATTRIBUTE_TEST_CASES_FOLDER_PATH in "${ATTRIBUTE_TEST_CASES_FOLDER_PATHS[@]}"; do + if [ ! -d "$ATTRIBUTE_TEST_CASES_FOLDER_PATH" ]; then + echo "Folder '$ATTRIBUTE_TEST_CASES_FOLDER_PATH' does not exist." + exit 1 + fi + + attribute_name=$(basename "$ATTRIBUTE_TEST_CASES_FOLDER_PATH") + TEST_OUTPUT_DIRECTORY="./test_cases/test_output/$attribute_name" + + if [ ! -d "$TEST_OUTPUT_DIRECTORY" ]; then + mkdir "$TEST_OUTPUT_DIRECTORY" + fi + + while IFS= read -r -d $'\0' file; do + ATTRIBUTE_TEST_CASES+=("$file") + done < <(find "$ATTRIBUTE_TEST_CASES_FOLDER_PATH" -type f -print0) + + for test_case in "${ATTRIBUTE_TEST_CASES[@]}"; do + file_name=$(basename "$test_case") + ./build/xml_converter --input-taco-path "$test_case" --output-taco-path "$TEST_OUTPUT_DIRECTORY/$file_name" 1> /dev/null + done +done + +./build/xml_converter_tests diff --git a/xml_converter/test_cases/test_output/can_fade_corrected_zero.xml b/xml_converter/test_cases/test_output/can_fade_corrected_zero.xml deleted file mode 100644 index 9b8ef76b..00000000 --- a/xml_converter/test_cases/test_output/can_fade_corrected_zero.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - diff --git a/xml_converter/test_cases/test_output/can_fade_is_false.xml b/xml_converter/test_cases/test_output/can_fade_is_false.xml deleted file mode 100644 index 0fbac02c..00000000 --- a/xml_converter/test_cases/test_output/can_fade_is_false.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - diff --git a/xml_converter/tests/test_xml_to_xml.cpp b/xml_converter/tests/test_bool.cpp similarity index 55% rename from xml_converter/tests/test_xml_to_xml.cpp rename to xml_converter/tests/test_bool.cpp index 9d76fc60..ca0c6907 100644 --- a/xml_converter/tests/test_xml_to_xml.cpp +++ b/xml_converter/tests/test_bool.cpp @@ -1,9 +1,8 @@ #include "../src/attribute/bool.hpp" -#include "../src/packaging_xml.hpp" -#include #include +#include -class XMLtoXMLTest : public ::testing::Test { +class BoolTest : public ::testing::Test { protected: void SetUp() override { } @@ -49,22 +48,14 @@ bool compare_files(const std::string& file1_path, const std::string& file2_path) return files_are_equal; } -TEST_F(XMLtoXMLTest, ValueIsValid) { - std::map marker_categories; - std::vector parsed_pois; - std::string xml_input = "../test_cases/can_fade_tests/can_fade_is_false.xml"; - std::string xml_output = "../test_cases/test_output/can_fade_is_false.xml"; - parse_xml_file(xml_input, &marker_categories, &parsed_pois); - write_xml_file(xml_output, &marker_categories, &parsed_pois); +TEST_F(BoolTest, ValueIsFalse) { + std::string xml_input = "./test_cases/can_fade_tests/can_fade_is_false.xml"; + std::string xml_output = "./test_cases/test_output/can_fade_tests/can_fade_is_false.xml"; EXPECT_TRUE(compare_files(xml_input, xml_output)); } -TEST_F(XMLtoXMLTest, ValueIsValidButCorrected) { - std::map marker_categories; - std::vector parsed_pois; - std::string xml_input = "../test_cases/can_fade_tests/can_fade_is_zero.xml"; - std::string xml_output = "../test_cases/test_output/can_fade_corrected_zero.xml"; - parse_xml_file(xml_input, &marker_categories, &parsed_pois); - write_xml_file(xml_output, &marker_categories, &parsed_pois); +TEST_F(BoolTest, ValueIsZero) { + std::string xml_input = "./test_cases/can_fade_tests/can_fade_is_zero.xml"; + std::string xml_output = "./test_cases/test_output/can_fade_tests/can_fade_is_zero.xml"; EXPECT_FALSE(compare_files(xml_input, xml_output)); } From 42455f83e2fabf06999a7b7c5219486e2c0d11bc Mon Sep 17 00:00:00 2001 From: klingbolt Date: Mon, 9 Oct 2023 20:35:53 -0400 Subject: [PATCH 5/9] Removed unnecessary include --- xml_converter/tests/test_bool.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/xml_converter/tests/test_bool.cpp b/xml_converter/tests/test_bool.cpp index ca0c6907..58d56003 100644 --- a/xml_converter/tests/test_bool.cpp +++ b/xml_converter/tests/test_bool.cpp @@ -1,4 +1,3 @@ -#include "../src/attribute/bool.hpp" #include #include From c49585891ff4b7cbc6bc7351a6bcc0424411706d Mon Sep 17 00:00:00 2001 From: klingbolt Date: Wed, 11 Oct 2023 00:01:59 -0400 Subject: [PATCH 6/9] Python script that runs tests on xml_converter --- xml_converter/attribute_test_script.sh | 30 ---- ...e_is_false.xml => can_fade_is_correct.xml} | 0 ...ero.xml => can_fade_is_different_file.xml} | 0 .../can_fade_is_wrong_output.xml | 8 + xml_converter/tests/attribute_testing.py | 140 ++++++++++++++++++ xml_converter/tests/test_bool.cpp | 60 -------- 6 files changed, 148 insertions(+), 90 deletions(-) delete mode 100755 xml_converter/attribute_test_script.sh rename xml_converter/test_cases/can_fade_tests/{can_fade_is_false.xml => can_fade_is_correct.xml} (100%) rename xml_converter/test_cases/can_fade_tests/{can_fade_is_zero.xml => can_fade_is_different_file.xml} (100%) create mode 100644 xml_converter/test_cases/can_fade_tests/can_fade_is_wrong_output.xml create mode 100755 xml_converter/tests/attribute_testing.py delete mode 100644 xml_converter/tests/test_bool.cpp diff --git a/xml_converter/attribute_test_script.sh b/xml_converter/attribute_test_script.sh deleted file mode 100755 index d463524d..00000000 --- a/xml_converter/attribute_test_script.sh +++ /dev/null @@ -1,30 +0,0 @@ -#!/bin/bash - -ATTRIBUTE_TEST_CASES_FOLDER_PATHS=("./test_cases/can_fade_tests") - -ATTRIBUTE_TEST_CASES=() - -for ATTRIBUTE_TEST_CASES_FOLDER_PATH in "${ATTRIBUTE_TEST_CASES_FOLDER_PATHS[@]}"; do - if [ ! -d "$ATTRIBUTE_TEST_CASES_FOLDER_PATH" ]; then - echo "Folder '$ATTRIBUTE_TEST_CASES_FOLDER_PATH' does not exist." - exit 1 - fi - - attribute_name=$(basename "$ATTRIBUTE_TEST_CASES_FOLDER_PATH") - TEST_OUTPUT_DIRECTORY="./test_cases/test_output/$attribute_name" - - if [ ! -d "$TEST_OUTPUT_DIRECTORY" ]; then - mkdir "$TEST_OUTPUT_DIRECTORY" - fi - - while IFS= read -r -d $'\0' file; do - ATTRIBUTE_TEST_CASES+=("$file") - done < <(find "$ATTRIBUTE_TEST_CASES_FOLDER_PATH" -type f -print0) - - for test_case in "${ATTRIBUTE_TEST_CASES[@]}"; do - file_name=$(basename "$test_case") - ./build/xml_converter --input-taco-path "$test_case" --output-taco-path "$TEST_OUTPUT_DIRECTORY/$file_name" 1> /dev/null - done -done - -./build/xml_converter_tests diff --git a/xml_converter/test_cases/can_fade_tests/can_fade_is_false.xml b/xml_converter/test_cases/can_fade_tests/can_fade_is_correct.xml similarity index 100% rename from xml_converter/test_cases/can_fade_tests/can_fade_is_false.xml rename to xml_converter/test_cases/can_fade_tests/can_fade_is_correct.xml diff --git a/xml_converter/test_cases/can_fade_tests/can_fade_is_zero.xml b/xml_converter/test_cases/can_fade_tests/can_fade_is_different_file.xml similarity index 100% rename from xml_converter/test_cases/can_fade_tests/can_fade_is_zero.xml rename to xml_converter/test_cases/can_fade_tests/can_fade_is_different_file.xml diff --git a/xml_converter/test_cases/can_fade_tests/can_fade_is_wrong_output.xml b/xml_converter/test_cases/can_fade_tests/can_fade_is_wrong_output.xml new file mode 100644 index 00000000..180562d2 --- /dev/null +++ b/xml_converter/test_cases/can_fade_tests/can_fade_is_wrong_output.xml @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/xml_converter/tests/attribute_testing.py b/xml_converter/tests/attribute_testing.py new file mode 100755 index 00000000..8b6a90ad --- /dev/null +++ b/xml_converter/tests/attribute_testing.py @@ -0,0 +1,140 @@ +from dataclasses import dataclass +import subprocess +import re +import os +from typing import Dict, List + +# Path to compiled C++ executable +xml_converter: str = "../build/xml_converter" + + +@dataclass +class Attribute_Tests: + attribute_name: str + attribute_path: str + tests: List[str] + output_result: Dict[str, List[str]] + expect_output_to_be_equal: Dict[str, bool] + expect_files_to_be_equal: Dict[str, bool] + + +def run_xml_converter(*args): + try: + # Build the command to execute the C++ program with the desired function and arguments + cmd = [xml_converter] + list(args) + + # Run the C++ program and capture its output + result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) + + # Check if the C++ program executed successfully + if result.returncode == 0: + return result.stdout + else: + return result.stderr + except Exception as e: + print("Error:", e) + return None + + +def are_files_equal(file_path1, file_path2) -> bool: + try: + with open(file_path1, 'rb') as file1, open(file_path2, 'rb') as file2: + content1 = file1.read() + content2 = file2.read() + + return content1 == content2 + + except (FileNotFoundError, PermissionError): + print(f"Error opening file {file_path1} or {file_path2}") + return False + + +def are_arrays_equal(array1, array2) -> bool: + if len(array1) != len(array2): + return False + + for i in range(len(array1)): + if array1[i] != array2[i]: + return False + + return True + + +arg_input_xml: str = "--input-taco-path" +arg_output_xml: str = "--output-taco-path" + +chrono_patterns = [ + "The taco parse function took [0-9]+ milliseconds to run", + "The xml write function took [0-9]+ milliseconds to run", + "The protobuf write function took [0-9]+ milliseconds to run" +] + + +if __name__ == "__main__": + # TODO: This will need to be extracted as more attributes and tests are added + attribute: Attribute_Tests = Attribute_Tests( + attribute_name="can_fade", + attribute_path="../test_cases/can_fade_tests", + tests=["_is_correct", "_is_different_file", "_is_wrong_output"], + output_result={ + "_is_correct": ['Loading taco pack ../test_cases/can_fade_tests/can_fade_is_correct.xml'], + "_is_different_file": ['Loading taco pack ../test_cases/can_fade_tests/can_fade_is_different_file.xml'], + "_is_wrong_output": ['WRONG'] + }, + expect_output_to_be_equal={ + "_is_correct": True, + "_is_different_file": True, + "_is_wrong_output": False, + }, + expect_files_to_be_equal={ + "_is_correct": True, + "_is_different_file": False, + "_is_wrong_output": True, + } + ) + + # Ensure that the test output directory is created + if not os.path.exists(attribute.attribute_path + "-output/"): + try: + os.makedirs(attribute.attribute_path + "-output/") + except OSError as e: + print(f"Error: {e}") + + for test in attribute.tests: + arg1 = attribute.attribute_path + "/" + attribute.attribute_name + test + ".xml" + arg2 = attribute.attribute_path + "-output/" + attribute.attribute_name + test + ".xml" + + result = run_xml_converter(arg_input_xml, arg1, arg_output_xml, arg2) + if result is not None: + # Remove lines about how long it took to run + split_result = result.split("\n") + filtered_array = [] + for line in split_result: + match_found = False + for pattern in chrono_patterns: + if re.search(pattern, line) or line == "": + match_found = True + break + if not match_found: + filtered_array.append(line) + + output_test: bool = True + file_test: bool = True + + if are_arrays_equal(filtered_array, attribute.output_result[test]) != attribute.expect_output_to_be_equal[test]: + print(f"Output did not match for test {attribute.attribute_name}{test}") + print(f"Expected {attribute.output_result[test]} and got {filtered_array}") + output_test = False + + if are_files_equal(arg1, arg2) != attribute.expect_files_to_be_equal[test]: + print(f"Files were incorrect for test {attribute.attribute_name}{test}") + print(f"Expected {attribute.expect_files_to_be_equal[test]}") + print(f"arg1: {arg1}") + print(f"arg2: {arg2}") + file_test = False + + if output_test and file_test: + print(f"Successful test {attribute.attribute_name}{test}") + + else: + print(f"Failed to execute {attribute.attribute_name}{test}") diff --git a/xml_converter/tests/test_bool.cpp b/xml_converter/tests/test_bool.cpp deleted file mode 100644 index 58d56003..00000000 --- a/xml_converter/tests/test_bool.cpp +++ /dev/null @@ -1,60 +0,0 @@ -#include -#include - -class BoolTest : public ::testing::Test { - protected: - void SetUp() override { - } - void TearDown() override { - } -}; - -bool compare_files(const std::string& file1_path, const std::string& file2_path) { - std::ifstream file1(file1_path); - std::ifstream file2(file2_path); - - if (!file1.is_open() || !file2.is_open()) { - std::cerr << "Error: Could not open one or both of the files." << std::endl; - return false; - } - - char char1, char2; - bool files_are_equal = true; - - while (true) { - char1 = file1.get(); - char2 = file2.get(); - - if (char1 != char2) { - files_are_equal = false; - break; - } - - if (file1.eof() && file2.eof()) { - break; // Reached the end of both files - } - - // If one file reaches the end before the other, they are not equal - if (file1.eof() || file2.eof()) { - files_are_equal = false; - break; - } - } - - file1.close(); - file2.close(); - - return files_are_equal; -} - -TEST_F(BoolTest, ValueIsFalse) { - std::string xml_input = "./test_cases/can_fade_tests/can_fade_is_false.xml"; - std::string xml_output = "./test_cases/test_output/can_fade_tests/can_fade_is_false.xml"; - EXPECT_TRUE(compare_files(xml_input, xml_output)); -} - -TEST_F(BoolTest, ValueIsZero) { - std::string xml_input = "./test_cases/can_fade_tests/can_fade_is_zero.xml"; - std::string xml_output = "./test_cases/test_output/can_fade_tests/can_fade_is_zero.xml"; - EXPECT_FALSE(compare_files(xml_input, xml_output)); -} From c64357e38ba46d23c697e84ca3605aecb3887eed Mon Sep 17 00:00:00 2001 From: klingbolt Date: Wed, 11 Oct 2023 00:04:49 -0400 Subject: [PATCH 7/9] Changed names of categories in xml to match new file names --- .../test_cases/can_fade_tests/can_fade_is_correct.xml | 4 ++-- .../test_cases/can_fade_tests/can_fade_is_different_file.xml | 4 ++-- .../test_cases/can_fade_tests/can_fade_is_wrong_output.xml | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/xml_converter/test_cases/can_fade_tests/can_fade_is_correct.xml b/xml_converter/test_cases/can_fade_tests/can_fade_is_correct.xml index 180562d2..5b9b8ac2 100644 --- a/xml_converter/test_cases/can_fade_tests/can_fade_is_correct.xml +++ b/xml_converter/test_cases/can_fade_tests/can_fade_is_correct.xml @@ -1,8 +1,8 @@ - + - + diff --git a/xml_converter/test_cases/can_fade_tests/can_fade_is_different_file.xml b/xml_converter/test_cases/can_fade_tests/can_fade_is_different_file.xml index 0fb5af9f..74c8c540 100644 --- a/xml_converter/test_cases/can_fade_tests/can_fade_is_different_file.xml +++ b/xml_converter/test_cases/can_fade_tests/can_fade_is_different_file.xml @@ -1,8 +1,8 @@ - + - + diff --git a/xml_converter/test_cases/can_fade_tests/can_fade_is_wrong_output.xml b/xml_converter/test_cases/can_fade_tests/can_fade_is_wrong_output.xml index 180562d2..cc6d9221 100644 --- a/xml_converter/test_cases/can_fade_tests/can_fade_is_wrong_output.xml +++ b/xml_converter/test_cases/can_fade_tests/can_fade_is_wrong_output.xml @@ -1,8 +1,8 @@ - + - + From 0cb87a44a7ea17316ff6ca83f2a715b8bdf38c87 Mon Sep 17 00:00:00 2001 From: klingbolt Date: Wed, 11 Oct 2023 00:18:38 -0400 Subject: [PATCH 8/9] Added to the Regex --- xml_converter/tests/attribute_testing.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/xml_converter/tests/attribute_testing.py b/xml_converter/tests/attribute_testing.py index 8b6a90ad..bdce57cb 100755 --- a/xml_converter/tests/attribute_testing.py +++ b/xml_converter/tests/attribute_testing.py @@ -64,9 +64,9 @@ def are_arrays_equal(array1, array2) -> bool: arg_output_xml: str = "--output-taco-path" chrono_patterns = [ - "The taco parse function took [0-9]+ milliseconds to run", - "The xml write function took [0-9]+ milliseconds to run", - "The protobuf write function took [0-9]+ milliseconds to run" + "^The taco parse function took [0-9]+ milliseconds to run$", + "^The xml write function took [0-9]+ milliseconds to run$", + "^The protobuf write function took [0-9]+ milliseconds to run$" ] From 9e21d431ca289fdb90039d500c042e288d42d6ce Mon Sep 17 00:00:00 2001 From: klingbolt Date: Sun, 15 Oct 2023 19:18:57 -0400 Subject: [PATCH 9/9] Moved test case values to seperate JSON --- xml_converter/tests/attribute_testing.py | 152 +++++++++++------------ xml_converter/tests/test_cases.json | 26 ++++ 2 files changed, 96 insertions(+), 82 deletions(-) create mode 100644 xml_converter/tests/test_cases.json diff --git a/xml_converter/tests/attribute_testing.py b/xml_converter/tests/attribute_testing.py index bdce57cb..c57e7f50 100755 --- a/xml_converter/tests/attribute_testing.py +++ b/xml_converter/tests/attribute_testing.py @@ -1,24 +1,16 @@ -from dataclasses import dataclass import subprocess +import json import re import os -from typing import Dict, List +from typing import List # Path to compiled C++ executable xml_converter: str = "../build/xml_converter" +json_file_path: str = "./test_cases.json" -@dataclass -class Attribute_Tests: - attribute_name: str - attribute_path: str - tests: List[str] - output_result: Dict[str, List[str]] - expect_output_to_be_equal: Dict[str, bool] - expect_files_to_be_equal: Dict[str, bool] - -def run_xml_converter(*args): +def run_xml_converter(*args: str) -> str: try: # Build the command to execute the C++ program with the desired function and arguments cmd = [xml_converter] + list(args) @@ -33,10 +25,10 @@ def run_xml_converter(*args): return result.stderr except Exception as e: print("Error:", e) - return None + return "" -def are_files_equal(file_path1, file_path2) -> bool: +def are_files_equal(file_path1: str, file_path2: str) -> bool: try: with open(file_path1, 'rb') as file1, open(file_path2, 'rb') as file2: content1 = file1.read() @@ -49,7 +41,7 @@ def are_files_equal(file_path1, file_path2) -> bool: return False -def are_arrays_equal(array1, array2) -> bool: +def are_arrays_equal(array1: List[str], array2: List[str]) -> bool: if len(array1) != len(array2): return False @@ -71,70 +63,66 @@ def are_arrays_equal(array1, array2) -> bool: if __name__ == "__main__": - # TODO: This will need to be extracted as more attributes and tests are added - attribute: Attribute_Tests = Attribute_Tests( - attribute_name="can_fade", - attribute_path="../test_cases/can_fade_tests", - tests=["_is_correct", "_is_different_file", "_is_wrong_output"], - output_result={ - "_is_correct": ['Loading taco pack ../test_cases/can_fade_tests/can_fade_is_correct.xml'], - "_is_different_file": ['Loading taco pack ../test_cases/can_fade_tests/can_fade_is_different_file.xml'], - "_is_wrong_output": ['WRONG'] - }, - expect_output_to_be_equal={ - "_is_correct": True, - "_is_different_file": True, - "_is_wrong_output": False, - }, - expect_files_to_be_equal={ - "_is_correct": True, - "_is_different_file": False, - "_is_wrong_output": True, - } - ) - - # Ensure that the test output directory is created - if not os.path.exists(attribute.attribute_path + "-output/"): - try: - os.makedirs(attribute.attribute_path + "-output/") - except OSError as e: - print(f"Error: {e}") - - for test in attribute.tests: - arg1 = attribute.attribute_path + "/" + attribute.attribute_name + test + ".xml" - arg2 = attribute.attribute_path + "-output/" + attribute.attribute_name + test + ".xml" - - result = run_xml_converter(arg_input_xml, arg1, arg_output_xml, arg2) - if result is not None: - # Remove lines about how long it took to run - split_result = result.split("\n") - filtered_array = [] - for line in split_result: - match_found = False - for pattern in chrono_patterns: - if re.search(pattern, line) or line == "": - match_found = True - break - if not match_found: - filtered_array.append(line) - - output_test: bool = True - file_test: bool = True - - if are_arrays_equal(filtered_array, attribute.output_result[test]) != attribute.expect_output_to_be_equal[test]: - print(f"Output did not match for test {attribute.attribute_name}{test}") - print(f"Expected {attribute.output_result[test]} and got {filtered_array}") - output_test = False - - if are_files_equal(arg1, arg2) != attribute.expect_files_to_be_equal[test]: - print(f"Files were incorrect for test {attribute.attribute_name}{test}") - print(f"Expected {attribute.expect_files_to_be_equal[test]}") - print(f"arg1: {arg1}") - print(f"arg2: {arg2}") - file_test = False - - if output_test and file_test: - print(f"Successful test {attribute.attribute_name}{test}") - - else: - print(f"Failed to execute {attribute.attribute_name}{test}") + try: + with open(json_file_path, 'r') as json_file: + data = json.load(json_file) + + for attribute_name in data.keys(): + print(attribute_name) + attribute_data = data[attribute_name] + + # Ensure that the test output directory is created + if not os.path.exists(attribute_data["path"] + "-output/"): + try: + os.makedirs(attribute_data["path"] + "-output/") + except OSError as e: + print(f"Error: {e}") + + for test in attribute_data["tests"].keys(): + arg1 = attribute_data["path"] + "/" + attribute_name + test + ".xml" + arg2 = attribute_data["path"] + "-output/" + attribute_name + test + ".xml" + + result = run_xml_converter(arg_input_xml, arg1, arg_output_xml, arg2) + if result != "": + # Remove lines that state how long it took to run + split_result = result.split("\n") + filtered_array: List[str] = [] + for line in split_result: + match_found = False + if line == "": + continue + for pattern in chrono_patterns: + if re.search(pattern, line): + match_found = True + break + if not match_found: + filtered_array.append(line) + + output_test: bool = True + file_test: bool = True + expected_results = attribute_data["tests"][test] + + if are_arrays_equal(filtered_array, expected_results["output_result"]) != expected_results["expect_output_to_be_equal"]: + print(f"Output did not match for test {attribute_name}{test}") + print(f"Expected {expected_results['output_result']} and got {filtered_array}") + output_test = False + + if are_files_equal(arg1, arg2) != expected_results["expect_files_to_be_equal"]: + print(f"Files were incorrect for test {attribute_name}{test}") + print(f"Expected {expected_results['expect_files_to_be_equal']}") + print(f"arg1: {arg1}") + print(f"arg2: {arg2}") + file_test = False + + if output_test and file_test: + print(f"Success: test {attribute_name}{test}") + + else: + print(f"Failed to execute {attribute_name}{test}") + + except FileNotFoundError: + print(f"The file '{json_file_path}' does not exist.") + except json.JSONDecodeError as e: + print(f"JSON decoding error: {e}") + except Exception as e: + print(f"An error occurred: {e}") diff --git a/xml_converter/tests/test_cases.json b/xml_converter/tests/test_cases.json new file mode 100644 index 00000000..a8542865 --- /dev/null +++ b/xml_converter/tests/test_cases.json @@ -0,0 +1,26 @@ +{ + "can_fade": { + "path": "../test_cases/can_fade_tests", + "tests": { + "_is_correct": { + "output_result": [ + "Loading taco pack ../test_cases/can_fade_tests/can_fade_is_correct.xml" + ], + "expect_output_to_be_equal": true, + "expect_files_to_be_equal": true + }, + "_is_different_file": { + "output_result": [ + "Loading taco pack ../test_cases/can_fade_tests/can_fade_is_different_file.xml" + ], + "expect_output_to_be_equal": true, + "expect_files_to_be_equal": false + }, + "_is_wrong_output": { + "output_result": [], + "expect_output_to_be_equal": false, + "expect_files_to_be_equal": true + } + } + } +}