Sha256: 997a5a79ce447334a14b0361e012cb1793d7ed90e3e9dd5d9949c3a4a55d4b80

Contents?: true

Size: 1.2 KB

Versions: 16

Compression:

Stored size: 1.2 KB

Contents

module Spec

  class HaveHelper < ShouldBase

    def initialize(target, expected=nil)
      @target = target
      @expected = expected == :no ? 0 : expected
      @min = false
      @max = false
    end
    
    def method_missing(sym, *args)
      fail_with_message(build_message(sym)) unless as_specified?(sym)
    end
    
    def collection(sym)
      @target.send(sym)
    end
    
    def actual_size(collection)
      return collection.length if collection.respond_to? :length
      return collection.size if collection.respond_to? :size
    end
    
    def build_message(sym)
      message = "<#{@target.class.to_s}> should have"
      message += " at least" if @min
      message += " at most" if @max
      message += " #{@expected} #{sym} (has #{actual_size(collection(sym))})"
    end
    
    def as_specified?(sym)
      return actual_size(collection(sym)) >= @expected if @min
      return actual_size(collection(sym)) <= @expected if @max
      return actual_size(collection(sym)) == @expected
    end

    def at
      self
    end
    
    def least(min)
      @expected = min
      @min = true
      self
    end
    
    def most(max)
      @expected = max
      @max = true
      self
    end

  end

end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
rspec-0.5.11 lib/spec/api/helper/have_helper.rb
rspec-0.5.1 lib/spec/api/helper/have_helper.rb
rspec-0.5.10 lib/spec/api/helper/have_helper.rb
rspec-0.5.12 lib/spec/api/helper/have_helper.rb
rspec-0.5.9 lib/spec/api/helper/have_helper.rb
rspec-0.5.14 lib/spec/api/helper/have_helper.rb
rspec-0.5.2 lib/spec/api/helper/have_helper.rb
rspec-0.5.6 lib/spec/api/helper/have_helper.rb
rspec-0.5.3 lib/spec/api/helper/have_helper.rb
rspec-0.5.7 lib/spec/api/helper/have_helper.rb
rspec-0.5.8 lib/spec/api/helper/have_helper.rb
rspec-0.5.13 lib/spec/api/helper/have_helper.rb
rspec-0.5.16 lib/spec/api/helper/have_helper.rb
rspec-0.5.15 lib/spec/api/helper/have_helper.rb
rspec-0.5.4 lib/spec/api/helper/have_helper.rb
rspec-0.5.5 lib/spec/api/helper/have_helper.rb