File: max_spec.rb

package info (click to toggle)
jruby 1.7.26-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 84,572 kB
  • sloc: ruby: 669,910; java: 253,056; xml: 35,152; ansic: 9,187; yacc: 7,267; cpp: 5,244; sh: 1,036; makefile: 345; jsp: 48; tcl: 40
file content (93 lines) | stat: -rw-r--r-- 2,979 bytes parent folder | download
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
87
88
89
90
91
92
93
require File.expand_path('../../../spec_helper', __FILE__)

ruby_version_is "1.8.7" do
  describe "Range#max" do
    it "returns the maximum value in the range when called with no arguments" do
      (1..10).max.should == 10
      (1...10).max.should == 9
      ('f'..'l').max.should == 'l'
      ('a'...'f').max.should == 'e'
    end

    ruby_version_is "1.9" do
      it "returns the maximum value in the Float range when called with no arguments" do
        (303.20..908.1111).max.should == 908.1111
      end

      it "raises TypeError when called on an exclusive range and a non Integer value" do
        lambda { (303.20...908.1111).max }.should raise_error(TypeError)
      end
    end

    ruby_version_is ""..."1.9" do
      it "raises TypeError when called on a Float range" do
        lambda { (303.20..908.1111).max }.should raise_error(TypeError)
      end
    end

    it "returns nil when the endpoint is less than the start point" do
      (100..10).max.should be_nil
      ('z'..'l').max.should be_nil
    end

    it "returns nil when the endpoint equals the start point and the range is exclusive" do
      (5...5).max.should be_nil
    end

    it "returns the endpoint when the endpoint equals the start point and the range is inclusive" do
      (5..5).max.should equal(5)
    end

    ruby_version_is "1.9" do
      it "returns nil when the endpoint is less than the start point in a Float range" do
        (3003.20..908.1111).max.should be_nil
      end

      it "returns end point when the range is Time..Time(included end point)" do
        time_start = Time.now
        time_end = Time.now + 1.0
        (time_start..time_end).max.should equal(time_end)
      end

      it "raises TypeError when called on a Time...Time(excluded end point)" do
        time_start = Time.now
        time_end = Time.now + 1.0
        lambda { (time_start...time_end).max  }.should raise_error(TypeError)
      end
    end
  end

  describe "Range#max given a block" do
    it "passes each pair of values in the range to the block" do
      acc = []
      (1..10).max {|a,b| acc << [a,b]; a }
      acc.flatten!
      (1..10).each do |value|
        acc.include?(value).should be_true
      end
    end

    it "passes each pair of elements to the block in reversed order" do
      acc = []
      (1..5).max {|a,b| acc << [a,b]; a }
      acc.should == [[2,1],[3,2], [4,3], [5, 4]]
    end

    it "calls #> and #< on the return value of the block" do
      obj = mock('obj')
      obj.should_receive(:>).exactly(2).times
      obj.should_receive(:<).exactly(2).times
      (1..3).max {|a,b| obj }
    end

    it "returns the element the block determines to be the maximum" do
      (1..3).max {|a,b| -3 }.should == 1
    end

    it "returns nil when the endpoint is less than the start point" do
      (100..10).max {|x,y| x <=> y}.should be_nil
      ('z'..'l').max {|x,y| x <=> y}.should be_nil
      (5...5).max {|x,y| x <=> y}.should be_nil
    end
  end
end