Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ruby homework2 additional tests
describe Collection do
let(:additional_tags) do
{
'John Coltrane' => %w[saxophone],
'Bach' => %w[piano polyphony],
}
end
let(:input) do
<<-END
'Round Midnight. John Coltrane. Jazz
Tutu. Miles Davis. Jazz, Fusion. weird, cool
Autumn Leaves. Bill Evans. Jazz. popular
Waltz for Debbie. Bill Evans. Jazz
'Round Midnight. Thelonious Monk. Jazz, Bebop
Toccata e Fuga. Bach. Classical, Baroque. popular
Goldberg Variations. Bach. Classical, Baroque
END
end
let(:collection) { Collection.new input, additional_tags }
it "can look up songs by artist" do
songs(artist: 'Bill Evans').map(&:name).should =~ ['Autumn Leaves', 'Waltz for Debbie']
end
it "can look up songs by name" do
songs(name: "'Round Midnight").map(&:artist).should =~ ['John Coltrane', 'Thelonious Monk']
end
it "can find songs by tag" do
songs(tags: 'baroque').map(&:name).should =~ ['Toccata e Fuga', 'Goldberg Variations']
songs(tags: 'saxophone').map(&:name).should =~ ["'Round Midnight"]
songs(tags: %w[baroque classical]).map(&:name).should =~ ['Toccata e Fuga', 'Goldberg Variations']
songs(tags: %w[piano jazz!]).map(&:name).should =~ ['Toccata e Fuga', 'Goldberg Variations']
songs(tags: 'jazz!').map(&:name).should =~ ['Toccata e Fuga', 'Goldberg Variations']
songs(tags: %w[classical! jazz!]).map(&:name).should =~ []
end
it "can find songs by filter" do
songs(filter: ->(song) { song.name.start_with?('Tu') }).map(&:name).should =~ ['Tutu']
end
it "returns whole collection when no filters applied" do
songs().map(&:name).should =~ songs.map(&:name)
end
it "returns empty list if no songs are found" do
songs(tags: 'jujijujiju').map(&:name).should =~ []
end
it "preserves multiple songs with same names" do
songs(name: "'Round Midnight").map(&:name).length.should eq 2
end
it "applies all filters" do
songs(artist: "Bill Evans", filter: ->(song) { song.name.start_with?('Au') }).map(&:name).should =~ ['Autumn Leaves']
end
it "constructs an object for each song" do
song = collection.find(name: 'Tutu').first
song.name.should eq 'Tutu'
song.artist.should eq 'Miles Davis'
song.genre.should eq 'Jazz'
song.subgenre.should eq 'Fusion'
song.tags.should include('weird', 'cool')
end
# By Ivajlo:
it "returns nil for subgenre when appropriate" do
songs(name: "'Round Midnight", artist: 'John Coltrane').first.subgenre.should eq nil
end
it "can look up songs by complex tags" do
songs_list = songs(tags: ['popular', 'jazz!'])
songs_list.first.name.should eq 'Toccata e Fuga'
songs_list.length.should eq 1
end
it "can find songs by tags and name" do
songs_list = songs(tags: 'jazz', name: "'Round Midnight")
songs_list.length.should eq 2
songs_list.first.artist.should eq 'John Coltrane'
songs_list.last.artist.should eq 'Thelonious Monk'
end
it "can find songs by blocks" do
songs_list = songs(filter: ->(song) { song.tags.include?('jazz') and song.name.length >= 14 })
songs_list.map(&:artist).should =~ ['John Coltrane', 'Thelonious Monk', 'Bill Evans']
end
def songs(options = {})
collection.find(options)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.