| 1 |
# |
| 2 |
# Copyright (C) 2007-2011 Edgewall Software, 2013-2025 the Babel team |
| 3 |
# All rights reserved. |
| 4 |
# |
| 5 |
# This software is licensed as described in the file LICENSE, which |
| 6 |
# you should have received as part of this distribution. The terms |
| 7 |
# are also available at https://github.com/python-babel/babel/blob/master/LICENSE. |
| 8 |
# |
| 9 |
# This software consists of voluntary contributions made by many |
| 10 |
# individuals. For the exact contribution history, see the revision |
| 11 |
# history and logs, available at https://github.com/python-babel/babel/commits/master/. |
| 12 |
|
| 13 |
import __future__ |
| 14 |
|
| 15 |
from io import BytesIO |
| 16 |
|
| 17 |
import pytest |
| 18 |
|
| 19 |
from babel import util |
| 20 |
from babel.util import parse_future_flags |
| 21 |
|
| 22 |
|
| 23 |
class _FF: |
| 24 |
division = __future__.division.compiler_flag |
| 25 |
print_function = __future__.print_function.compiler_flag |
| 26 |
with_statement = __future__.with_statement.compiler_flag |
| 27 |
unicode_literals = __future__.unicode_literals.compiler_flag |
| 28 |
|
| 29 |
|
| 30 |
def test_distinct(): |
| 31 |
assert list(util.distinct([1, 2, 1, 3, 4, 4])) == [1, 2, 3, 4] |
| 32 |
assert list(util.distinct('foobar')) == ['f', 'o', 'b', 'a', 'r'] |
| 33 |
|
| 34 |
|
| 35 |
def test_pathmatch(): |
| 36 |
assert util.pathmatch('**.py', 'bar.py') |
| 37 |
assert util.pathmatch('**.py', 'foo/bar/baz.py') |
| 38 |
assert not util.pathmatch('**.py', 'templates/index.html') |
| 39 |
assert util.pathmatch('**/templates/*.html', 'templates/index.html') |
| 40 |
assert not util.pathmatch('**/templates/*.html', 'templates/foo/bar.html') |
| 41 |
assert util.pathmatch('^foo/**.py', 'foo/bar/baz/blah.py') |
| 42 |
assert not util.pathmatch('^foo/**.py', 'blah/foo/bar/baz.py') |
| 43 |
assert util.pathmatch('./foo/**.py', 'foo/bar/baz/blah.py') |
| 44 |
assert util.pathmatch('./blah.py', 'blah.py') |
| 45 |
assert not util.pathmatch('./foo/**.py', 'blah/foo/bar/baz.py') |
| 46 |
|
| 47 |
|
| 48 |
def test_fixed_zone_negative_offset(): |
| 49 |
assert util.FixedOffsetTimezone(-60).zone == 'Etc/GMT-60' |
| 50 |
|
| 51 |
|
| 52 |
def test_fixed_zone_zero_offset(): |
| 53 |
assert util.FixedOffsetTimezone(0).zone == 'Etc/GMT+0' |
| 54 |
|
| 55 |
|
| 56 |
def test_fixed_zone_positive_offset(): |
| 57 |
assert util.FixedOffsetTimezone(330).zone == 'Etc/GMT+330' |
| 58 |
|
| 59 |
|
| 60 |
def parse_encoding(s): |
| 61 |
return util.parse_encoding(BytesIO(s.encode('utf-8'))) |
| 62 |
|
| 63 |
|
| 64 |
def test_parse_encoding_defined(): |
| 65 |
assert parse_encoding('# coding: utf-8') == 'utf-8' |
| 66 |
|
| 67 |
|
| 68 |
def test_parse_encoding_undefined(): |
| 69 |
assert parse_encoding('') is None |
| 70 |
|
| 71 |
|
| 72 |
def test_parse_encoding_non_ascii(): |
| 73 |
assert parse_encoding('K\xf6ln') is None |
| 74 |
|
| 75 |
|
| 76 |
@pytest.mark.parametrize('source, result', [ |
| 77 |
(''' |
| 78 |
from __future__ import print_function, |
| 79 |
division, with_statement, |
| 80 |
unicode_literals |
| 81 |
''', _FF.print_function | _FF.division | _FF.with_statement | _FF.unicode_literals), |
| 82 |
(''' |
| 83 |
from __future__ import print_function, division |
| 84 |
print('hello') |
| 85 |
''', _FF.print_function | _FF.division), |
| 86 |
(''' |
| 87 |
from __future__ import print_function, division, unknown,,,,, |
| 88 |
print 'hello' |
| 89 |
''', _FF.print_function | _FF.division), |
| 90 |
(''' |
| 91 |
from __future__ import ( |
| 92 |
print_function, |
| 93 |
division) |
| 94 |
''', _FF.print_function | _FF.division), |
| 95 |
(''' |
| 96 |
from __future__ import \\ |
| 97 |
print_function, \\ |
| 98 |
division |
| 99 |
''', _FF.print_function | _FF.division), |
| 100 |
]) |
| 101 |
def test_parse_future(source, result): |
| 102 |
fp = BytesIO(source.encode('latin-1')) |
| 103 |
flags = parse_future_flags(fp) |
| 104 |
assert flags == result |