Skip to content

Commit

Permalink
Edited cups.c so #state returns a Symbol instead of a String. Updated…
Browse files Browse the repository at this point in the history
… tests accordingly.
  • Loading branch information
ecin committed Mar 15, 2010
1 parent ff8563d commit a8b4c34
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 25 deletions.
24 changes: 12 additions & 12 deletions ext/cups.c
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down
34 changes: 21 additions & 13 deletions test/cups_test.rb
@@ -1,10 +1,18 @@
$LOAD_PATH.unshift File.expand_path( File.dirname(__FILE__) )

require "cups"
require "test/unit"

# The tests which don't make use of mocking go on the operating assumption that you have
# 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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -74,39 +82,39 @@ 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
assert_nil Cups.options_for("bollocks_printer")
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

Expand All @@ -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

0 comments on commit a8b4c34

Please sign in to comment.