<commit_before>import datetime
import sys
from io import StringIO
from unittest import mock
import sure
import contexts
from contexts import reporting
from .. import tools


class WhenPrintingASuccessSummary:
    def in_the_context_of_a_successful_run(self):
        self.stringio = StringIO()
        self.reporter = reporting.cli.SummarisingReporter(self.stringio)

        self.ctx1 = tools.create_context("")
        self.ctx2 = tools.create_context("")
        self.assertion1 = tools.create_assertion("")
        self.assertion2 = tools.create_assertion("")
        self.assertion3 = tools.create_assertion("")

        self.reporter.context_started(self.ctx1)
        self.reporter.assertion_started(self.assertion1)
        self.reporter.assertion_passed(self.assertion1)
        self.reporter.assertion_started(self.assertion2)
        self.reporter.assertion_passed(self.assertion2)
        self.reporter.context_ended(self.ctx1)

        self.reporter.context_started(self.ctx2)
        self.reporter.assertion_started(self.assertion3)
        self.reporter.assertion_passed(self.assertion3)
        self.reporter.context_ended(self.ctx2)

    def because_the_suite_ends(self):
        self.reporter.suite_ended(tools.create_suite())

    def it_should_print_the_summary_to_the_stream(self):
        self.stringio.getvalue().should.equal(
"""
----------------------------------------------------------------------
PASSED!
2 contexts, 3 assertions
""")


class WhenPrintingAFailureSummary:
    def establish_that_some_tests_have_failed(self):
        self.stringio = StringIO()
        self.reporter = reporting.cli.SummarisingReporter(self.stringio)

        self.context = tools.create_context("made.up_context")
        self.assertion = tools.create_assertion("made.up.assertion_1")
        tb1 = [('made_up_file.py', 3, 'made_up_function', 'frame1'),
               ('another_made_up_file.py', 2, 'another_made_up_function', 'frame2')]
        self.exception = tools.build_fake_assertion_error(tb1, "Gotcha")

        self.reporter.context_started(self.context)
        self.reporter.assertion_started(self.assertion)
        self.reporter.assertion_failed(self.assertion, self.exception)
        self.reporter.context_ended(self.context)

    def because_the_suite_ends(self):
        self.reporter.suite_ended(tools.create_suite)

    def it_should_print_the_failure_tracebacks(self):
        self.stringio.getvalue().should.equal("""
----------------------------------------------------------------------
made up context
  FAIL: made up assertion 1
    Traceback (most recent call last):
      File "made_up_file.py", line 3, in made_up_function
        frame1
      File "another_made_up_file.py", line 2, in another_made_up_function
        frame2
    test.tools.FakeAssertionError: Gotcha
----------------------------------------------------------------------
FAILED!
1 context, 1 assertion: 1 failed, 0 errors
""")


# TODO: break up this test
class WhenCapturingStdOut:
    def context(self):
        self.real_stdout = sys.stdout
        self.real_stderr = sys.stderr
        sys.stdout = self.fake_stdout = StringIO()
        sys.stderr = self.fake_stderr = StringIO()

        self.ctx1 = tools.create_context("context")
        self.ctx2 = tools.create_context("context")
        self.ctx3 = tools.create_context("context")
        self.ctx4 = tools.create_context("context")
        self.assertion1 = tools.create_assertion("assertion")
        self.assertion2 = tools.create_assertion("assertion")
        self.assertion3 = tools.create_assertion("assertion")
        self.assertion4 = tools.create_assertion("assertion")
        self.assertion5 = tools.create_assertion("assertion")

        self.stringio = StringIO()
        self.reporter = reporting.cli.StdOutCapturingReporter(self.stringio)
        self.notifier = reporting.shared.ReporterNotifier(self.reporter)

    def because_we_print_some_stuff(self):
        with self.notifier.run_suite(tools.create_suite()):
            with self.notifier.run_context(self.ctx1):
                print("passing context")
                with self.notifier.run_assertion(self.assertion1):
                    print("passing assertion")
                    print("to stderr", file=sys.stderr)

            with self.notifier.run_context(self.ctx2):
                print("failing context")
                with self.notifier.run_assertion(self.assertion2):
                    print("failing assertion")
                    raise tools.FakeAssertionError()
                with self.notifier.run_assertion(self.assertion3):
                    print("erroring assertion")
                    raise tools.FakeException()

            with self.notifier.run_context(self.ctx3):
                print("erroring context")
                with self.notifier.run_assertion(self.assertion4):
                    print("assertion in erroring context")
                raise tools.FakeException()

            with self.notifier.run_context(self.ctx4):
                with self.notifier.run_assertion(self.assertion5):
                    # don't print anything
                   raise tools.FakeAssertionError()

    def it_should_not_print_anything_to_stdout(self):
        self.fake_stdout.getvalue().should.be.empty

    def it_should_let_stderr_through(self):
        self.fake_stderr.getvalue().should.equal("to stderr\n")

    def it_should_output_the_captured_stdout_for_the_failures(self):
        self.stringio.getvalue().should.equal("""
----------------------------------------------------------------------<commit_msg>No longer using the Notifier in the stdout tests<commit_after>import datetime
import sys
from io import StringIO
from unittest import mock
import sure
import contexts
from contexts import reporting
from .. import tools


class WhenPrintingASuccessSummary:
    def in_the_context_of_a_successful_run(self):
        self.stringio = StringIO()
        self.reporter = reporting.cli.SummarisingReporter(self.stringio)

        self.ctx1 = tools.create_context("")
        self.ctx2 = tools.create_context("")
        self.assertion1 = tools.create_assertion("")
        self.assertion2 = tools.create_assertion("")
        self.assertion3 = tools.create_assertion("")

        self.reporter.context_started(self.ctx1)
        self.reporter.assertion_started(self.assertion1)
        self.reporter.assertion_passed(self.assertion1)
        self.reporter.assertion_started(self.assertion2)
        self.reporter.assertion_passed(self.assertion2)
        self.reporter.context_ended(self.ctx1)

        self.reporter.context_started(self.ctx2)
        self.reporter.assertion_started(self.assertion3)
        self.reporter.assertion_passed(self.assertion3)
        self.reporter.context_ended(self.ctx2)

    def because_the_suite_ends(self):
        self.reporter.suite_ended(tools.create_suite())

    def it_should_print_the_summary_to_the_stream(self):
        self.stringio.getvalue().should.equal(
"""
----------------------------------------------------------------------
PASSED!
2 contexts, 3 assertions
""")


class WhenPrintingAFailureSummary:
    def establish_that_some_tests_have_failed(self):
        self.stringio = StringIO()
        self.reporter = reporting.cli.SummarisingReporter(self.stringio)

        self.context = tools.create_context("made.up_context")
        self.assertion = tools.create_assertion("made.up.assertion_1")
        tb1 = [('made_up_file.py', 3, 'made_up_function', 'frame1'),
               ('another_made_up_file.py', 2, 'another_made_up_function', 'frame2')]
        self.exception = tools.build_fake_assertion_error(tb1, "Gotcha")

        self.reporter.context_started(self.context)
        self.reporter.assertion_started(self.assertion)
        self.reporter.assertion_failed(self.assertion, self.exception)
        self.reporter.context_ended(self.context)

    def because_the_suite_ends(self):
        self.reporter.suite_ended(tools.create_suite)

    def it_should_print_the_failure_tracebacks(self):
        self.stringio.getvalue().should.equal("""
----------------------------------------------------------------------
made up context
  FAIL: made up assertion 1
    Traceback (most recent call last):
      File "made_up_file.py", line 3, in made_up_function
        frame1
      File "another_made_up_file.py", line 2, in another_made_up_function
        frame2
    test.tools.FakeAssertionError: Gotcha
----------------------------------------------------------------------
FAILED!
1 context, 1 assertion: 1 failed, 0 errors
""")


# TODO: break up this test
class WhenCapturingStdOut:
    def context(self):
        self.real_stdout = sys.stdout
        self.real_stderr = sys.stderr
        sys.stdout = self.fake_stdout = StringIO()
        sys.stderr = self.fake_stderr = StringIO()

        self.suite = tools.create_suite()
        self.ctx1 = tools.create_context("context")
        self.ctx2 = tools.create_context("context")
        self.ctx3 = tools.create_context("context")
        self.ctx4 = tools.create_context("context")
        self.assertion1 = tools.create_assertion("assertion")
        self.assertion2 = tools.create_assertion("assertion")
        self.assertion3 = tools.create_assertion("assertion")
        self.assertion4 = tools.create_assertion("assertion")
        self.assertion5 = tools.create_assertion("assertion")

        self.stringio = StringIO()
        self.reporter = reporting.cli.StdOutCapturingReporter(self.stringio)

    def because_we_print_some_stuff(self):
        self.reporter.suite_started(self.suite)
        self.reporter.context_started(self.ctx1)
        print("passing context")
        self.reporter.assertion_started(self.assertion1)
        print("passing assertion")
        print("to stderr", file=sys.stderr)
        self.reporter.assertion_passed(self.assertion1)
        self.reporter.context_ended(self.ctx1)

        self.reporter.context_started(self.ctx2)
        print("failing context")
        self.reporter.assertion_started(self.assertion2)
        print("failing assertion")
        self.reporter.assertion_failed(self.assertion2, tools.FakeAssertionError())
        self.reporter.assertion_started(self.assertion3)
        print("erroring assertion")
        self.reporter.assertion_errored(self.assertion3, tools.FakeException())
        self.reporter.context_ended(self.ctx2)

        self.reporter.context_started(self.ctx3)
        print("erroring context")
        self.reporter.assertion_started(self.assertion4)
        print("assertion in erroring context")
        self.reporter.assertion_passed(self.assertion4)
        self.reporter.context_errored(self.ctx3, tools.FakeException())

        self.reporter.context_started(self.ctx4)
        self.reporter.assertion_started(self.assertion5)
        # don't print anything
        self.reporter.assertion_failed(self.assertion5, tools.FakeAssertionError())
        self.reporter.context_ended(self.ctx4)

        self.reporter.suite_ended(self.suite)

    def it_should_not_print_anything_to_stdout(self):
        self.fake_stdout.getvalue().should.be.empty

    def it_should_let_stderr_through(self):
        self.fake_stderr.getvalue().should.equal("to stderr\n")

    def it_should_output_the_captured_stdout_for_the_failures(self):
        self.stringio.getvalue().should.equal("""
----------------------------------------------------------------------
{'fraction_non_alphanumeric': 0.1443677357336363, 'fraction_numerical': 0.009700253430044568, 'mean_word_length': 2.6180840973759087, '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': '2343772', 'n_tokens_mistral': 3425, 'n_tokens_neox': 3275, 'n_words': 484}