Sha256: c1c3379537e1f1bac7ea1cf5b5a128674ecd5e8880af13feef5cf11d7ebb6581

Contents?: true

Size: 1.94 KB

Versions: 4

Compression:

Stored size: 1.94 KB

Contents

require 'spec_helper'

describe Must::Rule, "duck" do
  class CFoo
    def foo; 1; end
  end

  module MFoo
    def foo; 1; end
  end

  describe "duck typing test" do
    specify "[OK]" do
      1.must.duck(:to_s  ).should == 1
      1.must.duck("to_s" ).should == 1
      1.must.duck(".to_s").should == 1

      CFoo.must.duck(:to_s  ).should == CFoo
      CFoo.must.duck("to_s" ).should == CFoo
      CFoo.must.duck(".to_s").should == CFoo

      MFoo.must.duck(:to_s  ).should == MFoo
      MFoo.must.duck("to_s" ).should == MFoo
      MFoo.must.duck(".to_s").should == MFoo
    end

    specify "[NG]" do
      lambda { 1.must.duck(:foo  ) }.should raise_error(Must::Invalid)
      lambda { 1.must.duck("foo" ) }.should raise_error(Must::Invalid)
      lambda { 1.must.duck(".foo") }.should raise_error(Must::Invalid)
        
      lambda { CFoo.must.duck(:foo  ) }.should raise_error(Must::Invalid)
      lambda { CFoo.must.duck("foo" ) }.should raise_error(Must::Invalid)
      lambda { CFoo.must.duck(".foo") }.should raise_error(Must::Invalid)

      lambda { MFoo.must.duck(:foo  ) }.should raise_error(Must::Invalid)
      lambda { MFoo.must.duck("foo" ) }.should raise_error(Must::Invalid)
      lambda { MFoo.must.duck(".foo") }.should raise_error(Must::Invalid)
    end
  end

  context "(instance duck typing)" do
    specify "[OK]" do
      CFoo.must.duck("#foo").should == CFoo
      MFoo.must.duck("#foo").should == MFoo
    end

    specify "[NG]" do
      lambda { 1.must.duck("#bar")    }.should raise_error(Must::Invalid)
      lambda { CFoo.must.duck("#bar") }.should raise_error(Must::Invalid)
      lambda { MFoo.must.duck("#bar") }.should raise_error(Must::Invalid)
    end
  end

  context "(duck bang)" do
    specify "[OK]" do
      1.must.duck!("to_s").should == "1"
    end

    specify "[NG]" do
      lambda { 1.must.duck!("foo") }.should raise_error(Must::Invalid)
    end

    specify "callback" do
      1.must.duck!("to_xxx") { 2 }
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
must-0.3.2 spec/duck_spec.rb
must-0.3.1 spec/duck_spec.rb
must-0.3.0 spec/duck_spec.rb
must-0.2.9 spec/duck_spec.rb