summaryrefslogtreecommitdiff
path: root/spec/ruby/core/range/shared/cover_and_include.rb
blob: 7028afaa89d43d1eb7f51f8673a7d4d66ca412f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# -*- encoding: binary -*-
require_relative '../../../spec_helper'

describe :range_cover_and_include, shared: true do
  it "returns true if other is an element of self" do
    (0..5).send(@method, 2).should == true
    (-5..5).send(@method, 0).should == true
    (-1...1).send(@method, 10.5).should == false
    (-10..-2).send(@method, -2.5).should == true
    ('C'..'X').send(@method, 'M').should == true
    ('C'..'X').send(@method, 'A').should == false
    ('B'...'W').send(@method, 'W').should == false
    ('B'...'W').send(@method, 'Q').should == true
    (0xffff..0xfffff).send(@method, 0xffffd).should == true
    (0xffff..0xfffff).send(@method, 0xfffd).should == false
    (0.5..2.4).send(@method, 2).should == true
    (0.5..2.4).send(@method, 2.5).should == false
    (0.5..2.4).send(@method, 2.4).should == true
    (0.5...2.4).send(@method, 2.4).should == false
  end

  it "returns true if other is an element of self for endless ranges" do
    eval("(1..)").send(@method, 2.4).should == true
    eval("(0.5...)").send(@method, 2.4).should == true
  end

  it "returns true if other is an element of self for beginless ranges" do
    (..10).send(@method, 2.4).should == true
    (...10.5).send(@method, 2.4).should == true
  end

  it "compares values using <=>" do
    rng = (1..5)
    m = mock("int")
    m.should_receive(:coerce).and_return([1, 2])
    m.should_receive(:<=>).and_return(1)

    rng.send(@method, m).should be_false
  end

  it "raises an ArgumentError without exactly one argument" do
    ->{ (1..2).send(@method) }.should raise_error(ArgumentError)
    ->{ (1..2).send(@method, 1, 2) }.should raise_error(ArgumentError)
  end

  it "returns true if argument is equal to the first value of the range" do
    (0..5).send(@method, 0).should be_true
    ('f'..'s').send(@method, 'f').should be_true
  end

  it "returns true if argument is equal to the last value of the range" do
    (0..5).send(@method, 5).should be_true
    (0...5).send(@method, 4).should be_true
    ('f'..'s').send(@method, 's').should be_true
  end

  it "returns true if argument is less than the last value of the range and greater than the first value" do
    (20..30).send(@method, 28).should be_true
    ('e'..'h').send(@method, 'g').should be_true
    ("\u{999}".."\u{9999}").send @method, "\u{9995}"
  end

  it "returns true if argument is sole element in the range" do
    (30..30).send(@method, 30).should be_true
  end

  it "returns false if range is empty" do
    (30...30).send(@method, 30).should be_false
    (30...30).send(@method, nil).should be_false
  end

  it "returns false if the range does not contain the argument" do
    ('A'..'C').send(@method, 20.9).should be_false
    ('A'...'C').send(@method, 'C').should be_false
  end
end