Esempio n. 1
0
def test_python_function_blocks_edge_cases_1():
    # when this tests was written, the python_function_blocks function wasn't finding anything after "b(\n        1"
    s = '''def a():
    def b(n):
        return (
            n * 2
        )

    return b(
        1
    )'''
    assert python_function_blocks(s) == [
        'def a():\n    def b(n):\n        return (\n            n * 2\n        )\n\n    return b(\n        1\n    )',
        '    def b(n):\n        return (\n            n * 2\n        )',
    ]

    s = '''def a():
    return some_collection.get_objects(locator=l5) \
                      .get_distinct(case_insensitive=True) \
                      .filter(predicate=query(q5)) \
                      .values()'''
    assert python_function_blocks(s) == [
        'def a():\n    return some_collection.get_objects(locator=l5)                       .get_distinct(case_insensitive=True)                       .filter(predicate=query(q5))                       .values()'
    ]

    s = '''def a():
    return "foo" +\
    "bar"'''
    print(f'python_function_blocks(s): {python_function_blocks(s)}')
    assert python_function_blocks(s) == [
        'def a():\n    return "foo" +    "bar"'
    ]
Esempio n. 2
0
def test_python_function_blocks__ignore_nested_function():
    assert python_function_blocks(TEST_CODE_WITH_NESTED_FUNCTION) == [
        'def f(n):\n    """a."""\n    def sq(i):\n        """b."""\n        return i * i\n    return sq(n)',
        '    def sq(i):\n        """b."""\n        return i * i',
    ]
    assert python_function_blocks(
        TEST_CODE_WITH_NESTED_FUNCTION, ignore_nested_functions=True
    ) == [
        'def f(n):\n    """a."""\n    def sq(i):\n        """b."""\n        return i * i\n    return sq(n)'
    ]
Esempio n. 3
0
def test_python_function_blocks_1():
    result = python_function_blocks(SIMPLE_FUNCTION)
    assert result == ['def test(a) -> List:\n    print(a)\n\n    return []']

    s = '''def a(
    n: str, o: Callable
):
    print('foo')'''
    assert python_function_blocks(s) == [s]

    s = '''def a(
    n: str, o: Callable
):
    print("""in the beginning
was the word
and the word...""")
    x = 10
    if x == 1:
        x = 1
    elif x == 2:
        x = 2
    else:
      print('bar')


a = 1
b = 2
c = 3
myList = range(10)

def someMethod(x):
    something = x * 2
    def subfunction(something):
        print('here')
    return something

f = someMethod(b)

print(f)'''
    assert python_function_blocks(s) == [
        'def a(\n    n: str, o: Callable\n):\n    print("""in the beginning\nwas the word\nand the word...""")\n    x = 10\n    if x == 1:\n        x = 1\n    elif x == 2:\n        x = 2\n    else:\n      print(\'bar\')',
        "def someMethod(x):\n    something = x * 2\n    def subfunction(something):\n        print('here')\n    return something",
        "    def subfunction(something):\n        print('here')",
    ]

    s = '''@do.something
def _a(
    n: str, o: Callable
):
    print('foo')'''
    assert python_function_blocks(s) == [s]
    assert python_function_blocks(s, ignore_private_functions=True) == []
Esempio n. 4
0
def _update_test(file_path: str, function_name: str, new_assertion_value: Any) -> bool:
    """Update the test with the given function_name in the file at the given file_path with the new_assertion_value."""
    test_file_content = file_read(file_path)
    original_function_block = function_block = [
        i for i in python_function_blocks(test_file_content) if function_name in i
    ][0]
    function_block = function_block.replace(FILL_OUTPUT_SIGNAL_IN_CONTEXT, f'== {new_assertion_value.__repr__()}', 1)
    test_file_content = test_file_content.replace(original_function_block, function_block, 1)
    return file_write(file_path, test_file_content)
Esempio n. 5
0
def _update_test_with_error(file_path: str, function_name: str, error_type: str, erroneous_assertion: str) -> bool:
    """Update the test with the given function_name in the file at the given file_path with the new_assertion_value."""
    test_file_content = file_read(file_path)
    original_function_block = function_block = [
        i for i in python_function_blocks(test_file_content) if function_name in i
    ][0]
    # TODO: currently, the indentation of the new assertion is hard coded; eventually, I would like to get the indentation from the original assertion
    new_assertion = f'with pytest.raises({error_type}):\n        {erroneous_assertion}'
    full_erroneous_assertion = f'assert {erroneous_assertion} == \'{RAISES_OUTPUT_SIGNAL}\''
    function_block = function_block.replace(full_erroneous_assertion, new_assertion, 1)
    test_file_content = test_file_content.replace(original_function_block, function_block, 1)
    return file_write(file_path, test_file_content)
Esempio n. 6
0
def test_python_function_blocks__async_functions():
    assert python_function_blocks(TEST_CODE_WITH_ASYNC_FUNCTION) == [
        'def foo(n):\n    """Foo."""\n    return n',
        'async def bar(n):\n    """Some async func."""\n    return n',
    ]