Revision: 801 http://trac.macosforge.org/projects/ruby/changeset/801 Author: eloy.de.enige@gmail.com Date: 2009-01-20 16:21:56 -0800 (Tue, 20 Jan 2009) Log Message: ----------- Applied patch from #212 by Vincent Isambart and added test coverage. Also improved the other test descriptions and code and refactored the #map code. Modified Paths: -------------- MacRuby/trunk/lib/hotcocoa/mappings.rb MacRuby/trunk/test-macruby/cases/hotcocoa/mappings_test.rb MacRuby/trunk/test-macruby/cases/objc_test.rb Modified: MacRuby/trunk/lib/hotcocoa/mappings.rb =================================================================== --- MacRuby/trunk/lib/hotcocoa/mappings.rb 2009-01-20 20:09:22 UTC (rev 800) +++ MacRuby/trunk/lib/hotcocoa/mappings.rb 2009-01-21 00:21:56 UTC (rev 801) @@ -1,6 +1,6 @@ module HotCocoa module Mappings - + def self.reload Dir.glob(File.join(File.dirname(__FILE__), "mappings", "*.rb")).each do |mapping| load mapping @@ -8,7 +8,7 @@ end DefaultEmptyRect = [0,0,0,0] - + module TargetActionConvenience def on_action=(behavior) object = Object.new @@ -25,38 +25,45 @@ self end end - + def self.map(options, &block) framework = options.delete(:framework) mapped_name = options.keys.first mapped_value = options.values.first - const = Object.full_const_get(mapped_value) + if mapped_value.kind_of?(Class) - m = Mapper.map_instances_of(mapped_value, mapped_name, &block) - mappings[m.builder_method] = m - elsif framework.nil? || const - m = Mapper.map_instances_of(const, mapped_name, &block) - mappings[m.builder_method] = m + add_mapping(mapped_name, mapped_value, &block) else - on_framework(framework) do - m = Mapper.map_instances_of(const, mapped_name, &block) - mappings[m.builder_method] = m + constant = Object.full_const_get(mapped_value) + if framework.nil? || constant + add_mapping(mapped_name, constant, &block) + else + on_framework(framework) do + add_mapping(mapped_name, constant, &block) + end end end end - + + # Registers +mapped_name+ as a Mapper#builder_method for the given + # +mapped_value+. The +block+ is used as the Mapper#builder_method's body. + def self.add_mapping(mapped_name, mapped_value, &block) + m = Mapper.map_instances_of(mapped_value, mapped_name, &block) + mappings[m.builder_method] = m + end + def self.mappings @mappings ||= {} end - + def self.on_framework(name, &block) (frameworks[name.to_s.downcase] ||= []) << block end - + def self.frameworks @frameworks ||= {} end - + def self.framework_loaded(name) if frameworks[name.to_s.downcase] frameworks[name.to_s.downcase].each do |mapper| Modified: MacRuby/trunk/test-macruby/cases/hotcocoa/mappings_test.rb =================================================================== --- MacRuby/trunk/test-macruby/cases/hotcocoa/mappings_test.rb 2009-01-20 20:09:22 UTC (rev 800) +++ MacRuby/trunk/test-macruby/cases/hotcocoa/mappings_test.rb 2009-01-21 00:21:56 UTC (rev 801) @@ -17,48 +17,60 @@ assert Mappings.frameworks.is_a?(Hash) end - it "should register callbacks for the point of time when a framework is loaded with #on_framework" do - p = Proc.new {} - Mappings.on_framework(:Foo, &p) - assert_equal(Mappings.frameworks['foo'].last, p) + it "should create a mapping to a class with a Class instance given to #map" do + Mappings.map(:klass => SampleClass) {} + assert_equal SampleClass, Mappings.mappings[:klass].control_class end - it "should create a mapping to a class with #map" do - - block = Proc.new do - def alloc_with_options(options); options; end + it "should create a mapping to a class with a string name of the class given to #map" do + Mappings.map(:klass => 'SampleClass') {} + assert_equal SampleClass, Mappings.mappings[:klass].control_class + end + + it "should create a mapping to a class with a symbol name of the class given to #map" do + Mappings.map(:klass => 'SampleClass') {} + assert_equal SampleClass, Mappings.mappings[:klass].control_class + end + + it "should register the key, in the options given to #map, as the builder_method" do + Mappings.map(:klass => SampleClass) {} + assert_equal Mappings.mappings[:klass].builder_method, :klass + end + + it "should use the block given to #map as the control_module body" do + Mappings.map(:klass => SampleClass) do + def a_control_module_instance_method; end end - HotCocoa::Mappings.map({:foo => :SampleClass}, &block) - - m = HotCocoa::Mappings.mappings[:foo] - - assert_equal(m.control_class, SampleClass) - assert_equal(m.builder_method, :foo) - assert(m.control_module.instance_methods.include?(:alloc_with_options)) - + assert Mappings.mappings[:klass].control_module. + instance_methods.include?(:a_control_module_instance_method) end - it "should create a mapping to a class for a framework with #map" do - p = Proc.new {} - HotCocoa::Mappings.map({:foo => :SampleClass, :framework => :Anonymous}, &p) - # require 'pp'; pp HotCocoa::Mappings + it "should create a mapping to a class in a framework with #map" do + mock = mocked_object - # FIXME: This is not really nice. We test that the result exists, but not what it is. - assert_equal Mappings.frameworks["anonymous"].size, 1 + Mappings.map(:klass => 'ClassInTheFrameWork', :framework => 'TheFramework') do + mock.call! + end + Mappings.frameworks["theframework"].last.call + + assert mock.called? end - it "should call the framework's callbacks if it's passed to #framework_loaded" do - p = Proc.new { SampleClass.val = true } - Mappings.on_framework(:Foo, &p) - Mappings.framework_loaded(:Foo) + it "should execute the framework's callbacks when #framework_loaded is called" do + mock1, mock2 = mocked_object, mocked_object - assert_equal SampleClass.val, true + [mock1, mock2].each do |mock| + Mappings.on_framework('TheFramework') { mock.call! } + end + Mappings.framework_loaded('TheFramework') + + [mock1, mock2].each { |mock| assert mock.called? } end - it "should raise nothing if there's no entry for the framework passed to #framework_loaded" do + it "should do nothing if the framework passed to #framework_loaded isn't registered" do assert_nothing_raised do - Mappings.framework_loaded(:FrameworkDoesNotExist) + Mappings.framework_loaded('FrameworkDoesNotExist') end end @@ -66,4 +78,16 @@ flunk 'Pending.' end + private + + def mocked_object + mock = Object.new + def mock.call! + @called = true + end + def mock.called? + @called + end + mock + end end \ No newline at end of file Modified: MacRuby/trunk/test-macruby/cases/objc_test.rb =================================================================== --- MacRuby/trunk/test-macruby/cases/objc_test.rb 2009-01-20 20:09:22 UTC (rev 800) +++ MacRuby/trunk/test-macruby/cases/objc_test.rb 2009-01-21 00:21:56 UTC (rev 801) @@ -174,4 +174,4 @@ it "should instantiate with klass::alloc.init" do assert_kind_of NSObject, NSObject.alloc.init end -end +end \ No newline at end of file