File: times.rb

package info (click to toggle)
ruby2.7 2.7.4-1%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 112,576 kB
  • sloc: ruby: 849,454; ansic: 697,834; yacc: 45,100; xml: 25,367; pascal: 10,051; javascript: 6,575; sh: 3,848; makefile: 759; cpp: 713; asm: 333; python: 295; lisp: 97; sed: 94; perl: 62; awk: 36
file content (64 lines) | stat: -rw-r--r-- 2,174 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
describe :string_times, shared: true do
  class MyString < String; end

  it "returns a new string containing count copies of self" do
    @object.call("cool", 0).should == ""
    @object.call("cool", 1).should == "cool"
    @object.call("cool", 3).should == "coolcoolcool"
  end

  it "tries to convert the given argument to an integer using to_int" do
    @object.call("cool", 3.1).should == "coolcoolcool"
    @object.call("a", 3.999).should == "aaa"

    a = mock('4')
    a.should_receive(:to_int).and_return(4)

    @object.call("a", a).should == "aaaa"
  end

  it "raises an ArgumentError when given integer is negative" do
    -> { @object.call("cool", -3)    }.should raise_error(ArgumentError)
    -> { @object.call("cool", -3.14) }.should raise_error(ArgumentError)
  end

  it "raises a RangeError when given integer is a Bignum" do
    -> { @object.call("cool", 999999999999999999999) }.should raise_error(RangeError)
  end

  it "returns subclass instances" do
    @object.call(MyString.new("cool"), 0).should be_an_instance_of(MyString)
    @object.call(MyString.new("cool"), 1).should be_an_instance_of(MyString)
    @object.call(MyString.new("cool"), 2).should be_an_instance_of(MyString)
  end

  ruby_version_is ''...'2.7' do
    it "always taints the result when self is tainted" do
      ["", "OK", MyString.new(""), MyString.new("OK")].each do |str|
        str.taint

        [0, 1, 2].each do |arg|
          @object.call(str, arg).tainted?.should == true
        end
      end
    end
  end

  it "returns a String in the same encoding as self" do
    str = "\xE3\x81\x82".force_encoding Encoding::UTF_8
    result = @object.call(str, 2)
    result.encoding.should equal(Encoding::UTF_8)
  end

  platform_is wordsize: 32 do
    it "raises an ArgumentError if the length of the resulting string doesn't fit into a long" do
      -> { @object.call("abc", (2 ** 31) - 1) }.should raise_error(ArgumentError)
    end
  end

  platform_is wordsize: 64 do
    it "raises an ArgumentError if the length of the resulting string doesn't fit into a long" do
      -> { @object.call("abc", (2 ** 63) - 1) }.should raise_error(ArgumentError)
    end
  end
end