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
|
require 'spec_helper'
require 'ruby-progressbar/projectors/smoothed_average'
class ProgressBar
module Projectors
describe SmoothedAverage do
describe '.calculate' do
it 'can properly calculate a projection' do
first_projection = SmoothedAverage.calculate(4.5, 12, 0.1)
expect(first_projection).to be_within(0.001).of 11.25
second_projection = SmoothedAverage.calculate(8.2, 51, 0.7)
expect(second_projection).to be_within(0.001).of 21.04
third_projection = SmoothedAverage.calculate(41.8, 100, 0.59)
expect(third_projection).to be_within(0.001).of 65.662
end
end
describe '#projection' do
it 'can properly calculate a running average' do
projector = SmoothedAverage.new(:strength => 0.1)
projector.start
projector.progress = 5
projector.progress = 12
expect(projector.projection).to be_within(0.001).of 11.25
end
it 'knows the running average even when progress has been made' do
projector = SmoothedAverage.new(:total => 50)
projector.instance_variable_set(:@projection, 10)
projector.start :at => 0
expect(projector.projection).to be_zero
projector.progress += 40
expect(projector.projection).to eq 36.0
end
it 'knows the running average is reset even after progress is started' do
projector = SmoothedAverage.new(:total => 50)
projector.instance_variable_set(:@projection, 10)
projector.start :at => 0
expect(projector.projection).to be_zero
projector.start :at => 40
expect(projector.projection).to eq 0.0
end
end
describe '#start' do
it 'resets the projection' do
projector = SmoothedAverage.new
projector.start
projector.progress = 10
expect(projector.projection).not_to be_zero
projector.start
expect(projector.projection).to eq 0.0
end
end
describe '#reset' do
it 'resets the projection' do
projector = SmoothedAverage.new
projector.start
projector.progress = 10
expect(projector.projection).not_to be_zero
projector.reset
expect(projector.projection).to eq 0.0
end
it 'resets based on the starting position' do
projector = SmoothedAverage.new(:strength => 0.1)
projector.start(:at => 10)
projector.progress = 20
expect(projector.projection).not_to be_zero
projector.reset
projector.progress = 20
expect(projector.projection).to eq 9.0
end
end
describe '#strength' do
it 'allows the default strength to be overridden' do
projector = SmoothedAverage.new(:strength => 0.3)
expect(projector.strength).to eq 0.3
end
it 'has a default strength' do
expect(SmoothedAverage.new.strength).to eq 0.1
end
end
end
end
end
|