Sha256: fc8306752b8f4dc52f99459f9c13984570b53fb4972f7a2ec8e99194f61092de

Contents?: true

Size: 1022 Bytes

Versions: 52

Compression:

Stored size: 1022 Bytes

Contents

require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)

describe "String.try_convert" do
  ruby_version_is "1.9" do
    it "returns self for strings" do
      x = "abc"
      String.try_convert(x).should equal(x)
    end

    it "converts using :to_str" do
      str = "hello"
      obj = mock('stringlike')
      obj.should_receive(:to_str).and_return(str)
      String.try_convert(obj).should equal(str)
    end

    it "returns nil when there is no :to_str" do
      String.try_convert(-1).should be_nil
    end

    it "does not rescue exceptions" do
      obj = mock("to_str raises")
      obj.should_receive(:to_str).and_raise(RuntimeError)
      lambda{ String.try_convert obj }.should raise_error(RuntimeError)
    end

    it "checks the result of the conversion" do
      obj = mock('to_str invaled')
      obj.should_receive(:to_str).and_return(:invalid_to_str)
      lambda{ String.try_convert obj }.should raise_error(TypeError)
    end
  end
end

Version data entries

52 entries across 52 versions & 2 rubygems

Version Path
rhodes-7.6.0 spec/framework_spec/app/spec/core/string/try_convert_spec.rb
rhodes-7.5.1 spec/framework_spec/app/spec/core/string/try_convert_spec.rb
rhodes-7.4.1 spec/framework_spec/app/spec/core/string/try_convert_spec.rb
rhodes-7.1.17 spec/framework_spec/app/spec/core/string/try_convert_spec.rb
rhodes-6.2.0 spec/framework_spec/app/spec/core/string/try_convert_spec.rb
rhodes-6.0.11 spec/framework_spec/app/spec/core/string/try_convert_spec.rb
rhodes-5.5.18 spec/framework_spec/app/spec/core/string/try_convert_spec.rb
rhodes-5.5.17 spec/framework_spec/app/spec/core/string/try_convert_spec.rb
rhodes-5.5.15 spec/framework_spec/app/spec/core/string/try_convert_spec.rb
rhodes-5.5.0.22 spec/framework_spec/app/spec/core/string/try_convert_spec.rb
rhodes-5.5.2 spec/framework_spec/app/spec/core/string/try_convert_spec.rb
rhodes-5.5.0.7 spec/framework_spec/app/spec/core/string/try_convert_spec.rb
rhodes-5.5.0.3 spec/framework_spec/app/spec/core/string/try_convert_spec.rb
rhodes-5.5.0 spec/framework_spec/app/spec/core/string/try_convert_spec.rb
tauplatform-1.0.3 spec/framework_spec/app/spec/core/string/try_convert_spec.rb
tauplatform-1.0.2 spec/framework_spec/app/spec/core/string/try_convert_spec.rb
tauplatform-1.0.1 spec/framework_spec/app/spec/core/string/try_convert_spec.rb
rhodes-3.5.1.12 spec/framework_spec/app/spec/core/string/try_convert_spec.rb
rhodes-3.3.5 spec/framework_spec/app/spec/core/string/try_convert_spec.rb
rhodes-3.4.2 spec/framework_spec/app/spec/core/string/try_convert_spec.rb