[macruby-changes] [808] MacRuby/trunk
source_changes at macosforge.org
source_changes at macosforge.org
Thu Jan 22 08:31:00 PST 2009
Revision: 808
http://trac.macosforge.org/projects/ruby/changeset/808
Author: eloy.de.enige at gmail.com
Date: 2009-01-22 08:30:59 -0800 (Thu, 22 Jan 2009)
Log Message:
-----------
Added test coverage for whether or not a framework has been loaded. Added Mappings.framework_loaded? and did some refactoring. Added more documentation, although Mappings.map still really really needs some comprehensive docs?\226?\128?\166
Modified Paths:
--------------
MacRuby/trunk/lib/hotcocoa/mappings.rb
MacRuby/trunk/test-macruby/cases/hotcocoa/mappings_test.rb
Added Paths:
-----------
MacRuby/trunk/test-macruby/test_helper/assertions_helper.rb
Modified: MacRuby/trunk/lib/hotcocoa/mappings.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa/mappings.rb 2009-01-22 16:30:50 UTC (rev 807)
+++ MacRuby/trunk/lib/hotcocoa/mappings.rb 2009-01-22 16:30:59 UTC (rev 808)
@@ -26,17 +26,22 @@
end
end
+ # TODO: Needs docs for all possible invocations and examples!
def self.map(options, &block)
framework = options.delete(:framework)
mapped_name = options.keys.first
mapped_value = options.values.first
+ args = [mapped_name, mapped_value]
if mapped_value.kind_of?(Class)
- add_mapping(mapped_name, mapped_value, &block)
+ add_mapping(*args, &block)
else
- on_framework(framework) do
- constant = Object.full_const_get(mapped_value)
- add_mapping(mapped_name, constant, &block)
+ if framework.nil? || loaded_framework?(framework)
+ add_constant_mapping(*args, &block)
+ else
+ on_framework(framework) do
+ add_constant_mapping(*args, &block)
+ end
end
end
end
@@ -48,27 +53,34 @@
mappings[m.builder_method] = m
end
+ # Registers +mapped_name+ as a Mapper#builder_method for the given
+ # +constant+ string which will be looked up. The +block+ is used as the
+ # Mapper#builder_method's body.
+ def self.add_constant_mapping(mapped_name, constant, &block)
+ add_mapping(mapped_name, Object.full_const_get(constant), &block)
+ end
+
+ # Returns the Hash of mappings.
def self.mappings
@mappings ||= {}
end
+ # Registers a callback for after the specified framework has been loaded.
def self.on_framework(name, &block)
- name = name.nil? ? nil : name.to_s.downcase
- if name.nil? or loaded_frameworks.include?(name)
- block.call
- else
- (frameworks[name] ||= []) << block
- end
+ (frameworks[name.to_s.downcase] ||= []) << block
end
+ # Returns the Hash of mapped frameworks.
def self.frameworks
@frameworks ||= {}
end
+ # Returns the Set of loaded frameworks.
def self.loaded_frameworks
@loaded_frameworks ||= Set.new
end
+ # Registers a given framework as being loaded.
def self.framework_loaded(name)
name = name.to_s.downcase
loaded_frameworks << name
@@ -79,6 +91,11 @@
end
end
+ # Returns whether or not the framework has been loaded yet.
+ def self.loaded_framework?(name)
+ loaded_frameworks.include?(name.to_s.downcase)
+ end
+
end
end
Modified: MacRuby/trunk/test-macruby/cases/hotcocoa/mappings_test.rb
===================================================================
--- MacRuby/trunk/test-macruby/cases/hotcocoa/mappings_test.rb 2009-01-22 16:30:50 UTC (rev 807)
+++ MacRuby/trunk/test-macruby/cases/hotcocoa/mappings_test.rb 2009-01-22 16:30:59 UTC (rev 808)
@@ -104,6 +104,24 @@
assert_equal ClassFromFramework, Mappings.mappings[:klass].control_class
end
+ it "should keep a unique list of loaded_frameworks" do
+ assert_difference("Mappings.loaded_frameworks.length", +1) do
+ Mappings.framework_loaded('TheFramework')
+ Mappings.framework_loaded('TheFramework')
+ end
+
+ assert Mappings.loaded_frameworks.include?('theframework')
+ end
+
+ it "should return whether or not a framework has been loaded yet" do
+ Mappings.framework_loaded('TheFramework')
+ assert Mappings.loaded_framework?('TheFramework')
+
+ assert !Mappings.loaded_framework?('IHasNotBeenLoaded')
+ assert !Mappings.loaded_framework?(nil)
+ assert !Mappings.loaded_framework?('')
+ end
+
def test_reload
flunk 'Pending.'
end
Added: MacRuby/trunk/test-macruby/test_helper/assertions_helper.rb
===================================================================
--- MacRuby/trunk/test-macruby/test_helper/assertions_helper.rb (rev 0)
+++ MacRuby/trunk/test-macruby/test_helper/assertions_helper.rb 2009-01-22 16:30:59 UTC (rev 808)
@@ -0,0 +1,36 @@
+require "test/unit"
+
+module AssertionsExt
+ # Asserts that at the end of the block the result of evaluating the given
+ # +eval_string+ differs by +difference+.
+ #
+ # a = []
+ #
+ # assert_difference("a.length", +1) do
+ # a << "foo"
+ # end
+ #
+ # assert_difference("a.length", -1) do
+ # a.pop
+ # end
+ def assert_difference(eval_string, difference)
+ before = instance_eval(eval_string)
+ yield
+ assert_equal (before + difference), instance_eval(eval_string)
+ end
+
+ # Asserts that at the end of the block the result of evaluating the given
+ # +eval_string+ does _not_ differ.
+ #
+ # a = Set.new
+ # a << "foo"
+ #
+ # assert_no_difference("a.length") do
+ # a << "foo"
+ # end
+ def assert_no_difference(eval_string, &block)
+ assert_difference(eval_string, 0, &block)
+ end
+end
+
+Test::Unit::TestCase.send(:include, AssertionsExt)
\ No newline at end of file
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20090122/16d5205f/attachment.html>
More information about the macruby-changes
mailing list