[macruby-changes] [1363] MacRuby/branches/experimental/mspec

source_changes at macosforge.org source_changes at macosforge.org
Sat Apr 4 18:11:57 PDT 2009


Revision: 1363
          http://trac.macosforge.org/projects/ruby/changeset/1363
Author:   eloy.de.enige at gmail.com
Date:     2009-04-04 18:11:56 -0700 (Sat, 04 Apr 2009)
Log Message:
-----------
Added Object#variables helper and IncludeVariablesMatcher which are used for variable arrays which had strings pre 1.9 and symbols post 1.9.

Modified Paths:
--------------
    MacRuby/branches/experimental/mspec/lib/mspec/helpers.rb
    MacRuby/branches/experimental/mspec/lib/mspec/matchers.rb

Added Paths:
-----------
    MacRuby/branches/experimental/mspec/lib/mspec/helpers/variables.rb
    MacRuby/branches/experimental/mspec/lib/mspec/matchers/include_variables.rb
    MacRuby/branches/experimental/mspec/spec/helpers/variables_spec.rb
    MacRuby/branches/experimental/mspec/spec/matchers/include_variables_spec.rb

Added: MacRuby/branches/experimental/mspec/lib/mspec/helpers/variables.rb
===================================================================
--- MacRuby/branches/experimental/mspec/lib/mspec/helpers/variables.rb	                        (rev 0)
+++ MacRuby/branches/experimental/mspec/lib/mspec/helpers/variables.rb	2009-04-05 01:11:56 UTC (rev 1363)
@@ -0,0 +1,25 @@
+class Object
+  # Convenience helper for casting variable names as
+  # strings or symbols depending on if RUBY_VERSION
+  # is lower than 1.9. Before Ruby 1.9 all variable
+  # methods return arrays of symbols. However, since
+  # Ruby 1.9 these methods return arrays of symbols.
+  #
+  # Example:
+  #
+  # describe "This" do
+  #   before do
+  #     @instance = Object.new
+  #     @instance.instance_variable_set("@foo", "foo")
+  #     @instance.instance_variable_set("@bar", "bar")
+  #   end
+  #
+  #   it "contains specific instance variables" do
+  #     @instance.instance_variables.should == variables("@foo", "@bar")
+  #   end
+  # end
+  def variables(*vars)
+    RUBY_VERSION < '1.9' ? vars.map { |v| v.to_s } : vars.map { |v| v.to_sym }
+  end
+  alias_method :variable, :variables
+end
\ No newline at end of file

Modified: MacRuby/branches/experimental/mspec/lib/mspec/helpers.rb
===================================================================
--- MacRuby/branches/experimental/mspec/lib/mspec/helpers.rb	2009-04-05 00:40:27 UTC (rev 1362)
+++ MacRuby/branches/experimental/mspec/lib/mspec/helpers.rb	2009-04-05 01:11:56 UTC (rev 1363)
@@ -9,3 +9,4 @@
 require 'mspec/helpers/ruby_exe'
 require 'mspec/helpers/scratch'
 require 'mspec/helpers/tmp'
+require 'mspec/helpers/variables'

Added: MacRuby/branches/experimental/mspec/lib/mspec/matchers/include_variables.rb
===================================================================
--- MacRuby/branches/experimental/mspec/lib/mspec/matchers/include_variables.rb	                        (rev 0)
+++ MacRuby/branches/experimental/mspec/lib/mspec/matchers/include_variables.rb	2009-04-05 01:11:56 UTC (rev 1363)
@@ -0,0 +1,15 @@
+require 'mspec/helpers/variables'
+require 'mspec/matchers/include'
+
+class IncludeVariablesMatcher < IncludeMatcher
+  def initialize(*expected)
+    @expected = variables(*expected)
+  end
+end
+
+class Object
+  def include_variables(*variables)
+    IncludeVariablesMatcher.new(*variables)
+  end
+  alias_method :include_variable, :include_variables
+end
\ No newline at end of file

Modified: MacRuby/branches/experimental/mspec/lib/mspec/matchers.rb
===================================================================
--- MacRuby/branches/experimental/mspec/lib/mspec/matchers.rb	2009-04-05 00:40:27 UTC (rev 1362)
+++ MacRuby/branches/experimental/mspec/lib/mspec/matchers.rb	2009-04-05 01:11:56 UTC (rev 1363)
@@ -17,6 +17,7 @@
 require 'mspec/matchers/have_private_instance_method'
 require 'mspec/matchers/have_public_instance_method'
 require 'mspec/matchers/include'
+require 'mspec/matchers/include_variables'
 require 'mspec/matchers/match_yaml'
 require 'mspec/matchers/raise_error'
 require 'mspec/matchers/output'

Added: MacRuby/branches/experimental/mspec/spec/helpers/variables_spec.rb
===================================================================
--- MacRuby/branches/experimental/mspec/spec/helpers/variables_spec.rb	                        (rev 0)
+++ MacRuby/branches/experimental/mspec/spec/helpers/variables_spec.rb	2009-04-05 01:11:56 UTC (rev 1363)
@@ -0,0 +1,24 @@
+require File.dirname(__FILE__) + '/../spec_helper'
+require 'mspec/helpers/variables'
+
+describe Object, "#variables" do
+  before :each do
+    @ruby_version = Object.const_get :RUBY_VERSION
+  end
+
+  after :each do
+    Object.const_set :RUBY_VERSION, @ruby_version
+  end
+
+  it "casts as strings if RUBY_VERSION < 1.9" do
+    Object.const_set :RUBY_VERSION, "1.8.6"
+    variables(:foo, :bar).should == %w{ foo bar }
+    variables('foo', 'bar').should == %w{ foo bar }
+  end
+
+  it "casts as symbols if RUBY_VERSION >= 1.9" do
+    Object.const_set :RUBY_VERSION, "1.9.0"
+    variables(:foo, :bar).should == [:foo, :bar]
+    variables('foo', 'bar').should == [:foo, :bar]
+  end
+end

Added: MacRuby/branches/experimental/mspec/spec/matchers/include_variables_spec.rb
===================================================================
--- MacRuby/branches/experimental/mspec/spec/matchers/include_variables_spec.rb	                        (rev 0)
+++ MacRuby/branches/experimental/mspec/spec/matchers/include_variables_spec.rb	2009-04-05 01:11:56 UTC (rev 1363)
@@ -0,0 +1,63 @@
+require File.dirname(__FILE__) + '/../spec_helper'
+require 'mspec/expectations/expectations'
+require 'mspec/matchers/include_variables'
+
+describe IncludeVariablesMatcher do
+  it "inherits from IncludeMatcher" do
+    IncludeVariablesMatcher.new('@foo').should be_kind_of(IncludeMatcher)
+  end
+end
+
+describe IncludeVariablesMatcher, "if RUBY_VERSION < 1.9" do
+  before :each do
+    @ruby_version = Object.const_get :RUBY_VERSION
+    Object.const_set :RUBY_VERSION, "1.8.6"
+  end
+
+  after :each do
+    Object.const_set :RUBY_VERSION, @ruby_version
+  end
+
+  it "matches when the array of strings includes the variable name" do
+    matcher = IncludeVariablesMatcher.new('@foo')
+    matcher.matches?(%w{ @foo @bar }).should be_true
+
+    matcher = IncludeVariablesMatcher.new(:@foo)
+    matcher.matches?(%w{ @foo @bar }).should be_true
+  end
+
+  it "does not match when the array of strings does not include the variable name" do
+    matcher = IncludeVariablesMatcher.new('@baz')
+    matcher.matches?(%w{ @foo @bar }).should be_false
+
+    matcher = IncludeVariablesMatcher.new(:@baz)
+    matcher.matches?(%w{ @foo @bar }).should be_false
+  end
+end
+
+describe IncludeVariablesMatcher, "if RUBY_VERSION >= 1.9" do
+  before :each do
+    @ruby_version = Object.const_get :RUBY_VERSION
+    Object.const_set :RUBY_VERSION, "1.9.0"
+  end
+
+  after :each do
+    Object.const_set :RUBY_VERSION, @ruby_version
+  end
+
+  it "matches when the array of symbols includes the variable name" do
+    matcher = IncludeVariablesMatcher.new('@foo')
+    matcher.matches?([:@foo, :@bar]).should be_true
+
+    matcher = IncludeVariablesMatcher.new(:@foo)
+    matcher.matches?([:@foo, :@bar]).should be_true
+  end
+
+  it "does not match when the array of symbols does not include the variable name" do
+    matcher = IncludeVariablesMatcher.new('@baz')
+    matcher.matches?([:@foo, :@bar]).should be_false
+
+    matcher = IncludeVariablesMatcher.new(:@baz)
+    matcher.matches?([:@foo, :@bar]).should be_false
+  end
+end
\ No newline at end of file
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20090404/99888432/attachment-0001.html>


More information about the macruby-changes mailing list