Skip to content

Commit

Permalink
[TEST] Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eseiler committed Feb 29, 2024
1 parent 835abba commit 62fd6fe
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
7 changes: 7 additions & 0 deletions test/unit/io/sam_file/format_bam_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ struct sam_file_read<seqan3::format_bam> : 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',
Expand Down
5 changes: 5 additions & 0 deletions test/unit/io/sam_file/format_sam_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ struct sam_file_read<seqan3::format_sam> : 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{
Expand Down
65 changes: 64 additions & 1 deletion test/unit/io/sam_file/sam_file_format_test_template.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <seqan3/test/expect_range_eq.hpp>
#include <seqan3/test/pretty_printing.hpp>
#include <seqan3/test/streambuf.hpp>
#include <seqan3/test/tmp_directory.hpp>

using seqan3::operator""_cigar_operation;
using seqan3::operator""_dna5;
Expand Down Expand Up @@ -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<char>(warning_file), std::istreambuf_iterator<char>()};
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
// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 62fd6fe

Please sign in to comment.