<commit_before>#!/usr/bin/env python
from datetime import date, datetime
from decimal import Decimal, ROUND_UP, ROUND_DOWN
from unittest import TestCase

from wtforms import validators, widgets
from wtforms.fields import *
from wtforms.fields import Label
from wtforms.form import Form


class DummyPostData(dict):
    def getlist(self, key):
        v = self[key]
        if not isinstance(v, (list, tuple)):
            v = [v]
        return v

class AttrDict(object):
    def __init__(self, *args, **kw):
        self.__dict__.update(*args, **kw)

def make_form(_name='F', **fields):
    return type(_name, (Form, ), fields)


class DefaultsTest(TestCase):
    def test(self):
        expected = 42

        def default_callable():
            return expected

        test_value = TextField(default=expected).bind(Form(), 'a')
        test_value.process(None)
        self.assertEqual(test_value.data, expected)

        test_callable = TextField(default=default_callable).bind(Form(), 'a')
        test_callable.process(None)
        self.assertEqual(test_callable.data, expected)


class LabelTest(TestCase):
    def test(self):
        expected = u"""<label for="test">Caption</label>"""
        label = Label('test', u'Caption')
        self.assertEqual(label(), expected)
        self.assertEqual(str(label), expected)
        self.assertEqual(unicode(label), expected)
        self.assertEqual(label.__html__(), expected)
        self.assertEqual(label().__html__(), expected)
        self.assertEqual(label('hello'), u"""<label for="test">hello</label>""")
        self.assertEqual(TextField(u'hi').bind(Form(), 'a').label.text, u'hi')


class FlagsTest(TestCase):
    def setUp(self):
        t = TextField(validators=[validators.Required()]).bind(Form(), 'a')
        self.flags = t.flags

    def test_existing_values(self):
        self.assertEqual(self.flags.required, True)
        self.assert_('required' in self.flags)
        self.assertEqual(self.flags.optional, False)
        self.assert_('optional' not in self.flags)

    def test_assignment(self):
        self.assert_('optional' not in self.flags)
        self.flags.optional = True
        self.assertEqual(self.flags.optional, True)
        self.assert_('optional' in self.flags)

    def test_unset(self):
        self.flags.required = False
        self.assertEqual(self.flags.required, False)
        self.assert_('required' not in self.flags)


class FiltersTest(TestCase):
    class F(Form):
        a = TextField(default=' hello', filters=[lambda x: x.strip()])

    def test(self):
        self.assertEqual(self.F().a.data, 'hello')
        self.assertEqual(self.F(DummyPostData(a=['  foo bar  '])).a.data, 'foo bar')


class SelectFieldTest(TestCase):
    class F(Form):
        a = SelectField(choices=[('a', 'hello'), ('btest','bye')], default='a')<commit_msg>Added test for base field properties to improve coverage.<commit_after>#!/usr/bin/env python
from datetime import date, datetime
from decimal import Decimal, ROUND_UP, ROUND_DOWN
from unittest import TestCase

from wtforms import validators, widgets
from wtforms.fields import *
from wtforms.fields import Label
from wtforms.form import Form


class DummyPostData(dict):
    def getlist(self, key):
        v = self[key]
        if not isinstance(v, (list, tuple)):
            v = [v]
        return v

class AttrDict(object):
    def __init__(self, *args, **kw):
        self.__dict__.update(*args, **kw)

def make_form(_name='F', **fields):
    return type(_name, (Form, ), fields)


class DefaultsTest(TestCase):
    def test(self):
        expected = 42

        def default_callable():
            return expected

        test_value = TextField(default=expected).bind(Form(), 'a')
        test_value.process(None)
        self.assertEqual(test_value.data, expected)

        test_callable = TextField(default=default_callable).bind(Form(), 'a')
        test_callable.process(None)
        self.assertEqual(test_callable.data, expected)


class LabelTest(TestCase):
    def test(self):
        expected = u"""<label for="test">Caption</label>"""
        label = Label('test', u'Caption')
        self.assertEqual(label(), expected)
        self.assertEqual(str(label), expected)
        self.assertEqual(unicode(label), expected)
        self.assertEqual(label.__html__(), expected)
        self.assertEqual(label().__html__(), expected)
        self.assertEqual(label('hello'), u"""<label for="test">hello</label>""")
        self.assertEqual(TextField(u'hi').bind(Form(), 'a').label.text, u'hi')


class FlagsTest(TestCase):
    def setUp(self):
        t = TextField(validators=[validators.Required()]).bind(Form(), 'a')
        self.flags = t.flags

    def test_existing_values(self):
        self.assertEqual(self.flags.required, True)
        self.assert_('required' in self.flags)
        self.assertEqual(self.flags.optional, False)
        self.assert_('optional' not in self.flags)

    def test_assignment(self):
        self.assert_('optional' not in self.flags)
        self.flags.optional = True
        self.assertEqual(self.flags.optional, True)
        self.assert_('optional' in self.flags)

    def test_unset(self):
        self.flags.required = False
        self.assertEqual(self.flags.required, False)
        self.assert_('required' not in self.flags)


class FiltersTest(TestCase):
    class F(Form):
        a = TextField(default=' hello', filters=[lambda x: x.strip()])

    def test(self):
        self.assertEqual(self.F().a.data, 'hello')
        self.assertEqual(self.F(DummyPostData(a=['  foo bar  '])).a.data, 'foo bar')


class FieldTest(TestCase):
    class F(Form):
        a = TextField(default='hello')

    def setUp(self):
        self.field = self.F().a 

    def test_htmlstring(self):
        self.assert_(isinstance(self.field.__html__(), widgets.HTMLString))

    def test_str_coerce(self):
        self.assert_(isinstance(str(self.field), str))
        self.assertEqual(str(self.field), str(self.field()))

    def test_unicode_coerce(self):
        field = self.F().a
        self.assertEqual(unicode(self.field), self.field()) 


class SelectFieldTest(TestCase):
    class F(Form):
        a = SelectField(choices=[('a', 'hello'), ('btest','bye')], default='a')
{'fraction_non_alphanumeric': 0.14139802106607086, 'fraction_numerical': 0.0006383657835939994, 'mean_word_length': 3.240189445196211, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '5747871', 'n_tokens_mistral': 1915, 'n_tokens_neox': 1857, 'n_words': 405}