Created
February 7, 2012 08:00
-
-
Save jimweirich/1758146 to your computer and use it in GitHub Desktop.
Vital Ruby Lab 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class InvalidScoreError < StandardError | |
end | |
class Presentation | |
attr_reader :title, :presenter | |
def initialize(title, presenter) | |
@title = title | |
@presenter = presenter | |
@total = 0.0 | |
@count = 0 | |
end | |
def add_score(n) | |
fail InvalidScoreError if n > 5 || n < 1 | |
@total += n | |
@count += 1 | |
end | |
def average_score | |
@total / @count | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
require 'presentation' | |
describe Presentation do | |
let(:presentation) { Presentation.new("TITLE", "PRESENTER") } | |
it "has a title and presenter" do | |
presentation.title.should == "TITLE" | |
presentation.presenter.should == "PRESENTER" | |
end | |
it "can have scores added to it" do | |
lambda { presentation.add_score(4) }.should_not raise_error | |
end | |
it "can calculate the average score with a single score" do | |
presentation.add_score(4) | |
presentation.average_score.should == 4 | |
end | |
it "can calculate the average of two scores" do | |
presentation.add_score(4) | |
presentation.add_score(5) | |
presentation.average_score.should be_within(0.001).of(4.5) | |
end | |
it "rejects too large scores with an error" do | |
lambda { | |
presentation.add_score(6) | |
}.should raise_error(InvalidScoreError) | |
end | |
it "rejects too small scores with an error" do | |
lambda { | |
presentation.add_score(0) | |
}.should raise_error(InvalidScoreError) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment