# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import pytest

from pytest_testdox import constants


class TestReport(object):

    @pytest.fixture
    def testdir(self, testdir):
        testdir.makeconftest("""
            pytest_plugins = 'pytest_testdox.plugin'
        """)
        return testdir

    def test_should_print_a_green_passing_test(self, testdir):
        testdir.makepyfile("""
            def test_a_feature_is_working():
                assert True
        """)

        result = testdir.runpytest('--testdox')

        expected = '\033[92m ✓ a feature is working\033[0m'
        assert expected in result.stdout.str()

    def test_should_print_a_red_failing_test(self, testdir):
        testdir.makepyfile("""
            def test_a_failed_test_of_a_feature():
                assert False
        """)

        result = testdir.runpytest('--testdox')
        expected = '\033[91m ✗ a failed test of a feature\033[0m'

        assert expected in result.stdout.str()

    def test_should_print_a_yellow_skipped_test(self, testdir):
        testdir.makepyfile("""
            import pytest

            @pytest.mark.skip
            def test_a_skipped_test():
                pass
        """)

        result = testdir.runpytest('--testdox')
        expected = '\033[93m » a skipped test\033[0m'

        assert expected in result.stdout.str()

    def test_should_not_print_colors_when_disabled_by_parameter(self, testdir):
        testdir.makepyfile("""
            def test_a_feature_is_working():
                assert True
        """)
        result = testdir.runpytest('--color=no', '--testdox')

        assert '\033[92m' not in result.stdout.str()

    def test_should_output_plaintext_using_a_config_option(self, testdir):
        testdir.makeini("""
            [pytest]
            testdox_format=plaintext
        """)
        testdir.makepyfile("""
            def test_a_feature_is_working():
                assert True
        """)
        result = testdir.runpytest('--testdox')

        expected = '\033[92m [x] a feature is working\033[0m'
        assert expected in result.stdout.str()

    def test_should_print_the_test_class_name(self, testdir):
        testdir.makepyfile("""
            class TestFoo(object):
                def test_foo(self):
                    pass

            class TestBar(object):
                def test_bar(self):
                    pass
        """)
        result = testdir.runpytest('--testdox')

        lines = result.stdout.get_lines_after('Foo')
        assert '✓ foo' in lines[0]

        lines = result.stdout.get_lines_after('Bar')
        assert '✓ bar' in lines[0]

    def test_should_print_the_module_name_of_a_test_without_class(
        self,
        testdir
    ):
        testdir.makefile('.py', test_module_name="""
            def test_a_failed_test_of_a_feature():
                assert False
        """)

        result = testdir.runpytest('--testdox')
        result.stdout.fnmatch_lines(['module name'])

    def test_should_print_test_summary(self, testdir):
        testdir.makefile('.py', test_module_name="""
            def test_a_passing_test():
                assert True
        """)

        result = testdir.runpytest('--testdox')
        assert '1 passed' in result.stdout.str()

    def test_should_use_python_patterns_configuration(self, testdir):
        testdir.makeini("""
            [pytest]
            python_classes=Describe*
            python_files=*spec.py
            python_functions=it*
        """)
        testdir.makefile('.py', module_spec="""
            class DescribeTest(object):
                def it_runs(self):
                    pass
        """)

        result = testdir.runpytest('--testdox')

        lines = result.stdout.get_lines_after('Test')
        assert '✓ runs' in lines[0]

    def test_should_override_test_titles_with_title_mark(
        self,
        testdir
    ):
        testdir.makefile('.py', test_module_name="""
            import pytest

            @pytest.mark.{}('''
                My Title
                My precious title
            ''')
            def test_a_passing_test():
                assert True
        """.format(
            constants.TITLE_MARK
        ))

        result = testdir.runpytest('--testdox')

        assert 'My Title\n   My precious title' in result.stdout.str()

    def test_should_override_class_names_with_class_name_mark(
        self,
        testdir
    ):
        testdir.makefile('.py', test_module_name="""
            import pytest

            @pytest.mark.{}('''
                My Class
                My precious class
            ''')
            class TestClass(object):

                def test_foo(self):
                    pass
        """.format(
            constants.CLASS_NAME_MARK
        ))

        result = testdir.runpytest('--testdox')

        assert 'My Class\nMy precious class' in result.stdout.str()

    def test_should_override_test_titles_with_title_mark_parametrize(
        self,
        testdir
    ):
        testdir.makefile('.py', test_module_name="""
            import pytest

            @pytest.mark.parametrize('par', ['param1', 'param2'])
            @pytest.mark.{}('''
                My Title
                My precious title
            ''')
            def test_a_passing_test(par):
                assert True
        """.format(
            constants.TITLE_MARK
        ))

        result = testdir.runpytest('--testdox')

        assert 'My Title\n   My precious title[param' in result.stdout.str()

@@ -5243,84 +5243,35 @@
 %7B%7D('
-''%0A                My Title%0A                My precious title%0A            ''
+should pass with parameters
 ')%0A 
@@ -5473,38 +5473,35 @@
 rt '
-My Title%5Cn   My precious title
+should pass with parameters
 %5Bpar

Use a simple title to focus on parametrized part