diff --git a/tests/test_parser_record.py b/tests/test_parser_record.py index b8d5f02..42e0b89 100644 --- a/tests/test_parser_record.py +++ b/tests/test_parser_record.py @@ -4,8 +4,10 @@ import io import sys +import pytest from vcfpy import parser +from vcfpy import exceptions __author__ = "Manuel Holtgrewe " @@ -178,3 +180,16 @@ def test_missing_pass(recwarn): RESULT = p.parse_next_record() assert str(RESULT) == EXPECTED assert list(recwarn) == [] + + +def test_parse_line_invalid_number_of_fields(): + """Test parsing VCF file exception message""" + HEADER = "##fileformat=VCFv4.2\n#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT\n" + LINES = "20\t1\t.\tC\tG\t.\tPASS\t.\tGT\n" + EXPECTED = "The line contains an invalid number of fields. Was 9 but expected 8" + p = parser.Parser(io.StringIO(HEADER + LINES), "") + p.parse_header() + with pytest.raises(exceptions.InvalidRecordException) as record_error: + p.parse_next_record() + + assert EXPECTED in str(record_error.value) diff --git a/vcfpy/parser.py b/vcfpy/parser.py index 83a815e..4a0879d 100644 --- a/vcfpy/parser.py +++ b/vcfpy/parser.py @@ -514,7 +514,7 @@ def _split_line(self, line_str): raise exceptions.InvalidRecordException( ( "The line contains an invalid number of fields. Was " - "{} but expected {}\n{}".format(len(arr), 9 + len(self.samples.names), line_str) + "{} but expected {}\n{}".format(len(arr), self.expected_fields, line_str) ) ) return arr