Skip to content

Commit

Permalink
make the different (edge) cases for class or module scope work.
Browse files Browse the repository at this point in the history
  • Loading branch information
janwijbrand committed Feb 20, 2009
1 parent c14c3fd commit 0721f5e
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 25 deletions.
7 changes: 2 additions & 5 deletions src/martian/directive.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,8 @@ def get(self, directive, component, get_default):
return result

# we may be really dealing with an instance or a module here
#if not util.isclass(component):
# result = directive.store.get(directive, component, _USE_DEFAULT)
# if result is not _USE_DEFAULT:
# return result
# return get_default(component, component)
if not util.isclass(component):
return get_default(component, component)

# now we need to loop through the mro, potentially twice
mro = inspect.getmro(component)
Expand Down
45 changes: 44 additions & 1 deletion src/martian/directive.txt
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,49 @@ the default, ``None``::
>>> layer.bind().get(testmodule.Foo, testmodule) is None
True




Like with CLASS scope directive where values set are inherited by subclasses,
values set on a class or module level are inherited too, even if the subclass
is defined another module::

>>> class testmodule_a(FakeModule):
... layer('Value set on baseclass module')
... class FooA(object):
... pass
>>> from martiantest.fake import testmodule_a
>>>
>>> class testmodule_b(FakeModule):
... class FooB(testmodule_a.FooA):
... pass
>>> from martiantest.fake import testmodule_b

On the baseclass::

>>> layer.bind().get(testmodule_a.FooA)
'Value set on baseclass module'

Inherited by the subclass::

>>> layer.bind().get(testmodule_b.FooB)
'Value set on baseclass module'

Whenever there's a directive set on the baseclass' module, it will take
precedence like with "normal" inheritance::

>>> class testmodule_c(FakeModule):
... layer('Value set on subclass module')
... class FooC(testmodule_a.FooA):
... pass
>>> from martiantest.fake import testmodule_c

>>> layer.bind().get(testmodule_c.FooC)
'Value set on subclass module'




Let's now look at this using a directive with CLASS scope only::

>>> class layer2(Directive):
Expand Down Expand Up @@ -898,7 +941,7 @@ The ``validateClass`` validator only accepts a class::

>>> from martian import validateClass
>>> class klass(Directive):
... scope = CLASS
... scope = CLASS
... store = ONCE
... validate = validateClass

Expand Down
61 changes: 42 additions & 19 deletions src/martian/edgecase.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,12 @@ directive:
Directive scope and default edge cases
--------------------------------------

>>> from martian import Directive, CLASS_OR_MODULE, CLASS, MODULE
>>> from martian import ONCE

MODULE scope directive on a module, with no explicit value::

>>> class mydir(Directive):
>>> class mydir(martian.Directive):
... scope = MODULE
... store = ONCE
>>> class module_no_explicit_value(FakeModule):
Expand All @@ -62,7 +65,7 @@ MODULE scope directive on a module, with no explicit value::

MODULE scope directive on a module, with an explicit value::

>>> class mydir2(Directive):
>>> class mydir2(martian.Directive):
... scope = MODULE
... store = ONCE
>>> class module_with_explicit_value(FakeModule):
Expand All @@ -74,7 +77,7 @@ MODULE scope directive on a module, with an explicit value::

MODULE scope directive on a module, with no explicit value, with a custom default::

>>> class mydir(Directive):
>>> class mydir(martian.Directive):
... scope = MODULE
... store = ONCE
>>> class module_custom_default(FakeModule):
Expand All @@ -87,7 +90,7 @@ MODULE scope directive on a module, with no explicit value, with a custom defaul

CLASS scope directive on a class, with no explicit value::

>>> class mydir(Directive):
>>> class mydir(martian.Directive):
... scope = CLASS
... store = ONCE
>>> class module_get_from_class_no_explicit(FakeModule):
Expand All @@ -99,7 +102,7 @@ CLASS scope directive on a class, with no explicit value::

CLASS scope directive on an instance, with no explicit value::

>>> class mydir(Directive):
>>> class mydir(martian.Directive):
... scope = CLASS
... store = ONCE
>>> class module_get_from_instance_no_explicit(FakeModule):
Expand All @@ -112,7 +115,7 @@ CLASS scope directive on an instance, with no explicit value::

CLASS scope directive on a class, with an explicit value::

>>> class mydir(Directive):
>>> class mydir(martian.Directive):
... scope = CLASS
... store = ONCE
>>> class module_get_from_class_with_explicit(FakeModule):
Expand All @@ -124,7 +127,7 @@ CLASS scope directive on a class, with an explicit value::

CLASS scope directive on an instance, with an explicit value::

>>> class mydir(Directive):
>>> class mydir(martian.Directive):
... scope = CLASS
... store = ONCE
>>> class module_get_from_instance_with_explicit(FakeModule):
Expand All @@ -137,7 +140,7 @@ CLASS scope directive on an instance, with an explicit value::

CLASS scope directive on a class, with a custom default::

>>> class mydir(Directive):
>>> class mydir(martian.Directive):
... scope = CLASS
... store = ONCE
>>> class module_get_from_class_with_custom(FakeModule):
Expand All @@ -152,7 +155,7 @@ CLASS scope directive on a class, with a custom default::

CLASS scope directive on an instance, with a custom default::

>>> class mydir(Directive):
>>> class mydir(martian.Directive):
... scope = CLASS
... store = ONCE
>>> class module_get_from_instance_with_custom(FakeModule):
Expand All @@ -166,7 +169,7 @@ CLASS scope directive on an instance, with a custom default::

CLASS_OR_MODULE scope directive on a module, with no explicit value::

>>> class mydir(Directive):
>>> class mydir(martian.Directive):
... scope = CLASS_OR_MODULE
... store = ONCE
>>> class module(FakeModule):
Expand All @@ -178,7 +181,7 @@ CLASS_OR_MODULE scope directive on a module, with no explicit value::

CLASS_OR_MODULE scope directive on a class, with no explicit value::

>>> class mydir(Directive):
>>> class mydir(martian.Directive):
... scope = CLASS_OR_MODULE
... store = ONCE
>>> class module(FakeModule):
Expand All @@ -191,7 +194,7 @@ CLASS_OR_MODULE scope directive on a class, with no explicit value::

CLASS_OR_MODULE scope directive on an instance, with no explicit value::

>>> class mydir(Directive):
>>> class mydir(martian.Directive):
... scope = CLASS_OR_MODULE
... store = ONCE
>>> class module(FakeModule):
Expand All @@ -205,7 +208,7 @@ CLASS_OR_MODULE scope directive on an instance, with no explicit value::

CLASS_OR_MODULE scope directive on a module, with an explicit value::

>>> class mydir(Directive):
>>> class mydir(martian.Directive):
... scope = CLASS_OR_MODULE
... store = ONCE
>>> class module(FakeModule):
Expand All @@ -217,7 +220,7 @@ CLASS_OR_MODULE scope directive on a module, with an explicit value::

CLASS_OR_MODULE scope directive on a class, with an explicit value::

>>> class mydir(Directive):
>>> class mydir(martian.Directive):
... scope = CLASS_OR_MODULE
... store = ONCE
>>> class module(FakeModule):
Expand All @@ -230,7 +233,7 @@ CLASS_OR_MODULE scope directive on a class, with an explicit value::

CLASS_OR_MODULE scope directive on an instance, with an explicit value::

>>> class mydir(Directive):
>>> class mydir(martian.Directive):
... scope = CLASS_OR_MODULE
... store = ONCE
>>> class module(FakeModule):
Expand All @@ -244,7 +247,7 @@ CLASS_OR_MODULE scope directive on an instance, with an explicit value::

CLASS_OR_MODULE scope directive on a module, with a custom default::

>>> class mydir(Directive):
>>> class mydir(martian.Directive):
... scope = CLASS_OR_MODULE
... store = ONCE
>>> class module(FakeModule):
Expand All @@ -255,7 +258,7 @@ CLASS_OR_MODULE scope directive on a module, with a custom default::

CLASS_OR_MODULE scope directive on a class, with a custom default::

>>> class mydir(Directive):
>>> class mydir(martian.Directive):
... scope = CLASS_OR_MODULE
... store = ONCE
>>> class module(FakeModule):
Expand All @@ -268,7 +271,7 @@ CLASS_OR_MODULE scope directive on a class, with a custom default::

CLASS_OR_MODULE scope directive on an instance, with a custom default::

>>> class mydir(Directive):
>>> class mydir(martian.Directive):
... scope = CLASS_OR_MODULE
... store = ONCE
>>> class module(FakeModule):
Expand All @@ -278,4 +281,24 @@ CLASS_OR_MODULE scope directive on an instance, with a custom default::
... obj = MyClass()
>>> from martiantest.fake import module
>>> mydir.bind(get_default=custom_get_default).get(module.obj)
'custom default?'
'custom default'

CLASS_OR_MODULE scope directive on the module, with inheritance::

>>> class mydir(martian.Directive):
... scope = CLASS_OR_MODULE
... store = ONCE
>>> class module_b(FakeModule):
... fake_module = True
... mydir('a value')
... class B(object):
... pass
>>> from martiantest.fake import module_b
>>> class module_a(FakeModule):
... fake_module = True
... class A(module_b.B):
... pass
>>> from martiantest.fake import module_a
>>> mydir.bind(get_default=custom_get_default).get(module_a.A)
'a value'

0 comments on commit 0721f5e

Please sign in to comment.