From 62fd6fefb20584697bd3185c280abd5ae5688001 Mon Sep 17 00:00:00 2001 From: Enrico Seiler Date: Thu, 29 Feb 2024 11:07:17 +0100 Subject: [PATCH] [TEST] Add tests --- test/unit/io/sam_file/format_bam_test.cpp | 7 ++ test/unit/io/sam_file/format_sam_test.cpp | 5 ++ .../sam_file_format_test_template.hpp | 65 ++++++++++++++++++- 3 files changed, 76 insertions(+), 1 deletion(-) diff --git a/test/unit/io/sam_file/format_bam_test.cpp b/test/unit/io/sam_file/format_bam_test.cpp index aeb88f084c..9f4d04f4f9 100644 --- a/test/unit/io/sam_file/format_bam_test.cpp +++ b/test/unit/io/sam_file/format_bam_test.cpp @@ -37,6 +37,13 @@ struct sam_file_read : public sam_file_data '\x33', '\x34', '\x0a', '\x01', '\x00', '\x00', '\x00', '\x04', '\x00', '\x00', '\x00', '\x72', '\x65', '\x66', '\x00', '\x22', '\x00', '\x00', '\x00'}; + std::string unknown_tag_header{ + '\x42', '\x41', '\x4d', '\x01', '\x25', '\x00', '\x00', '\x00', '\x40', '\x48', '\x44', '\x09', '\x56', + '\x4e', '\x3a', '\x31', '\x2e', '\x36', '\x09', '\x70', '\x62', '\x3a', '\x35', '\x2e', '\x30', '\x2e', + '\x30', '\x0a', '\x40', '\x53', '\x51', '\x09', '\x53', '\x4e', '\x3a', '\x72', '\x65', '\x66', '\x09', + '\x4c', '\x4e', '\x3a', '\x33', '\x34', '\x0a', '\x01', '\x00', '\x00', '\x00', '\x04', '\x00', '\x00', + '\x00', '\x72', '\x65', '\x66', '\x00', '\x22', '\x00', '\x00', '\x00'}; + std::string big_header_input{ '\x42', '\x41', '\x4D', '\x01', '\xB7', '\x01', '\x00', '\x00', '\x40', '\x48', '\x44', '\x09', '\x56', '\x4E', '\x3A', '\x31', '\x2E', '\x36', '\x09', '\x53', '\x4F', '\x3A', '\x63', '\x6F', '\x6F', '\x72', '\x64', '\x69', diff --git a/test/unit/io/sam_file/format_sam_test.cpp b/test/unit/io/sam_file/format_sam_test.cpp index c68d9ee5b8..c954b9d640 100644 --- a/test/unit/io/sam_file/format_sam_test.cpp +++ b/test/unit/io/sam_file/format_sam_test.cpp @@ -18,6 +18,11 @@ struct sam_file_read : public sam_file_data std::string minimal_header{ R"(@HD VN:1.6 @SQ SN:ref LN:34 +)"}; + + std::string unknown_tag_header{ + R"(@HD VN:1.6 pb:5.0.0 +@SQ SN:ref LN:34 )"}; std::string big_header_input{ diff --git a/test/unit/io/sam_file/sam_file_format_test_template.hpp b/test/unit/io/sam_file/sam_file_format_test_template.hpp index c207b8bf86..4e90c62950 100644 --- a/test/unit/io/sam_file/sam_file_format_test_template.hpp +++ b/test/unit/io/sam_file/sam_file_format_test_template.hpp @@ -19,6 +19,7 @@ #include #include #include +#include using seqan3::operator""_cigar_operation; using seqan3::operator""_dna5; @@ -356,6 +357,67 @@ TYPED_TEST_P(sam_file_read, issue2423) EXPECT_EQ(fin.header().ref_dict.size(), 64u); } +TYPED_TEST_P(sam_file_read, unknown_header_tag) +{ + // Default: Warnings to cerr + { + typename TestFixture::stream_type istream{this->unknown_tag_header}; + seqan3::sam_file_input fin{istream, TypeParam{}}; + testing::internal::CaptureStdout(); + testing::internal::CaptureStderr(); + EXPECT_NO_THROW(fin.begin()); + EXPECT_EQ(testing::internal::GetCapturedStdout(), ""); + EXPECT_EQ(testing::internal::GetCapturedStderr(), "Unsupported SAM header tag in @HD: pb\n"); + } + // Redirect to cout + { + typename TestFixture::stream_type istream{this->unknown_tag_header}; + seqan3::sam_file_input fin{istream, TypeParam{}}; + fin.options.warning_stream = std::addressof(std::cout); + testing::internal::CaptureStdout(); + testing::internal::CaptureStderr(); + EXPECT_NO_THROW(fin.begin()); + EXPECT_EQ(testing::internal::GetCapturedStdout(), "Unsupported SAM header tag in @HD: pb\n"); + EXPECT_EQ(testing::internal::GetCapturedStderr(), ""); + } + // Redirect to file + { + seqan3::test::tmp_directory tmp{}; + auto filename = tmp.path() / "warnings.txt"; + + // Scope for ofstream-RAII + { + std::ofstream warning_file{filename}; + ASSERT_TRUE(warning_file.good()); + + typename TestFixture::stream_type istream{this->unknown_tag_header}; + seqan3::sam_file_input fin{istream, TypeParam{}}; + fin.options.warning_stream = std::addressof(warning_file); + testing::internal::CaptureStdout(); + testing::internal::CaptureStderr(); + EXPECT_NO_THROW(fin.begin()); + EXPECT_EQ(testing::internal::GetCapturedStdout(), ""); + EXPECT_EQ(testing::internal::GetCapturedStderr(), ""); + } + + std::ifstream warning_file{filename}; + ASSERT_TRUE(warning_file.good()); + std::string content{std::istreambuf_iterator(warning_file), std::istreambuf_iterator()}; + EXPECT_EQ(content, "Unsupported SAM header tag in @HD: pb\n"); + } + // Silence + { + typename TestFixture::stream_type istream{this->unknown_tag_header}; + seqan3::sam_file_input fin{istream, TypeParam{}}; + fin.options.warning_stream = nullptr; + testing::internal::CaptureStdout(); + testing::internal::CaptureStderr(); + EXPECT_NO_THROW(fin.begin()); + EXPECT_EQ(testing::internal::GetCapturedStdout(), ""); + EXPECT_EQ(testing::internal::GetCapturedStderr(), ""); + } +} + // ---------------------------------------------------------------------------- // sam_file_write // ---------------------------------------------------------------------------- @@ -703,7 +765,8 @@ REGISTER_TYPED_TEST_SUITE_P(sam_file_read, cigar_vector, format_error_ref_id_not_in_reference_information, format_error_uneven_hexadecimal_tag, - issue2423); + issue2423, + unknown_header_tag); REGISTER_TYPED_TEST_SUITE_P(sam_file_write, no_records,