>>> import tw2.core as twc

>>> twc.Validator().to_python('')

>>> twc.Validator(required=True).to_python('')
Traceback (most recent call last):
    ...
ValidationError: Enter a value

>>> twc.Validator().to_python(' a ')
'a'
>>> twc.Validator(strip=False).to_python(' a ')
' a '

>>> twc.LengthValidator(min=1).to_python([])
Traceback (most recent call last):
    ...
ValidationError: Enter a value

>>> twc.LengthValidator(min=1).to_python([1])
[1]

>>> twc.LengthValidator(min=2).to_python([1])
Traceback (most recent call last):
    ...
ValidationError: Value is too short

>>> twc.LengthValidator(max=2).to_python([1, 2])
[1, 2]

>>> twc.LengthValidator(max=2).to_python([1, 2, 3])
Traceback (most recent call last):
    ...
ValidationError: Value is too long

>>> twc.StringLengthValidator(min=1).to_python('')
Traceback (most recent call last):
    ...
ValidationError: Enter a value

>>> twc.StringLengthValidator(required=False, min=1).to_python('')
Traceback (most recent call last):
    ...
ValidationError: Enter a value

>>> twc.StringLengthValidator(min=1).to_python('a')
'a'
>>> twc.StringLengthValidator(min=2).to_python('a')
Traceback (most recent call last):
    ...
ValidationError: Must be at least 2 characters

>>> twc.StringLengthValidator(max=2).to_python('aa')
'aa'
>>> twc.StringLengthValidator(max=2).to_python('aaa')
Traceback (most recent call last):
    ...
ValidationError: Cannot be longer than 2 characters

>>> twc.ListLengthValidator(min=1).to_python([])
Traceback (most recent call last):
    ...
ValidationError: Enter a value

>>> twc.ListLengthValidator(min=1).to_python([1])
[1]

>>> twc.ListLengthValidator(min=2).to_python([1])
Traceback (most recent call last):
    ...
ValidationError: Select at least 2

>>> twc.ListLengthValidator(max=2).to_python([1, 2])
[1, 2]

>>> twc.ListLengthValidator(max=2).to_python([1, 2, 3])
Traceback (most recent call last):
    ...
ValidationError: Select no more than 2

>>> twc.IntValidator().to_python('123')
123

>>> twc.IntValidator().to_python(' 123 ')
123

>>> twc.IntValidator().to_python('x')
Traceback (most recent call last):
    ...
ValidationError: Must be an integer

>>> twc.IntValidator().to_python('') is None
True

>>> twc.IntValidator().to_python(None) is None
True

>>> twc.IntValidator().to_python(0)
0

>>> twc.IntValidator(required=True).to_python(None)
Traceback (most recent call last):
    ...
ValidationError: Enter a value

>>> twc.IntValidator(required=True).to_python(0)
0

>>> twc.IntValidator(min=5).to_python(4)
Traceback (most recent call last):
    ...
ValidationError: Must be at least 5

>>> twc.IntValidator(min=5).to_python(5)
5

>>> twc.IntValidator(max=5).to_python(5)
5

>>> twc.IntValidator(max=5).to_python(6)
Traceback (most recent call last):
    ...
ValidationError: Cannot be more than 5

>>> twc.IntValidator().from_python(0)
'0'

>>> twc.IntValidator().from_python(None)
''

>>> twc.BoolValidator().to_python('')

>>> twc.BoolValidator().to_python('on')
True

>>> twc.BoolValidator().to_python('off')
False

>>> twc.BoolValidator(required=True).to_python(None)
Traceback (most recent call last):
    ...
ValidationError: You must select this

>>> twc.BoolValidator(required=True).to_python(False)
False

>>> twc.EmailValidator().to_python('paj@pajhome.org.uk')
'paj@pajhome.org.uk'

>>> twc.EmailValidator().to_python('paj')
Traceback (most recent call last):
    ...
ValidationError: Must be a valid email address

>>> class Test(twc.CompoundWidget):
...   pw = twc.Widget()
...   pw_conf = twc.Widget(validator=twc.MatchValidator('pw'))
...

>>> Test.validate({'pw': 'fred'})
Traceback (most recent call last):
    ...
ValidationError

>>> Test.validate({'pw_conf': 'fred'})
Traceback (most recent call last):
    ...
ValidationError

>>> Test.validate({'pw': 'fred', 'pw_conf': 'derf'})
Traceback (most recent call last):
    ...
ValidationError

>>> Test.validate({'pw': 'fred', 'pw_conf': 'fred'})
{'pw_conf': u'fred', 'pw': u'fred'}

>>> class Test(twc.CompoundWidget):
...   id = twc.Widget()
...   report = twc.Widget(key='id')
...

>>> ins = Test.req()

>>> ins._validate({'id':'test'})
{'id': 'test'}

>>> ins.c.report.value
'test'

>>> class MyVal(twc.Validator):
...   def _valid_python(self, value, state):
...     assert(state['a'] is twc.Invalid)
...

>>> class Test(twc.CompoundWidget):
...    a = twc.Widget(validator=twc.IntValidator)
...    b = twc.Widget(validator=MyVal)
...

>>> Test.validate({'a': 'x'})
Traceback (most recent call last):
    ...
ValidationError
