Sha256: df0f57f7dfe2a68ec4402ff6a5b1e364128a4c7dc5252c9284c2f7f402e92477

Contents?: true

Size: 1.19 KB

Versions: 7

Compression:

Stored size: 1.19 KB

Contents

require 'spec_helper'
require 'gorillib/numeric/clamp'

describe Numeric, :simple_spec => true do
  describe '#clamp' do
    it 'should return self if neither min nor max are given' do
      5.clamp().should == 5
    end

    it 'should return min if x < min' do
      5.clamp(6).should == 6
    end

    it 'should return self if x >= min' do
      5.clamp(4).should   == 5
      5.clamp(5).should   == 5
      5.clamp(nil).should == 5
    end

    it 'should return max if x > max' do
      5.clamp(nil, 4).should == 4
      5.clamp(4,   4).should == 4
    end

    it 'should return self if x <= max' do
      5.clamp(4, 6).should   == 5
      5.clamp(5, 5).should   == 5
      5.clamp(nil, 6).should == 5
      5.clamp(nil, 5).should == 5
    end

    it 'lets me mix floats and ints' do
      (5.0).clamp(4, 6).should   == 5.0
      (5).clamp(4.0, 6.0).should == 5.0
      (5.0).clamp(6.0, 7.0).should == 6.0
      (5.0).clamp(4, 6).should be_a_kind_of(Float)
      (5.0).clamp(6, 6).should be_a_kind_of(Integer)
    end

    it 'should raise if min > max' do
      lambda{ 5.clamp(6, 3) }.should raise_error(ArgumentError)
    end

    it 'lets me set min == max' do
      5.clamp(6, 6).should == 6
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
gorillib-0.6.0 spec/gorillib/numeric/clamp_spec.rb
gorillib-0.5.2 spec/gorillib/numeric/clamp_spec.rb
gorillib-0.5.0 spec/gorillib/numeric/clamp_spec.rb
gorillib-0.4.2 spec/gorillib/numeric/clamp_spec.rb
gorillib-0.4.2pre spec/gorillib/numeric/clamp_spec.rb
gorillib-0.4.0pre spec/gorillib/numeric/clamp_spec.rb
gorillib-0.4.1pre spec/gorillib/numeric/clamp_spec.rb