Skip to content

Instantly share code, notes, and snippets.

@J-Scag
Created October 8, 2013 23:25
Show Gist options
  • Select an option

  • Save J-Scag/6893552 to your computer and use it in GitHub Desktop.

Select an option

Save J-Scag/6893552 to your computer and use it in GitHub Desktop.
100% coverage jukebox spec with simplecov
# A Jukebox filled with songs
# that responds to help, list, play, and exit
require_relative 'song'
class Jukebox
attr_accessor :songs
APPROVED_COMMANDS = [:list, :help, :exit]
def initialize(songs)
@songs = songs
@on = true
end
def on?
@on
end
def help
"Please select help, list, exit, or play."
end
def command(input)
response = ''
if APPROVED_COMMANDS.include?(input.strip.downcase.to_sym)
response = self.send(input)
elsif input.start_with?("play")
song_request = input.split("play").last.strip
response = self.play(song_request)
else
response = "invalid command"
end
response
end
def exit
@on = false
"Goodbye, thanks for listening!"
end
def list
list = ""
songs.each_with_index do |song, i|
list += "#{i+1} #{song.name}\n"
end
list
end
def play(song_request)
"now playing #{song_request}"
end
end
require 'simplecov'
SimpleCov.start
require 'json'
require 'rspec'
require_relative './jukebox'
require_relative './song'
#use this song data for your tests
songs = []
songs << Song.new("The Phoenix - 1901")
songs << Song.new("Tokyo Police Club - Wait Up")
songs << Song.new("Sufjan Stevens - Too Much")
songs << Song.new("The Naked and the Famous - Young Blood")
songs << Song.new("(Far From) Home - Tiga")
songs << Song.new("The Cults - Abducted")
songs << Song.new("The Phoenix - Consolation Prizes")
describe Song do
it "can have a name" do
jazz = Song.new("Blue Rondo a la Turk")
jazz.name.should eq("Blue Rondo a la Turk")
end
end
describe Jukebox do
let(:box) { Jukebox.new(songs) }
it "is on by default" do
box.on?.should eq(true)
end
it "can be turned off" do
box.exit
box.on?.should eq(false)
end
it "can take the play command" do
box.command("play The Phoenix - Consolation Prizes").
should eq("now playing The Phoenix - Consolation Prizes")
end
it "can take the list command" do
box.command("list").
should include("1 The Phoenix - 1901\n2 Tokyo Police Club - Wait Up")
end
it "can take the help command" do
box.command("help").
should eq("Please select help, list, exit, or play.")
end
it "ignores invalid commands" do
box.command("KNEEL BEFORE ZOD").
should eq("invalid command")
end
end
class Song
attr_accessor :name
def initialize(name)
@name = name
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment