Skip to content

Commit

Permalink
Custom u function to avoid the u literal
Browse files Browse the repository at this point in the history
  • Loading branch information
jmwri committed Sep 29, 2016
1 parent 7e05824 commit 6482aa6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 49 deletions.
56 changes: 7 additions & 49 deletions voluptuous/tests/tests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import copy
from nose.tools import assert_equal, assert_raises, assert_true
import sys

from voluptuous import (
Schema, Required, Extra, Invalid, In, Remove, Literal,
Expand All @@ -9,7 +8,7 @@
validate, ExactSequence, Equal, Unordered, Number
)
from voluptuous.humanize import humanize_error
from voluptuous.util import to_utf8_py2
from voluptuous.util import to_utf8_py2, u


def test_exact_sequence():
Expand Down Expand Up @@ -560,11 +559,9 @@ def fn(arg):
assert_raises(Invalid, fn, 1)


def test_unicode_key_is_converted_to_utf8_when_in_marker_py2():
def test_unicode_key_is_converted_to_utf8_when_in_marker():
"""Verify that when using unicode key the 'u' prefix is not thrown in the exception"""
if sys.version_info >= (3,):
return
schema = Schema({Required(u'q'): 1})
schema = Schema({Required(u('q')): 1})
# Can't use nose's raises (because we need to access the raised
# exception, nor assert_raises which fails with Python 2.6.9.
try:
Expand All @@ -585,24 +582,8 @@ def test_number_validation_with_string():
assert False, "Did not raise Invalid for String"



def test_unicode_key_is_converted_to_utf8_when_in_marker_py3():
"""Verify that when using unicode key the 'u' prefix is not thrown in the exception"""
if sys.version_info < (3,):
return
schema = Schema({Required('q'): 1})
# Can't use nose's raises (because we need to access the raised
# exception, nor assert_raises which fails with Python 2.6.9.
try:
schema({})
except Invalid as e:
assert_equal(str(e), "required key not provided @ data['q']")


def test_unicode_key_is_converted_to_utf8_when_plain_text_py2():
if sys.version_info >= (3,):
return
key = u'q'
def test_unicode_key_is_converted_to_utf8_when_plain_text():
key = u('q')
schema = Schema({key: int})
# Can't use nose's raises (because we need to access the raised
# exception, nor assert_raises which fails with Python 2.6.9.
Expand Down Expand Up @@ -638,20 +619,6 @@ def test_number_when_precision_scale_none_yield_decimal_true():
assert_equal(out_.get("number"), 12345678901234)



def test_unicode_key_is_converted_to_utf8_when_plain_text_py3():
if sys.version_info < (3,):
return
key = 'q'
schema = Schema({key: int})
# Can't use nose's raises (because we need to access the raised
# exception, nor assert_raises which fails with Python 2.6.9.
try:
schema({key: 'will fail'})
except Invalid as e:
assert_equal(str(e), "expected int for dictionary value @ data['q']")


def test_number_when_precision_none_n_valid_scale_case1_yield_decimal_true():
""" test with Number with no precision and valid scale case 1"""
schema = Schema({"number" : Number(scale=2, yield_decimal=True)})
Expand All @@ -666,17 +633,8 @@ def test_number_when_precision_none_n_valid_scale_case2_yield_decimal_true():
assert_equal(float(out_.get("number")), 123456789012.00)


def test_to_utf8_py2():
if sys.version_info >= (3,):
return
s = u'hello'
assert_true(isinstance(to_utf8_py2(s), str))


def test_to_utf8_py3():
if sys.version_info < (3,):
return
s = 'hello'
def test_to_utf8():
s = u('hello')
assert_true(isinstance(to_utf8_py2(s), str))


Expand Down
8 changes: 8 additions & 0 deletions voluptuous/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,11 @@ def to_utf8_py2(data):
if sys.version_info < (3,) and isinstance(data, unicode):
return data.encode('utf-8')
return data


if sys.version_info < (3,):
def u(x):
return unicode(x)
else:
def u(x):
return x

0 comments on commit 6482aa6

Please sign in to comment.