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 = Stories(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 = Stories(magic(), 'foo', logger)

    story.tree = tree
    ret = await Lexicon.if_condition(logger, story, tree['1'])
    assert ret == case[1]
Beispiel #3
0
def story(app, logger):
    asset_dir = examples.__path__[0]

    with open(asset_dir + '/stories.json', 'r') as file:
        app.stories = ujson.loads(file.read())['stories']

    return Stories(app, 'hello.story', logger)
Beispiel #4
0
async def test_story_execute(patch, app, logger, story, async_mock):
    patch.object(Story, 'execute_line', new=async_mock(return_value=None))
    patch.object(Stories, 'first_line')
    story.prepare()
    await Story.execute(logger, story)
    assert Stories.first_line.call_count == 1
    logger.log.assert_called_with('story-execution', None)
    Story.execute_line.mock.assert_called_with(logger,
                                               story, Stories.first_line())
Beispiel #5
0
def test_get_str_for_logging(long):
    def make_string(length):
        out = ''
        for i in range(0, length):
            out += 'a'
        return out

    test_str = 'hello world'
    if long:
        test_str = make_string(1024)

    actual_val = Stories.get_str_for_logging(test_str)

    if long:
        assert actual_val == f'{test_str[:MAX_BYTES_LOGGING]} ... ' \
                             f'({1024-MAX_BYTES_LOGGING} bytes truncated)'
    else:
        assert actual_val == 'hello world'
Beispiel #6
0
    story = storyscript.Api.loads(all_lines, features={'globals': True})
    errors = story.errors()
    if len(errors) > 0:
        print(f'Failed to compile the following story:'
              f'\n\n{all_lines}',
              file=sys.stderr)
        raise errors[0]

    app = MagicMock()

    app.stories = {story_name: story.result()}
    app.environment = {}

    context = {}

    story = Stories(app, story_name, logger)
    story.prepare(context)
    try:
        await Story.execute(logger, story)
    except StoryscriptError as e:
        try:
            assert isinstance(case.assertion, RuntimeExceptionAssertion)
            case.assertion.verify(e)
        except BaseException as e:
            print(
                f'Failed to assert exception for the following story:'
                f'\n\n{all_lines}',
                file=sys.stderr)
            raise e
        return
    except BaseException as e:
Beispiel #7
0
def story(app, logger):
    return Stories(app, 'hello.story', logger)