require_relative 'spec_helper' require 'wright/util' describe Wright::Util do def stub_os(target_os) RbConfig.stub_const(:CONFIG, 'target_os' => target_os) do yield end end def without_bundler if defined?(Bundler) Object.stub_remove_const(:Bundler) { yield } else yield end end def with_bundler if defined?(Bundler) yield else fake_bundler = Class.new def fake_bundler.with_clean_env; end Object.stub_const(:Bundler, fake_bundler) { yield } end end describe 'filename_to_classname' do it 'should convert filenames to class names' do classname = Wright::Util.filename_to_classname('foo_bar/baz') classname.must_equal 'FooBar::Baz' end end describe 'class_to_resource_name' do it 'should convert classes to resource names' do resource_name = Wright::Util.class_to_resource_name(Object) resource_name.must_equal 'object' end end describe 'os_family' do before(:each) do @debian_os_release = < { mock.with_clean_env } mock.expect(:with_clean_env, []) mock.expect(:inside_block, []) with_bundler do Bundler.stub(:with_clean_env, with_clean_env_stub) do Wright::Util.bundler_clean_env { mock.inside_block } end end mock.verify end end describe 'fetch_last' do it 'should fetch the value of the last candidate key from a hash' do hash = { candidate1: :value1, candidate2: :value2, foo: :bar } candidates = [:candidate2, :candidate1] Wright::Util.fetch_last(hash, candidates).must_equal :value2 end it 'should return the default value if no candidate key is found' do hash = { foo: :foo, bar: :bar, baz: :baz } candidates = [:qux, :quux] Wright::Util.fetch_last(hash, candidates).must_be_nil Wright::Util.fetch_last(hash, candidates, :default).must_equal :default end end end