<s> import sys <EOL> _old_argv = list ( sys . argv ) <EOL> sys . argv . extend ( [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> import os . path <EOL> import unittest <EOL> from openhtf import conf <EOL> conf . Declare ( '<STR_LIT>' ) <EOL> conf . Declare ( '<STR_LIT>' ) <EOL> conf . Declare ( '<STR_LIT>' ) <EOL> conf . Declare ( '<STR_LIT>' ) <EOL> conf . Declare ( '<STR_LIT>' ) <EOL> conf . Declare ( '<STR_LIT>' , default_value = None ) <EOL> conf . Declare ( '<STR_LIT>' , default_value = '<STR_LIT:default>' ) <EOL> conf . Declare ( '<STR_LIT>' ) <EOL> def tearDownModule ( ) : <EOL> sys . argv = _old_argv <EOL> class TestConf ( unittest . TestCase ) : <EOL> YAML_FILENAME = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' ) <EOL> BAD_FORMAT = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' ) <EOL> NOT_A_DICT = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' ) <EOL> def tearDown ( self ) : <EOL> conf . _flags . config_file = None <EOL> conf . Reset ( ) <EOL> def testYamlConfig ( self ) : <EOL> with open ( self . YAML_FILENAME , '<STR_LIT:rb>' ) as yamlfile : <EOL> conf . _flags . config_file = yamlfile <EOL> conf . Reset ( ) <EOL> self . assertEquals ( '<STR_LIT>' , conf . yaml_test_key ) <EOL> def testLoadOverride ( self ) : <EOL> conf . Load ( overridden_key = '<STR_LIT>' ) <EOL> conf . Load ( overridden_key = '<STR_LIT>' ) <EOL> self . assertEquals ( '<STR_LIT>' , conf . overridden_key ) <EOL> def testLoadNoOverride ( self ) : <EOL> conf . Load ( overridden_key = '<STR_LIT>' ) <EOL> conf . Load ( overridden_key = '<STR_LIT>' , _override = False ) <EOL> self . assertEquals ( '<STR_LIT>' , conf . overridden_key ) <EOL> def testLoadFromDict ( self ) : <EOL> conf . LoadFromDict ( { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEquals ( '<STR_LIT>' , conf . overridden_key ) <EOL> def testDefaults ( self ) : <EOL> self . assertEquals ( '<STR_LIT:default>' , conf . string_default ) <EOL> self . assertIsNone ( conf . none_default ) <EOL> with self . assertRaises ( conf . UnsetKeyError ) : <EOL> conf . no_default <EOL> def testFlagValues ( self ) : <EOL> self . assertEquals ( '<STR_LIT>' , conf . flag_key ) <EOL> self . assertEquals ( '<STR_LIT>' , conf . other_flag ) <EOL> conf . Load ( flag_key = '<STR_LIT>' ) <EOL> self . assertEquals ( '<STR_LIT>' , conf . flag_key ) <EOL> def testAsDict ( self ) : <EOL> conf . Load ( station_id = '<STR_LIT>' ) <EOL> self . assertEquals ( { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:default>' , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , conf . _asdict ( ) ) <EOL> def testUndeclared ( self ) : <EOL> with self . assertRaises ( conf . UndeclaredKeyError ) : <EOL> conf . undeclared <EOL> def testWeirdAttribute ( self ) : <EOL> with self . assertRaises ( AttributeError ) : <EOL> conf . _dont_do_this <EOL> with self . assertRaises ( AttributeError ) : <EOL> conf . _dont_do_this_either = None <EOL> def testCantSetViaAttribute ( self ) : <EOL> with self . assertRaises ( AttributeError ) : <EOL> conf . overridden_key = None <EOL> def testMultipleDeclaration ( self ) : <EOL> conf . Declare ( '<STR_LIT>' ) <EOL> with self . assertRaises ( conf . KeyAlreadyDeclaredError ) : <EOL> conf . Declare ( '<STR_LIT>' ) <EOL> def testInvalidKey ( self ) : <EOL> with self . assertRaises ( conf . InvalidKeyError ) : <EOL> conf . Declare ( '<STR_LIT>' ) <EOL> with self . assertRaises ( conf . InvalidKeyError ) : <EOL> conf . Declare ( '<STR_LIT>' ) <EOL> def testBadConfigFile ( self ) : <EOL> with open ( self . NOT_A_DICT , '<STR_LIT:rb>' ) as yamlfile : <EOL> conf . _flags . config_file = yamlfile <EOL> with self . assertRaises ( conf . ConfigurationInvalidError ) : <EOL> conf . Reset ( ) <EOL> with open ( self . BAD_FORMAT , '<STR_LIT:rb>' ) as yamlfile : <EOL> conf . _flags . config_file = yamlfile <EOL> with self . assertRaises ( conf . ConfigurationInvalidError ) : <EOL> conf . Reset ( ) <EOL> def testInjectPositionalArgs ( self ) : <EOL> @ conf . InjectPositionalArgs <EOL> def TestFunction ( string_default , no_default , not_declared ) : <EOL> self . assertEquals ( '<STR_LIT:default>' , string_default ) <EOL> self . assertEquals ( '<STR_LIT>' , no_default ) <EOL> self . assertEquals ( '<STR_LIT>' , not_declared ) <EOL> TestFunction ( no_default = '<STR_LIT>' , not_declared = '<STR_LIT>' ) <EOL> def testInjectPositionalArgsOverrides ( self ) : <EOL> @ conf . InjectPositionalArgs <EOL> def TestFunction ( string_default , none_default = '<STR_LIT>' ) : <EOL> self . assertEquals ( '<STR_LIT>' , string_default ) <EOL> self . assertEquals ( '<STR_LIT>' , none_default ) <EOL> TestFunction ( string_default = '<STR_LIT>' ) <EOL> def testInjectPositionalArgsClass ( self ) : <EOL> class TestClass ( object ) : <EOL> @ conf . InjectPositionalArgs <EOL> def __init__ ( self , string_default ) : <EOL> self . string_default = string_default <EOL> instance = TestClass ( ) <EOL> self . assertEquals ( '<STR_LIT:default>' , instance . string_default ) </s>
