File: upto_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 (127 lines) | stat: -rw-r--r-- 3,645 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes.rb', __FILE__)

describe "String#upto" do
  it "passes successive values, starting at self and ending at other_string, to the block" do
    a = []
    "*+".upto("*3") { |s| a << s }
    a.should == ["*+", "*,", "*-", "*.", "*/", "*0", "*1", "*2", "*3"]
  end

  it "calls the block once even when start eqals stop" do
    a = []
    "abc".upto("abc") { |s| a << s }
    a.should == ["abc"]
  end

  # This is weird (in 1.8), but MRI behaves like that
  ruby_version_is '' ... '1.9' do
    it "calls block with self even if self is less than stop but stop length is less than self length" do
	   a = []
	   "25".upto("5") { |s| a << s }
	   a.should == ["25"]
    end
  end

  ruby_version_is '1.9' do
    it "doesn't call block with self even if self is less than stop but stop length is less than self length" do
      a = []
      "25".upto("5") { |s| a << s }
      a.should == []
    end
  end

  it "doesn't call block if stop is less than self and stop length is less than self length" do
    a = []
    "25".upto("1") { |s| a << s }
    a.should == []
  end

  it "doesn't call the block if self is greater than stop" do
    a = []
    "5".upto("2") { |s| a << s }
    a.should == []
  end

  it "stops iterating as soon as the current value's character count gets higher than stop's" do
    a = []
    "96".upto("AA") { |s| a << s }
    a.should == ["96", "97", "98", "99"]
  end

  it "returns self" do
    "abc".upto("abd") { }.should == "abc"
    "5".upto("2") { |i| i }.should == "5"
  end

  it "tries to convert other to string using to_str" do
    other = mock('abd')
    def other.to_str() "abd" end

    a = []
    "abc".upto(other) { |s| a << s }
    a.should == ["abc", "abd"]
  end

  it "raises a TypeError if other can't be converted to a string" do
    lambda { "abc".upto(123) { }      }.should raise_error(TypeError)
    lambda { "abc".upto(mock('x')){ } }.should raise_error(TypeError)
  end


  ruby_version_is ''...'1.9' do
    it "raises a TypeError on symbols" do
      lambda { "abc".upto(:def) { }     }.should raise_error(TypeError)
    end

    it "raises a LocalJumpError if other is a string but no block was given" do
      lambda { "abc".upto("def") }.should raise_error(LocalJumpError)
    end

    it "uses succ even for single letters" do
      a = []
      "9".upto("A"){ |s| a << s}
      a.should == ["9"]
    end
  end

  ruby_version_is '1.9' do
    it "does not work with symbols" do
      lambda { "a".upto(:c).to_a }.should raise_error(TypeError)
    end

    it "returns an enumerator when no block given" do
      enum = "aaa".upto("baa", true)
      enum.should be_an_instance_of(enumerator_class)
      enum.count.should == 26**2
    end

    it "returns non-alphabetic characters in the ASCII range for single letters" do
      "9".upto("A").to_a.should == ["9", ":", ";", "<", "=", ">", "?", "@", "A"]
      "Z".upto("a").to_a.should == ["Z", "[", "\\", "]", "^", "_", "`", "a"]
      "z".upto("~").to_a.should == ["z", "{", "|", "}", "~"]
    end
  end

  ruby_version_is '1.8.7' do
    it "stops before the last value if exclusive" do
      a = []
      "a".upto("d", true) { |s| a << s}
      a.should == ["a", "b", "c"]
    end
  end

  describe "on sequence of numbers" do
    ruby_version_is ''...'1.9' do
      it "calls the block with only self" do
        "8".upto("11").to_a.should == ["8"]
      end
    end

    ruby_version_is '1.9' do
      it "calls the block as Integer#upto"  do
        "8".upto("11").to_a.should == 8.upto(11).map(&:to_s)
      end
    end
  end
end