<commit_before># -*- coding: utf-8 -*-
# pylint: disable=C0103,C0301,R0201,W0401,W0614
#   C0103   Invalid name (setUp(), ...)
#   C0301   Line too long
#   R0201   Method could be a function
#   W0401   Wildcard import
#   W0614   Unused import ... from wildcard import

import struct
import sys
import tempfile
from mock import Mock, patch
from nose.tools import *
from behave.formatter import formatters
from behave.formatter import pretty
from behave.formatter import tag_count
from behave.model import Tag, Feature, Scenario, Step
import unittest

class TestGetTerminalSize(unittest.TestCase):
    def setUp(self):
        try:
            self.ioctl_patch = patch('fcntl.ioctl')
            self.ioctl = self.ioctl_patch.start()
        except ImportError:
            self.ioctl_patch = None
            self.ioctl = None
        self.zero_struct = struct.pack('HHHH', 0, 0, 0, 0)

    def tearDown(self):
        if self.ioctl_patch:
            self.ioctl_patch.stop()

    def test_windows_fallback(self):
        platform = sys.platform
        sys.platform = 'windows'

        eq_(pretty.get_terminal_size(), (80, 24))

        sys.platform = platform

    def test_termios_fallback(self):
        try:
            __pychecker__ = "unusednames=termios"
            import termios
            return
        except ImportError:
            pass

        eq_(pretty.get_terminal_size(), (80, 24))

    def test_exception_in_ioctl(self):
        try:
            import termios
        except ImportError:
            return

        __pychecker__ = "unusednames=args,kwargs"
        def raiser(*args, **kwargs):
            # pylint: disable=W0613
            #   W0613   Unused argument (args, kwargs)
            raise Exception('yeehar!')

        self.ioctl.side_effect = raiser

        eq_(pretty.get_terminal_size(), (80, 24))
        self.ioctl.assert_called_with(0, termios.TIOCGWINSZ, self.zero_struct)

    def test_happy_path(self):
        try:
            import termios
        except ImportError:
            return

        self.ioctl.return_value = struct.pack('HHHH', 17, 23, 5, 5)

        eq_(pretty.get_terminal_size(), (23, 17))
        self.ioctl.assert_called_with(0, termios.TIOCGWINSZ, self.zero_struct)

    def test_zero_size_fallback(self):
        try:
            import termios
        except ImportError:
            return

        self.ioctl.return_value = self.zero_struct

        eq_(pretty.get_terminal_size(), (80, 24))
        self.ioctl.assert_called_with(0, termios.TIOCGWINSZ, self.zero_struct)


def _tf():
    '''Open a temp file that looks a bunch like stdout.
    NOTE: TemporaryFile is deleted when it is closed or deleted.
    '''
    if sys.version_info[0] == 3:
        # in python3 it's got an encoding and accepts new-style strings
        return tempfile.TemporaryFile(mode='w', encoding='UTF-8')

    # pre-python3 it's not got an encoding and accepts encoded data
    # (old-style strings)
    return tempfile.TemporaryFile(mode='w')


class FormatterTests(unittest.TestCase):
    formatter_name = "plain"    #< DEFAULT

    def setUp(self):
        self.config = Mock()
        self.config.color = True
        self.config.format = [self.formatter_name]

    _line = 0
    @property
    def line(self):
        self._line += 1
        return self._line

    def _formatter(self, file, config, counts=None):
        # pylint: disable=W0613,W0622
        #   W0613   Unused argumnet (counts, needed by derived class)
        #   W0622   Redefining built-in file
        __pychecker__ = "unusednames=counts"
        f = formatters.get_formatter(config, file)
        f.uri('<string>')
        return f

    def _feature(self, keyword=u'k\xe9yword', name=u'name', tags=[u'spam', u'ham'],
            location=u'location', description=[u'description'], scenarios=[],
            background=None):
        # pylint: disable=W0102,W0613
        #   W0102   Dangerous default value [...]
        #   W0613   Unused argument (args, kwargs)
        __pychecker__ = "unusednames=location"
        line = self.line
        tags = [Tag(name, line) for name in tags]
        return Feature('<string>', line, keyword, name, tags=tags,
            description=description, scenarios=scenarios,
            background=background)

    def _scenario(self, keyword=u'k\xe9yword', name=u'name', tags=[], steps=[]):
        # pylint: disable=W0102
        #   W0102   Dangerous default value [...]
        line = self.line
        tags = [Tag(name, line) for name in tags]
        return Scenario('<string>', line, keyword, name, tags=tags, steps=steps)

    def _step(self, keyword=u'k\xe9yword', step_type='given', name=u'name', text=None, table=None):
        line = self.line
        return Step('<string>', line, keyword, step_type, name, text=text, table=table)

    def test_feature(self):
        # this test does not actually check the result of the formatting; it
        # just exists to make sure that formatting doesn't explode in the face of
        # unicode and stuff
        p = self._formatter(_tf(), self.config)
        f = self._feature()
        p.feature(f)

    def test_scenario(self):
        p = self._formatter(_tf(), self.config)
        f = self._feature()
        p.feature(f)
        s = self._scenario()
        p.scenario(s)

    def test_step(self):
        p = self._formatter(_tf(), self.config)
        f = self._feature()
        p.feature(f)
        s = self._scenario()
        p.scenario(s)
        s = self._step()
        p.step(s)
        s.status = u'passed'
        p.result(s)


class TestPretty(FormatterTests):
    formatter_name = 'pretty'


class TestPlain(FormatterTests):
    formatter_name = 'plain'


class TestJson(FormatterTests):
    formatter_name = 'json'


class TestTagCount(FormatterTests):
    formatter_name = 'plain'

    def _formatter(self, stream, config, tag_counts=None):
        if tag_counts is None:
            tag_counts = {}
        f = formatters.get_formatter(config, stream)
        f.uri('<string>')
        f = tag_count.TagCountFormatter(f, tag_counts)
        f.uri('<string>')
        return f

    def test_tag_count(self):
        counts = {}
        p = self._formatter(_tf(), self.config, counts)<commit_msg>Add format=progress as testcase.<commit_after># -*- coding: utf-8 -*-
# pylint: disable=C0103,C0301,R0201,W0401,W0614
#   C0103   Invalid name (setUp(), ...)
#   C0301   Line too long
#   R0201   Method could be a function
#   W0401   Wildcard import
#   W0614   Unused import ... from wildcard import

import struct
import sys
import tempfile
from mock import Mock, patch
from nose.tools import *
from behave.formatter import formatters
from behave.formatter import pretty
from behave.formatter import tag_count
from behave.model import Tag, Feature, Scenario, Step
import unittest

class TestGetTerminalSize(unittest.TestCase):
    def setUp(self):
        try:
            self.ioctl_patch = patch('fcntl.ioctl')
            self.ioctl = self.ioctl_patch.start()
        except ImportError:
            self.ioctl_patch = None
            self.ioctl = None
        self.zero_struct = struct.pack('HHHH', 0, 0, 0, 0)

    def tearDown(self):
        if self.ioctl_patch:
            self.ioctl_patch.stop()

    def test_windows_fallback(self):
        platform = sys.platform
        sys.platform = 'windows'

        eq_(pretty.get_terminal_size(), (80, 24))

        sys.platform = platform

    def test_termios_fallback(self):
        try:
            __pychecker__ = "unusednames=termios"
            import termios
            return
        except ImportError:
            pass

        eq_(pretty.get_terminal_size(), (80, 24))

    def test_exception_in_ioctl(self):
        try:
            import termios
        except ImportError:
            return

        __pychecker__ = "unusednames=args,kwargs"
        def raiser(*args, **kwargs):
            # pylint: disable=W0613
            #   W0613   Unused argument (args, kwargs)
            raise Exception('yeehar!')

        self.ioctl.side_effect = raiser

        eq_(pretty.get_terminal_size(), (80, 24))
        self.ioctl.assert_called_with(0, termios.TIOCGWINSZ, self.zero_struct)

    def test_happy_path(self):
        try:
            import termios
        except ImportError:
            return

        self.ioctl.return_value = struct.pack('HHHH', 17, 23, 5, 5)

        eq_(pretty.get_terminal_size(), (23, 17))
        self.ioctl.assert_called_with(0, termios.TIOCGWINSZ, self.zero_struct)

    def test_zero_size_fallback(self):
        try:
            import termios
        except ImportError:
            return

        self.ioctl.return_value = self.zero_struct

        eq_(pretty.get_terminal_size(), (80, 24))
        self.ioctl.assert_called_with(0, termios.TIOCGWINSZ, self.zero_struct)


def _tf():
    '''Open a temp file that looks a bunch like stdout.
    NOTE: TemporaryFile is deleted when it is closed or deleted.
    '''
    if sys.version_info[0] == 3:
        # in python3 it's got an encoding and accepts new-style strings
        return tempfile.TemporaryFile(mode='w', encoding='UTF-8')

    # pre-python3 it's not got an encoding and accepts encoded data
    # (old-style strings)
    return tempfile.TemporaryFile(mode='w')


class FormatterTests(unittest.TestCase):
    formatter_name = "plain"    #< DEFAULT

    def setUp(self):
        self.config = Mock()
        self.config.color = True
        self.config.format = [self.formatter_name]

    _line = 0
    @property
    def line(self):
        self._line += 1
        return self._line

    def _formatter(self, file, config, counts=None):
        # pylint: disable=W0613,W0622
        #   W0613   Unused argumnet (counts, needed by derived class)
        #   W0622   Redefining built-in file
        __pychecker__ = "unusednames=counts"
        f = formatters.get_formatter(config, file)
        f.uri('<string>')
        return f

    def _feature(self, keyword=u'k\xe9yword', name=u'name', tags=[u'spam', u'ham'],
            location=u'location', description=[u'description'], scenarios=[],
            background=None):
        # pylint: disable=W0102,W0613
        #   W0102   Dangerous default value [...]
        #   W0613   Unused argument (args, kwargs)
        __pychecker__ = "unusednames=location"
        line = self.line
        tags = [Tag(name, line) for name in tags]
        return Feature('<string>', line, keyword, name, tags=tags,
            description=description, scenarios=scenarios,
            background=background)

    def _scenario(self, keyword=u'k\xe9yword', name=u'name', tags=[], steps=[]):
        # pylint: disable=W0102
        #   W0102   Dangerous default value [...]
        line = self.line
        tags = [Tag(name, line) for name in tags]
        return Scenario('<string>', line, keyword, name, tags=tags, steps=steps)

    def _step(self, keyword=u'k\xe9yword', step_type='given', name=u'name', text=None, table=None):
        line = self.line
        return Step('<string>', line, keyword, step_type, name, text=text, table=table)

    def test_feature(self):
        # this test does not actually check the result of the formatting; it
        # just exists to make sure that formatting doesn't explode in the face of
        # unicode and stuff
        p = self._formatter(_tf(), self.config)
        f = self._feature()
        p.feature(f)

    def test_scenario(self):
        p = self._formatter(_tf(), self.config)
        f = self._feature()
        p.feature(f)
        s = self._scenario()
        p.scenario(s)

    def test_step(self):
        p = self._formatter(_tf(), self.config)
        f = self._feature()
        p.feature(f)
        s = self._scenario()
        p.scenario(s)
        s = self._step()
        p.step(s)
        s.status = u'passed'
        p.result(s)


class TestPretty(FormatterTests):
    formatter_name = 'pretty'


class TestPlain(FormatterTests):
    formatter_name = 'plain'


class TestJson(FormatterTests):
    formatter_name = 'json'

class TestProgress(FormatterTests):
    formatter_name = "progress"

class TestTagCount(FormatterTests):
    formatter_name = 'plain'

    def _formatter(self, stream, config, tag_counts=None):
        if tag_counts is None:
            tag_counts = {}
        f = formatters.get_formatter(config, stream)
        f.uri('<string>')
        f = tag_count.TagCountFormatter(f, tag_counts)
        f.uri('<string>')
        return f

    def test_tag_count(self):
        counts = {}
        p = self._formatter(_tf(), self.config, counts)
{'fraction_non_alphanumeric': 0.10825848501708926, 'fraction_numerical': 0.020984023527541532, 'mean_word_length': 2.4171645844649645, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, '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': '318864', 'n_tokens_mistral': 4081, 'n_tokens_neox': 3819, 'n_words': 954}