def test_config_util_initialisation():
    '''Test ConfigUtil initialises properly'''
    template = fudge.Fake("template")
    config_util = ConfigUtil(template)

    config_util.values |should| equal_to({})
    config_util.template |should| be(template)
    def test_it_raises_attributeError_when_key_doesnt_exist(self):
        config_util = ConfigUtil()
        config_util.values = {"a":1}

        config_util.find_value("a") |should| be(1)

        finder = lambda : config_util.find_value("b")
        finder |should| throw(AttributeError, "Config has no value for b")
    def test_it_doesnt_care_if_no_config_file(self):
        config_util = ConfigUtil()
        fake_find_config_file = fudge.Fake("find_config_file").expects_call().returns(None)
        fake_apply_config_file = fudge.Fake("apply_config_file")

        with fudge.patched_context(config_util, 'find_config_file', fake_find_config_file):
            with fudge.patched_context(config_util, 'apply_config_file', fake_apply_config_file):
                config_util.use_config_file()
    def test_it_applies_config_file_if_one_is_found(self):
        config_util = ConfigUtil()
        config_file = fudge.Fake("config_file")
        fake_find_config_file = fudge.Fake("find_config_file").expects_call().returns(config_file)
        fake_apply_config_file = fudge.Fake("apply_config_file").expects_call().with_args(config_file)

        with fudge.patched_context(config_util, 'find_config_file', fake_find_config_file):
            with fudge.patched_context(config_util, 'apply_config_file', fake_apply_config_file):
                config_util.use_config_file()
 def test_it_sets_options_from_an_json_file(self):
     contents = """
         { "a" : "stuff"
         , "b" : "things"
         }
     """
     with a_temp_file(contents) as filename:
         config_util = ConfigUtil()
         config_util.values |should| equal_to({})
         config_util.apply_config_file(filename)
         config_util.values |should| equal_to({"a":"stuff", "b":"things"})
    def test_it_doesnt_override_existing_non_default_values(self):
        contents = """
            { "a" : "stuff"
            , "b" : "things"
            , "c" : "and"
            , "d" : "shells"
            , "e-f" : "blah"
            , "g-h" : "other"
            }
        """
        with a_temp_file(contents) as filename:
            config_util = ConfigUtil()
            c_val = Default(21)
            config_util.use_options({'b':21, 'c':c_val, 'd':None, "g-h":"meh"})
            config_util.values |should| equal_to({'b':21, 'c':c_val, 'd':None, "g_h":"meh"})

            config_util.apply_config_file(filename)
            config_util.values |should| equal_to({"a":"stuff", "b":21, "c":"and", 'd':None, "e_f":"blah", "g_h":"meh"})
 def setUp(self):
     self.config_util = ConfigUtil()
     self.fullpath = fudge.Fake('fullpath')
     self.filename = fudge.Fake('filename')
 def setup(self):
     self.config_util = ConfigUtil()
     self.config_util.values |should| equal_to({})
 def test_it_returns_value_as_is(self):
     val = fudge.Fake("val")
     config_util = ConfigUtil()
     config_util.values = {"c":val}
     config_util.find_value("c") |should| be(val)
 def test_it_returns_actual_value_if_finds_default(self):
     config_util = ConfigUtil()
     config_util.values = {"a":Default(21)}
     config_util.find_value("a") |should| be(21)
def test_config_util_normalising_keys():
    '''Test ConfigUtil knows how to normalise a key'''
    config_util = ConfigUtil()
    config_util.normalise_key("blah") |should| equal_to("blah")
    config_util.normalise_key("blah_stuff-things") |should| equal_to("blah_stuff_things")
    config_util.normalise_key("blah-stuff-things") |should| equal_to("blah_stuff_things")