def test_migrate_ctx_assignment(returned_class, foo_value): """ Migrate Success(foo='bar'). Migrate story context variable assignment from the Success() keyword arguments to the assignment expression. """ before = dedent(""" from stories import story, {returned_class} class Action: @story def do(I): I.one def one(self, ctx): return {returned_class}(foo={foo_value}) """).format(returned_class=returned_class, foo_value=foo_value) after = dedent(""" from stories import story, {returned_class} class Action: @story def do(I): I.one def one(self, ctx): ctx.foo = {foo_value} return {returned_class}() """).format(returned_class=returned_class, foo_value=foo_value) assert _upgrade(before) == after
def test_migrate_empty_ctx(returned_class): """Don't modify methods without variable assignment in any case.""" source = dedent(""" from stories import story, {returned_class} class Action: @story def do(I): I.one def one(self, ctx): return {returned_class}() """).format(returned_class=returned_class) assert _upgrade(source) == source
def test_migrate_ctx_assignment_multiline_with_comment(returned_class, foo_value, bar_value): """Migrate Success(foo='bar', baz='quiz'). Where 'foo' and 'bar' have place on the different sequential lines and there is a comment line between them. """ before = dedent(""" from stories import story, {returned_class} class Action: @story def do(I): I.one def one(self, ctx): return {returned_class}( foo={foo_value}, # ... bar={bar_value}, ) """).format(returned_class=returned_class, foo_value=foo_value, bar_value=bar_value) after = dedent(""" from stories import story, {returned_class} class Action: @story def do(I): I.one def one(self, ctx): ctx.foo = {foo_value} ctx.bar = {bar_value} return {returned_class}() """).format(returned_class=returned_class, foo_value=foo_value, bar_value=bar_value) assert _upgrade(before) == after
def test_migrate_ctx_assignment_with_indentation(returned_class, foo_value): """ Migrate Success(foo='bar') after if False: pass. This should not crash because of dedent right before return statement. """ before = dedent(""" from stories import story, {returned_class} class Action: @story def do(I): I.one def one(self, ctx): if False: pass return {returned_class}(foo={foo_value}) """).format(returned_class=returned_class, foo_value=foo_value) after = dedent(""" from stories import story, {returned_class} class Action: @story def do(I): I.one def one(self, ctx): if False: pass ctx.foo = {foo_value} return {returned_class}() """).format(returned_class=returned_class, foo_value=foo_value) assert _upgrade(before) == after