Esempio n. 1
0
def create_stmt_from_constant(
        constant: ast.Constant,
        testcase: tc.TestCase) -> Optional[prim_stmt.PrimitiveStatement]:
    """Creates a statement from an ast.constant node.

    Args:
        constant: the ast.Constant statement
        testcase: the testcase containing the statement

    Returns:
        The corresponding statement.
    """
    if constant.value is None:
        return prim_stmt.NoneStatement(testcase, constant.value)

    val = constant.value
    if isinstance(val, bool):
        return prim_stmt.BooleanPrimitiveStatement(testcase, val)
    if isinstance(val, int):
        return prim_stmt.IntPrimitiveStatement(testcase, val)
    if isinstance(val, float):
        return prim_stmt.FloatPrimitiveStatement(testcase, val)
    if isinstance(val, str):
        return prim_stmt.StringPrimitiveStatement(testcase, val)
    if isinstance(val, bytes):
        return prim_stmt.BytesPrimitiveStatement(testcase, val)
    logger.info(
        "Could not find case for constant while handling assign statement.")
    return None
Esempio n. 2
0
 def _create_primitive(
     test_case: tc.TestCase,
     parameter_type: Type,
     position: int,
     recursion_depth: int,
 ) -> vr.VariableReference:
     if parameter_type == int:
         statement: prim.PrimitiveStatement = prim.IntPrimitiveStatement(
             test_case)
     elif parameter_type == float:
         statement = prim.FloatPrimitiveStatement(test_case)
     elif parameter_type == bool:
         statement = prim.BooleanPrimitiveStatement(test_case)
     else:
         statement = prim.StringPrimitiveStatement(test_case)
     ret = test_case.add_statement(statement, position)
     ret.distance = recursion_depth
     return ret
Esempio n. 3
0
def create_stmt_from_unaryop(
        unaryop: ast.UnaryOp,
        testcase: tc.TestCase) -> Optional[prim_stmt.PrimitiveStatement]:
    """Creates a statement from an ast.unaryop node.

    Args:
        unaryop: the ast.UnaryOp statement
        testcase: the testcase containing the statement

    Returns:
        The corresponding statement.
    """
    val = unaryop.operand.value  # type: ignore
    if isinstance(val, bool):
        return prim_stmt.BooleanPrimitiveStatement(testcase, not val)
    if isinstance(val, float):
        return prim_stmt.FloatPrimitiveStatement(testcase, (-1) * val)
    if isinstance(val, int):
        return prim_stmt.IntPrimitiveStatement(testcase, (-1) * val)
    logger.info(
        "Could not find case for unary operator while handling assign statement."
    )
    return None
def test_primitive_statement_mutate(test_case_mock):
    statement = prim.BooleanPrimitiveStatement(test_case_mock, True)
    statement.mutate()
    assert not statement.value
def test_boolean_primitive_statement_delta(test_case_mock):
    statement = prim.BooleanPrimitiveStatement(test_case_mock, True)
    statement.delta()
    assert not statement.value
def test_bool_primitive_statement_randomize_value(test_case_mock):
    statement = prim.BooleanPrimitiveStatement(test_case_mock)
    statement.randomize_value()
    assert statement.value or not statement.value