1
0
mirror of https://github.com/danbee/mpd-client synced 2025-03-04 08:39:09 +00:00

Make all the tests work

This commit is contained in:
Lee Machin 2013-12-11 11:37:32 +00:00
parent 5e45aad95a
commit 494c1795f4
13 changed files with 111 additions and 56 deletions

View File

@ -21,7 +21,11 @@ module MPDClient
end
def year
@first_song.year
@first_song.date
end
def <=>(other)
year <=> other.year
end
def to_h

View File

@ -3,6 +3,12 @@ module MPDClient
include ClassToProc
attr :name
def initialize(name)
@name = name
end
class << self
def all
MPDClient.conn.artists.sort.map(&self)

View File

@ -5,7 +5,7 @@ module MPDClient
include Comparable
extend Forwardable
delegate %i(id track artist album title time pos) => :@song
delegate %i(id track artist album title genre date time pos) => :@song
def initialize(song, pos: nil)
@song = song

View File

@ -1,30 +1,34 @@
require 'spec_helper'
describe Album do
describe MPDClient::Album do
subject { MPDClient::Album }
let(:song1) { MPD::Song.new({ album: 'Back in Black', genre: 'Rock', date: '1980' }) }
let(:song2) { MPD::Song.new({ album: 'Highway to Hell', genre: 'Rock', date: '1979' }) }
before do
MPDConnection.mpd.stub(:albums).and_return([song1.album, song2.album])
MPDConnection.mpd.stub(:search).and_return([song1, song2])
MPDConnection.mpd.stub(:search).with(:album, song1.album).and_return([song1])
MPDConnection.mpd.stub(:search).with(:album, song2.album).and_return([song2])
MPDClient.conn.tap do |mpd|
mpd.stub(:albums).and_return([song1.album, song2.album])
mpd.stub(:where).and_return([song1, song2])
mpd.stub(:where).with(album: song1.album).and_return([song1])
mpd.stub(:where).with(album: song2.album).and_return([song2])
end
end
it 'has attributes based on first song' do
album = Album.new(song1.album)
album = subject.new(song1.album)
expect(album.title).to eq(song1.album)
expect(album.genre).to eq(song1.genre)
expect(album.year).to eq(song1.date)
end
it 'should return a list of albums' do
expect(Album.by_artist('AC/DC')).to have(2).items
expect(subject.by_artist('AC/DC')).to have(2).items
end
it 'should sort the albums by year' do
albums = Album.by_artist('AC/DC')
albums = subject.by_artist('AC/DC')
expect(albums.sort.map(&:year)).to eq(['1979', '1980'])
end

View File

@ -0,0 +1,23 @@
require 'spec_helper'
describe MPDClient::Artist do
subject { MPDClient::Artist }
let(:artist) { subject.new('Alice Cooper') }
let(:artists) { ['Alice Cooper', 'Jimmy Eat World', 'Dream Theater'] }
before do
MPDClient.conn.stub(:artists).and_return(artists)
end
it 'has attributes' do
expect(artist.name).to eq('Alice Cooper')
end
it 'returns all artists' do
expect(subject.all).to have(3).items
expect(subject.all.map(&:name)).to eq(artists.sort)
end
end

View File

@ -0,0 +1,23 @@
require 'spec_helper'
describe MPDClient::Connection do
subject { MPDClient::Connection }
let(:conn) { subject.new('localhost', 6600) }
describe "#each_conn" do
let(:fake_users) { [1, 2, 3] }
it "enumerates the connected users" do
fake_users.each {|n| conn.connected_users << n }
conn.each_conn.to_a.should eq(fake_users)
end
end
describe "#connected_users" do
it "doesn't allow duplicate connections" do
3.times { conn.connected_users << "user" }
conn.connected_users.should have(1).item
end
end
end

View File

@ -0,0 +1,4 @@
require 'spec_helper'
describe MPDClient::Control do
end

View File

@ -1,16 +1,18 @@
require 'spec_helper'
describe Song do
describe MPDClient::Song do
subject { MPDClient::Song }
let(:song1) { MPD::Song.new({ title: 'Back in Black', album: 'Back in Black', genre: 'Rock', date: '1980' }) }
let(:song2) { MPD::Song.new({ title: 'Highway to Hell', album: 'Highway to Hell', genre: 'Rock', date: '1979' }) }
before do
MPDConnection.mpd.stub(:queue).and_return([song1, song2])
MPDClient.conn.stub(:queue).and_return([song1, song2])
end
it 'returns the queue of songs' do
queue = Song.queue
queue = subject.queue
expect(queue).to have(2).items
end
end

View File

@ -0,0 +1,23 @@
require 'spec_helper'
describe MPDClient do
subject { MPDClient }
before do
subject.connect!
end
describe "#listen" do
it "exposes `Connection#on` for easy event listening" do
subject.conn.should_receive(:on).exactly(3).times
subject.listen do
on(:first) { "something" }
on(:second) { "something" }
on(:third) { "something" }
end
end
end
end

View File

@ -1,18 +0,0 @@
require 'spec_helper'
describe Artist do
let(:artist) { Artist.new('Alice Cooper') }
let(:artists) { ['Alice Cooper', 'Jimmy Eat World', 'Dream Theater'] }
it 'has attributes' do
expect(artist.name).to eq('Alice Cooper')
end
it 'returns all artists' do
MPDConnection.mpd.stub(:artists).and_return(artists)
expect(Artist.all).to have(3).items
expect(Artist.all.map(&:name)).to eq(artists.sort)
end
end

View File

@ -1,4 +0,0 @@
require 'spec_helper'
describe Control do
end

View File

@ -1,8 +0,0 @@
require 'spec_helper'
describe MPDClient do
it 'should respond to GET' do
get '/'
expect(last_response.status).to eq(404)
end
end

View File

@ -1,20 +1,16 @@
ENV['RACK_ENV'] = 'test'
require File.join(File.dirname(__FILE__), '..', 'mpd_client.rb')
require 'sinatra'
require File.expand_path('../lib/mpd_client', __dir__)
require 'rspec'
require 'rspec/mocks'
require 'rack/test'
# setup test environment
set :environment, :test
set :run, false
set :raise_errors, true
set :logging, false
def app
MPDClient
end
RSpec.configure do |config|
config.include Rack::Test::Methods
config.before(:each) do
allow_message_expectations_on_nil
MPDClient::Connection.any_instance.stub(:connected?).and_return(true)
end
end