예제 #1
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)
예제 #2
0
    def test_invalid_dict(self):

        # Pass in some invalid data
        for invalid in ["", "invalid", ["list"], 5, None]:
            with self.assertRaises(ValueError):
                coverage_data = CoverageData()
                coverage_data.load_from_dict('root_dir', '', invalid)
예제 #3
0
    def test_get_relative_unknown_path(self):

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

        # Unknown path returns None, even if in the root dir
        self.assertIs(coverage_data.rel_src_path('unknown'), None)
        self.assertIs(coverage_data.rel_src_path('/root_dir/unknown'), None)
예제 #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_get_relative_src_path(self):

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

        # Check that we can retrieve the relative source path
        self.assertEqual(coverage_data.rel_src_path(u'/root_dir/src1.js'),
                         'prepend/to/path/src1.js')
        self.assertEqual(coverage_data.rel_src_path(u'/root_dir/subdir/src2.js'),
                         'prepend/to/path/subdir/src2.js')
예제 #6
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)
예제 #7
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})
예제 #8
0
    def assert_output_equals(self, coverage_dict, expected_output):
        """
        Asserts that the output from the coverage reporter
        is equal to `expected_output`.

        `coverage_dict` is a dict of the form:

            { SRC_PATH: [ COVER_INFO, ...]

        where COVER_INFO is either:

        * None: no coverage info
        * integer: number of times line was hit

        for example:

            { 'src.js': [ 1, 0, None, 1]}

        This assumes that the output is XML-parseable; it will
        parse the XML to ignore whitespace between elements.
        """

        # Munge the dict into the right format
        coverage_dict = {src_path: {'lineData': line_data}
                         for src_path, line_data in coverage_dict.items()}

        # Create a `CoverageData` instance.
        # Since this involves no network or filesystem access
        # we don't bother mocking it.
        data = CoverageData()
        data.load_from_dict('root_dir', '', coverage_dict)

        # Write the report to the output file in the temp directory
        self.reporter.write_report(data)

        # Read the data back in from the output file
        with open(self.OUTPUT_FILE_NAME) as output_file:
            output_str = output_file.read()

        # Run the reports through the XML parser to normalize format
        output_str = etree.tostring(etree.fromstring(output_str))
        expected_output = etree.tostring(etree.fromstring(expected_output))

        # Check that the the output matches what we expect
        self.assertEqual(output_str, expected_output)
예제 #9
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)
예제 #10
0
    def test_total_coverage(self):
        coverage_data = CoverageData()
        coverage_data.load_from_dict('root_dir', '', self.TEST_COVERAGE_DICT)

        # Total coverage is 6/8 = 0.75
        self.assertEqual(coverage_data.total_coverage(), 0.75)
예제 #11
0
    def test_coverage_for_src(self):
        coverage_data = CoverageData()
        coverage_data.load_from_dict('root_dir', '', self.TEST_COVERAGE_DICT)

        # The coverage for root_dir/src1.js is 3/4 = 0.75
        self.assertEqual(coverage_data.coverage_for_src('root_dir/src1.js'), 0.75)