def test_SingleNodeValidator_transform_region_no_list(): """Test that there is no transformation if there is a region""" testsrc = redbaron.RedBaron("1+1") validator = reddel_server.SingleNodeValidator() expected = (testsrc, (1, 1), (1, 3)) assert expected == validator.transform(testsrc, start=reddel_server.Position(1, 1), end=reddel_server.Position(1, 3))
def test_get_node_of_region_slice_list_declaration(): """Test get_node_of_region for when the nodes slice a list declaration""" testsrc = redbaron.RedBaron("[1, 2, 3, 4, 5, 6]") start = reddel_server.Position(1, 8) end = reddel_server.Position(1, 14) expected = "3, 4, 5" assert expected == reddel_server.get_node_of_region(testsrc, start, end).dumps()
def test_get_node_of_region_same(): """Test get_node_of_region for when start and end are the same nodes""" testsrc = redbaron.RedBaron("lambda: 1+1") start = reddel_server.Position(1, 1) end = reddel_server.Position(1, 2) expected = testsrc[0] assert expected == reddel_server.get_node_of_region(testsrc, start, end)
def test_get_node_of_region_simple(): """Test get_node_of_region for when start and end are part of a simple expression""" testsrc = redbaron.RedBaron("1+1") start = reddel_server.Position(1, 1) end = reddel_server.Position(1, 3) expected = testsrc[0] assert expected == reddel_server.get_node_of_region(testsrc, start, end)
def test_OptionalRegionValidator_transform_region(): """Test that the region is extracted when specified""" testsrc = redbaron.RedBaron("a=1+1\nb=5") start = reddel_server.Position(1, 1) end = reddel_server.Position(1, 4) validator = reddel_server.OptionalRegionValidator() result = validator.transform(testsrc, start, end) expected = (testsrc[0], reddel_server.Position(1, 1), reddel_server.Position(1, 5)) assert expected == result, "Expected that the region was extracted and the bounding box was updated."
def test_get_node_of_region_slice_for_loop(): """Test get_node_of_region for when the nodes slice a for loop body""" testsrc = redbaron.RedBaron("for i in range(10):\n" " a = 1 + 2\n" " b = 4\n" " c = 5\n" " d = 7\n") start = reddel_server.Position(3, 7) end = reddel_server.Position(5, 8) expected = "b = 4\n c = 5\n d = 7" assert expected == reddel_server.get_node_of_region(testsrc, start, end).dumps()
def test_get_node_of_region_slice_list(): """Test get_node_of_region for when the nodes are only a slice in a node list""" testsrc = redbaron.RedBaron("1+1\n" "a=1\n" "for i in range(10):\n" " b=i\n" "c=3") start = reddel_server.Position(2, 3) # a="1" end = reddel_server.Position(4, 6) # b"="1 expected = redbaron.NodeList(testsrc.node_list[2:5]) assert expected.dumps() == reddel_server.get_node_of_region(testsrc, start, end).dumps()
def test_SingleNodeValidator_region_valid(): """Test that the validator does not raise when there is one node in the region""" testsrc = redbaron.RedBaron("1+1\n2+2") validator = reddel_server.SingleNodeValidator() validator(testsrc, start=reddel_server.Position(2, 1), end=reddel_server.Position(2, 3))
def test_SingleNodeValidator_region_invalid(): """Test that the validator raises when there is more than one node in the region""" testsrc = redbaron.RedBaron("1+1\n2+2") validator = reddel_server.SingleNodeValidator() with pytest.raises(reddel_server.ValidationException): validator(testsrc, start=reddel_server.Position(1, 1), end=reddel_server.Position(2, 3))
def test_MandatoryRegionValidator_region(): """Test that there has to be a region""" testsrc = redbaron.RedBaron("1+1") validator = reddel_server.MandatoryRegionValidator() validator(testsrc, start=reddel_server.Position(1, 1), end=reddel_server.Position(1, 3))
def test_TypeValidator_invalid_region_no_list(): """Test that the validator raises for invalid sources with a region and no list""" testsrc = redbaron.RedBaron("def foo():\n\ta=1") validator = reddel_server.TypeValidator(['def']) with pytest.raises(reddel_server.ValidationException): validator(testsrc, start=reddel_server.Position(2, 3), end=reddel_server.Position(2, 4))
def test_TypeValidator_valid_region_no_list(): """Test a valid source where the region specifies a single node""" testsrc = redbaron.RedBaron("a=1\ndef foo(): pass\nb=2") validator = reddel_server.TypeValidator(['def']) validator(testsrc, start=reddel_server.Position(2, 1), end=reddel_server.Position(2, 1))
def test_TypeValidator_valid_region_list(): """Test a valid source that is a list with a region""" testsrc = redbaron.RedBaron("a=1\ndef foo(): pass\ndef bar(): pass") validator = reddel_server.TypeValidator(['def']) validator(testsrc, start=reddel_server.Position(2, 1), end=reddel_server.Position(3, 1))
def test_get_node_of_region_different_level(): """Test get_node_of_region for when the nodes for start and end are on different levels""" testsrc = redbaron.RedBaron("def foo(arg): a = 1+1") start = reddel_server.Position(1, 9) # "arg" end = reddel_server.Position(1, 20) # "+" assert reddel_server.get_node_of_region(testsrc, start, end) == testsrc[0] # the function definition
def test_get_node_of_region_same_level(): """Test get_node_of_region for when the nodes for start and end are on the same level""" testsrc = redbaron.RedBaron("1+1\n2*2") start = reddel_server.Position(1, 2) end = reddel_server.Position(2, 2) assert reddel_server.get_node_of_region(testsrc, start, end).dumps() == testsrc.dumps()
def test_get_node_of_region_bad_region(): """Test get_node_of_region with an invalid region""" testsrc = redbaron.RedBaron("1+1") start = reddel_server.Position(0, 1) end = reddel_server.Position(1, 3) assert testsrc == reddel_server.get_node_of_region(testsrc, start, end)