File: range_spec.rb

package info (click to toggle)
ruby-enumerable-statistics 2.0.7%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,132 kB
  • sloc: ruby: 2,220; ansic: 1,921; javascript: 408; makefile: 8; sh: 4
file content (103 lines) | stat: -rw-r--r-- 2,006 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
require 'spec_helper'
require 'enumerable/statistics'
require 'delegate'

RSpec.describe Enumerable do
  describe '#sum' do
    subject(:sum) { enum.sum(init, &block) }
    let(:init) { 0 }
    let(:block) { nil }

    with_enum 1..0 do
      it_is_int_equal(0)

      with_init(0.0) do
        it_is_float_equal(0.0)
      end

      context 'with a conversion block' do
        it 'does not call the conversion block' do
          expect { |b|
            enum.sum(&b)
          }.not_to yield_control
        end
      end
    end

    with_enum 3..3 do
      it_is_int_equal(3)

      with_init(0.0) do
        it_is_float_equal(3.0)
      end
    end

    with_enum 3..5 do
      it_is_int_equal(12)
    end

    with_enum 1..2 do
      with_init(10)do
        it_is_int_equal(13)

        with_conversion ->(v) { v * 2 }, 'v * 2' do
          it_is_int_equal(16)
        end
      end
    end

    it 'calls a block for each item once' do
      yielded = []
      range = 1..3
      expect(range.each.sum {|x| yielded << x; x * 2 }).to eq(12)
      expect(yielded).to eq(range.to_a)
    end

    with_enum :a..:b do
      specify do
        expect { subject }.to raise_error(TypeError)
      end
    end

    with_enum "a".."c" do
      with_init("") do
        it { is_expected.to eq("abc") }
      end
    end
  end

  describe '#mean' do
    subject(:mean) { enum.mean(&block) }
    let(:block) { nil }

    with_enum 1..0 do
      it_is_float_equal(0.0)

      context 'with a conversion block' do
        it_is_float_equal(0.0)

        it 'does not call the block' do
          expect { |b|
            enum.mean(&b)
          }.not_to yield_control
        end
      end
    end

    with_enum 3..3 do
      it_is_float_equal(3.0)

      with_conversion ->(v) { v * 2 }, 'v * 2' do
        it_is_float_equal(6.0)
      end
    end

    with_enum 3..5 do
      it_is_float_equal(4.0)

      with_conversion ->(v) { v * 2 }, 'v * 2' do
        it_is_float_equal(8.0)
      end
    end
  end
end