import autopath

from py.test import raises
from config import *
from option import *
from error import SpecialOwnersError

def make_description():
    gcoption = ChoiceOption('name', 'GC name', ['ref', 'framework'], 'ref')
    ## dummy 1
    gcdummy = BoolOption('dummy', 'dummy', default=False)
    objspaceoption = ChoiceOption('objspace', 'Object space',
                                ['std', 'thunk'], 'std')
    booloption = BoolOption('bool', 'Test boolean option', default=True)
    intoption = IntOption('int', 'Test int option', default=0)
    floatoption = FloatOption('float', 'Test float option', default=2.3)
    stroption = StrOption('str', 'Test string option', default="abc")
    boolop = BoolOption('boolop', 'Test boolean option op', default=True)
    wantref_option = BoolOption('wantref', 'Test requires', default=False,
                                    requires=['boolop'])
    wantframework_option = BoolOption('wantframework', 'Test requires',
                                      default=False,
                                      requires=['boolop'])
    gcgroup = OptionDescription('gc', '', [gcoption, gcdummy, floatoption])
    descr = OptionDescription('constraints', '', [gcgroup, booloption, objspaceoption,
                                           wantref_option, stroption,
                                           wantframework_option,
                                           intoption, boolop])
    return descr

def test_override_are_default_owner():
    "config.override() implies that the owner is 'default' again"
    descr = make_description()
    config = Config(descr, bool=False)
    # defaut
    assert config.gc._cfgimpl_value_owners['dummy'] == 'default'
    # user
    config.gc.dummy = True
    assert config.gc._cfgimpl_value_owners['dummy'] == 'user'
    assert config._cfgimpl_values['gc']._cfgimpl_value_owners['dummy'] == 'user'
    #Options have an available default setting and can give it back
    assert config._cfgimpl_descr._children[0]._children[1].getdefault() == False
    config.override({'gc.dummy':True})
    assert config.gc._cfgimpl_value_owners['dummy'] == 'default'
    # user again
    config.gc.dummy = False
    assert config.gc._cfgimpl_value_owners['dummy'] == 'user'

def test_change_owner():
    descr = make_description()
    # here the owner is 'default'
    config = Config(descr, bool=False)
    # the default owner is 'user' (which is not 'default')
    # Still not getting it ? read the docs
    config.gc.dummy = True
    assert config.gc._cfgimpl_value_owners['dummy'] == 'user'
    config.cfgimpl_set_owner('eggs')
    config.set(dummy=False)
    assert config.gc._cfgimpl_value_owners['dummy'] == 'eggs'
    config.cfgimpl_set_owner('spam')
    gcdummy = config.unwrap_from_path('gc.dummy')
    gcdummy.setowner(config.gc, 'blabla')
    assert config.gc._cfgimpl_value_owners['dummy'] == 'blabla'
    config.gc.dummy = True
    assert config.gc._cfgimpl_value_owners['dummy'] == 'spam'

#____________________________________________________________
# special owners
def test_auto_owner():
    descr = make_description()
    config = Config(descr, bool=False)
    config.gc.setoption('dummy', True, 'auto')
    raises(HiddenOptionError, "config.gc.dummy")
    raises(ConflictConfigError, "config.gc.setoption('dummy', False, 'auto')")
    # shall return an auto value...
    #assert config.gc.dummy == 'auto_dummy_value'

def test_cannot_override_special_owners():
    descr = make_description()
    config = Config(descr, bool=False)
    config.gc.setoption('dummy', True, 'auto')
    raises(SpecialOwnersError, "config.override({'gc.dummy': True})")

def test_fill_owner():    
    "fill option"
    descr = make_description()
    config = Config(descr, bool=False)
    assert config.bool == False
    assert config.gc.dummy == False
    # 'fill' special values
    config.gc.setoption('dummy', True, 'fill')
    assert config.gc.dummy == False

def test_auto_fill_and_override():    
    descr = make_description()
    config = Config(descr, bool=False)
    booloption = config.unwrap_from_path('bool')
    booloption.callback = 'identical'
    booloption.setowner(config, 'auto')
    assert config.bool == 'identicalbool'
    gcdummy = config.unwrap_from_path('gc.dummy')
    gcdummy.callback = 'identical'
    gcdummy.setowner(config.gc, 'fill')
    raises(SpecialOwnersError, "config.override({'gc.dummy':True})")
    config.gc.setoption('dummy', False, 'fill')
    # value is returned
    assert config.gc.dummy == False


