From d13dd482b5ba3be269e214042ab50969dff2ff92 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 11 Jan 2012 21:09:52 -0800 Subject: [PATCH] Some Config::Top class tests --- lib/vagrant/config/top.rb | 5 +++-- test/unit/vagrant/config/top_test.rb | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 test/unit/vagrant/config/top_test.rb diff --git a/lib/vagrant/config/top.rb b/lib/vagrant/config/top.rb index 4d49c290a..8c2d0e5f4 100644 --- a/lib/vagrant/config/top.rb +++ b/lib/vagrant/config/top.rb @@ -7,8 +7,9 @@ module Vagrant # # If you're looking to create your own configuration class, see {Base}. class Top < Base - def initialize + def initialize(registry=nil) @keys = {} + @registry = registry || Vagrant.config_keys end # We use method_missing as a way to get the configuration that is used @@ -16,7 +17,7 @@ module Vagrant def method_missing(name, *args) return @keys[name] if @keys.has_key?(name) - config_klass = Vagrant.config_keys.get(name.to_sym) + config_klass = @registry.get(name.to_sym) if config_klass # Instantiate the class and return the instance @keys[name] = config_klass.new diff --git a/test/unit/vagrant/config/top_test.rb b/test/unit/vagrant/config/top_test.rb new file mode 100644 index 000000000..21daf8c8e --- /dev/null +++ b/test/unit/vagrant/config/top_test.rb @@ -0,0 +1,27 @@ +require File.expand_path("../../../base", __FILE__) + +require "vagrant/registry" + +describe Vagrant::Config::Top do + include_context "unit" + + let(:registry) { Vagrant::Registry.new } + let(:instance) { described_class.new(registry) } + + it "should load in the proper config class" do + registry.register(:foo, Object) + + instance.foo.should be_kind_of(Object) + end + + it "should load the proper config class only once" do + registry.register(:foo, Object) + + obj = instance.foo + instance.foo.should eql(obj) + end + + it "still raises a method missing error if invalid key" do + expect { instance.foo }.to raise_error(NoMethodError) + end +end