From a8b4c342a5d4cefdc281e04482fb712d0453edbc Mon Sep 17 00:00:00 2001 From: ecin Date: Mon, 15 Mar 2010 15:02:47 -0400 Subject: [PATCH] Edited cups.c so #state returns a Symbol instead of a String. Updated tests accordingly. --- ext/cups.c | 24 ++++++++++++------------ test/cups_test.rb | 34 +++++++++++++++++++++------------- 2 files changed, 33 insertions(+), 25 deletions(-) diff --git a/ext/cups.c b/ext/cups.c index 9a8dc2b..e33f4cb 100644 --- a/ext/cups.c +++ b/ext/cups.c @@ -6,35 +6,35 @@ cups_dest_t *dests, *dest; VALUE rubyCups, printJobs; // Need to abstract this out of cups.c -char* ipp_state_to_string(int state) +VALUE ipp_state_to_symbol(int state) { - char *jstate; + VALUE jstate; switch (state) { case IPP_JOB_PENDING : - jstate = "Pending..."; + jstate = ID2SYM(rb_intern("pending")); break; case IPP_JOB_HELD : - jstate = "Held"; + jstate = ID2SYM(rb_intern("held")); break; case IPP_JOB_PROCESSING : - jstate = "Processing..."; + jstate = ID2SYM(rb_intern("processing")); break; case IPP_JOB_STOPPED : - jstate = "Stopped"; + jstate = ID2SYM(rb_intern("stopped")); break; case IPP_JOB_CANCELED : - jstate = "Cancelled"; + jstate = ID2SYM(rb_intern("cancelled")); break; case IPP_JOB_ABORTED : - jstate = "Aborted"; + jstate = ID2SYM(rb_intern("aborted")); break; case IPP_JOB_COMPLETED : - jstate = "Completed"; + jstate = ID2SYM(rb_intern("completed")); break; default: - jstate = "Unknown Job Code..."; + jstate = ID2SYM(rb_intern("unknown")); } return jstate; } @@ -254,7 +254,7 @@ static VALUE cups_get_job_state(VALUE self) // Free job array cupsFreeJobs(num_jobs, jobs); - jstate = rb_str_new2(ipp_state_to_string(job_state)); + jstate = ipp_state_to_symbol(job_state); return jstate; } @@ -333,7 +333,7 @@ static VALUE cups_get_jobs(VALUE self, VALUE printer) juser = rb_str_new2(jobs[i].user); jsize = INT2NUM(jobs[i].size); jformat = rb_str_new2(jobs[i].format); - jstate = rb_str_new2(ipp_state_to_string(jobs[i].state)); + jstate = ID2SYM(ipp_state_to_symbol(jobs[i].state)); rb_hash_aset(job_info_hash, ID2SYM(rb_intern("title")), jtitle); rb_hash_aset(job_info_hash, ID2SYM(rb_intern("submitted_by")), juser); diff --git a/test/cups_test.rb b/test/cups_test.rb index dd34dd0..a95828b 100644 --- a/test/cups_test.rb +++ b/test/cups_test.rb @@ -1,3 +1,5 @@ +$LOAD_PATH.unshift File.expand_path( File.dirname(__FILE__) ) + require "cups" require "test/unit" @@ -5,6 +7,12 @@ # the CUPS command line utilities installed and in your $PATH class CupsTest < Test::Unit::TestCase + + def setup + @printer = Cups.show_destinations.select {|p| p =~ /pdf/i}.first + raise "Can't fine a PDF printer to run tests with." unless @printer + end + def test_same_printers_returned lplist = `lpstat -a`.split("\n").map { |pr| pr.split(' ').first } cups_destinations = Cups.show_destinations @@ -18,7 +26,7 @@ def test_can_instantiate_print_job_object_with_correct_args end assert_nothing_raised do - Cups::PrintJob.new("/path", "PDF_Printer") + Cups::PrintJob.new("/path", @printer) Cups::PrintJob.new("/path") end end @@ -50,7 +58,7 @@ def test_we_cant_print_nonexistent_files end def test_print_job_cancellation - pj = Cups::PrintJob.new(sample, "soft_class") + pj = Cups::PrintJob.new(sample, @printer) pj.print assert_not_nil pj.job_id assert_equal pj.cancel, true @@ -62,9 +70,9 @@ def test_all_jobs_returns_hash end def test_all_jobs_hash_contains_info_hash - pj = Cups::PrintJob.new(sample, "PDF_Printer") + pj = Cups::PrintJob.new(sample, @printer) pj.print - info = Cups.all_jobs("PDF_Printer")[pj.job_id] + info = Cups.all_jobs(@printer)[pj.job_id] assert info.is_a?(Hash) assert info.keys.all?{|key| [:title, :format, :submitted_by, :state, :size].include?(key)} end @@ -74,7 +82,7 @@ def test_dest_list_returns_array end def test_dest_options_returns_hash_if_real - assert Cups.options_for("PDF_Printer").is_a?(Hash) + assert Cups.options_for(@printer).is_a?(Hash) end def test_dest_options_returns_nil_if_not_real @@ -82,31 +90,31 @@ def test_dest_options_returns_nil_if_not_real end def test_job_failed_boolean - pj = Cups::PrintJob.new(sample, "soft_class") + pj = Cups::PrintJob.new(sample, @printer) pj.print pj.cancel assert !pj.failed? end def test_returns_failure_string_on_cancellation - pj = Cups::PrintJob.new(blank_sample, "PDF_Printer") + pj = Cups::PrintJob.new(blank_sample, @printer) pj.print - + assert pj.job_id == 0 # Failed jobs have an ID of zero assert pj.failed? - assert pj.error_reason.is_a?(String) + assert pj.error_reason.is_a?(Symbol) end def test_job_state_string - pj = Cups::PrintJob.new(sample, "soft_class") + pj = Cups::PrintJob.new(sample, @printer) assert_nil pj.state # A job can't have a state if it hasn't been assigned a job_id yet assert !pj.completed? pj.print pj.cancel - assert pj.state == "Cancelled" + assert pj.state == :cancelled assert !pj.completed? end @@ -120,10 +128,10 @@ def test_print_job_attributes private def sample - "#{Dir.pwd}/sample.txt" + "#{File.dirname(__FILE__)}/sample.txt" end def blank_sample - "#{Dir.pwd}/sample_blank.txt" + "#{File.dirname(__FILE__)}/sample_blank.txt" end end