Sha256: fb9960dc05bd5051905cfedc517bf7f6a55649bcb108ea792b6739f1e7a158b7

Contents?: true

Size: 1.2 KB

Versions: 9

Compression:

Stored size: 1.2 KB

Contents

require File.dirname(__FILE__)+'/../spec_helper'
require 'gorillib/numeric/clamp'

describe Numeric 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

9 entries across 9 versions & 1 rubygems

Version Path
gorillib-0.1.7 spec/numeric/clamp_spec.rb
gorillib-0.1.6 spec/numeric/clamp_spec.rb
gorillib-0.1.5 spec/numeric/clamp_spec.rb
gorillib-0.1.4 spec/numeric/clamp_spec.rb
gorillib-0.1.3 spec/numeric/clamp_spec.rb
gorillib-0.1.2 spec/numeric/clamp_spec.rb
gorillib-0.1.1 spec/numeric/clamp_spec.rb
gorillib-0.1.0 spec/numeric/clamp_spec.rb
gorillib-0.0.8 spec/numeric/clamp_spec.rb