From 494c1795f44cf6d0cc8a69f8eaa3cfbb04ed0300 Mon Sep 17 00:00:00 2001 From: Lee Machin Date: Wed, 11 Dec 2013 11:37:32 +0000 Subject: [PATCH] Make all the tests work --- lib/mpd_client/album.rb | 6 ++++- lib/mpd_client/artist.rb | 6 +++++ lib/mpd_client/song.rb | 2 +- spec/{models => lib/mpd_client}/album_spec.rb | 20 +++++++++------- spec/lib/mpd_client/artist_spec.rb | 23 +++++++++++++++++++ spec/lib/mpd_client/connection_spec.rb | 23 +++++++++++++++++++ spec/lib/mpd_client/control_spec.rb | 4 ++++ spec/{models => lib/mpd_client}/song_spec.rb | 8 ++++--- spec/lib/mpd_client_spec.rb | 23 +++++++++++++++++++ spec/models/artist_spec.rb | 18 --------------- spec/models/control_spec.rb | 4 ---- spec/mpd_client_spec.rb | 8 ------- spec/spec_helper.rb | 22 ++++++++---------- 13 files changed, 111 insertions(+), 56 deletions(-) rename spec/{models => lib/mpd_client}/album_spec.rb (53%) create mode 100644 spec/lib/mpd_client/artist_spec.rb create mode 100644 spec/lib/mpd_client/connection_spec.rb create mode 100644 spec/lib/mpd_client/control_spec.rb rename spec/{models => lib/mpd_client}/song_spec.rb (71%) create mode 100644 spec/lib/mpd_client_spec.rb delete mode 100644 spec/models/artist_spec.rb delete mode 100644 spec/models/control_spec.rb delete mode 100644 spec/mpd_client_spec.rb diff --git a/lib/mpd_client/album.rb b/lib/mpd_client/album.rb index 676af23..eedf71b 100644 --- a/lib/mpd_client/album.rb +++ b/lib/mpd_client/album.rb @@ -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 diff --git a/lib/mpd_client/artist.rb b/lib/mpd_client/artist.rb index c75e500..8b0bb3f 100644 --- a/lib/mpd_client/artist.rb +++ b/lib/mpd_client/artist.rb @@ -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) diff --git a/lib/mpd_client/song.rb b/lib/mpd_client/song.rb index 3e1e89a..260287a 100644 --- a/lib/mpd_client/song.rb +++ b/lib/mpd_client/song.rb @@ -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 diff --git a/spec/models/album_spec.rb b/spec/lib/mpd_client/album_spec.rb similarity index 53% rename from spec/models/album_spec.rb rename to spec/lib/mpd_client/album_spec.rb index 1bd4669..0abf21f 100644 --- a/spec/models/album_spec.rb +++ b/spec/lib/mpd_client/album_spec.rb @@ -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 diff --git a/spec/lib/mpd_client/artist_spec.rb b/spec/lib/mpd_client/artist_spec.rb new file mode 100644 index 0000000..5032b2c --- /dev/null +++ b/spec/lib/mpd_client/artist_spec.rb @@ -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 diff --git a/spec/lib/mpd_client/connection_spec.rb b/spec/lib/mpd_client/connection_spec.rb new file mode 100644 index 0000000..f61b7c1 --- /dev/null +++ b/spec/lib/mpd_client/connection_spec.rb @@ -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 diff --git a/spec/lib/mpd_client/control_spec.rb b/spec/lib/mpd_client/control_spec.rb new file mode 100644 index 0000000..9e84b27 --- /dev/null +++ b/spec/lib/mpd_client/control_spec.rb @@ -0,0 +1,4 @@ +require 'spec_helper' + +describe MPDClient::Control do +end diff --git a/spec/models/song_spec.rb b/spec/lib/mpd_client/song_spec.rb similarity index 71% rename from spec/models/song_spec.rb rename to spec/lib/mpd_client/song_spec.rb index 93f3430..d19ccfb 100644 --- a/spec/models/song_spec.rb +++ b/spec/lib/mpd_client/song_spec.rb @@ -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 diff --git a/spec/lib/mpd_client_spec.rb b/spec/lib/mpd_client_spec.rb new file mode 100644 index 0000000..dde8037 --- /dev/null +++ b/spec/lib/mpd_client_spec.rb @@ -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 diff --git a/spec/models/artist_spec.rb b/spec/models/artist_spec.rb deleted file mode 100644 index ac7559c..0000000 --- a/spec/models/artist_spec.rb +++ /dev/null @@ -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 diff --git a/spec/models/control_spec.rb b/spec/models/control_spec.rb deleted file mode 100644 index 39f0c42..0000000 --- a/spec/models/control_spec.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'spec_helper' - -describe Control do -end diff --git a/spec/mpd_client_spec.rb b/spec/mpd_client_spec.rb deleted file mode 100644 index 7c600f1..0000000 --- a/spec/mpd_client_spec.rb +++ /dev/null @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ea68e56..120b0b4 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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