summaryrefslogtreecommitdiff
path: root/spec/ruby/core/range/shared/cover_and_include.rb
blob: 97721a7307304f51c5a8c069a33fa7dc0586b592 (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
77
78
79
80
81
82
83
84
85
86
# 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
    (1..).send(@method, 2.4).should == true
    (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 "returns false if values are not comparable" do
    (1..10).send(@method, nil).should == false
    (1...10).send(@method, nil).should == false

    (..10).send(@method, nil).should == false
    (...10).send(@method, nil).should == false

    (1..).send(@method, nil).should == false
    (1...).send(@method, nil).should == false
  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 == false
  end

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

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

  it "returns true if argument is equal to the last value of the range" do
    (0..5).send(@method, 5).should == true
    (0...5).send(@method, 4).should == true
    ('f'..'s').send(@method, 's').should == 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 == true
    ('e'..'h').send(@method, 'g').should == true
  end

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

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

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