Beispiel #1
0
                min_size=1))
def test_compare_equality(left_value, operator_value_tuples):
    """Test type setting of Compare node representing comparators: ''==', '!=', '>=', '<=', 'is'. """
    for operator, value in operator_value_tuples:
        if isinstance(value, str):
            assume(len(value) > 2)
    program = f'{repr(left_value)} '
    for operator, value in operator_value_tuples:
        program += ' '.join([operator, repr(value)])
    module, _ = cs._parse_text(program)
    compare_node = list(module.nodes_of_class(astroid.Compare))[0]
    assert compare_node.type_constraints.type == bool


@given(hs.lists(cs.comparator_operator, min_size=3),
       cs.homogeneous_list(min_size=4))
def test_compare_equality(operators, values):
    """Test type setting of Compare node representing comparators: '<', '>', 'in'. """
    for value in values:
        assume(value)
        if isinstance(value, str):
            assume(len(value) > 2)
    a = list(zip(operators, values))
    pre = []
    for operator, value in a:
        pre.append(str(operator))
        pre.append(str(value))
    # pre_input_program = [str(elt) for tuple in zip(operator, values) for elt in tuple]
    program = f'{str(values[0])} ' + ' '.join(pre)
    module, _ = cs._parse_text(program)
    compare_node = list(module.nodes_of_class(astroid.Compare))[0]
Beispiel #2
0
@given(cs.primitive_values)
def test_simple_literal(const):
    """Test Const nodes representing int, bool, float, and None literal values."""
    assume(not isinstance(const, str))
    module = cs._parse_text(str(const))
    cs._verify_type_setting(module, astroid.Const, type(const))


@given(cs.tuple_strategy(min_size=2))
def test_tuple(t_tuple):
    """ Test Tuple nodes representing a tuple of various types."""
    module = cs._parse_text(str(t_tuple))
    cs._verify_type_setting(module, astroid.Tuple, Tuple[tuple(type(x) for x in t_tuple)])


@given(cs.homogeneous_list(min_size=2))
def test_homogeneous_lists(lst):
    """Test List nodes representing a list of values of the same primitive type."""
    module = cs._parse_text(str(lst))
    cs._verify_type_setting(module, astroid.List, List[type(lst[0])])


@given(cs.random_list(min_size=2))
def test_random_lists(lst):
    """Test List nodes representing a list of values of different primitive types."""
    assume(not isinstance(lst[0], type(lst[1])))
    module = cs._parse_text(str(lst))
    cs._verify_type_setting(module, astroid.List, List[Any])


@given(cs.homogeneous_dictionary(min_size=1))
Beispiel #3
0
import astroid
import nose
from hypothesis import given, settings, assume
from typing import Callable, Any
import tests.custom_hypothesis_support as cs
settings.load_profile("pyta")


@given(cs.homogeneous_list(min_size=1))
def test_for_homogeneous_list(iterable):
    """Test whether visitors properly set the type constraint of the a For node representing for/else statement
     iterating over a homogeneous list.
    """
    program = f'for elt in {iterable}:\n' \
              f'    x = elt\n'
    module, TypeInferrer = cs._parse_text(program)
    for_node = list(module.nodes_of_class(astroid.For))[0]
    local_type_var = module.type_environment.lookup_in_env('x')
    inferred_type = TypeInferrer.type_constraints.lookup_concrete(local_type_var)
    assert inferred_type == for_node.iter.type_constraints.type.__args__[0]


@given(cs.random_list(min_size=2))
def test_for_heterogeneous_list(iterable):
    """Test whether visitors properly set the type constraint of the a For node representing for/else statement
     iterating over a heterogeneous list.
    """
    assume(type(iterable[0]) != type(iterable[1]))
    val_types = [type(val) for val in iterable]
    if int in val_types:
        assume(bool not in val_types)
Beispiel #4
0
@given(cs.primitive_values, hs.lists(hs.tuples(cs.comparator_operator_equality, cs.primitive_values), min_size=1))
def test_compare_equality(left_value, operator_value_tuples):
    """Test type setting of Compare node representing comparators: ''==', '!=', '>=', '<=', 'is'. """
    for operator, value in operator_value_tuples:
        if isinstance(value, str):
            assume(len(value) > 2)
    program = f'{repr(left_value)} '
    for operator, value in operator_value_tuples:
        program += ' '.join([operator, repr(value)])
    module, _ = cs._parse_text(program)
    compare_node = list(module.nodes_of_class(astroid.Compare))[0]
    assert compare_node.type_constraints.type == bool


@nottest
@given(hs.lists(cs.comparator_operator, min_size=3), cs.homogeneous_list(min_size=4))
def test_compare_equality(operators, values):
    """Test type setting of Compare node representing comparators: '<', '>', 'in'. """
    for value in values:
        assume(value)
        if isinstance(value, str):
            assume(len(value) > 2)
    a = list(zip(operators, values))
    pre = []
    for operator, value in a:
        pre.append(str(operator))
        pre.append(str(value))
    # pre_input_program = [str(elt) for tuple in zip(operator, values) for elt in tuple]
    program = f'{str(values[0])} ' + ' '.join(pre)
    module, _ = cs._parse_text(program)
    compare_node = list(module.nodes_of_class(astroid.Compare))[0]