<commit_before>from unittest.case import TestCase
from mock import Mock, call, ANY, MagicMock
from pyspecs import _runner as runner
from tests import examples


class TestSpecs(TestCase):
    def setUp(self):
        self.reporter = Mock()
        self.capture = MagicMock()
        self.loader = Mock()

    def run_spec(self, spec, spec_description):
        self.loader.load_specs.return_value = [spec]
        runner.run_specs(self.loader, self.reporter, self.capture)
        self.mock = self.reporter
        self.calls = self.mock.mock_calls
        self.spec = spec_description

    def test_full_passing(self):
        self.run_spec(
            examples.fully_implemented_and_passing,
            'fully implemented and passing'
        )
        self.mock.assert_has_calls([
            call.success(self.spec, 'given', 'some scenario'),
            call.success(self.spec, 'when', 'something is invoked'),
            call.success(self.spec, 'collect', 'results'),
            call.success(self.spec, 'then', 'something happens'),
            call.success(self.spec, 'then', 'something is calculated'),
            call.success(self.spec, 'after', 'cleanup')]
        )

    def test_spec_with_assertion_failure(self):
        self.run_spec(examples.spec_with_failure, 'spec with failure')

        self.mock.assert_has_calls([
            call.failure(self.spec, 'it should fail', ANY),
            call.success(self.spec, 'then', 'it should run other assertions')],
            any_order=True
        )
        # TODO: make assertions about the exception that was reported

    def test_spec_with_assertion_error(self):
        self.run_spec(
            examples.spec_with_assertion_error, 'spec with assertion error'
        )
        self.mock.assert_has_calls([
            call.error(self.spec, 'then', 'it should raise an error', ANY),
            call.success(self.spec, 'then', 'it should run other assertions'),
            call.success(self.spec, 'after', 'cleanup')
        ])
        # TODO: make assertions about the exception that was reported

    def test_spec_with_error_before_assertions(self):
        self.run_spec(
            examples.spec_with_error_before_assertions,
            'spec with error before assertions'
        )
        self.mock.assert_has_calls([
            call.error(self.spec, 'given', 'an exception is raised', ANY),
            call.success(self.spec, 'after', 'should be executed to clean up')
        ])
        self.assertNotIn(call.success(ANY, 'when', ANY), self.calls)
        self.assertNotIn(call.success(ANY, 'collect', ANY), self.calls)
        self.assertNotIn(call.success(ANY, 'then', ANY), self.calls)
        # TODO: make assertions about the exception that was reported

    def test_spec_with_error_before_assertions_with_no_cleanup(self):
        self.run_spec(
            examples.spec_with_error_before_assertions_without_cleanup,
            'spec with error before assertions without cleanup'
        )
        self.assertSequenceEqual(self.mock.mock_calls, [
            call.error(self.spec, 'when', 'an exception is raised', ANY),
        ])

    def test_spec_with_error_after_assertions(self):
        self.run_spec(
            examples.spec_with_error_after_assertions,
            'spec with error after assertions'
        )
        self.mock.assert_has_calls([
            call.success(self.spec, 'given', 'setup'),
            call.success(self.spec, 'when', 'action'),
            call.success(self.spec, 'collect', 'result'),
            call.success(self.spec, 'then', 'something'),
            call.success(self.spec, 'then', 'something else'),
            call.error(self.spec, 'after', 'an exception is raised', ANY)
        ])
        # TODO: make assertions about the exception that was reported

    def test_spec_with_errors_before_and_after_assertions(self):
        self.run_spec(
            examples.spec_with_error_before_and_after_assertions,
            'spec with error before and after assertions'
        )
        self.reporter.assert_has_calls([
            call.success(self.spec, 'given', 'setup'),
            call.success(self.spec, 'when', 'action'),
            call.error(self.spec, 'collect', 'result', ANY),
            call.error(self.spec, 'after', 'an exception is raised', ANY)
        ])
        # TODO: make assertions about the exceptions that were reported

    def test_spec_with_no_assertions(self):
        self.run_spec(
            examples.spec_without_assertions,
            'spec without assertions'
        )
        self.assertEqual(
            self.reporter.mock_calls[0],
            call.error(self.spec, 'collect steps', 'not implemented', ANY)
        )

        # TODO: exception should indicate that the spec is not implemented

    def test_spec_that_fails_initialization(self):
        self.run_spec(
            examples.spec_that_fails_initialization,
            'spec that fails initialization'
        )
        self.reporter.assert_has_calls([call.error(self.spec, ANY, ANY, ANY)])

        # TODO: exception should indicate that spec steps were not applied correctly.

    def test_improper_use_of_spec_step_methods(self):
        pass
        # more than one given, when, collect, or after step

    def test_inheritance_of_steps(self):
        pass<commit_msg>Filled in assertions verifying caught exceptions.<commit_after>from unittest.case import TestCase
from mock import Mock, call, ANY, MagicMock
from pyspecs import _runner as runner
from pyspecs._should import ShouldError
from tests import examples


class TestSpecs(TestCase):
    def setUp(self):
        self.capture = MagicMock()
        self.loader = Mock()
        self.mock = Mock()
        self.calls = self.mock.mock_calls

    def run_spec(self, spec, spec_description):
        self.spec = spec_description
        self.loader.load_specs.return_value = [spec]
        runner.run_specs(self.loader, self.mock, self.capture)

    def _extract_exception_from_call(self, call_index):
        return self.calls[call_index][1][-1][1]

    def test_full_passing(self):
        self.run_spec(
            examples.fully_implemented_and_passing,
            'fully implemented and passing'
        )
        self.mock.assert_has_calls([
            call.success(self.spec, 'given', 'some scenario'),
            call.success(self.spec, 'when', 'something is invoked'),
            call.success(self.spec, 'collect', 'results'),
            call.success(self.spec, 'then', 'something happens'),
            call.success(self.spec, 'then', 'something is calculated'),
            call.success(self.spec, 'after', 'cleanup')]
        )

    def test_spec_with_assertion_failure(self):
        self.run_spec(examples.spec_with_failure, 'spec with failure')

        self.mock.assert_has_calls([
            call.failure(self.spec, 'it should fail', ANY),
            call.success(self.spec, 'then', 'it should run other assertions')],
            any_order=True
        )
        self.assertIsInstance(self._extract_exception_from_call(0), ShouldError)

    def test_spec_with_assertion_error(self):
        self.run_spec(
            examples.spec_with_assertion_error, 'spec with assertion error'
        )
        self.mock.assert_has_calls([
            call.error(self.spec, 'then', 'it should raise an error', ANY),
            call.success(self.spec, 'then', 'it should run other assertions'),
            call.success(self.spec, 'after', 'cleanup')
        ])
        self.assertIsInstance(self._extract_exception_from_call(0), KeyError)

    def test_spec_with_error_before_assertions(self):
        self.run_spec(
            examples.spec_with_error_before_assertions,
            'spec with error before assertions'
        )
        self.mock.assert_has_calls([
            call.error(self.spec, 'given', 'an exception is raised', ANY),
            call.success(self.spec, 'after', 'should be executed to clean up')
        ])
        self.assertNotIn(call.success(ANY, 'when', ANY), self.calls)
        self.assertNotIn(call.success(ANY, 'collect', ANY), self.calls)
        self.assertNotIn(call.success(ANY, 'then', ANY), self.calls)
        self.assertIsInstance(self._extract_exception_from_call(0), KeyError)

    def test_spec_with_error_before_assertions_with_no_cleanup(self):
        self.run_spec(
            examples.spec_with_error_before_assertions_without_cleanup,
            'spec with error before assertions without cleanup'
        )
        self.assertSequenceEqual(self.mock.mock_calls, [
            call.error(self.spec, 'when', 'an exception is raised', ANY),
        ])
        self.assertIsInstance(self._extract_exception_from_call(0), KeyError)

    def test_spec_with_error_after_assertions(self):
        self.run_spec(
            examples.spec_with_error_after_assertions,
            'spec with error after assertions'
        )
        self.mock.assert_has_calls([
            call.success(self.spec, 'given', 'setup'),
            call.success(self.spec, 'when', 'action'),
            call.success(self.spec, 'collect', 'result'),
            call.success(self.spec, 'then', 'something'),
            call.success(self.spec, 'then', 'something else'),
            call.error(self.spec, 'after', 'an exception is raised', ANY)
        ])
        self.assertIsInstance(self._extract_exception_from_call(-1), KeyError)

    def test_spec_with_errors_before_and_after_assertions(self):
        self.run_spec(
            examples.spec_with_error_before_and_after_assertions,
            'spec with error before and after assertions'
        )
        self.mock.assert_has_calls([
            call.success(self.spec, 'given', 'setup'),
            call.success(self.spec, 'when', 'action'),
            call.error(self.spec, 'collect', 'result', ANY),
            call.error(self.spec, 'after', 'an exception is raised', ANY)
        ])
        self.assertIsInstance(self._extract_exception_from_call(2), KeyError)
        self.assertIsInstance(self._extract_exception_from_call(3), ValueError)

    def test_spec_with_no_assertions(self):
        self.run_spec(
            examples.spec_without_assertions,
            'spec without assertions'
        )
        self.assertEqual(
            self.mock.mock_calls[0],
            call.error(self.spec, 'collect steps', 'not implemented', ANY)
        )
        self.assertIsInstance(
            self._extract_exception_from_call(0), NotImplementedError)

    def test_spec_that_fails_initialization(self):
        self.run_spec(
            examples.spec_that_fails_initialization,
            'spec that fails initialization'
        )
        self.mock.assert_has_calls([call.error(self.spec, ANY, ANY, ANY)])
        self.assertIsInstance(
            self._extract_exception_from_call(0), NotImplementedError)

    def test_improper_use_of_spec_step_methods(self):
        pass
        # more than one given, when, collect, or after step

    def test_inheritance_of_steps(self):
        pass
{'fraction_non_alphanumeric': 0.10441256582531323, 'fraction_numerical': 0.0012711094970038134, 'mean_word_length': 2.6138451443569553, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5480278', 'n_tokens_mistral': 3170, 'n_tokens_neox': 3133, 'n_words': 669}