示例#1
0
def test_html_formatter_format_option_bs4(htmlbeautifier):

    fake_options = type('fake_obj', (object, ), {'formatter_version': 'bs4'})
    htmlbeautifier.default_options = Mock(return_value=fake_options)
    from codeformatter import htmlformatter

    mocked_formatter = Mock()
    mocked_formatter.settings = {'codeformatter_html_options': {}}

    with patch.object(
            htmlformatter.HtmlFormatter,
            'format_with_bs4',
            return_value=('from_bs4', '')) as mocked_bs4, patch.object(
                htmlformatter.HtmlFormatter,
                'format_with_beautifier',
                return_value=('from_beautifier',
                              '')) as mocked_beautifier:  # noqa

        input_text = 'test'.encode('utf8')

        htmlformatter.use_bs4 = False
        cff = htmlformatter.HtmlFormatter(mocked_formatter)
        out, err = cff.format(input_text)

        sublime.error_message.assert_called_once_with(
            u'CodeFormatter\n\nUnable to load BeautifulSoup HTML '
            u'formatter. The old RegExp-based formatter was '
            u'automatically used for you instead.')
        mocked_beautifier.assert_called_once_with(input_text.decode('utf-8'))
        assert out == 'from_beautifier'
        assert err == ''

        sublime.reset_mock()
        mocked_bs4.reset_mock()
        mocked_beautifier.reset_mock()

        htmlformatter.use_bs4 = True
        cff = htmlformatter.HtmlFormatter(mocked_formatter)
        out, err = cff.format(input_text)

        assert not sublime.error_message.called
        mocked_bs4.assert_called_once_with(input_text.decode('utf-8'))
        assert out == 'from_bs4'
        assert err == ''
示例#2
0
def test_html_formatter_format_not_option_bs4(htmlbeautifier):

    fake_options = type('fake_obj', (object, ), {'formatter_version': '-_-'})
    htmlbeautifier.default_options = Mock(return_value=fake_options)
    from codeformatter import htmlformatter

    mocked_formatter = Mock()
    mocked_formatter.settings = {'codeformatter_html_options': {}}

    with patch.object(
            htmlformatter.HtmlFormatter,
            'format_with_bs4',
            return_value=('from_bs4', '')) as mocked_bs4, patch.object(
                htmlformatter.HtmlFormatter,
                'format_with_beautifier',
                return_value=('from_beautifier',
                              '')) as mocked_beautifier:  # noqa

        htmlformatter.use_bs4 = False
        input_text = 'test'.encode('utf8')
        cff = htmlformatter.HtmlFormatter(mocked_formatter)
        out, err = cff.format(input_text)

        assert not sublime.error_message.called
        assert not mocked_bs4.called
        mocked_beautifier.assert_called_once_with(input_text.decode('utf-8'))
        assert out == 'from_beautifier'
        assert err == ''

        sublime.reset_mock()
        mocked_bs4.reset_mock()
        mocked_beautifier.reset_mock()

        htmlformatter.use_bs4 = True
        cff = htmlformatter.HtmlFormatter(mocked_formatter)
        out, err = cff.format(input_text)

        assert not sublime.error_message.called
        assert not mocked_bs4.called
        mocked_beautifier.assert_called_once_with(input_text.decode('utf-8'))
        assert out == 'from_beautifier'
        assert err == ''
示例#3
0
def test_html_formatter_format_on_save_enabled(htmlbeautifier, options,
                                               filename, expected):

    fake_options = type('fake_obj', (object, ), {})
    htmlbeautifier.default_options = Mock(return_value=fake_options)
    from codeformatter import htmlformatter

    mocked_formatter = Mock()
    mocked_formatter.settings = {'codeformatter_html_options': options}

    formatter = htmlformatter.HtmlFormatter(mocked_formatter)
    res = formatter.format_on_save_enabled(filename)
    assert res is expected
示例#4
0
def test_html_formatter_format_with_bs4_exception(htmlbeautifier, mocked_bs4):

    fake_options = type('fake_obj', (object, ), {})
    htmlbeautifier.default_options = Mock(return_value=fake_options)
    mocked_bs4.side_effect = Exception('some error')
    from codeformatter import htmlformatter

    mocked_formatter = Mock()
    mocked_formatter.settings = {'codeformatter_html_options': {}}

    formatter = htmlformatter.HtmlFormatter(mocked_formatter)
    res = formatter.format_with_bs4('testing')

    assert res == ('', 'some error')
示例#5
0
def test_html_formatter_format_with_beautifier_exception(
        htmlbeautifier, text, expected):

    fake_options = type('fake_obj', (object, ), {})
    htmlbeautifier.default_options = Mock(return_value=fake_options)
    htmlbeautifier.beautify.side_effect = Exception(text)
    from codeformatter import htmlformatter

    mocked_formatter = Mock()
    mocked_formatter.settings = {'codeformatter_html_options': {}}

    formatter = htmlformatter.HtmlFormatter(mocked_formatter)
    res = formatter.format_with_beautifier('out of words')

    assert res == expected
示例#6
0
def test_html_formatter_format_with_beautifier_valid(htmlbeautifier, result,
                                                     expected):

    fake_options = type('fake_obj', (object, ), {})
    htmlbeautifier.default_options = Mock(return_value=fake_options)
    htmlbeautifier.beautify = Mock(return_value=result)
    from codeformatter import htmlformatter

    mocked_formatter = Mock()
    mocked_formatter.settings = {'codeformatter_html_options': {}}

    formatter = htmlformatter.HtmlFormatter(mocked_formatter)
    res = formatter.format_with_beautifier('another one')

    htmlbeautifier.beautify.assert_called_once_with('another one',
                                                    fake_options)
    assert res == expected
示例#7
0
def test_html_formatter_format_with_bs4_valid(htmlbeautifier, mocked_bs4,
                                              current_options, expected):

    fake_options = type('fake_obj', (object, ), {})
    htmlbeautifier.default_options = Mock(return_value=fake_options)
    from codeformatter import htmlformatter

    mocked_formatter = Mock()
    mocked_formatter.settings = {'codeformatter_html_options': current_options}

    mocked_bs4_instance = mocked_bs4.return_value
    mocked_bs4_instance.prettify = Mock(return_value='returned value')

    formatter = htmlformatter.HtmlFormatter(mocked_formatter)
    res = formatter.format_with_bs4('testing_call')

    mocked_bs4.assert_called_once_with('testing_call', 'html.parser')
    mocked_bs4_instance.prettify.assert_called_once_with(formatter=None,
                                                         indent_size=expected)
    assert res == ('returned value', '')