Skip to content

Commit

Permalink
supporting dinamic provides
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielfalcao committed Feb 24, 2012
1 parent b2f0e7b commit 9c0d0c8
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 9 deletions.
46 changes: 38 additions & 8 deletions sure/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# #!/usr/bin/env python
# -*- coding: utf-8 -*-
# <sure - assertion toolbox>
# Copyright (C) <2010> Gabriel Falcão <gabriel@nacaolivre.org>
# Copyright (C) <2010-2012> Gabriel Falcão <gabriel@nacaolivre.org>
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
Expand All @@ -28,12 +28,14 @@
import sys
import inspect
import traceback
from datetime import datetime
from functools import wraps
from pprint import pformat

from copy import deepcopy
from pprint import pformat
from functools import wraps
from datetime import datetime
from collections import Iterable
version = '0.8.0'

version = '0.8.1'


def _get_file_name(func):
Expand Down Expand Up @@ -613,13 +615,39 @@ def action_for(context, provides=None, depends_on=None):
depends_on = []

def register_providers(func, attr):
if re.search(ur'^[{]\d+[}]$', attr):
return # ignore dinamically declared provides

if not attr in context.__sure_providers_of__:
context.__sure_providers_of__[attr] = []

context.__sure_providers_of__[attr].append(func)

def ensure_providers(func, attr):
assert hasattr(context, attr), \
def register_dinamic_providers(func, attr, args, kwargs):
found = re.search(ur'^[{](\d+)[}]$', attr)
if not found:
return # ignore dinamically declared provides

index = int(found.group(1))
assert index < len(args), \
'the dinamic provider index: {%d} is bigger than %d, which is ' \
'the length of the positional arguments passed to %s' % (
index, len(args), func.__name__)

attr = args[index]

if not attr in context.__sure_providers_of__:
context.__sure_providers_of__[attr] = []

context.__sure_providers_of__[attr].append(func)

def ensure_providers(func, attr, args, kwargs):
found = re.search(ur'^[{](\d+)[}]$', attr)
if found:
index = int(found.group(1))
attr = args[index]

assert attr in context, \
'the action "%s" was supposed to provide the attribute "%s" ' \
'into the context, but it did not. Please double check its ' \
'implementation' % (func.__name__, attr)
Expand Down Expand Up @@ -668,10 +696,12 @@ def decorate_and_absorb(func):

@wraps(func)
def wrapper(*args, **kw):
[register_dinamic_providers(func, attr, args, kw)
for attr in provides]
context.__sure_actions_ran__.append((func, args, kw))
check_dependencies(func)
result = func(*args, **kw)
[ensure_providers(func, attr) for attr in provides]
[ensure_providers(func, attr, args, kw) for attr in provides]
context.__sure_action_results__.append(result)
return context

Expand Down
33 changes: 32 additions & 1 deletion test_sure.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# #!/usr/bin/env python
# -*- coding: utf-8 -*-
# <sure - assertion toolbox>
# Copyright (C) <2010> Gabriel Falcão <gabriel@nacaolivre.org>
# Copyright (C) <2010-2012> Gabriel Falcão <gabriel@nacaolivre.org>
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
Expand Down Expand Up @@ -994,3 +994,34 @@ def access_nonexisting_attr():
'Maybe you misspelled it ? Well, here are the options: ' \
'[\'name\', \'foo\']',
)


def test_actions_providing_dinamically_named_variables():
"the actions should be able to declare the variables they provide"
from sure import action_for, scenario

def with_setup(context):
@action_for(context, provides=['var1', '{0}'])
def the_context_has_variables(first_arg):
context.var1 = 123
context[first_arg] = "qwerty"

@scenario(with_setup)
def the_providers_are_working(Then):
Then.the_context_has_variables('JohnDoe')
assert hasattr(Then, 'var1')
assert 'JohnDoe' in Then
assert hasattr(Then, '__sure_providers_of__')

providers = Then.__sure_providers_of__
action = Then.the_context_has_variables.__name__

providers_of_var1 = [p.__name__ for p in providers['var1']]
assert that(providers_of_var1).contains(action)

providers_of_JohnDoe = [p.__name__ for p in providers['JohnDoe']]
assert that(providers_of_JohnDoe).contains(action)

return True

assert the_providers_are_working()

0 comments on commit 9c0d0c8

Please sign in to comment.