require File.dirname(__FILE__) + '/test_helper' class OtherThing < HashCollector dsl_static dsl_accessor :thing, :other end class BuildOn < HashCollector dsl_accessor :ook end class TestHashCollector < Test::Unit::TestCase def setup @hash = { :colour => :red, :hue => 15 } @collected_hash = { :saturation => 17, :opacity => 0.43, :grooviness => 100 } @full_hash = @hash.merge( @collected_hash ) @collector = HashCollector.new end def teardown end context 'static hash collector' do setup do @collector = OtherThing.new end should "throw NoMethodError on collection" do assert_raise( NoMethodError ) do @collector.collect { thing 2; blah 3 } end assert_raise( NoMethodError ) do @collector.collect {|c| c.blah = 3 } end assert_raise( NoMethodError ) { @collector.blah } assert_raise( NoMethodError ) { @collector.blah = 5 } end should "understand defined attributes" do assert_nothing_raised do @collector.collect do thing 1 other 2 end end assert_nothing_raised { @collector.thing = 2 } assert_nothing_raised { @collector.other = 4 } end should "be dynamic" do assert BuildOn.dynamic? end end def assert_collected( collector ) assert_equal 17, collector.saturation assert_equal 0.43, collector.opacity assert_equal 100, collector.grooviness end def assert_hashed( collector ) assert_equal :red, collector.colour assert_equal 15, collector.hue end should "collect from a hash" do @collector.collect( @hash ) assert_equal @hash, @collector.to_hash end should 'not fail on a nil hash' do @collector.collect nil assert_equal 0, @collector.to_hash.size end should 'collect from a block, with dsl setters' do @collector.collect do saturation 17 opacity 0.43 grooviness 100 end assert_collected( @collector ) assert_equal @collected_hash, @collector.to_hash end should 'collect from a block, with = setters' do @collector.collect do |hc| hc.saturation = 17 hc.opacity = 0.43 hc.grooviness = 100 end assert_collected( @collector ) assert_equal @collected_hash, @collector.to_hash end should 'collect from a hash and block, with dsl setters' do @collector.collect( @hash ) do saturation 17 opacity 0.43 grooviness 100 end assert_collected( @collector ) assert_hashed( @collector ) assert_equal @full_hash, @collector.to_hash end should 'collect from a hash and block with = setters' do @collector.collect( @hash ) do |hc| hc.saturation = 17 hc.opacity = 0.43 hc.grooviness = 100 end assert_collected( @collector ) assert_hashed( @collector ) assert_equal @full_hash, @collector.to_hash end should 'not fail construction with a nil hash' do collector = HashCollector.new nil assert_equal 0, @collector.to_hash.size end should 'construct from a hash and block, with dsl setters' do collector = HashCollector.new( @hash ) do saturation 17 opacity 0.43 grooviness 100 end assert_collected( collector ) assert_hashed( collector ) assert_equal @full_hash, collector.to_hash end should 'construct from a hash and bslock with = setters' do collector = HashCollector.new( @hash ) do |hc| hc.saturation = 17 hc.opacity = 0.43 hc.grooviness = 100 end assert_collected( collector ) assert_hashed( collector ) assert_equal @full_hash, collector.to_hash end should 'not conflict with Object methods' do hc = HashCollector.new do display 'something' sample "This is a long string designed to test the code to the very limits of possibility." end assert_equal 'something', hc.display assert_equal "This is a long string designed to test the code to the very limits of possibility.", hc.sample end should 'work when called with &block instead of a do ... end' do options = gather_block do display 'activity' order 'lower(activity)' sample 'Troubleshooting' conditions 'active = true' end assert_equal 'activity', options[:display] end should 'work when called with options and &block instead of a do ... end' do options = gather_block :blah => 'ook' do display 'activity' order 'lower(activity)' sample 'Troubleshooting' conditions 'active = true' end assert_equal 'activity', options[:display] assert_equal 'ook', options[:blah] end def gather_block( options = {}, &block ) HashCollector.new( options, &block ).to_hash end end