spec/keytar/key_builder_spec.rb in keytar-0.9.0 vs spec/keytar/key_builder_spec.rb in keytar-1.0.0

- old
+ new

@@ -1,203 +1,166 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper') - class Foo - include KeyBuilder + include Keytar + + def ymd + Time.now.strftime("%y%m%d") + end end - describe KeyBuilder do - - describe 'class methods' do - it 'should respond to "key" method by returning downcase of class name' do - Foo.key.should == "foo" + describe 'build_key class method' do + before do + @options = {:name => "foo", :args => nil} end - it 'should respond to "awesome_key" method by returning :class, :delimiter, :name' do - Foo.awesome_key.should == "foo:awesome" + describe 'build_key' do + it 'calls other class methods' do + Foo.should_receive(:build_key_hash) + Foo.should_receive(:key_hash_to_ordered_array) + Foo.should_receive(:key_from_array) + Foo.build_key(@options) + end end - it 'should respond to "awesome_key(number)" method by returning :class, :delimiter, :name, :delimiter, :arg' do - number = rand(100) - Foo.awesome_key(number).should == "foo:awesome:#{number}" - end + describe 'build_key_hash' do + it 'removes key and _key from the :name option' do + Foo.build_key_hash(@options.merge(:name => "foo_key"))[:name].should == "foo" + Foo.build_key_hash(@options.merge(:name => "key"))[:name].should == "" + Foo.build_key_hash(@options.merge(:name => "fookey"))[:name].should == "fookey" + end - it 'should dynamically define a class method capeable of producing different keys' do - key1 = Foo.define_method_time_test_key(1) - key2 = Foo.define_method_time_test_key(2) - Foo.respond_to?(:define_method_time_test_key).should be_true - key2.should_not == key1 - end + describe 'prefix' do + it 'takes in prefix converts key_prefix to prefix' do + prefix = "prefix" + Foo.build_key_hash(@options.merge(:key_prefix => prefix))[:prefix].should == prefix + end + it 'favors direct options over class settings' do + prefix = "prefix" + Foo.should_not_receive(:key_prefix) + Foo.build_key_hash(@options.merge(:key_prefix => prefix))[:prefix].should == prefix + end - - it 'should call method_missing on a non-existant method' do - begin - Foo.thismethoddoesnotexist - rescue => ex + it "defaults to class settings when no direct option is given" do + prefix = "classPrefix" + Foo.should_receive(:key_prefix).and_return(prefix) + Foo.build_key_hash(@options)[:prefix].should == prefix + end end - ex.class.should == NoMethodError - end - end + describe "suffix" do + it 'takes in suffix and converts key_suffix to suffix' do + suffix = "sufix" + Foo.build_key_hash(@options.merge(:key_suffix => suffix))[:suffix].should == suffix + end - describe 'instance methods' do - before(:each) do - @foo = Foo.new - end + it 'favors direct options over class settings' do + suffix = "suffix" + Foo.should_not_receive(:key_suffix) + Foo.build_key_hash(@options.merge(:key_suffix => suffix))[:suffix].should == suffix + end - it 'should respond to "key" method by returning pluralized downcase of class name' do - @foo.key.should == "foos" + it "defaults to class settings when no direct option is given" do + suffix = "classSuffix" + Foo.should_receive(:key_suffix).and_return(suffix) + Foo.build_key_hash(@options)[:suffix].should == suffix + end + end end - it 'should respond to "awesome_key" method by returning :class, :delimiter, :name' do - @foo.awesome_key.should == "foos:awesome" - end + describe 'key_hash_to_rdered_array' do + before do + @options.merge!(:prefix => "prefix", :base => "base", :name => "name", :suffix => "suffix", :version => 1) + end - it 'should respond to "awesome_key(number)" method by returning :class, :delimiter, :name, :delimiter, :arg' do - number = rand(100) - @foo.awesome_key(number).should == "foos:awesome:#{number}" - end + it "converts a hash to an array based on default order" do + Foo.key_hash_to_ordered_array(@options).should == [@options[:prefix], @options[:base], @options[:name], @options[:unique], @options[:suffix], @options[:version], @options[:v]] + end - it 'should dynamically define an instance method capeable of producing different keys' do - key1 = @foo.define_method_time_test_key(1) - key2 = @foo.define_method_time_test_key(2) - @foo.respond_to?(:define_method_time_test_key).should be_true - key2.should_not == key1 - end + it "order output using direct option before class config" do + Foo.should_not_receive(:key_order) + key_order = [:prefix, :base, :name, :unique, :args, :suffix, :version, :v].reverse + Foo.key_hash_to_ordered_array(@options.merge(:key_order => key_order)).should == [@options[:prefix], @options[:base], @options[:name], @options[:unique], @options[:suffix], @options[:version], @options[:v]].reverse + end - it 'should dynamically define an instance method capeable of producing different keys' do - key1 = @foo.define_method_time_test_key(1) - key2 = @foo.define_method_time_test_key(2) - @foo.respond_to?(:define_method_time_test_key).should be_true - key2.should_not == key1 + it "convert each arg in args to a string" do + args = ["hey", 1 , "what", 2, "up", []] + Foo.key_hash_to_ordered_array(@options.merge(:args => args)).include?(args.map(&:to_s)).should be_true + end end - it 'should call method_missing on a non-existant method' do - begin - @foo.thismethoddoesnotexist - rescue => ex + describe 'key_from_array' do + before do + @key_array = ["foo", "Bar", "oH", "YEAH"] end - ex.class.should == NoMethodError - end - end - # test last - describe 'class configurations' do - after(:each) do - # todo find a better way of resetting all these values - Foo.key_delimiter KeyBuilder::DEFAULTS[:key_delimiter] - Foo.key_order KeyBuilder::DEFAULTS[:key_order] - Foo.key_prefix KeyBuilder::DEFAULTS[:key_prefix] - Foo.key_suffix KeyBuilder::DEFAULTS[:key_suffix] - Foo.key_pluralize_instances KeyBuilder::DEFAULTS[:key_pluralize_instances] - Foo.key_case KeyBuilder::DEFAULTS[:key_case] - Foo.key_plural KeyBuilder::DEFAULTS[:key_plural] - Foo.key_unique KeyBuilder::DEFAULTS[:key_unique] - Foo.key_cache_methods KeyBuilder::DEFAULTS[:key_cache_methods] - end + it "strip out nil from the array and have no spaces" do + Foo.key_from_array([nil, nil]).match(/nil/).should be_false + Foo.key_from_array([nil, nil]).match(/\s/).should be_false + end - it 'should change key_delimiter' do - key_delimiter = "|" - Foo.key_delimiter key_delimiter - Foo.key_delimiter.should == key_delimiter - Foo.awesome_key.should == "foo#{key_delimiter}awesome" - end + it "return a string" do + Foo.key_from_array(@key_array).class.should == String + end + it "keep the key case consistent (downcase by default)" do + Foo.key_from_array(@key_array).should == @key_array.map(&:downcase).join(":") + end - it 'should change key_order' do - order_array = [:prefix, :name, :unique, :base, :args, :suffix] - Foo.key_order order_array - Foo.key_order.should == order_array - Foo.awesome_key.should == "awesome:foo" - end + it "allow different cases to be passed in via options" do + Foo.should_not_receive(:key_case) + Foo.key_from_array(@key_array, :key_case => :upcase).should == @key_array.map(&:upcase).join(":") + end - it 'should change key_prefix' do - key_prefix = "memcache" - Foo.key_prefix key_prefix - Foo.key_prefix.should == key_prefix - Foo.awesome_key.should == "#{key_prefix}:foo:awesome" + it "flattens all inputs" do + array = @key_array << [[["1"], ["2"]],["3"]] + Foo.key_from_array(array, :key_case => :upcase).should == array.flatten.map(&:upcase).join(":") + end end + end - it 'should change key_suffix' do - key_suffix = "slave" - Foo.key_suffix key_suffix - Foo.key_suffix.should == key_suffix - Foo.awesome_key.should == "foo:awesome:#{key_suffix}" + describe 'build_key instance method' do + before do + @options = {:name => "foo", :args => nil} + @foo = Foo.new end - it 'should change key_pluralize_instances' do - key_pluralize_instances = false - Foo.key_pluralize_instances key_pluralize_instances - Foo.key_pluralize_instances.should == key_pluralize_instances - foo = Foo.new - foo.awesome_key.should == "foo:awesome" - end + describe 'build_key' do + it 'sets_base to class name and pluralizes it' do + @foo.class.should_receive(:build_key).with(hash_including(:base => @foo.class.to_s.downcase + "s")) + @foo.build_key(@options) + end - it 'should change key_case' do - key_case = :upcase - Foo.key_case key_case - Foo.key_case.should == key_case - Foo.awesome_key.should == "FOO:AWESOME" - end + it 'allows a manual over-ride of base' do + base = "base" + @foo.class.should_receive(:build_key).with(hash_including(:base => base + "s")) + @foo.build_key(@options.merge(:base => base)) + end - it 'should change key_plural' do - key_plural = "fooz" - Foo.key_plural key_plural - Foo.key_plural.should == key_plural - foo = Foo.new - foo.awesome_key.should == "fooz:awesome" - end + it "don't pluralize base if key_pluralize_instances is set to false" do + @foo.class.should_not_receive(:key_pluralize_instances) + @foo.class.should_receive(:build_key).with(hash_including(:base => @foo.class.to_s.downcase)) + @foo.build_key(@options.merge(:key_pluralize_instances => false)) + end - it 'should change key_unique' do - Foo.class_eval { def timeish; (Time.now.to_i * 0.01).floor; end} - key_unique = :timeish - Foo.key_unique key_unique - Foo.key_unique.should == key_unique - foo = Foo.new - foo.awesome_key.should == "foos:awesome:#{foo.timeish}" - end + it "don't pluralize base if key_pluralize_instances is set to false" do + @foo.class.should_not_receive(:key_pluralize_instances) + @foo.class.should_receive(:build_key).with(hash_including(:base => @foo.class.to_s.downcase)) + @foo.build_key(@options.merge(:key_pluralize_instances => false)) + end - # todo move tests and assertsions to seperate describe and it blocks - it 'should allow all configurations to be set using a hash' do - # variables - key_delimiter = "/" - key_order = [:prefix, :base, :suffix] - key_prefix = "before" - key_suffix = "after" - key_pluralize_instances = false - key_case = :upcase - key_plural = "zoosk" - key_unique = "doesn-t_apply_to_instance_methods" - key_cache_methods = false - # config - Foo.keyfig :key_delimiter => key_delimiter, - :key_order => key_order, - :key_prefix => key_prefix, - :key_suffix => key_suffix, - :key_pluralize_instances => key_pluralize_instances, - :key_case => key_case, - :key_plural => key_plural, - :key_unique => key_unique, - :key_cache_methods => key_cache_methods - # assertions - Foo.key_delimiter.should == key_delimiter - Foo.key_order.should == key_order - Foo.key_prefix.should == key_prefix - Foo.key_suffix.should == key_suffix - Foo.key_pluralize_instances.should == key_pluralize_instances - Foo.key_case.should == key_case - Foo.key_plural.should == key_plural - Foo.key_unique.should == key_unique - Foo.key_cache_methods == key_cache_methods - end + it "allow unique method to be passed in via options" do + @foo.class.should_not_receive(:key_unique) + @foo.class.should_receive(:build_key).with(hash_including(:unique => @foo.ymd)) + @foo.build_key(@options.merge(:key_unique => :ymd)) + end - it 'should change key_cache_methods' do - Foo.key_cache_methods false - key1 = Foo.config_define_method_time_test_key(1) - Foo.respond_to?(:config_define_method_time_test_key).should be_false - end - + it "set unique based on configuration" do + @foo.class.should_receive(:key_unique).at_least(:once).and_return("ymd") + @foo.class.should_receive(:build_key).with(hash_including(:unique => @foo.ymd)) + @foo.build_key + end + end end - end \ No newline at end of file