mockspotify 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/mockspotify.rb CHANGED
@@ -1,11 +1,41 @@
1
+ require 'ffi'
1
2
  require 'rbconfig'
2
3
 
3
- module MockSpotify
4
- VERSION = "0.0.1"
4
+ module Spotify
5
+ module Mock
6
+ VERSION = '0.1.0'
5
7
 
6
- class << self
7
- def lib_path
8
+ # @return [String] path to the libmockspotify C extension binary.
9
+ def self.path
8
10
  File.expand_path('../../src/libmockspotify.', __FILE__) << RbConfig::CONFIG['DLEXT']
9
11
  end
12
+
13
+ # Overridden to always ffi_lib the mock path.
14
+ def ffi_lib(*)
15
+ super(Mock.path)
16
+ end
17
+
18
+ # Overriden to not throw an error on missing functions.
19
+ def attach_function(*)
20
+ super
21
+ rescue FFI::NotFoundError => e
22
+ warn "#{e.message}" if $VERBOSE
23
+ end
10
24
  end
25
+
26
+ extend FFI::Library
27
+ extend Mock
28
+ require 'spotify'
29
+
30
+ $VERBOSE = true
31
+ attach_function :mock_user, :mocksp_user_create, [:string, :string, :string, :string, :relation_type, :bool], :user
32
+ attach_function :mock_track, :mocksp_track_create, [:string, :int, :array, :pointer, :int, :int, :int, :int, :error, :bool], :track
33
+ attach_function :mock_image, :mocksp_image_create, [:image_id, :imageformat, :size_t, :pointer, :error], :image
34
+ attach_function :mock_artist, :mocksp_artist_create, [:string, :bool], :artist
35
+ attach_function :mock_album, :mocksp_album_create, [:string, :artist, :int, :pointer, :image_id, :albumtype, :bool, :bool], :album
36
+
37
+ attach_function :mock_artistbrowse, :mocksp_artistbrowse_create, [:artist, :bool], :artistbrowse
38
+
39
+ attach_function :mock_playlist_event, :mocksp_playlist_event, [:int, :playlist], :void
40
+ $VERBOSE = false
11
41
  end
data/mockspotify.gemspec CHANGED
@@ -13,6 +13,8 @@ Gem::Specification.new do |gem|
13
13
  gem.require_paths = ["lib", "src"]
14
14
  gem.extensions << 'src/extconf.rb'
15
15
 
16
- gem.version = MockSpotify::VERSION
16
+ gem.add_dependency 'spotify'
17
+
18
+ gem.version = Spotify::Mock::VERSION
17
19
  gem.platform = Gem::Platform::RUBY
18
20
  end
data/src/album.c CHANGED
@@ -5,13 +5,11 @@
5
5
  /*** MockSpotify API ***/
6
6
 
7
7
  sp_album *
8
- mocksp_album_create(char *name, sp_artist *artist, int year, byte * cover,
9
- int type, int loaded, int available)
8
+ mocksp_album_create(const char *name, sp_artist *artist, int year, const byte *cover,
9
+ sp_albumtype type, bool loaded, bool available)
10
10
  {
11
- sp_album *a;
11
+ sp_album *a = ALLOC(sp_album);
12
12
 
13
- a = malloc(sizeof(sp_album));
14
- memset(a, 0, sizeof(sp_album));
15
13
  strcpy(a->name, name);
16
14
  a->artist = artist;
17
15
  a->year = year;
data/src/artist.c CHANGED
@@ -7,10 +7,7 @@
7
7
  sp_artist *
8
8
  mocksp_artist_create(const char *name, int loaded)
9
9
  {
10
- sp_artist *a;
11
-
12
- a = malloc(sizeof(sp_artist));
13
- memset(a, 0, sizeof(sp_artist));
10
+ sp_artist *a = ALLOC(sp_artist);
14
11
  strcpy(a->name, name);
15
12
  a->loaded = loaded;
16
13
  return a;
data/src/libmockspotify.h CHANGED
@@ -28,7 +28,7 @@ struct sp_album {
28
28
  int year;
29
29
  byte cover[20];
30
30
  int type;
31
- int loaded;
31
+ bool loaded;
32
32
  bool available;
33
33
  };
34
34
 
@@ -141,8 +141,8 @@ mocksp_playlist_event(event_type event, sp_playlist *p);
141
141
  /*** Mock object creation ***/
142
142
 
143
143
  sp_album *
144
- mocksp_album_create(char *name, sp_artist *artist, int year, byte *cover,
145
- int type, int loaded, int available);
144
+ mocksp_album_create(const char *name, sp_artist *artist, int year, const byte *cover,
145
+ sp_albumtype type, bool loaded, bool available);
146
146
 
147
147
  sp_albumbrowse *
148
148
  mocksp_albumbrowse_create(sp_album *album, bool loaded);
@@ -154,15 +154,15 @@ sp_artistbrowse *
154
154
  mocksp_artistbrowse_create(sp_artist *artist, bool loaded);
155
155
 
156
156
  sp_playlist *
157
- mocksp_playlist_create(char *name);
157
+ mocksp_playlist_create(const char *name);
158
158
 
159
159
  sp_track *
160
- mocksp_track_create(char *name, int num_artists, sp_artist ** artists,
160
+ mocksp_track_create(const char *name, int num_artists, sp_artist ** artists,
161
161
  sp_album * album, int duration, int popularity,
162
- int disc, int index, sp_error error, int loaded);
162
+ int disc, int index, sp_error error, bool loaded);
163
163
 
164
164
  sp_user *
165
- mocksp_user_create(char *canonical_name, char *display_name, char *full_name,
166
- char *picture, sp_relation_type relation, bool loaded);
165
+ mocksp_user_create(const char *canonical_name, const char *display_name, const char *full_name,
166
+ const char *picture, sp_relation_type relation, bool loaded);
167
167
 
168
168
  #endif /* LIBMOCKSPOTIFY_API_H */
data/src/link.c CHANGED
@@ -30,7 +30,7 @@ sp_link *
30
30
  sp_link_create_from_user(sp_user *user)
31
31
  {
32
32
  char *link = ALLOC_N(char, 1024);
33
- snprintf(link, "spotify:user:%s", user->canonical_name);
33
+ sprintf(link, "spotify:user:%s", user->canonical_name);
34
34
  return sp_link_create_from_string(link);
35
35
  }
36
36
 
data/src/search.c CHANGED
@@ -20,7 +20,7 @@ sp_search_create(sp_session *session, const char *query, int track_offset,
20
20
  int artist_offset, int artist_count,
21
21
  search_complete_cb *callback, void *userdata)
22
22
  {
23
- sp_search *search = malloc(sizeof(sp_search));
23
+ sp_search *search = ALLOC(sp_search);
24
24
 
25
25
  if (!strncmp(query, "!loaded", 7))
26
26
  search->loaded = 0;
@@ -30,10 +30,11 @@ sp_search_create(sp_session *session, const char *query, int track_offset,
30
30
  search->num_albums = 3;
31
31
  search->num_artists = 2;
32
32
  search->total_tracks = 24;
33
- search->query = malloc(strlen(query) + 1);
33
+ search->query = ALLOC_N(char, strlen(query) + 1);
34
34
  strcpy(search->query, query);
35
35
  search->error = 3;
36
- search->did_you_mean = "did_you_mean";
36
+ search->did_you_mean = ALLOC_N(char, strlen("did_you_mean") + 1);
37
+ sprintf(search->did_you_mean, "did_you_mean");
37
38
  search->artist[0] = mocksp_artist_create("foo", 1);
38
39
  search->artist[1] = mocksp_artist_create("bar", 1);
39
40
  search->album[0] = mocksp_album_create("baz", search->artist[0], 2001,
data/src/track.c CHANGED
@@ -5,14 +5,12 @@
5
5
  /*** MockSpotify API ***/
6
6
 
7
7
  sp_track *
8
- mocksp_track_create(char *name, int num_artists, sp_artist **artists,
8
+ mocksp_track_create(const char *name, int num_artists, sp_artist **artists,
9
9
  sp_album *album, int duration, int popularity,
10
- int disc, int index, sp_error error, int loaded)
10
+ int disc, int index, sp_error error, bool loaded)
11
11
  {
12
- sp_track *t;
12
+ sp_track *t = ALLOC(sp_track);
13
13
 
14
- t = malloc(sizeof(sp_track));
15
- memset(t, 0, sizeof(sp_track));
16
14
  strcpy(t->name, name);
17
15
  t->loaded = loaded;
18
16
  t->disc = disc;
@@ -22,6 +20,7 @@ mocksp_track_create(char *name, int num_artists, sp_artist **artists,
22
20
  t->popularity = popularity;
23
21
  t->album = album;
24
22
  t->starred = 0;
23
+
25
24
  return t;
26
25
  }
27
26
 
data/src/user.c CHANGED
@@ -5,15 +5,15 @@
5
5
  /*** MockSpotify API ***/
6
6
 
7
7
  sp_user *
8
- mocksp_user_create(char *canonical_name, char *display_name, char *full_name,
9
- char *picture, sp_relation_type relation, bool loaded)
8
+ mocksp_user_create(const char *canonical_name, const char *display_name, const char *full_name,
9
+ const char *picture, sp_relation_type relation, bool loaded)
10
10
  {
11
11
  sp_user *user = ALLOC(sp_user);
12
12
 
13
- user->canonical_name = canonical_name;
14
- user->display_name = display_name;
15
- user->full_name = full_name;
16
- user->picture = picture;
13
+ strcpy(user->canonical_name, canonical_name);
14
+ strcpy(user->display_name, display_name);
15
+ strcpy(user->full_name, full_name);
16
+ strcpy(user->picture, picture);
17
17
  user->relation = relation;
18
18
  user->loaded = loaded;
19
19
 
@@ -1,12 +1,31 @@
1
1
  require 'mockspotify'
2
2
  require 'minitest/autorun'
3
3
 
4
- describe MockSpotify do
4
+ describe Spotify::Mock do
5
5
  it "should define VERSION" do
6
- MockSpotify::VERSION.must_be_kind_of String
6
+ Spotify::Mock::VERSION.must_be_kind_of String
7
7
  end
8
8
 
9
9
  it "should provide a path to libmockspotify" do
10
- File.exist?(MockSpotify.lib_path).must_equal true
10
+ Spotify::Mock.path.must_be_kind_of String
11
+ Spotify::Mock.path.must_match /libmockspotify\.(so|bundle)/
12
+ end
13
+
14
+ it "should have injected itself into Spotify's ancestor chain" do
15
+ Spotify.singleton_class.ancestors.take(2).must_equal [Spotify::Mock, FFI::Library]
16
+ end
17
+
18
+ describe ".mock_artist" do
19
+ before do
20
+ @artist = Spotify.mock_artist "Bonkers", true
21
+ end
22
+
23
+ it "should have the proper name" do
24
+ Spotify.artist_name(@artist).must_equal "Bonkers"
25
+ end
26
+
27
+ it "should have the proper load status" do
28
+ Spotify.artist_is_loaded(@artist).must_equal true
29
+ end
11
30
  end
12
31
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mockspotify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,18 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
  date: 2011-07-20 00:00:00.000000000Z
13
- dependencies: []
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: spotify
16
+ requirement: &2157317180 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2157317180
14
25
  description:
15
26
  email: kim@burgestrand.se
16
27
  executables: []