def test_mutations_handler_exception(story, patch): def exc(*args): raise Exception() patch.object(StringMutations, 'replace', side_effect=exc) mutation = {'mutation': 'replace'} with pytest.raises(StoryscriptError): Mutations.mutate(mutation, 'string', story, None)
def test_mutation_int_ops(story, op): story.context = {'foo': 10} mutation = { '$OBJECT': 'mutation', 'mutation': op[0], 'args': [{ '$OBJECT': 'argument', 'name': op[0], 'argument': { '$OBJECT': 'number', 'number': 10 } }] } line = {'args': [{'paths': ['foo']}, {'paths': ['foo']}, mutation]} # To test a case where len(args) is 2, for example, `a increment` # len(args) is 3 when the statement is `a = a increment` if op[0] == 'increment': line['args'].pop(0) assert story.context['foo'] == 10 assert Mutations.mutate(mutation, 10, story, line) == 10 + op[1] assert story.context['foo'] == 10 + op[1]
def test_mutations_string_pop(story): mutation = { 'mutation': 'pop', 'args': [{ '$OBJECT': 'argument', 'name': 'pop', 'argument': { '$OBJECT': 'string', 'string': 'y' } }] } value = {'a': 1, 'y': 2} assert Mutations.mutate(mutation, value, story, None) == 2 assert Mutations.mutate(mutation, value, story, None) is None
def test_mutations_string_random(story): mutation = {'mutation': 'random'} options = [28, 12, 8, 1] for i in range(0, 10): choice = Mutations.mutate(mutation, options, story, None) assert choice in options
def test_mutations_string_replace(story): mutation = { 'mutation': 'replace', 'args': [{ '$OBJECT': 'argument', 'name': 'replace', 'argument': { '$OBJECT': 'string', 'string': 'y' } }, { '$OBJECT': 'argument', 'name': 'with', 'argument': { '$OBJECT': 'string', 'string': 'Y' } }] } assert Mutations.mutate(mutation, 'asyncy', story, None) == 'asYncY'
def test_mutations_unexpected_mutation(story): mutation = {'mutation': 'foo'} with pytest.raises(StoryscriptError): Mutations.mutate(mutation, 'string', story, None)
def test_mutations_unexpected_type(story): mutation = {'mutation': 'foo'} with pytest.raises(AsyncyError): Mutations.mutate(mutation, Mutations, story, None)