def test_lines_with_simple_body(self, level: int): f = py.Function( "f", params=[], statements=[ py.OpaqueBlock("print('Hello!')"), py.OpaqueBlock("return") ], ) x = py.Line.INDENT_UNIT assert [str(l) for l in f.lines(level)] == [ x * level + "def f():", x * (level + 1) + "print('Hello!')", x * (level + 1) + "return", ]
def test_lines_indents_correctly(self, input_block: str, indent_level: int, expected: str): lines = py.OpaqueBlock(input_block).lines(indent_level) print("lines =") pprint.pprint(lines) with patch("transformer.python.Line.INDENT_UNIT", " "): assert "\n".join(str(line) for line in lines) == expected
def test_lines_with_hidden_comments(self, level: int): stmt = py.OpaqueBlock("foo", comments=["x"]) f = py.Function("f", params=[], statements=[stmt], comments=["1", "2"]) x = py.Line.INDENT_UNIT assert [str(l) for l in f.lines(level, comments=False)] == [ x * level + "def f():", *[str(l) for l in stmt.lines(level + 1, comments=False)], ]
def test_repr(self): stmts = [py.OpaqueBlock("raise")] assert ( repr( py.Class(name="C", statements=stmts, superclasses=["A"], comments=["hi"])) == f"Class(name='C', statements={stmts!r}, superclasses=['A'], comments=['hi'])" )
def test_repr(self): stmts = [py.OpaqueBlock("raise")] assert ( repr( py.Function(name="f", params=["a"], statements=stmts, comments=["hi"])) == f"Function(name='f', params=['a'], statements={stmts!r}, comments=['hi'])" )
def test_lines_with_hidden_comments(self, level: int): stmt = py.OpaqueBlock("foo", comments=["x"]) c = py.Class("C", statements=[stmt], superclasses=[], comments=["1", "2"]) x = py.Line.INDENT_UNIT assert [str(l) for l in c.lines(level, comments=False)] == [ x * level + "class C:", *[str(l) for l in stmt.lines(level + 1, comments=False)], ]
def from_task(cls, task: "Task") -> "Task2": # TODO: Remove me as soon as the old Task is no longer used and Task2 is # renamed to Task. # See https://github.com/zalando-incubator/Transformer/issues/11. t = cls(name=task.name, request=task.request) if task.locust_request: expr_view = py.ExpressionView( name="this task's request field", target=lambda: task.locust_request, converter=lreq_to_expr, ) else: expr_view = py.ExpressionView( name="this task's request field", target=lambda: t.request, converter=req_to_expr, ) t.statements = [ *[py.OpaqueBlock(x) for x in task.locust_preprocessing], py.Assignment("response", expr_view), *[py.OpaqueBlock(x) for x in task.locust_postprocessing], ] return t
def from_task(cls, task: "Task") -> "Task2": # TODO: Remove me as soon as the old Task is no longer used and Task2 is # renamed to Task. # See https://github.com/zalando-incubator/Transformer/issues/11. locust_request = task.locust_request if locust_request is None: locust_request = LocustRequest.from_request(task.request) return cls( name=task.name, request=task.request, statements=[ py.OpaqueBlock(block) for block in [ *task.locust_preprocessing, locust_request.as_locust_action(), *task.locust_postprocessing, ] ], )
def locust_program(scenarios: Sequence[Scenario]) -> py.Program: """ Converts a ScenarioGroup into a Locust File. """ global_code_blocks = { # TODO: Replace me with a plugin framework that accesses the full tree. # See https://github.com/zalando-incubator/Transformer/issues/11. block_name: py.OpaqueBlock("\n".join(block), comments=[block_name]) for scenario in scenarios for block_name, block in scenario.global_code_blocks.items() } return [ py.Import(["re"], comments=[LOCUSTFILE_COMMENT]), *locust_detected_version(), *locust_imports(), *locust_classes(scenarios), *global_code_blocks.values(), ]
def locust_detected_version() -> py.Program: return [ py.Import(["LooseVersion"], source="distutils.version"), py.Import(["__version__"], source="locust"), py.OpaqueBlock("LOCUST_MAJOR_VERSION = LooseVersion(__version__).version[0]"), ]
def test_repr(self): text = " a'\" b " assert (repr(py.OpaqueBlock( block=text, comments=["hi"])) == "OpaqueBlock(' a\\'\" b ', comments=['hi'])")
def test_lines_with_hidden_comments(self, level: int): x = py.Line.INDENT_UNIT ob = py.OpaqueBlock("hello", comments=["1", "2"]) assert [str(l) for l in ob.lines(level, comments=False) ] == [x * level + "hello"]
def test_lines_indents_correctly(self, input_block: str, indent_level: int, expected: str, indent_unit): lines = py.OpaqueBlock(input_block).lines(indent_level) print("lines =") pprint.pprint(lines) assert "\n".join(str(line) for line in lines) == expected
def test_lines_returns_block_lines_without_empty_top_and_bottom(self): ob = py.OpaqueBlock("\n\n a\n b\n\n\n c\n\n\n") assert len(ob.lines()) == 5
def test_lines_returns_block_lines_if_top_and_bottom_are_not_empty(self): ob = py.OpaqueBlock(" a\n b\n\n\n c") assert len(ob.lines()) == 5
def test_lines_raises_for_empty_input_block(self, block: str): with pytest.raises(ValueError): py.OpaqueBlock(block)