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

source_changes at macosforge.org source_changes at macosforge.org
Sun Apr 5 09:56:12 PDT 2009


Revision: 1369
          http://trac.macosforge.org/projects/ruby/changeset/1369
Author:   eloy.de.enige at gmail.com
Date:     2009-04-05 09:56:12 -0700 (Sun, 05 Apr 2009)
Log Message:
-----------
Added have_class_variable and have_instance_variable and refactored have_constant.

Modified Paths:
--------------
    MacRuby/branches/experimental/mspec/lib/mspec/matchers/have_constant.rb
    MacRuby/branches/experimental/mspec/lib/mspec/matchers/stringsymboladapter.rb
    MacRuby/branches/experimental/mspec/lib/mspec/matchers.rb
    MacRuby/branches/experimental/mspec/spec/matchers/stringsymboladapter_spec.rb

Added Paths:
-----------
    MacRuby/branches/experimental/mspec/lib/mspec/matchers/have_class_variable.rb
    MacRuby/branches/experimental/mspec/lib/mspec/matchers/have_instance_variable.rb
    MacRuby/branches/experimental/mspec/lib/mspec/matchers/variable.rb
    MacRuby/branches/experimental/mspec/spec/matchers/have_class_variable_spec.rb
    MacRuby/branches/experimental/mspec/spec/matchers/have_instance_variable_spec.rb

Removed 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

Deleted: MacRuby/branches/experimental/mspec/lib/mspec/helpers/variables.rb
===================================================================
--- MacRuby/branches/experimental/mspec/lib/mspec/helpers/variables.rb	2009-04-05 05:22:02 UTC (rev 1368)
+++ MacRuby/branches/experimental/mspec/lib/mspec/helpers/variables.rb	2009-04-05 16:56:12 UTC (rev 1369)
@@ -1,25 +0,0 @@
-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

Added: MacRuby/branches/experimental/mspec/lib/mspec/matchers/have_class_variable.rb
===================================================================
--- MacRuby/branches/experimental/mspec/lib/mspec/matchers/have_class_variable.rb	                        (rev 0)
+++ MacRuby/branches/experimental/mspec/lib/mspec/matchers/have_class_variable.rb	2009-04-05 16:56:12 UTC (rev 1369)
@@ -0,0 +1,12 @@
+require 'mspec/matchers/variable'
+
+class HaveClassVariableMatcher < VariableMatcher
+  self.variables_method = :class_variables
+  self.description      = 'class variable'
+end
+
+class Object
+  def have_class_variable(variable)
+    HaveClassVariableMatcher.new(variable)
+  end
+end
\ No newline at end of file

Modified: MacRuby/branches/experimental/mspec/lib/mspec/matchers/have_constant.rb
===================================================================
--- MacRuby/branches/experimental/mspec/lib/mspec/matchers/have_constant.rb	2009-04-05 05:22:02 UTC (rev 1368)
+++ MacRuby/branches/experimental/mspec/lib/mspec/matchers/have_constant.rb	2009-04-05 16:56:12 UTC (rev 1369)
@@ -1,30 +1,12 @@
-require 'mspec/matchers/stringsymboladapter'
+require 'mspec/matchers/variable'
 
-class HaveConstantMatcher
-  include StringSymbolAdapter
-
-  def initialize(name)
-    @name = convert_name name
-  end
-
-  def matches?(mod)
-    @mod = mod
-    @mod.constants.include? @name
-  end
-
-  def failure_message
-    ["Expected #{@mod} to have constant '#{@name.to_s}'",
-     "but it does not"]
-  end
-
-  def negative_failure_message
-    ["Expected #{@mod} NOT to have constant '#{@name.to_s}'",
-     "but it does"]
-  end
+class HaveConstantMatcher < VariableMatcher
+  self.variables_method = :constants
+  self.description      = 'constant'
 end
 
 class Object
-  def have_constant(name)
-    HaveConstantMatcher.new name
+  def have_constant(variable)
+    HaveConstantMatcher.new(variable)
   end
 end

Added: MacRuby/branches/experimental/mspec/lib/mspec/matchers/have_instance_variable.rb
===================================================================
--- MacRuby/branches/experimental/mspec/lib/mspec/matchers/have_instance_variable.rb	                        (rev 0)
+++ MacRuby/branches/experimental/mspec/lib/mspec/matchers/have_instance_variable.rb	2009-04-05 16:56:12 UTC (rev 1369)
@@ -0,0 +1,12 @@
+require 'mspec/matchers/variable'
+
+class HaveInstanceVariableMatcher < VariableMatcher
+  self.variables_method = :instance_variables
+  self.description      = 'instance variable'
+end
+
+class Object
+  def have_instance_variable(variable)
+    HaveInstanceVariableMatcher.new(variable)
+  end
+end
\ No newline at end of file

Deleted: MacRuby/branches/experimental/mspec/lib/mspec/matchers/include_variables.rb
===================================================================
--- MacRuby/branches/experimental/mspec/lib/mspec/matchers/include_variables.rb	2009-04-05 05:22:02 UTC (rev 1368)
+++ MacRuby/branches/experimental/mspec/lib/mspec/matchers/include_variables.rb	2009-04-05 16:56:12 UTC (rev 1369)
@@ -1,15 +0,0 @@
-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/stringsymboladapter.rb
===================================================================
--- MacRuby/branches/experimental/mspec/lib/mspec/matchers/stringsymboladapter.rb	2009-04-05 05:22:02 UTC (rev 1368)
+++ MacRuby/branches/experimental/mspec/lib/mspec/matchers/stringsymboladapter.rb	2009-04-05 16:56:12 UTC (rev 1369)
@@ -3,6 +3,6 @@
 module StringSymbolAdapter
   def convert_name(name)
     version = SpecVersion.new(RUBY_VERSION) <=> "1.9"
-    version < 0 ? name.to_s : name
+    version < 0 ? name.to_s : name.to_sym
   end
 end

Added: MacRuby/branches/experimental/mspec/lib/mspec/matchers/variable.rb
===================================================================
--- MacRuby/branches/experimental/mspec/lib/mspec/matchers/variable.rb	                        (rev 0)
+++ MacRuby/branches/experimental/mspec/lib/mspec/matchers/variable.rb	2009-04-05 16:56:12 UTC (rev 1369)
@@ -0,0 +1,28 @@
+require 'mspec/matchers/stringsymboladapter'
+
+class VariableMatcher
+  include StringSymbolAdapter
+
+  class << self
+    attr_accessor :variables_method, :description
+  end
+
+  def initialize(variable)
+    @variable = convert_name(variable)
+  end
+
+  def matches?(object)
+    @object = object
+    @object.send(self.class.variables_method).include? @variable
+  end
+
+  def failure_message
+    ["Expected #{@object} to have #{self.class.description} '#{@variable}'",
+     "but it does not"]
+  end
+
+  def negative_failure_message
+    ["Expected #{@object} NOT to have #{self.class.description} '#{@variable}'",
+     "but it does"]
+  end
+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 05:22:02 UTC (rev 1368)
+++ MacRuby/branches/experimental/mspec/lib/mspec/matchers.rb	2009-04-05 16:56:12 UTC (rev 1369)
@@ -12,12 +12,13 @@
 require 'mspec/matchers/equal_element'
 require 'mspec/matchers/equal_utf16'
 require 'mspec/matchers/have_constant'
+require 'mspec/matchers/have_class_variable'
 require 'mspec/matchers/have_instance_method'
+require 'mspec/matchers/have_instance_variable'
 require 'mspec/matchers/have_method'
 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'

Deleted: MacRuby/branches/experimental/mspec/spec/helpers/variables_spec.rb
===================================================================
--- MacRuby/branches/experimental/mspec/spec/helpers/variables_spec.rb	2009-04-05 05:22:02 UTC (rev 1368)
+++ MacRuby/branches/experimental/mspec/spec/helpers/variables_spec.rb	2009-04-05 16:56:12 UTC (rev 1369)
@@ -1,24 +0,0 @@
-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/have_class_variable_spec.rb
===================================================================
--- MacRuby/branches/experimental/mspec/spec/matchers/have_class_variable_spec.rb	                        (rev 0)
+++ MacRuby/branches/experimental/mspec/spec/matchers/have_class_variable_spec.rb	2009-04-05 16:56:12 UTC (rev 1369)
@@ -0,0 +1,75 @@
+require File.dirname(__FILE__) + '/../spec_helper'
+require 'mspec/expectations/expectations'
+require 'mspec/matchers/have_class_variable'
+
+class IVarModMock; end
+
+shared_examples_for "have_class_variable, on all Ruby versions" do
+  after :all do
+    Object.const_set :RUBY_VERSION, @ruby_version
+  end
+
+  it "matches when mod has the class variable, given as string" do
+    matcher = HaveClassVariableMatcher.new('@foo')
+    matcher.matches?(IVarModMock).should be_true
+  end
+
+  it "matches when mod has the class variable, given as symbol" do
+    matcher = HaveClassVariableMatcher.new(:@foo)
+    matcher.matches?(IVarModMock).should be_true
+  end
+
+  it "does not match when mod hasn't got the class variable, given as string" do
+    matcher = HaveClassVariableMatcher.new('@bar')
+    matcher.matches?(IVarModMock).should be_false
+  end
+
+  it "does not match when mod hasn't got the class variable, given as symbol" do
+    matcher = HaveClassVariableMatcher.new(:@bar)
+    matcher.matches?(IVarModMock).should be_false
+  end
+
+  it "provides a failure message for #should" do
+    matcher = HaveClassVariableMatcher.new(:@bar)
+    matcher.matches?(IVarModMock)
+    matcher.failure_message.should == [
+      "Expected IVarModMock to have class variable '@bar'",
+      "but it does not"
+    ]
+  end
+
+  it "provides a failure messoge for #should_not" do
+    matcher = HaveClassVariableMatcher.new(:@bar)
+    matcher.matches?(IVarModMock)
+    matcher.negative_failure_message.should == [
+      "Expected IVarModMock NOT to have class variable '@bar'",
+      "but it does"
+    ]
+  end
+end
+
+describe HaveClassVariableMatcher, "on RUBY_VERSION < 1.9" do
+  before :all do
+    @ruby_version = Object.const_get :RUBY_VERSION
+    Object.const_set :RUBY_VERSION, '1.8.6'
+
+    def IVarModMock.class_variables
+      ['@foo']
+    end
+  end
+
+  it_should_behave_like "have_class_variable, on all Ruby versions"
+end
+
+describe HaveClassVariableMatcher, "on RUBY_VERSION >= 1.9" do
+  before :all do
+    @ruby_version = Object.const_get :RUBY_VERSION
+    Object.const_set :RUBY_VERSION, '1.9.0'
+
+    def IVarModMock.class_variables
+      [:@foo]
+    end
+  end
+
+  it_should_behave_like "have_class_variable, on all Ruby versions"
+end
\ No newline at end of file

Added: MacRuby/branches/experimental/mspec/spec/matchers/have_instance_variable_spec.rb
===================================================================
--- MacRuby/branches/experimental/mspec/spec/matchers/have_instance_variable_spec.rb	                        (rev 0)
+++ MacRuby/branches/experimental/mspec/spec/matchers/have_instance_variable_spec.rb	2009-04-05 16:56:12 UTC (rev 1369)
@@ -0,0 +1,75 @@
+require File.dirname(__FILE__) + '/../spec_helper'
+require 'mspec/expectations/expectations'
+require 'mspec/matchers/have_instance_variable'
+
+shared_examples_for "have_instance_variable, on all Ruby versions" do
+  after :all do
+    Object.const_set :RUBY_VERSION, @ruby_version
+  end
+
+  it "matches when object has the instance variable, given as string" do
+    matcher = HaveInstanceVariableMatcher.new('@foo')
+    matcher.matches?(@object).should be_true
+  end
+
+  it "matches when object has the instance variable, given as symbol" do
+    matcher = HaveInstanceVariableMatcher.new(:@foo)
+    matcher.matches?(@object).should be_true
+  end
+
+  it "does not match when object hasn't got the instance variable, given as string" do
+    matcher = HaveInstanceVariableMatcher.new('@bar')
+    matcher.matches?(@object).should be_false
+  end
+
+  it "does not match when object hasn't got the instance variable, given as symbol" do
+    matcher = HaveInstanceVariableMatcher.new(:@bar)
+    matcher.matches?(@object).should be_false
+  end
+
+  it "provides a failure message for #should" do
+    matcher = HaveInstanceVariableMatcher.new(:@bar)
+    matcher.matches?(@object)
+    matcher.failure_message.should == [
+      "Expected #{@object.inspect} to have instance variable '@bar'",
+      "but it does not"
+    ]
+  end
+
+  it "provides a failure messoge for #should_not" do
+    matcher = HaveInstanceVariableMatcher.new(:@bar)
+    matcher.matches?(@object)
+    matcher.negative_failure_message.should == [
+      "Expected #{@object.inspect} NOT to have instance variable '@bar'",
+      "but it does"
+    ]
+  end
+end
+
+describe HaveInstanceVariableMatcher, "on RUBY_VERSION < 1.9" do
+  before :all do
+    @ruby_version = Object.const_get :RUBY_VERSION
+    Object.const_set :RUBY_VERSION, '1.8.6'
+
+    @object = Object.new
+    def @object.instance_variables
+      ['@foo']
+    end
+  end
+
+  it_should_behave_like "have_instance_variable, on all Ruby versions"
+end
+
+describe HaveInstanceVariableMatcher, "on RUBY_VERSION >= 1.9" do
+  before :all do
+    @ruby_version = Object.const_get :RUBY_VERSION
+    Object.const_set :RUBY_VERSION, '1.9.0'
+
+    @object = Object.new
+    def @object.instance_variables
+      [:@foo]
+    end
+  end
+
+  it_should_behave_like "have_instance_variable, on all Ruby versions"
+end
\ No newline at end of file

Deleted: MacRuby/branches/experimental/mspec/spec/matchers/include_variables_spec.rb
===================================================================
--- MacRuby/branches/experimental/mspec/spec/matchers/include_variables_spec.rb	2009-04-05 05:22:02 UTC (rev 1368)
+++ MacRuby/branches/experimental/mspec/spec/matchers/include_variables_spec.rb	2009-04-05 16:56:12 UTC (rev 1369)
@@ -1,63 +0,0 @@
-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

Modified: MacRuby/branches/experimental/mspec/spec/matchers/stringsymboladapter_spec.rb
===================================================================
--- MacRuby/branches/experimental/mspec/spec/matchers/stringsymboladapter_spec.rb	2009-04-05 05:22:02 UTC (rev 1368)
+++ MacRuby/branches/experimental/mspec/spec/matchers/stringsymboladapter_spec.rb	2009-04-05 16:56:12 UTC (rev 1369)
@@ -2,11 +2,9 @@
 require 'mspec/expectations/expectations'
 require 'mspec/matchers/stringsymboladapter'
 
-class StringSymbolSpecs
+describe StringSymbolAdapter, "#convert_name" do
   include StringSymbolAdapter
-end
 
-describe StringSymbolAdapter, "#convert_name" do
   before :all do
     @verbose = $VERBOSE
     $VERBOSE = nil
@@ -18,8 +16,6 @@
 
   before :each do
     @ruby_version = Object.const_get :RUBY_VERSION
-
-    @name = mock("name")
   end
 
   after :each do
@@ -28,13 +24,15 @@
 
   it "converts the name to a string if RUBY_VERSION < 1.9" do
     Object.const_set :RUBY_VERSION, "1.8.6"
-    @name.should_receive(:to_s).and_return("method_name")
-    StringSymbolSpecs.new.convert_name @name
+
+    convert_name("name").should == "name"
+    convert_name(:name).should  == "name"
   end
 
-  it "does not convert the name to a string if RUBY_VERSION >= 1.9" do
+  it "converts the name to a symbol if RUBY_VERSION >= 1.9" do
     Object.const_set :RUBY_VERSION, "1.9.0"
-    @name.should_not_receive(:to_s)
-    StringSymbolSpecs.new.convert_name @name
+
+    convert_name("name").should == :name
+    convert_name(:name).should  == :name
   end
 end
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20090405/fdb79c9f/attachment-0001.html>


More information about the macruby-changes mailing list