blob: c6f215548fcd31754a6bbc544764e9a0ffb9d5d7 [file] [log] [blame]
#!/usr/bin/python3
import unittest
import lcov_parser
from io import StringIO
def expected_coverage(covered, uncovered):
lines = {} # Type: Dict[int, bool]
for cov in covered:
lines[cov] = True
for uncov in uncovered:
lines[uncov] = False
return lines
class LcovParserTest(unittest.TestCase):
def test_small_dir_passes(self):
want_data = {} # Type: Dict[Text, lcov_parser.FileData]
want_data['/kunit/test/strerror.c'] = expected_coverage([156], [154])
want_data['/kunit/test/strerror-test.c'] = expected_coverage([14], [16])
with StringIO(_TEST_FILE_DATA.strip()) as input:
test_name, coverage_data = lcov_parser.parse(input)
self.assertEqual(test_name, 'kunit_presubmit_tests')
self.assertEqual(coverage_data, want_data)
def test_raises_errors(self):
with self.assertRaises(lcov_parser.LcovSyntaxError):
with StringIO('') as input_data:
test_name, data = lcov_parser.parse(input_data)
with self.assertRaises(lcov_parser.LcovSyntaxError):
with StringIO('invalid_first_line\n') as input_data:
test_name, data = lcov_parser.parse(input_data)
with self.assertRaises(lcov_parser.LcovSyntaxError):
with StringIO('TN:valid_line\nDA:invalid_second,str\n') as input_data:
test_name, data = lcov_parser.parse(input_data)
_TEST_FILE_DATA = """
TN:kunit_presubmit_tests
SF:/kunit/test/strerror.c
DA:154,0
DA:156,10
end_of_record
SF:/kunit/test/strerror-test.c
DA:14,1
DA:16,0
end_of_record
"""
if __name__ == '__main__':
unittest.main()