<commit_before>require_relative '../../spec_helper'
require_relative 'fixtures/classes'

describe 'Comparable#clamp' do
  it 'raises an Argument error unless given 2 parameters' do
    c = ComparableSpecs::Weird.new(0)
    -> { c.clamp(c) }.should raise_error(ArgumentError)
    -> { c.clamp(c, c, c) }.should raise_error(ArgumentError)
  end

  it 'raises an Argument error unless the 2 parameters are correctly ordered' do
    one = ComparableSpecs::WithOnlyCompareDefined.new(1)
    two = ComparableSpecs::WithOnlyCompareDefined.new(2)
    c = ComparableSpecs::Weird.new(3)

    -> { c.clamp(two, one) }.should raise_error(ArgumentError)
    one.should_receive(:<=>).any_number_of_times.and_return(nil)
    -> { c.clamp(one, two) }.should raise_error(ArgumentError)
  end

  it 'returns self if within the given parameters' do
    one = ComparableSpecs::WithOnlyCompareDefined.new(1)
    two = ComparableSpecs::WithOnlyCompareDefined.new(2)
    three = ComparableSpecs::WithOnlyCompareDefined.new(3)
    c = ComparableSpecs::Weird.new(2)

    c.clamp(one, two).should equal(c)
    c.clamp(two, two).should equal(c)
    c.clamp(one, three).should equal(c)
    c.clamp(two, three).should equal(c)
  end

  it 'returns the min parameter if smaller than it' do
    one = ComparableSpecs::WithOnlyCompareDefined.new(1)
    two = ComparableSpecs::WithOnlyCompareDefined.new(2)
    c = ComparableSpecs::Weird.new(0)

    c.clamp(one, two).should equal(one)
  end

  it 'returns the max parameter if greater than it' do
    one = ComparableSpecs::WithOnlyCompareDefined.new(1)
    two = ComparableSpecs::WithOnlyCompareDefined.new(2)
    c = ComparableSpecs::Weird.new(3)

    c.clamp(one, two).should equal(two)
  end
end<commit_msg>Comparable#clamp with a range [Feature #14784]<commit_after>require_relative '../../spec_helper'
require_relative 'fixtures/classes'

describe 'Comparable#clamp' do
  ruby_version_is ""..."2.7" do
    it 'raises an Argument error unless given 2 parameters' do
      c = ComparableSpecs::Weird.new(0)
      -> { c.clamp(c) }.should raise_error(ArgumentError)
      -> { c.clamp(c, c, c) }.should raise_error(ArgumentError)
    end
  end

  it 'raises an Argument error unless the 2 parameters are correctly ordered' do
    one = ComparableSpecs::WithOnlyCompareDefined.new(1)
    two = ComparableSpecs::WithOnlyCompareDefined.new(2)
    c = ComparableSpecs::Weird.new(3)

    -> { c.clamp(two, one) }.should raise_error(ArgumentError)
    one.should_receive(:<=>).any_number_of_times.and_return(nil)
    -> { c.clamp(one, two) }.should raise_error(ArgumentError)
  end

  it 'returns self if within the given parameters' do
    one = ComparableSpecs::WithOnlyCompareDefined.new(1)
    two = ComparableSpecs::WithOnlyCompareDefined.new(2)
    three = ComparableSpecs::WithOnlyCompareDefined.new(3)
    c = ComparableSpecs::Weird.new(2)

    c.clamp(one, two).should equal(c)
    c.clamp(two, two).should equal(c)
    c.clamp(one, three).should equal(c)
    c.clamp(two, three).should equal(c)
  end

  it 'returns the min parameter if smaller than it' do
    one = ComparableSpecs::WithOnlyCompareDefined.new(1)
    two = ComparableSpecs::WithOnlyCompareDefined.new(2)
    c = ComparableSpecs::Weird.new(0)

    c.clamp(one, two).should equal(one)
  end

  it 'returns the max parameter if greater than it' do
    one = ComparableSpecs::WithOnlyCompareDefined.new(1)
    two = ComparableSpecs::WithOnlyCompareDefined.new(2)
    c = ComparableSpecs::Weird.new(3)

    c.clamp(one, two).should equal(two)
  end

  ruby_version_is "2.7" do
    it 'returns self if within the given range parameters' do
      one = ComparableSpecs::WithOnlyCompareDefined.new(1)
      two = ComparableSpecs::WithOnlyCompareDefined.new(2)
      three = ComparableSpecs::WithOnlyCompareDefined.new(3)
      c = ComparableSpecs::Weird.new(2)

      c.clamp(one..two).should equal(c)
      c.clamp(two..two).should equal(c)
      c.clamp(one..three).should equal(c)
      c.clamp(two..three).should equal(c)
    end

    it 'returns the minimum value of the range parameters if smaller than it' do
      one = ComparableSpecs::WithOnlyCompareDefined.new(1)
      two = ComparableSpecs::WithOnlyCompareDefined.new(2)
      c = ComparableSpecs::Weird.new(0)

      c.clamp(one..two).should equal(one)
    end

    it 'returns the maximum value of the range parameters if greater than it' do
      one = ComparableSpecs::WithOnlyCompareDefined.new(1)
      two = ComparableSpecs::WithOnlyCompareDefined.new(2)
      c = ComparableSpecs::Weird.new(3)

      c.clamp(one..two).should equal(two)
    end

    it 'raises an Argument error if the range parameter is exclusive' do
      one = ComparableSpecs::WithOnlyCompareDefined.new(1)
      two = ComparableSpecs::WithOnlyCompareDefined.new(2)
      c = ComparableSpecs::Weird.new(3)

      -> { c.clamp(one...two) }.should raise_error(ArgumentError)
    end
  end
end
{'fraction_non_alphanumeric': 0.11757526444263629, 'fraction_numerical': 0.010984540276647681, 'mean_word_length': 3.907185628742515, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2203141', 'n_tokens_mistral': 1700, 'n_tokens_neox': 1664, 'n_words': 363}