[rspec-devel] [ rspec-Bugs-11101 ] StringHelpers.starts_with?(prefix) assumes a string parameter for _prefix_

noreply at rubyforge.org noreply at rubyforge.org
Thu May 24 18:03:51 EDT 2007


Bugs item #11101, was opened at 2007-05-24 17:11
You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=3149&aid=11101&group_id=797

>Category: runner module
Group: v1.0 (example)
>Status: Closed
>Resolution: Accepted
Priority: 3
Submitted By: sinclair  bain (sinclair)
>Assigned to: Aslak Hellesøy (aslak_hellesoy)
Summary: StringHelpers.starts_with?(prefix) assumes a string parameter for _prefix_

Initial Comment:
rails plugin : rspec 1_0_0
file: rspec/lib/spec/expectations/extensions/string_and_symbol.rb

The starts_with? implementation assume that the _prefix_ arg is a string in that it calls #length without a #to_s (for example) so if it is passed an Fixnum, or nil then it ends abruptly.

module Spec
  module Expectations
    module StringHelpers
      def starts_with?(prefix)
        to_s[0..(prefix.length - 1)] == prefix
      end
    end
  end
end

By changing the implementation to call #to_s on prefix (for example) the method is slightly more forgiving - along the lines of: 

    def starts_with?(prefix = '')
      to_s[0..(prefix.to_s.length - 1)] == prefix.to_s
    end

By doing this there is no requirement on the caller to assume any particular implementation.

Hope this helps,
sinclair

----------------------------------------------------------------------

Comment By: Aslak Hellesøy (aslak_hellesoy)
Date: 2007-05-24 18:03

Message:
Applied in r2026

----------------------------------------------------------------------

Comment By: Aslak Hellesøy (aslak_hellesoy)
Date: 2007-05-24 17:54

Message:
Out of curiosity, is the current implementation causing specs to fail? If so, can you give an example?

----------------------------------------------------------------------

Comment By: David Chelimsky (dchelimsky)
Date: 2007-05-24 17:17

Message:
That's a pretty good idea. Feel like making a patch w/ specs?

----------------------------------------------------------------------

You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=3149&aid=11101&group_id=797


More information about the rspec-devel mailing list