Source

moin-2.0 / build / lib / MoinMoin / util / _tests / test_forms.py

# -*- coding: utf-8 -*-
# Copyright: 2011 Prashant Kumar <contactprashantat AT gmail DOT com>
# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.

"""
MoinMoin - MoinMoin.util.forms Tests
"""

from MoinMoin.util import forms
import pytest

class Bind(object):
    """ class for self defined test_bind attributes """
    def __init__(self):
        self.label = 'test_content'
        self.default_value = 'test_bind_default'
        self.optional = True
        self.properties = {'autofocus': False, 'placeholder': None}
        self.errors = None
test_bind = Bind()

test_attributes = {'type': 'submit', u'required': 'test_required', 'autofocus': None, 'placeholder': None}

def test_label_filter():
    # when content is None
    result1 = forms.label_filter('test_tagname', test_attributes, None, 'test_context', test_bind)
    expected = 'test_content'
    assert result1 == expected

    # when content is not None
    result2 = forms.label_filter('test_tagname', test_attributes, 'new_content', 'test_context', test_bind)
    expected = 'new_content'
    assert result2 == expected

def test_button_filter():
    result = forms.button_filter('test_tagname', test_attributes, 'new_content', 'test_context', None)
    expected = 'new_content'
    assert result == expected

    # attributes.get('type') in ['submit', 'reset', ])
    content_result = forms.button_filter('input', test_attributes, 'new_content', 'test_context', test_bind)
    expected = 'new_content'
    assert content_result == expected
    attributes_result = test_attributes['value']
    expected = 'test_bind_default'
    assert attributes_result == expected

    # tagname == 'button'
    content_result = forms.button_filter('button', test_attributes, None, 'test_context', test_bind)
    expected = 'test_bind_default'
    assert content_result == expected

def test_required_filter():
    test_bind.optional = False
    test_attributes[u'class'] = 'test_class'
    content_result = forms.required_filter('test_tagname', test_attributes, 'new_content', 'test_context', test_bind)
    expected = 'new_content'
    assert content_result == expected
    attribute_result = test_attributes[u'class']
    expected = u'required'
    assert attribute_result == expected

    # tagname == 'input'
    content_result = forms.required_filter('input', test_attributes, 'new_content', 'test_context', test_bind)
    expected = 'new_content'
    assert content_result == expected
    attribute_result = test_attributes[u'required']
    expected = u'required'
    assert attribute_result == expected

def test_autofocus_filter():
    test_bind.properties = {'autofocus': True}
    content_result = forms.autofocus_filter('test_tagname', test_attributes, 'new_content', 'test_context', test_bind)
    assert content_result == 'new_content'
    attribute_result = test_attributes[u'autofocus']
    assert attribute_result == u'autofocus'

def test_placeholder_filter():
    test_bind.properties['placeholder'] = 'test_placeholder'
    content_result = forms.placeholder_filter('test_tagname', test_attributes, 'new_content', 'test_context', test_bind)
    assert content_result == 'new_content'
    attribute_result = test_attributes['placeholder']
    assert attribute_result == 'test_placeholder'

def test_error_filter_factory():
    # when 'class' not in test_attributes
    test_bind.errors = 'test_errors'
    test_attributes.pop(u'class')
    test_fun_returned = forms.error_filter_factory('test_moin_error')
    content_result = test_fun_returned('test_tagname', test_attributes, 'new_content', 'test_context', test_bind)
    assert content_result == 'new_content'
    attribute_result = test_attributes['class']
    assert attribute_result == 'test_moin_error'

    # class in test_attributes
    test_attributes['class'] = 'test_attribute_class'
    content_result = test_fun_returned('test_tagname', test_attributes, 'new_content', 'test_context', test_bind)
    assert content_result == 'new_content'
    attribute_result = test_attributes['class']
    expected = 'test_attribute_class test_moin_error'
    assert attribute_result == expected
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.