From b78a5d918231b49a729e2dec7b4bac6a1ee75b9d Mon Sep 17 00:00:00 2001 From: Tom Nixon Date: Fri, 30 Apr 2021 16:40:25 +0100 Subject: [PATCH] tests: read test data directly from the source dir These used to be copied to the build dir, but they tended to get out of date, as they were only copied during configuration. The test data directory is baked into the test executables, so they can be ran outside of ctest easily. This adds a function to get the full path of a test data, so if we want to change this again it should be a bit easier. --- tests/CMakeLists.txt | 14 +++++++++----- tests/helper/file_comparator.hpp | 4 +++- tests/test_config.hpp.in | 9 +++++++++ ...udio_block_format_direct_speakers_tests.cpp | 18 +++++++++++------- ...xml_parser_audio_block_format_hoa_tests.cpp | 3 ++- ...parser_audio_block_format_objects_tests.cpp | 4 +++- .../xml_parser_audio_channel_format_tests.cpp | 9 +++++---- tests/xml_parser_audio_content_tests.cpp | 8 +++++--- tests/xml_parser_audio_object_tests.cpp | 11 +++++++---- tests/xml_parser_audio_pack_format_tests.cpp | 8 ++++---- tests/xml_parser_audio_programme_tests.cpp | 5 +++-- tests/xml_parser_audio_stream_format_tests.cpp | 9 +++++---- tests/xml_parser_audio_track_format_tests.cpp | 9 +++++---- tests/xml_parser_audio_track_uid_tests.cpp | 5 +++-- tests/xml_parser_common_definitions_tests.cpp | 3 ++- ...parser_find_audio_format_extended_tests.cpp | 9 +++++---- .../xml_parser_unresolved_references_tests.cpp | 3 ++- 17 files changed, 83 insertions(+), 48 deletions(-) create mode 100644 tests/test_config.hpp.in diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3a42ef59..30781ce3 100755 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,5 +1,11 @@ include(${PROJECT_SOURCE_DIR}/submodules/catch2.cmake) +set(TEST_DATA_DIR "${CMAKE_CURRENT_SOURCE_DIR}/test_data") +configure_file(test_config.hpp.in test_config.hpp) + +# working directory for tests containing misc. outputs +file(MAKE_DIRECTORY "${PROJECT_BINARY_DIR}/test_data_out") + # --- unit tests --- function(add_adm_test name) add_executable(${name} ${name}.cpp) @@ -10,18 +16,16 @@ function(add_adm_test name) ) target_include_directories(${name} PRIVATE ${PROJECT_SOURCE_DIR}/submodules + # for including test_config.h + ${CMAKE_CURRENT_BINARY_DIR} ) add_test( NAME ${name} COMMAND $ - WORKING_DIRECTORY "${PROJECT_BINARY_DIR}/test_data" + WORKING_DIRECTORY "${PROJECT_BINARY_DIR}/test_data_out" ) endfunction() -# copy test files so unit test can find them relative to their running location -# when executed as "test" target -file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/test_data" DESTINATION ${PROJECT_BINARY_DIR}) - add_adm_test("adm_auto_parenting_tests") add_adm_test("adm_common_definitions_tests") add_adm_test("adm_document_tests") diff --git a/tests/helper/file_comparator.hpp b/tests/helper/file_comparator.hpp index 847fcc02..81d43910 100644 --- a/tests/helper/file_comparator.hpp +++ b/tests/helper/file_comparator.hpp @@ -2,6 +2,7 @@ #include #include #include +#include "test_config.hpp" class FileComparator : public Catch::MatcherBase { public: @@ -11,7 +12,8 @@ class FileComparator : public Catch::MatcherBase { virtual bool match(const std::string& received) const override { bool check_successful = false; - std::ifstream acceptedFile(filename_ + ".accepted." + extension_); + std::ifstream acceptedFile( + data_file(filename_ + ".accepted." + extension_)); if (acceptedFile.is_open()) { std::string acceptedStr((std::istreambuf_iterator(acceptedFile)), std::istreambuf_iterator()); diff --git a/tests/test_config.hpp.in b/tests/test_config.hpp.in new file mode 100644 index 00000000..8c65747a --- /dev/null +++ b/tests/test_config.hpp.in @@ -0,0 +1,9 @@ +#pragma once +#include + +#cmakedefine TEST_DATA_DIR "${TEST_DATA_DIR}" + +inline std::string data_file(const std::string &fname) { + // use macro form so we get an error if it's not defined in cmake + return TEST_DATA_DIR "/" + fname; +} diff --git a/tests/xml_parser_audio_block_format_direct_speakers_tests.cpp b/tests/xml_parser_audio_block_format_direct_speakers_tests.cpp index 09447b12..86e89485 100644 --- a/tests/xml_parser_audio_block_format_direct_speakers_tests.cpp +++ b/tests/xml_parser_audio_block_format_direct_speakers_tests.cpp @@ -4,10 +4,12 @@ #include "adm/elements/audio_channel_format.hpp" #include "adm/parse.hpp" #include "adm/errors.hpp" +#include "test_config.hpp" TEST_CASE("xml_parser/audio_block_format_direct_speakers") { using namespace adm; - auto document = parseXml("xml_parser/audio_block_format_direct_speakers.xml"); + auto document = + parseXml(data_file("xml_parser/audio_block_format_direct_speakers.xml")); auto channelFormat = document->lookup(parseAudioChannelFormatId("AC_00011001")); REQUIRE(channelFormat->get() @@ -31,8 +33,8 @@ TEST_CASE("xml_parser/audio_block_format_direct_speakers") { TEST_CASE("xml_parser/audio_block_format_direct_speakers_cartesian") { using namespace adm; - auto document = - parseXml("xml_parser/audio_block_format_direct_speakers_cartesian.xml"); + auto document = parseXml( + data_file("xml_parser/audio_block_format_direct_speakers_cartesian.xml")); REQUIRE(!document->getElements().empty()); auto channelFormat = document->lookup(parseAudioChannelFormatId("AC_00011001")); @@ -84,15 +86,17 @@ TEST_CASE("xml_parser/audio_block_format_direct_speakers_cartesian") { TEST_CASE("xml_parser/audio_block_format_direct_speakers_cartesian_bad_bound") { using namespace adm; REQUIRE_THROWS_AS( - parseXml("xml_parser/" - "audio_block_format_direct_speakers_cartesian_bad_bound.xml"), + parseXml(data_file( + "xml_parser/" + "audio_block_format_direct_speakers_cartesian_bad_bound.xml")), error::XmlParsingError); } TEST_CASE("xml_parser/audio_block_format_direct_speakers_cartesian_bad_coord") { using namespace adm; REQUIRE_THROWS_AS( - parseXml("xml_parser/" - "audio_block_format_direct_speakers_cartesian_bad_coord.xml"), + parseXml(data_file( + "xml_parser/" + "audio_block_format_direct_speakers_cartesian_bad_coord.xml")), error::XmlParsingError); } diff --git a/tests/xml_parser_audio_block_format_hoa_tests.cpp b/tests/xml_parser_audio_block_format_hoa_tests.cpp index a05e8936..0d3df925 100644 --- a/tests/xml_parser_audio_block_format_hoa_tests.cpp +++ b/tests/xml_parser_audio_block_format_hoa_tests.cpp @@ -4,10 +4,11 @@ #include "adm/elements/audio_channel_format.hpp" #include "adm/elements/frequency.hpp" #include "adm/parse.hpp" +#include "test_config.hpp" TEST_CASE("xml_parser/audio_block_format_hoa") { using namespace adm; - auto document = parseXml("xml_parser/audio_block_format_hoa.xml"); + auto document = parseXml(data_file("xml_parser/audio_block_format_hoa.xml")); auto channelFormat = document->lookup(parseAudioChannelFormatId("AC_00041001")); REQUIRE(channelFormat->get() diff --git a/tests/xml_parser_audio_block_format_objects_tests.cpp b/tests/xml_parser_audio_block_format_objects_tests.cpp index d1708767..6c2fe392 100644 --- a/tests/xml_parser_audio_block_format_objects_tests.cpp +++ b/tests/xml_parser_audio_block_format_objects_tests.cpp @@ -4,11 +4,13 @@ #include "adm/elements/audio_channel_format.hpp" #include "adm/elements/frequency.hpp" #include "adm/parse.hpp" +#include "test_config.hpp" TEST_CASE("xml_parser/audio_block_format_objects") { using namespace adm; { - auto document = parseXml("xml_parser/audio_block_format_objects.xml"); + auto document = + parseXml(data_file("xml_parser/audio_block_format_objects.xml")); auto channelFormat = document->lookup(parseAudioChannelFormatId("AC_00031001")); REQUIRE(channelFormat->get() diff --git a/tests/xml_parser_audio_channel_format_tests.cpp b/tests/xml_parser_audio_channel_format_tests.cpp index 43cfa804..2d0b1b82 100644 --- a/tests/xml_parser_audio_channel_format_tests.cpp +++ b/tests/xml_parser_audio_channel_format_tests.cpp @@ -5,11 +5,12 @@ #include "adm/elements/frequency.hpp" #include "adm/parse.hpp" #include "adm/errors.hpp" +#include "test_config.hpp" TEST_CASE("xml_parser/audio_channel_format") { using namespace adm; { - auto document = parseXml("xml_parser/audio_channel_format.xml"); + auto document = parseXml(data_file("xml_parser/audio_channel_format.xml")); auto channelFormat = document->lookup(parseAudioChannelFormatId("AC_00031002")); @@ -27,7 +28,7 @@ TEST_CASE("xml_parser/audio_channel_format") { } TEST_CASE("xml_parser/audio_channel_format_duplicate_id") { - REQUIRE_THROWS_AS( - adm::parseXml("xml_parser/audio_channel_format_duplicate_id.xml"), - adm::error::XmlParsingDuplicateId); + REQUIRE_THROWS_AS(adm::parseXml(data_file( + "xml_parser/audio_channel_format_duplicate_id.xml")), + adm::error::XmlParsingDuplicateId); } diff --git a/tests/xml_parser_audio_content_tests.cpp b/tests/xml_parser_audio_content_tests.cpp index 3ae57676..2e44a1be 100644 --- a/tests/xml_parser_audio_content_tests.cpp +++ b/tests/xml_parser_audio_content_tests.cpp @@ -4,10 +4,11 @@ #include "adm/elements/audio_content.hpp" #include "adm/parse.hpp" #include "adm/errors.hpp" +#include "test_config.hpp" TEST_CASE("xml_parser/audio_content") { using namespace adm; - auto document = parseXml("xml_parser/audio_content.xml"); + auto document = parseXml(data_file("xml_parser/audio_content.xml")); auto audioContent = document->lookup(parseAudioContentId("ACO_1001")); REQUIRE(audioContent->has() == true); @@ -26,6 +27,7 @@ TEST_CASE("xml_parser/audio_content") { } TEST_CASE("xml_parser/audio_content_duplicate_id") { - REQUIRE_THROWS_AS(adm::parseXml("xml_parser/audio_content_duplicate_id.xml"), - adm::error::XmlParsingDuplicateId); + REQUIRE_THROWS_AS( + adm::parseXml(data_file("xml_parser/audio_content_duplicate_id.xml")), + adm::error::XmlParsingDuplicateId); } diff --git a/tests/xml_parser_audio_object_tests.cpp b/tests/xml_parser_audio_object_tests.cpp index a8b9fbed..2779e470 100644 --- a/tests/xml_parser_audio_object_tests.cpp +++ b/tests/xml_parser_audio_object_tests.cpp @@ -5,10 +5,11 @@ #include "adm/elements/audio_object.hpp" #include "adm/parse.hpp" #include "adm/errors.hpp" +#include "test_config.hpp" TEST_CASE("xml_parser/audio_object") { using namespace adm; - auto document = parseXml("xml_parser/audio_object.xml"); + auto document = parseXml(data_file("xml_parser/audio_object.xml")); auto audioObject = document->lookup(parseAudioObjectId("AO_1001")); REQUIRE(audioObject->has() == true); @@ -32,13 +33,15 @@ TEST_CASE("xml_parser/audio_object") { } TEST_CASE("xml_parser/audio_object_duplicate_id") { - REQUIRE_THROWS_AS(adm::parseXml("xml_parser/audio_object_duplicate_id.xml"), - adm::error::XmlParsingDuplicateId); + REQUIRE_THROWS_AS( + adm::parseXml(data_file("xml_parser/audio_object_duplicate_id.xml")), + adm::error::XmlParsingDuplicateId); } TEST_CASE("xml_parser/audio_object_interaction") { using namespace adm; - auto document = adm::parseXml("xml_parser/audio_object_interaction.xml"); + auto document = + adm::parseXml(data_file("xml_parser/audio_object_interaction.xml")); auto audioObjects = document->getElements(); REQUIRE(audioObjects.size() == 2); diff --git a/tests/xml_parser_audio_pack_format_tests.cpp b/tests/xml_parser_audio_pack_format_tests.cpp index 8bbf4366..ad6001a6 100644 --- a/tests/xml_parser_audio_pack_format_tests.cpp +++ b/tests/xml_parser_audio_pack_format_tests.cpp @@ -5,10 +5,11 @@ #include "adm/elements/audio_pack_format.hpp" #include "adm/parse.hpp" #include "adm/errors.hpp" +#include "test_config.hpp" TEST_CASE("xml_parser/audio_pack_format") { using namespace adm; - auto document = parseXml("../test_data/xml_parser/audio_pack_format.xml"); + auto document = parseXml(data_file("xml_parser/audio_pack_format.xml")); auto audioPackFormat = document->lookup(parseAudioPackFormatId("AP_00011001")); @@ -26,14 +27,13 @@ TEST_CASE("xml_parser/audio_pack_format") { TEST_CASE("xml_parser/audio_pack_format_duplicate_id") { REQUIRE_THROWS_AS( - adm::parseXml("../test_data/xml_parser/audio_pack_format_duplicate_id.xml"), + adm::parseXml(data_file("xml_parser/audio_pack_format_duplicate_id.xml")), adm::error::XmlParsingDuplicateId); } - TEST_CASE("xml_parser/audio_pack_format_hoa") { using namespace adm; - auto document = parseXml("../test_data/xml_parser/audio_pack_format_hoa.xml"); + auto document = parseXml(data_file("xml_parser/audio_pack_format_hoa.xml")); auto audioPackFormatGeneric = document->lookup(parseAudioPackFormatId("AP_00041001")); diff --git a/tests/xml_parser_audio_programme_tests.cpp b/tests/xml_parser_audio_programme_tests.cpp index 8dcae725..3582f4fe 100644 --- a/tests/xml_parser_audio_programme_tests.cpp +++ b/tests/xml_parser_audio_programme_tests.cpp @@ -4,11 +4,12 @@ #include "adm/elements/audio_programme.hpp" #include "adm/parse.hpp" #include "adm/errors.hpp" +#include "test_config.hpp" TEST_CASE("xml_parser/audio_programme") { { using namespace adm; - auto document = parseXml("xml_parser/audio_programme.xml"); + auto document = parseXml(data_file("xml_parser/audio_programme.xml")); auto audioProgramme = document->lookup(parseAudioProgrammeId("APR_1001")); REQUIRE(audioProgramme->has() == true); @@ -43,6 +44,6 @@ TEST_CASE("xml_parser/audio_programme") { TEST_CASE("xml_parser/audio_programme_duplicate_id") { REQUIRE_THROWS_AS( - adm::parseXml("xml_parser/audio_programme_duplicate_id.xml"), + adm::parseXml(data_file("xml_parser/audio_programme_duplicate_id.xml")), adm::error::XmlParsingDuplicateId); } diff --git a/tests/xml_parser_audio_stream_format_tests.cpp b/tests/xml_parser_audio_stream_format_tests.cpp index 384e151b..0cac9253 100644 --- a/tests/xml_parser_audio_stream_format_tests.cpp +++ b/tests/xml_parser_audio_stream_format_tests.cpp @@ -5,10 +5,11 @@ #include "adm/elements/frequency.hpp" #include "adm/parse.hpp" #include "adm/errors.hpp" +#include "test_config.hpp" TEST_CASE("xml_parser/audio_stream_format") { using namespace adm; - auto document = parseXml("xml_parser/audio_stream_format.xml"); + auto document = parseXml(data_file("xml_parser/audio_stream_format.xml")); auto streamFormat = document->lookup(parseAudioStreamFormatId("AS_00031001")); REQUIRE(streamFormat->get() .get() == 0x1001u); @@ -19,7 +20,7 @@ TEST_CASE("xml_parser/audio_stream_format") { } TEST_CASE("xml_parser/audio_stream_format_duplicate_id") { - REQUIRE_THROWS_AS( - adm::parseXml("xml_parser/audio_stream_format_duplicate_id.xml"), - adm::error::XmlParsingDuplicateId); + REQUIRE_THROWS_AS(adm::parseXml(data_file( + "xml_parser/audio_stream_format_duplicate_id.xml")), + adm::error::XmlParsingDuplicateId); } diff --git a/tests/xml_parser_audio_track_format_tests.cpp b/tests/xml_parser_audio_track_format_tests.cpp index 1f49739c..8f22da8e 100644 --- a/tests/xml_parser_audio_track_format_tests.cpp +++ b/tests/xml_parser_audio_track_format_tests.cpp @@ -5,10 +5,11 @@ #include "adm/elements/frequency.hpp" #include "adm/parse.hpp" #include "adm/errors.hpp" +#include "test_config.hpp" TEST_CASE("xml_parser/audio_track_format") { using namespace adm; - auto document = parseXml("xml_parser/audio_track_format.xml"); + auto document = parseXml(data_file("xml_parser/audio_track_format.xml")); auto trackFormat = document->lookup(parseAudioTrackFormatId("AT_00030001_01")); REQUIRE( @@ -24,7 +25,7 @@ TEST_CASE("xml_parser/audio_track_format") { } TEST_CASE("xml_parser/audio_track_format_duplicate_id") { - REQUIRE_THROWS_AS( - adm::parseXml("xml_parser/audio_track_format_duplicate_id.xml"), - adm::error::XmlParsingDuplicateId); + REQUIRE_THROWS_AS(adm::parseXml(data_file( + "xml_parser/audio_track_format_duplicate_id.xml")), + adm::error::XmlParsingDuplicateId); } diff --git a/tests/xml_parser_audio_track_uid_tests.cpp b/tests/xml_parser_audio_track_uid_tests.cpp index bba975a4..0db3fb9a 100644 --- a/tests/xml_parser_audio_track_uid_tests.cpp +++ b/tests/xml_parser_audio_track_uid_tests.cpp @@ -4,10 +4,11 @@ #include "adm/elements/audio_track_uid.hpp" #include "adm/parse.hpp" #include "adm/errors.hpp" +#include "test_config.hpp" TEST_CASE("xml_parser/audio_track_uid") { using namespace adm; - auto document = parseXml("xml_parser/audio_track_uid.xml"); + auto document = parseXml(data_file("xml_parser/audio_track_uid.xml")); auto audioTrackUid = document->lookup(parseAudioTrackUidId("ATU_00000001")); REQUIRE(audioTrackUid->has() == true); @@ -22,6 +23,6 @@ TEST_CASE("xml_parser/audio_track_uid") { TEST_CASE("xml_parser/audio_track_uid_duplicate_id") { REQUIRE_THROWS_AS( - adm::parseXml("xml_parser/audio_track_uid_duplicate_id.xml"), + adm::parseXml(data_file("xml_parser/audio_track_uid_duplicate_id.xml")), adm::error::XmlParsingDuplicateId); } diff --git a/tests/xml_parser_common_definitions_tests.cpp b/tests/xml_parser_common_definitions_tests.cpp index f5682a1d..1aec3bf4 100644 --- a/tests/xml_parser_common_definitions_tests.cpp +++ b/tests/xml_parser_common_definitions_tests.cpp @@ -1,7 +1,8 @@ #include #include #include "adm/parse.hpp" +#include "test_config.hpp" TEST_CASE("xml_parser/with_common_definitions") { - adm::parseXml("xml_parser/with_common_definitions.xml"); + adm::parseXml(data_file("xml_parser/with_common_definitions.xml")); } diff --git a/tests/xml_parser_find_audio_format_extended_tests.cpp b/tests/xml_parser_find_audio_format_extended_tests.cpp index 23405fbf..b1422d71 100644 --- a/tests/xml_parser_find_audio_format_extended_tests.cpp +++ b/tests/xml_parser_find_audio_format_extended_tests.cpp @@ -1,17 +1,18 @@ #include #include #include "adm/parse.hpp" +#include "test_config.hpp" TEST_CASE("xml_parser/find_audio_format_extended_ebu") { - adm::parseXml("xml_parser/find_audio_format_extended_ebu.xml"); + adm::parseXml(data_file("xml_parser/find_audio_format_extended_ebu.xml")); } TEST_CASE("find_audio_format_extended_itu") { - adm::parseXml("xml_parser/find_audio_format_extended_itu.xml", + adm::parseXml(data_file("xml_parser/find_audio_format_extended_itu.xml"), adm::xml::ParserOptions::recursive_node_search); } TEST_CASE("xml_parser/find_audio_format_extended_ebu_with_other_metadata") { - adm::parseXml( - "xml_parser/find_audio_format_extended_ebu_with_other_metadata.xml"); + adm::parseXml(data_file( + "xml_parser/find_audio_format_extended_ebu_with_other_metadata.xml")); } diff --git a/tests/xml_parser_unresolved_references_tests.cpp b/tests/xml_parser_unresolved_references_tests.cpp index a39f48b5..f2237bbe 100644 --- a/tests/xml_parser_unresolved_references_tests.cpp +++ b/tests/xml_parser_unresolved_references_tests.cpp @@ -2,6 +2,7 @@ #include #include "adm/parse.hpp" #include "adm/errors.hpp" +#include "test_config.hpp" std::string formatFilepath(const std::string& filename) { std::stringstream ss; @@ -16,7 +17,7 @@ TEST_CASE("xml_parser/unresolved_references") { "audio_track_uid_1", "audio_track_uid_2", "audio_track_format", "audio_stream_format_1", "audio_stream_format_2"}) { SECTION(filename) { - REQUIRE_THROWS_AS(adm::parseXml(formatFilepath(filename)), + REQUIRE_THROWS_AS(adm::parseXml(data_file(formatFilepath(filename))), adm::error::XmlParsingUnresolvedReference); } }