async def test_if_condition_2(patch, logger, magic): tree = { '1': { 'ln': '1', 'method': 'if', 'enter': '2', 'next': '2' }, '2': { 'ln': '2', 'parent': '1', 'next': '3' }, '3': { 'ln': '3', 'next': None, 'method': 'execute' } } patch.object(Lexicon, '_is_if_condition_true', return_value=False) story = Story(magic(), 'foo', logger) story.tree = tree ret = await Lexicon.if_condition(logger, story, tree['1']) assert ret == '3'
async def test_if_condition(patch, logger, magic, case): tree = { '1': { 'ln': '1', 'method': 'if', 'parent': None, 'enter': '2', 'next': '2' }, '2': { 'ln': '2', 'parent': '1', 'next': '3' }, '3': { 'ln': '3', 'method': 'elif', 'parent': None, 'enter': '4', 'next': '4' }, '4': { 'ln': '4', 'parent': '3', 'next': '5' }, '5': { 'ln': '5', 'method': 'elif', 'parent': None, 'enter': '6', 'next': '6' }, '6': { 'ln': '6', 'parent': '5', 'next': '7' }, '7': { 'ln': '7', 'method': 'else', 'parent': None, 'next': '8', 'enter': '8' }, '8': { 'ln': '8', 'parent': '7', 'next': None } } patch.object(Lexicon, '_is_if_condition_true', side_effect=case[0]) story = Story(magic(), 'foo', logger) story.tree = tree ret = await Lexicon.if_condition(logger, story, tree['1']) assert ret == case[1]
async def test_lexicon_try_catch(patch, magic, logger, tree): story = Story(magic(), 'foo', logger) story.context = {} story.tree = tree execute_block = Lexicon.execute_block should_throw_exc = story.tree['4']['command'] == 'fail' async def execute_block_proxy(*args): line = args[2] method = line['method'] # throw an exception inside the try block if method == 'try' or method == 'catch' and should_throw_exc: raise StoryscriptError() else: assert method == 'finally' or method == 'catch' return await execute_block(*args) patch.object(Lexicon, 'execute_block', side_effect=execute_block_proxy) if should_throw_exc: with pytest.raises(StoryscriptError): final_line = await Lexicon.try_catch( story.logger, story, story.tree['1'] ) assert final_line == '6' else: final_line = await Lexicon.try_catch( story.logger, story, story.tree['1'] ) if story.tree['3']['method'] == 'catch': assert final_line is None or final_line == '5' else: assert final_line is None assert 'err' not in story.context
async def test_if_condition_1(patch, logger, magic): tree = { '1': { 'ln': '1', 'method': 'if', 'parent': None, 'enter': '2', 'next': '2' }, '2': { 'ln': '2', 'parent': '1', 'next': None } } patch.object(Lexicon, '_is_if_condition_true', return_value=False) patch.object(Lexicon, 'line_number_or_none') story = Story(magic(), 'foo', logger) story.tree = tree ret = await Lexicon.if_condition(logger, story, tree['1']) assert ret is None
async def test_lexicon_while(patch, magic, logger, line): story = Story(magic(), 'foo', logger) story.tree = { '1': { 'method': 'while', 'ln': '1', 'args': [{ '$OBJECT': 'expression', 'expression': 'less', 'values': [{ '$OBJECT': 'path', 'paths': ['i'] }, { '$OBJECT': 'int', 'int': 10 }] }], 'enter': '2', 'next': '2' }, '2': { 'method': 'expression', 'ln': '2', 'name': ['i'], 'args': [{ '$OBJECT': 'expression', 'expression': 'sum', 'values': [{ '$OBJECT': 'path', 'paths': ['i'] }, { '$OBJECT': 'int', 'int': 1 }] }], 'parent': '1', } } resolve = story.resolve execute_line = Lexicon.execute_line def proxy_resolve(*args, **kwargs): return resolve(*args, **kwargs) async def proxy_execute_line(my_logger, my_story, my_line): return await execute_line(my_logger, my_story, my_line) line_number_or_none = Lexicon.line_number_or_none def proxy_line_number_or_none(*args): return line_number_or_none(*args) line = story.line def proxy_line(line_): return line(line_) patch.object(Lexicon, 'line_number_or_none', side_effect=proxy_line_number_or_none) patch.object(Lexicon, 'execute_line', side_effect=proxy_execute_line) patch.object(story, 'line', side_effect=proxy_line) patch.object(story, 'resolve', side_effect=proxy_resolve) patch.object(story, 'line_has_parent', return_value=True) story.context = {'i': 0} await Lexicon.while_(logger, story, story.tree['1']) Lexicon.execute_line.assert_called_with(logger, story, '2') Lexicon.line_number_or_none.assert_called_with( story.next_block(story.tree['1'])) assert Lexicon.execute_line.call_count == 10 assert story.line_has_parent.call_count >= 10 assert story.resolve.call_count >= 10 story.resolve.assert_called() story.line.assert_called() story.line_has_parent.assert_called()