Skip to content

Instantly share code, notes, and snippets.

@donnoman
Created February 3, 2011 19:17
Show Gist options
  • Save donnoman/809989 to your computer and use it in GitHub Desktop.
Save donnoman/809989 to your computer and use it in GitHub Desktop.
Just a simple recursion test
#faking the rails class to keep the test simple
class Rails
def self.env
"development"
end
end
def get_descriptor(descriptor, descriptor_keys = Rails.env, i = 0)
if descriptor.is_a?(Array) || descriptor.is_a?(String)
descriptor
elsif descriptor.is_a?(Hash)
if descriptor_keys.is_a?(Array)
get_descriptor(descriptor[descriptor_keys[i]], descriptor_keys, i + 1)
else
descriptor[descriptor_keys]
end
end
end
describe "descriptor" do
it "should return self when subject is a string" do
subject = "subject"
get_descriptor(subject).should == subject
end
it "should return self when subject is an array" do
subject = %w(subject1 subject2 subject3)
get_descriptor(subject).should == subject
end
it "should return the hash member of development" do
subject = {"development" => "subject"}
get_descriptor(subject).should == "subject"
end
it "should return nil when no member matches a descriptor key" do
subject = {"gibberish" => "subject"}
get_descriptor(subject).should be_nil
end
context "with multiple descriptor keys" do
before :each do
@keys = %w(development test)
end
it "should return something" do
subject = { "development" => "subject1", "test" => "subject2"}
get_descriptor(subject,@keys).should == "subject1" #why?
# I would have guessed the intent would have been to return something
# like ["subject1","subject2"].
# What is the desired purpose of the function?
# What is a sample input of a hash and keys and expected return value look like.
end
it "should probably still return something" do
subject = { "test" => "subject2"}
get_descriptor(subject,@keys).should == "subject2" #maybe?
#as it is anything but the first key will ever be returned.
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment