예제 #1
0
    def test_load_from_dict(self):

        # Load the data
        coverage_data = CoverageData()
        coverage_data.load_from_dict('/root_dir', '', self.TEST_COVERAGE_DICT)

        # Check that it gets parsed correctly
        self.assertEqual(coverage_data.src_list(),
                         [u'/root_dir/src1.js', u'/root_dir/subdir/src2.js'])

        self.assertEqual(coverage_data.line_dict_for_src('/root_dir/src1.js'),
                         {0: True, 2: True, 3: False, 5: True})

        self.assertEqual(coverage_data.line_dict_for_src('/root_dir/subdir/src2.js'),
                         {0: True, 1: True, 2: True, 3: False})
예제 #2
0
    def test_covered_and_uncovered_src(self, mock_num_file_lines):

        # Set an expected source file
        coverage_data = CoverageData()
        coverage_data.add_expected_src('/root_dir', 'uncovered.js')

        # Configure the num file lines for the source file
        num_lines = 10
        mock_num_file_lines.return_value = num_lines

        # Provide coverage info for other source files,
        # but not the one we passed to the constructor
        coverage_data.load_from_dict('/root_dir', '', self.TEST_COVERAGE_DICT)

        # Expect that all sources are reported (including the uncovered src)
        self.assertEqual(coverage_data.src_list(), 
                         [u'/root_dir/src1.js',
                          u'/root_dir/subdir/src2.js',
                          u'/root_dir/uncovered.js'])

        # Expect that the source is reported as 0% covered
        self.assertEqual(coverage_data.line_dict_for_src('/root_dir/uncovered.js'),
                         {line_num: False for line_num in range(num_lines)})

        # Expect that total coverage is smaller because
        # of the uncovered file.
        expected_coverage = 6.0 / (8.0 + num_lines)
        self.assertEqual(coverage_data.total_coverage(), expected_coverage)
예제 #3
0
    def test_different_root_dirs(self):

        # Load data from two different root dirs
        coverage_data = CoverageData()
        coverage_data.load_from_dict('/root_1', '', self.TEST_COVERAGE_DICT)
        coverage_data.load_from_dict('/root_2', '', self.TEST_COVERAGE_DICT)

        # We should get two separate sources
        self.assertEqual(coverage_data.src_list(),
                         ['/root_1/src1.js', '/root_1/subdir/src2.js',
                          '/root_2/src1.js', '/root_2/subdir/src2.js'])

        # But the data in each should be the same
        expected = {0: True, 2: True, 3: False, 5: True}
        self.assertEqual(coverage_data.line_dict_for_src('/root_1/src1.js'),
                         expected)
        self.assertEqual(coverage_data.line_dict_for_src('/root_2/src1.js'),
                         expected)
예제 #4
0
    def test_missing_key(self):

        # Load data with a JSON dict that is missing the line keys
        coverage_data = CoverageData()
        coverage_data.load_from_dict('root_dir', '',
                                     {'/src1': {'missing key': True}})

        # Expect that no coverage information is loaded
        self.assertEqual(coverage_data.src_list(), [])
        self.assertIs(coverage_data.line_dict_for_src('root_dir/src1'), None)
예제 #5
0
    def test_multiple_load_from_dict(self):

        # Load the data
        coverage_data = CoverageData()
        coverage_data.load_from_dict('/root_dir', '', self.TEST_COVERAGE_DICT)

        # Load additional data covering the lines that were uncovered
        lines = [0, 1, 0, 1, 1, None]
        coverage_data.load_from_dict('/root_dir', '', {'/src1.js': {'lineData': lines}})

        # Check that the two sources are combined correctly
        expected = {0: True, 1: True, 2: True, 3: True, 4: True, 5: True}
        self.assertEqual(coverage_data.line_dict_for_src('/root_dir/src1.js'), expected)
예제 #6
0
    def test_uncovered_src(self, mock_num_file_lines):

        # Set an expected source file
        coverage_data = CoverageData()
        coverage_data.add_expected_src('/root_dir', 'src1.js')

        # Configure the num file lines for the source file
        num_lines = 10
        mock_num_file_lines.return_value = num_lines

        # Provide no coverage information (did NOT call `load_from_dict()`)

        # Expect that the source is still reported
        self.assertEqual(coverage_data.src_list(), ['/root_dir/src1.js'])
        self.assertEqual(coverage_data.rel_src_path('/root_dir/src1.js'), 'src1.js')

        # Expect that the source is reported as 0% covered
        self.assertEqual(coverage_data.line_dict_for_src('/root_dir/src1.js'),
                         {line_num: False for line_num in range(num_lines)})

        # Expect that total coverage is 0%
        self.assertEqual(coverage_data.total_coverage(), 0.0)