mb-discid 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES ADDED
@@ -0,0 +1,20 @@
1
+ = Changelog
2
+
3
+ == 0.1.2 (2007-07-04)
4
+ * Support the method +put+ to set the TOC information directly instead of
5
+ reading it from a device.
6
+ * Fixed possible core dump if +read+ was called twice and failed the
7
+ second time.
8
+ * New to_s method (returns string representation of the ID itself).
9
+ * Complete RDoc documentation.
10
+
11
+ == 0.1.1 (2007-06-03)
12
+ * Minor changes to source to support MS compiler
13
+ * Provide Win32 binary gem
14
+ * Changed require of library to "require 'mb-discid'" (was "require 'DiscID'"
15
+ before, which was likely to cause problems)
16
+
17
+ == 0.1.0 (2007-06-02)
18
+ * Initial release
19
+
20
+ $Id: CHANGES 94 2007-07-04 19:45:56Z phw $
data/README CHANGED
@@ -1,7 +1,7 @@
1
1
  = MB-DiscID - Ruby bindings for MusicBrainz libdiscid
2
2
 
3
3
  == About
4
- MB-DiscID provides Ruby bindings for the MusicBrainz DiscID library libdsicid.
4
+ MB-DiscID provides Ruby bindings for the MusicBrainz DiscID library libdiscid.
5
5
  It allows you to calculate MusicBrainz DiscIDs from audio CDs which you can use
6
6
  to find the release entry for your CD in the MusicBrainz database.
7
7
 
@@ -12,25 +12,29 @@ http://musicbrainz.org/doc/libdiscid for more information on how to do this.
12
12
  === Installation with RubyGems
13
13
  gem install mb-discid
14
14
 
15
- == Installation on Windows operating systems.
16
- For Windows a binary gem "mb-discid-0.1.1-mswin32.gem" is available. To install
17
- it follow the instructions below:
15
+ === Installation on Windows operating systems.
16
+ For Windows a binary gem <tt>mb-discid-x.x.x-mswin32.gem</tt> is available. To
17
+ install it follow the instructions below:
18
18
 
19
19
  1. Make sure you have at least Ruby 1.8.6 installed.
20
20
  2. Install RubyGems, if you don't already have it.
21
- 3. Download "libdiscid-0.1.1-win32bin.zip" from
21
+ 3. Download <tt>libdiscid-0.1.1-win32bin.zip</tt> from
22
22
  http://musicbrainz.org/doc/libdiscid.
23
- 4. Copy the file "discid.dll" from "libdiscid-0.1.1-win32bin.zip" to your Ruby
24
- bin directory (normally "C:\ruby\bin".
25
- 5. Run "gem install mb-discid" to install MB-DiscID.
26
- 6. To test the installation try to run "discid.rb" in the examples directory.
23
+ 4. Copy the file <tt>discid.dll</tt> from <tt>libdiscid-0.1.1-win32bin.zip</tt>
24
+ to your Ruby bin directory (normally <tt>C:\ruby\bin</tt>).
25
+ 5. Run <tt>gem install mb-discid</tt> to install MB-DiscID.
26
+ 6. To test the installation try to run <tt>examples/discid.rb</tt>.
27
27
 
28
28
  === Installing from source
29
29
  rake install
30
30
 
31
31
  == Usage
32
- See the files in the examples directory for usage information.
32
+ See the documentation of MusicBrainz::DiscID or the example files in the
33
+ +examples+ directory for usage information.
33
34
 
34
35
  == License
35
- Copyright (c) 2007, Philipp Wolfer
36
- Licensing information can be found in the file LICENSE.
36
+ MB-DiscID is Copyright (c) 2007 Philipp Wolfer.
37
+ It is free softare distributed under a BSD style license. See
38
+ LICENSE[link:files/LICENSE.html] for details.
39
+
40
+ $Id: README 89 2007-06-26 15:09:34Z phw $
data/Rakefile CHANGED
@@ -1,4 +1,4 @@
1
- # $Id: Rakefile 69 2007-06-03 14:14:25Z phw $
1
+ # $Id: Rakefile 86 2007-06-05 10:27:42Z phw $
2
2
  # Copyright (c) 2007, Philipp Wolfer
3
3
  # All rights reserved.
4
4
  # See LICENSE for permissions.
@@ -7,6 +7,8 @@
7
7
 
8
8
  require 'rubygems'
9
9
  require 'rake/gempackagetask'
10
+ require 'rake/testtask'
11
+ require 'rake/rdoctask'
10
12
 
11
13
  task :default do
12
14
  puts "Please see 'rake --tasks' for an overview of the available tasks."
@@ -15,7 +17,7 @@ end
15
17
  # Packaging tasks: -------------------------------------------------------
16
18
 
17
19
  PKG_NAME = 'mb-discid'
18
- PKG_VERSION = '0.1.1'
20
+ PKG_VERSION = '0.1.2'
19
21
  PKG_SUMMARY = 'Ruby bindings for libdiscid.'
20
22
  PKG_AUTHOR = 'Philipp Wolfer'
21
23
  PKG_EMAIL = 'phw@rubyforge.org'
@@ -25,11 +27,13 @@ PKG_DESCRIPTION = <<EOF
25
27
  for more information on libdiscid and MusicBrainz.
26
28
  EOF
27
29
  PKG_FILES = FileList[
28
- 'Rakefile', 'LICENSE', 'README',
30
+ 'Rakefile', 'LICENSE', 'README', 'CHANGES',
29
31
  'examples/**/*',
30
32
  'ext/**/*.{c,rb}',
31
- 'lib/**/*.rb'
33
+ 'lib/**/*.rb',
34
+ 'test/**/*.rb'
32
35
  ]
36
+ PKG_EXTRA_RDOC_FILES = ['README', 'LICENSE', 'CHANGES']
33
37
 
34
38
  spec = Gem::Specification.new do |spec|
35
39
  spec.name = PKG_NAME
@@ -53,6 +57,8 @@ spec = Gem::Specification.new do |spec|
53
57
  spec.email = PKG_EMAIL
54
58
  spec.homepage = PKG_HOMEPAGE
55
59
  spec.rubyforge_project = 'rbrainz'
60
+ spec.has_rdoc = true
61
+ spec.extra_rdoc_files = PKG_EXTRA_RDOC_FILES
56
62
  end
57
63
 
58
64
  Rake::GemPackageTask.new(spec) do |pkg|
@@ -109,3 +115,22 @@ task :rebuild => [:clobber_build, :build]
109
115
  desc 'Remove all files created during the build process'
110
116
  task :clobber => [:clobber_build, :clobber_package]
111
117
 
118
+ # Test tasks: ------------------------------------------------------------
119
+
120
+ desc "Run just the unit tests"
121
+ Rake::TestTask.new(:test => [:rebuild]) do |test|
122
+ test.test_files = FileList['test/test*.rb']
123
+ test.libs = ['lib', 'ext']
124
+ test.warning = true
125
+ end
126
+
127
+ # Documentation tasks: ---------------------------------------------------
128
+
129
+ Rake::RDocTask.new do |rdoc|
130
+ rdoc.title = "MB-DiscID %s" % PKG_VERSION
131
+ rdoc.main = 'README'
132
+ rdoc.rdoc_dir = 'doc/api'
133
+ rdoc.rdoc_files.include('lib/**/*.rb', 'ext/**/*.c', PKG_EXTRA_RDOC_FILES)
134
+ rdoc.options << '--inline-source' << '--line-numbers' #<< '--diagram'
135
+ end
136
+
data/examples/discid.rb CHANGED
@@ -1,16 +1,29 @@
1
1
  #!/usr/bin/env ruby
2
+ #
3
+ # Example script for MB-DiscID.
4
+ #
5
+ # This script will read the disc ID from the default device and print
6
+ # the results. You can specify an alternate device to use by giving the
7
+ # device's name as the first command line argument.
8
+ #
9
+ # Example:
10
+ # ./discid.rb /dev/dvd
11
+ #
12
+ # $Id: discid.rb 84 2007-06-05 00:41:14Z phw $
2
13
 
3
14
  require 'mb-discid'
4
15
 
5
16
  # Read the device name from the command line or use the default.
6
17
  device = $*[0] ? $*[0] : MusicBrainz::DiscID.default_device
7
18
 
19
+ puts "Reading TOC from device '#{device}'."
20
+
8
21
  disc = MusicBrainz::DiscID.new
9
22
  disc.read(device)
10
23
 
11
24
  print <<EOF
25
+
12
26
  DiscID : #{disc.id}
13
- Submit via : #{disc.submission_url}
14
27
  FreeDB ID : #{disc.freedb_id}
15
28
  First track: #{disc.first_track_num}
16
29
  Last track : #{disc.last_track_num}
@@ -22,3 +35,5 @@ disc.tracks do |offset, length|
22
35
  puts "Track #{track}: Offset #{offset}, Length #{length}"
23
36
  track += 1
24
37
  end
38
+
39
+ puts "\nSubmit via #{disc.submission_url}"
data/ext/extconf.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
- # $Id: extconf.rb 69 2007-06-03 14:14:25Z phw $
2
+ # $Id: extconf.rb 68 2007-06-03 14:12:21Z phw $
3
3
 
4
4
  require 'mkmf'
5
5
 
data/ext/mb_discid.c CHANGED
@@ -1,12 +1,14 @@
1
- /*---------------------------------------------------------------------------
2
- $Id: mb_discid.c 67 2007-06-03 14:09:23Z phw $
3
- Copyright (c) 2007, Philipp Wolfer
4
- All rights reserved.
5
- See LICENSE for permissions.
6
-
7
- Ruby bindings for libdiscid. See http://musicbrainz.org/doc/libdiscid
8
- for more information on libdiscid and MusicBrainz.
9
- ---------------------------------------------------------------------------*/
1
+ /**
2
+ * $Id: mb_discid.c 95 2007-07-04 20:47:24Z phw $
3
+ *
4
+ * Ruby bindings for libdiscid. See http://musicbrainz.org/doc/libdiscid
5
+ * for more information on libdiscid and MusicBrainz.
6
+ *
7
+ * Author:: Philipp Wolfer (mailto:phw@rubyforge.org)
8
+ * Copyright:: Copyright (c) 2007, Philipp Wolfer
9
+ * License:: MB-DiscID is free software distributed under a BSD style license.
10
+ * See LICENSE for permissions.
11
+ */
10
12
 
11
13
  #include "ruby.h"
12
14
  #include "discid/discid.h"
@@ -24,7 +26,7 @@ static VALUE cDiscID;
24
26
  /**
25
27
  * Returns the DiscID as a string.
26
28
  *
27
- * Returns nil if no ID was yet read.
29
+ * Returns +nil+ if no ID was yet read.
28
30
  */
29
31
  static VALUE mb_discid_id(VALUE self)
30
32
  {
@@ -42,7 +44,7 @@ static VALUE mb_discid_id(VALUE self)
42
44
  /**
43
45
  * Returns a submission URL for the DiscID as a string.
44
46
  *
45
- * Returns nil if no ID was yet read.
47
+ * Returns +nil+ if no ID was yet read.
46
48
  */
47
49
  static VALUE mb_discid_submission_url(VALUE self)
48
50
  {
@@ -60,7 +62,7 @@ static VALUE mb_discid_submission_url(VALUE self)
60
62
  /**
61
63
  * Return a FreeDB DiscID as a string.
62
64
  *
63
- * Returns nil if no ID was yet read.
65
+ * Returns +nil+ if no ID was yet read.
64
66
  */
65
67
  static VALUE mb_discid_freedb_id(VALUE self)
66
68
  {
@@ -76,9 +78,9 @@ static VALUE mb_discid_freedb_id(VALUE self)
76
78
  }
77
79
 
78
80
  /**
79
- * Return the number of the first track on this disc.
81
+ * Return the number of the first track on this disc (usually 1).
80
82
  *
81
- * Returns nil if no ID was yet read.
83
+ * Returns +nil+ if no ID was yet read.
82
84
  */
83
85
  static VALUE mb_discid_first_track_num(VALUE self)
84
86
  {
@@ -96,7 +98,7 @@ static VALUE mb_discid_first_track_num(VALUE self)
96
98
  /**
97
99
  * Return the number of the last track on this disc.
98
100
  *
99
- * Returns nil if no ID was yet read.
101
+ * Returns +nil+ if no ID was yet read.
100
102
  */
101
103
  static VALUE mb_discid_last_track_num(VALUE self)
102
104
  {
@@ -114,7 +116,7 @@ static VALUE mb_discid_last_track_num(VALUE self)
114
116
  /**
115
117
  * Return the length of the disc in sectors.
116
118
  *
117
- * Returns nil if no ID was yet read.
119
+ * Returns +nil+ if no ID was yet read.
118
120
  */
119
121
  static VALUE mb_discid_sectors(VALUE self)
120
122
  {
@@ -130,12 +132,13 @@ static VALUE mb_discid_sectors(VALUE self)
130
132
  }
131
133
 
132
134
  /**
133
- * Returns an array of [offset, length] tuples for each track.
135
+ * Returns an array of <tt>[offset, length]</tt> tuples for each track.
134
136
  *
135
- * If a block is given this method returns nil and instead iterates over the
136
- * block calling the block with two arguments |offset, length|.
137
+ * Offset and length are both integer values representing sectors.
138
+ * If a block is given this method returns +nil+ and instead iterates over the
139
+ * block calling the block with two arguments <tt>|offset, length|</tt>.
137
140
  *
138
- * Returns always nil if no ID was yet read. The block won't be called in
141
+ * Returns always +nil+ if no ID was yet read. The block won't be called in
139
142
  * this case.
140
143
  */
141
144
  static VALUE mb_discid_tracks(VALUE self)
@@ -144,14 +147,14 @@ static VALUE mb_discid_tracks(VALUE self)
144
147
  return Qnil;
145
148
  else
146
149
  {
147
- DiscId *disc;
148
- Data_Get_Struct(self, DiscId, disc);
150
+ DiscId *disc; /* Pointer to the disc struct */
151
+ VALUE result = rb_ary_new(); /* Array of all [offset, length] tuples */
152
+ VALUE tuple; /* Array to store one [offset, length] tuple. */
153
+ int track; /* Counter for the track number to process. */
149
154
 
150
- { // Stupid MS compiler.
155
+ Data_Get_Struct(self, DiscId, disc);
151
156
 
152
- VALUE result = rb_ary_new(); // Array of all [offset, length] tuples
153
- VALUE tuple; // Array to store one [offset, length] tuple.
154
- int track = discid_get_first_track_num(disc); // Track number
157
+ track = discid_get_first_track_num(disc); /* First track number */
155
158
  while (track <= discid_get_last_track_num(disc))
156
159
  {
157
160
  tuple = rb_ary_new3(2,
@@ -170,8 +173,6 @@ static VALUE mb_discid_tracks(VALUE self)
170
173
  return Qnil;
171
174
  else
172
175
  return result;
173
-
174
- } // Stupid MS compiler.
175
176
  }
176
177
  }
177
178
 
@@ -179,39 +180,90 @@ static VALUE mb_discid_tracks(VALUE self)
179
180
  * Read the disc ID from the given device.
180
181
  *
181
182
  * If no device is given the default device of the platform will be used.
183
+ * Throws an <tt>Exception</tt> if the CD's TOC can not be read.
182
184
  */
183
185
  static VALUE mb_discid_read(int argc, VALUE *argv, VALUE self)
184
186
  {
185
- DiscId *disc;
186
- Data_Get_Struct(self, DiscId, disc);
187
-
188
- { // Stupid MS compiler.
187
+ DiscId *disc; /* Pointer to the disc struct */
188
+ VALUE device = Qnil; /* The device string as a Ruby string */
189
+ char* cdevice; /* The device string as a C string */
189
190
 
190
- VALUE device = Qnil; // The device string as a Ruby string.
191
- char* cdevice; // The device string as a C string.
191
+ Data_Get_Struct(self, DiscId, disc);
192
192
 
193
- // Check the number of arguments
193
+ /* Check the number of arguments */
194
194
  if (argc > 1)
195
195
  rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)", argc);
196
- // Convert the given device to a T_STRING
197
- else if (argc > 0)
198
- device = rb_funcall(argv[0], rb_intern("to_s"), 0, 0);
196
+ /* Convert the given device to a T_STRING */
197
+ else if (argc == 1)
198
+ {
199
+ if(rb_respond_to(argv[0], rb_intern("to_s")))
200
+ device = rb_funcall(argv[0], rb_intern("to_s"), 0, 0);
201
+ else
202
+ rb_raise(rb_eTypeError, "wrong argument type (expected String)");
203
+ }
199
204
 
200
- // Use the default device if none was given.
205
+ /* Use the default device if none was given. */
201
206
  if (argc < 1 || RSTRING(device)->len == 0)
202
207
  cdevice = discid_get_default_device();
203
208
  else
204
209
  cdevice = STR2CSTR(device);
205
210
 
206
- // Read the discid
211
+ /* Mark the disc id as unread in case something goes wrong. */
212
+ rb_iv_set(self, "@read", Qfalse);
213
+
214
+ /* Read the discid */
207
215
  if (discid_read(disc, cdevice) == 0)
208
216
  rb_raise(rb_eException, discid_get_error_msg(disc));
209
- else // Remember that we already read the ID.
217
+ else /* Remember that we already read the ID. */
210
218
  rb_iv_set(self, "@read", Qtrue);
211
219
 
212
220
  return Qnil;
213
221
 
214
- } // Stupid MS compiler.
222
+ }
223
+
224
+ /**
225
+ * Set the TOC information directly instead of reading it from a device.
226
+ *
227
+ * Use this instead of read if the TOC information was already read elsewhere
228
+ * and you want to recalculate the ID.
229
+ * Throws an <tt>Exception</tt> if the CD's TOC can not be read.
230
+ *
231
+ * <b>Parameters:</b>
232
+ * [first_track] The number of the first track on the disc (usually 1).
233
+ * [sectors] The total number of sectors on the disc.
234
+ * [offsets] Array of all track offsets. The number of tracks must not exceed 99.
235
+ */
236
+ static VALUE mb_discid_put(VALUE self, VALUE first_track, VALUE sectors,
237
+ VALUE offsets)
238
+ {
239
+ DiscId *disc; /* Pointer to the disc struct */
240
+ long length = RARRAY(offsets)->len; /* length of the offsets array */
241
+ int cfirst = NUM2INT(first_track); /* number of the first track */
242
+ int clast = length + 1 - cfirst; /* number of the last track */
243
+ int coffsets[100]; /* C array to hold the offsets */
244
+ int i = 1; /* Counter for iterating over coffsets*/
245
+
246
+ Data_Get_Struct(self, DiscId, disc);
247
+
248
+ /* Convert the Ruby array to an C array of integers. discid_puts expects
249
+ always an offsets array with exactly 100 elements. */
250
+ coffsets[0] = NUM2INT(sectors); /* 0 is always the leadout track */
251
+ while (i <= length && i < 100)
252
+ {
253
+ coffsets[i] = NUM2INT(rb_ary_entry(offsets, i - 1));
254
+ i++;
255
+ }
256
+
257
+ /* Mark the disc id as unread in case something goes wrong. */
258
+ rb_iv_set(self, "@read", Qfalse);
259
+
260
+ /* Read the discid */
261
+ if (discid_put(disc, cfirst, clast, coffsets) == 0)
262
+ rb_raise(rb_eException, discid_get_error_msg(disc));
263
+ else /* Remember that we already read the ID. */
264
+ rb_iv_set(self, "@read", Qtrue);
265
+
266
+ return Qnil;
215
267
  }
216
268
 
217
269
  /**
@@ -249,7 +301,8 @@ VALUE mb_discid_default_device(VALUE class)
249
301
  /**
250
302
  * Initialize the DiscID class and make it available in Ruby.
251
303
  */
252
- void Init_MB_DiscID() {
304
+ void Init_MB_DiscID()
305
+ {
253
306
  mMusicBrainz = rb_define_module("MusicBrainz");
254
307
  cDiscID = rb_define_class_under(mMusicBrainz, "DiscID", rb_cObject);
255
308
  rb_define_singleton_method(cDiscID, "new", mb_discid_new, -1);
@@ -257,6 +310,7 @@ void Init_MB_DiscID() {
257
310
  mb_discid_default_device, 0);
258
311
 
259
312
  rb_define_method(cDiscID, "read", mb_discid_read, -1);
313
+ rb_define_method(cDiscID, "put", mb_discid_put, 3);
260
314
  rb_define_method(cDiscID, "id", mb_discid_id, 0);
261
315
  rb_define_method(cDiscID, "submission_url", mb_discid_submission_url, 0);
262
316
  rb_define_method(cDiscID, "freedb_id", mb_discid_freedb_id, 0);
data/lib/mb-discid.rb CHANGED
@@ -1,12 +1,88 @@
1
- # $Id: mb-discid.rb 67 2007-06-03 14:09:23Z phw $
2
- # Copyright (c) 2007, Philipp Wolfer
3
- # All rights reserved.
4
- # See LICENSE for permissions.
5
-
1
+ # $Id: mb-discid.rb 82 2007-06-05 00:21:51Z phw $
2
+ #
6
3
  # Just a helper file to allow loading the MB-DiscID library with
7
4
  # <tt>require 'mb-discid'</tt>, which is the only recommended way
8
5
  # to load the library.
6
+ #
9
7
  # This file may even provide extensions to the library in the future
10
8
  # to avoid to have to write everything in C.
9
+ #
10
+ # Author:: Philipp Wolfer (mailto:phw@rubyforge.org)
11
+ # Copyright:: Copyright (c) 2007, Philipp Wolfer
12
+ # License:: MB-DiscID is free software distributed under a BSD style license.
13
+ # See LICENSE[file:../LICENSE.html] for permissions.
11
14
 
12
15
  require 'MB_DiscID.so'
16
+
17
+ module MusicBrainz
18
+
19
+ # Class to calculate a MusicBrainz DiscID from an audio CD in the drive.
20
+ #
21
+ # This is basically an interface to the libdiscid C library
22
+ # (http://musicbrainz.org/doc/libdiscid).
23
+ #
24
+ # == Usage
25
+ # === Basic Usage:
26
+ #
27
+ # require 'mb-discid'
28
+ #
29
+ # # Create a new DiscID object
30
+ # disc = MusicBrainz::DiscID.new
31
+ #
32
+ # # Read the TOC from the default device.
33
+ # # An audio CD must be inserted in the drive. An exception will be thrown
34
+ # # if the CD can't be read.
35
+ # disc.read
36
+ #
37
+ # # Print information about the disc:
38
+ # print <<EOF
39
+ # DiscID : #{disc.id}
40
+ # Submit via : #{disc.submission_url}
41
+ # FreeDB ID : #{disc.freedb_id}
42
+ # First track: #{disc.first_track_num}
43
+ # Last track : #{disc.last_track_num}
44
+ # Sectors : #{disc.sectors}
45
+ # EOF
46
+ #
47
+ # # Print information about individual tracks:
48
+ # track = disc.first_track_num
49
+ # disc.tracks do |offset, length|
50
+ # puts "Track #{track}: Offset #{offset}, Length #{length}"
51
+ # track += 1
52
+ # end
53
+ #
54
+ # === Specifying the device to read from:
55
+ #
56
+ # # Create a new DiscID object and read the disc in /dev/dvd:
57
+ # disc = MusicBrainz::DiscID.new
58
+ # disc.read('/dev/dvd')
59
+ #
60
+ # # Create a new DiscID object and directly read the disc in /dev/dvd:
61
+ # disc = MusicBrainz::DiscID.new('/dev/dvd')
62
+ #
63
+ # # Create a new DiscID object and directly read the disc in the platform's
64
+ # # default device:
65
+ # disc = MusicBrainz::DiscID.new(MusicBrainz::DiscID.default_device)
66
+ #
67
+ # === Calculating the DiscID for a given TOC
68
+ #
69
+ # disc = MusicBrainz::DiscID.new
70
+ #
71
+ # first_track = 1
72
+ # sectors = 224556
73
+ # track_offsets = [150, 9078, 13528, 34182, 53768, 70987, 96424,
74
+ # 118425, 136793, 159514, 179777, 198006]
75
+ #
76
+ # disc.put(first_track, sectors, track_offsets)
77
+ # puts disc.id # Should print "T_prJXQSrqbnH8OE.dgOKsHm5Uw-"
78
+ #
79
+ class DiscID
80
+
81
+ # DiscID to String conversion. Same as calling the method id.
82
+ def to_s
83
+ return id.to_s
84
+ end
85
+
86
+ end
87
+
88
+ end
@@ -0,0 +1,108 @@
1
+ # $Id: test_discid.rb 95 2007-07-04 20:47:24Z phw $
2
+ #
3
+ # Author:: Philipp Wolfer (mailto:phw@rubyforge.org)
4
+ # Copyright:: Copyright (c) 2007, Philipp Wolfer
5
+ # License:: RBrainz is free software distributed under a BSD style license.
6
+ # See LICENSE[file:../LICENSE.html] for permissions.
7
+
8
+ require 'test/unit'
9
+ require 'mb-discid'
10
+
11
+ # Helper class which can't be converted into a string.
12
+ class NotAString
13
+
14
+ private
15
+
16
+ def to_s
17
+ end
18
+
19
+ end
20
+
21
+ # Unit test for the MusicBrainz::DiscID class.
22
+ class TestDiscID < Test::Unit::TestCase
23
+
24
+ def setup
25
+ @fiction_disc_id = 'Wn8eRBtfLDfM0qjYPdxrz.Zjs_U-'
26
+ @fiction_first_track = 1
27
+ @fiction_last_track = 10
28
+ @fiction_sectors = 206535
29
+ @fiction_offsets = [150, 18901, 39738, 59557, 79152, 100126,
30
+ 124833, 147278, 166336, 182560]
31
+ @fiction_lengths = [18751, 20837, 19819, 19595, 20974,
32
+ 24707, 22445, 19058, 16224, 23975]
33
+ end
34
+
35
+ def teardown
36
+ end
37
+
38
+ # Test reading the disc id from a device.
39
+ # We would need some kind of small test data to do this.
40
+ #def test_read
41
+ # assert false, "Not implemented yet"
42
+ #end
43
+
44
+ # Test how read reacts on different arguments.
45
+ # Those reads should all fail, but they must never cause a segmentation fault.
46
+ def test_read_invalid_arguments
47
+ assert_raise(TypeError) {MusicBrainz::DiscID.new(NotAString.new)}
48
+ assert_raise(Exception) {MusicBrainz::DiscID.new(1)}
49
+ assert_raise(Exception) {MusicBrainz::DiscID.new('invalid_device')}
50
+ assert_raise(Exception) {MusicBrainz::DiscID.new(:invalid_device)}
51
+ assert_raise(ArgumentError) {MusicBrainz::DiscID.new(
52
+ MusicBrainz::DiscID.default_device,
53
+ 'second argument')}
54
+
55
+ disc = MusicBrainz::DiscID.new
56
+ assert_raise(TypeError) {disc.read(NotAString.new)}
57
+ assert_raise(Exception) {disc.read(1)}
58
+ assert_raise(Exception) {disc.read('invalid_device')}
59
+ assert_raise(Exception) {disc.read(:invalid_device)}
60
+ assert_raise(ArgumentError) {disc.read(MusicBrainz::DiscID.default_device,
61
+ 'second argument')}
62
+ end
63
+
64
+ # Test calculation of the disc id if the TOC information
65
+ # gets set by the put method.
66
+ # All attributes should be nil after a failure, even if there was a
67
+ # successfull put before.
68
+ def test_put
69
+ disc = MusicBrainz::DiscID.new
70
+ assert_equal nil, disc.id
71
+ assert_equal '', disc.to_s
72
+ assert_equal nil, disc.first_track_num
73
+ assert_equal nil, disc.last_track_num
74
+ assert_equal nil, disc.sectors
75
+ assert_equal nil, disc.tracks
76
+
77
+ # First erroneous put
78
+ assert_raise(Exception) {disc.put(-1, @fiction_sectors, @fiction_offsets)}
79
+ assert_equal nil, disc.id
80
+ assert_equal '', disc.to_s
81
+ assert_equal nil, disc.first_track_num
82
+ assert_equal nil, disc.last_track_num
83
+ assert_equal nil, disc.sectors
84
+ assert_equal nil, disc.tracks
85
+
86
+ # Second successfull put
87
+ assert_nothing_raised {disc.put(@fiction_first_track, @fiction_sectors,
88
+ @fiction_offsets)}
89
+ assert_equal @fiction_disc_id, disc.id
90
+ assert_equal @fiction_disc_id, disc.to_s
91
+ assert_equal @fiction_first_track, disc.first_track_num
92
+ assert_equal @fiction_last_track, disc.last_track_num
93
+ assert_equal @fiction_sectors, disc.sectors
94
+ assert_equal @fiction_offsets, disc.tracks.map{|t| t[0]}
95
+ assert_equal @fiction_lengths, disc.tracks.map{|t| t[1]}
96
+
97
+ # Third erroneous put
98
+ assert_raise(Exception) {disc.put(@fiction_first_track, @fiction_sectors,
99
+ Array.new(100, 1))}
100
+ assert_equal nil, disc.id
101
+ assert_equal '', disc.to_s
102
+ assert_equal nil, disc.first_track_num
103
+ assert_equal nil, disc.last_track_num
104
+ assert_equal nil, disc.sectors
105
+ assert_equal nil, disc.tracks
106
+ end
107
+
108
+ end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
3
3
  specification_version: 1
4
4
  name: mb-discid
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.1
7
- date: 2007-06-03 00:00:00 +02:00
6
+ version: 0.1.2
7
+ date: 2007-07-04 00:00:00 +02:00
8
8
  summary: Ruby bindings for libdiscid.
9
9
  require_paths:
10
10
  - lib
@@ -16,7 +16,7 @@ description: Ruby bindings for libdiscid. See http://musicbrainz.org/doc/libdisc
16
16
  autorequire: mb-discid
17
17
  default_executable:
18
18
  bindir: bin
19
- has_rdoc: false
19
+ has_rdoc: true
20
20
  required_ruby_version: !ruby/object:Gem::Version::Requirement
21
21
  requirements:
22
22
  - - ">"
@@ -33,16 +33,20 @@ files:
33
33
  - Rakefile
34
34
  - LICENSE
35
35
  - README
36
+ - CHANGES
36
37
  - examples/discid.rb
37
38
  - ext/mb_discid.c
38
39
  - ext/extconf.rb
39
40
  - lib/mb-discid.rb
41
+ - test/test_discid.rb
40
42
  test_files: []
41
43
 
42
44
  rdoc_options: []
43
45
 
44
- extra_rdoc_files: []
45
-
46
+ extra_rdoc_files:
47
+ - README
48
+ - LICENSE
49
+ - CHANGES
46
50
  executables: []
47
51
 
48
52
  extensions: