Skip to content

Commit 62fd6fe

Browse files
committed
[TEST] Add tests
1 parent 835abba commit 62fd6fe

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

test/unit/io/sam_file/format_bam_test.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ struct sam_file_read<seqan3::format_bam> : public sam_file_data
3737
'\x33', '\x34', '\x0a', '\x01', '\x00', '\x00', '\x00', '\x04', '\x00', '\x00', '\x00',
3838
'\x72', '\x65', '\x66', '\x00', '\x22', '\x00', '\x00', '\x00'};
3939

40+
std::string unknown_tag_header{
41+
'\x42', '\x41', '\x4d', '\x01', '\x25', '\x00', '\x00', '\x00', '\x40', '\x48', '\x44', '\x09', '\x56',
42+
'\x4e', '\x3a', '\x31', '\x2e', '\x36', '\x09', '\x70', '\x62', '\x3a', '\x35', '\x2e', '\x30', '\x2e',
43+
'\x30', '\x0a', '\x40', '\x53', '\x51', '\x09', '\x53', '\x4e', '\x3a', '\x72', '\x65', '\x66', '\x09',
44+
'\x4c', '\x4e', '\x3a', '\x33', '\x34', '\x0a', '\x01', '\x00', '\x00', '\x00', '\x04', '\x00', '\x00',
45+
'\x00', '\x72', '\x65', '\x66', '\x00', '\x22', '\x00', '\x00', '\x00'};
46+
4047
std::string big_header_input{
4148
'\x42', '\x41', '\x4D', '\x01', '\xB7', '\x01', '\x00', '\x00', '\x40', '\x48', '\x44', '\x09', '\x56', '\x4E',
4249
'\x3A', '\x31', '\x2E', '\x36', '\x09', '\x53', '\x4F', '\x3A', '\x63', '\x6F', '\x6F', '\x72', '\x64', '\x69',

test/unit/io/sam_file/format_sam_test.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ struct sam_file_read<seqan3::format_sam> : public sam_file_data
1818
std::string minimal_header{
1919
R"(@HD VN:1.6
2020
@SQ SN:ref LN:34
21+
)"};
22+
23+
std::string unknown_tag_header{
24+
R"(@HD VN:1.6 pb:5.0.0
25+
@SQ SN:ref LN:34
2126
)"};
2227

2328
std::string big_header_input{

test/unit/io/sam_file/sam_file_format_test_template.hpp

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <seqan3/test/expect_range_eq.hpp>
2020
#include <seqan3/test/pretty_printing.hpp>
2121
#include <seqan3/test/streambuf.hpp>
22+
#include <seqan3/test/tmp_directory.hpp>
2223

2324
using seqan3::operator""_cigar_operation;
2425
using seqan3::operator""_dna5;
@@ -356,6 +357,67 @@ TYPED_TEST_P(sam_file_read, issue2423)
356357
EXPECT_EQ(fin.header().ref_dict.size(), 64u);
357358
}
358359

360+
TYPED_TEST_P(sam_file_read, unknown_header_tag)
361+
{
362+
// Default: Warnings to cerr
363+
{
364+
typename TestFixture::stream_type istream{this->unknown_tag_header};
365+
seqan3::sam_file_input fin{istream, TypeParam{}};
366+
testing::internal::CaptureStdout();
367+
testing::internal::CaptureStderr();
368+
EXPECT_NO_THROW(fin.begin());
369+
EXPECT_EQ(testing::internal::GetCapturedStdout(), "");
370+
EXPECT_EQ(testing::internal::GetCapturedStderr(), "Unsupported SAM header tag in @HD: pb\n");
371+
}
372+
// Redirect to cout
373+
{
374+
typename TestFixture::stream_type istream{this->unknown_tag_header};
375+
seqan3::sam_file_input fin{istream, TypeParam{}};
376+
fin.options.warning_stream = std::addressof(std::cout);
377+
testing::internal::CaptureStdout();
378+
testing::internal::CaptureStderr();
379+
EXPECT_NO_THROW(fin.begin());
380+
EXPECT_EQ(testing::internal::GetCapturedStdout(), "Unsupported SAM header tag in @HD: pb\n");
381+
EXPECT_EQ(testing::internal::GetCapturedStderr(), "");
382+
}
383+
// Redirect to file
384+
{
385+
seqan3::test::tmp_directory tmp{};
386+
auto filename = tmp.path() / "warnings.txt";
387+
388+
// Scope for ofstream-RAII
389+
{
390+
std::ofstream warning_file{filename};
391+
ASSERT_TRUE(warning_file.good());
392+
393+
typename TestFixture::stream_type istream{this->unknown_tag_header};
394+
seqan3::sam_file_input fin{istream, TypeParam{}};
395+
fin.options.warning_stream = std::addressof(warning_file);
396+
testing::internal::CaptureStdout();
397+
testing::internal::CaptureStderr();
398+
EXPECT_NO_THROW(fin.begin());
399+
EXPECT_EQ(testing::internal::GetCapturedStdout(), "");
400+
EXPECT_EQ(testing::internal::GetCapturedStderr(), "");
401+
}
402+
403+
std::ifstream warning_file{filename};
404+
ASSERT_TRUE(warning_file.good());
405+
std::string content{std::istreambuf_iterator<char>(warning_file), std::istreambuf_iterator<char>()};
406+
EXPECT_EQ(content, "Unsupported SAM header tag in @HD: pb\n");
407+
}
408+
// Silence
409+
{
410+
typename TestFixture::stream_type istream{this->unknown_tag_header};
411+
seqan3::sam_file_input fin{istream, TypeParam{}};
412+
fin.options.warning_stream = nullptr;
413+
testing::internal::CaptureStdout();
414+
testing::internal::CaptureStderr();
415+
EXPECT_NO_THROW(fin.begin());
416+
EXPECT_EQ(testing::internal::GetCapturedStdout(), "");
417+
EXPECT_EQ(testing::internal::GetCapturedStderr(), "");
418+
}
419+
}
420+
359421
// ----------------------------------------------------------------------------
360422
// sam_file_write
361423
// ----------------------------------------------------------------------------
@@ -703,7 +765,8 @@ REGISTER_TYPED_TEST_SUITE_P(sam_file_read,
703765
cigar_vector,
704766
format_error_ref_id_not_in_reference_information,
705767
format_error_uneven_hexadecimal_tag,
706-
issue2423);
768+
issue2423,
769+
unknown_header_tag);
707770

708771
REGISTER_TYPED_TEST_SUITE_P(sam_file_write,
709772
no_records,

0 commit comments

Comments
 (0)