Skip to content

Instantly share code, notes, and snippets.

@tcocca
Created September 14, 2011 14:57
Show Gist options
  • Save tcocca/1216785 to your computer and use it in GitHub Desktop.
Save tcocca/1216785 to your computer and use it in GitHub Desktop.
require 'spec_helper'
inputs = [:path, :hash, :file, :tempfile, :stringio]
outputs = [:path, :file, :tempfile, :stringio, :nil]
def get_input(input_type, file_name = 'fields.pdf')
case input_type
when :path
path_to_pdf(file_name)
when :hash
{path_to_pdf(file_name) => nil}
when :file
File.new(path_to_pdf(file_name))
when :tempfile
t = Tempfile.new('specs')
t.write(File.read(path_to_pdf(file_name)))
t
when :stringio
StringIO.new(File.read(path_to_pdf(file_name)))
end
end
def get_output(output_type)
case output_type
when :path
path_to_pdf('output.spec')
when :file
File.new(path_to_pdf('output.spec'), 'w+')
when :tempfile
Tempfile.new('specs2')
when :stringio
StringIO.new()
when :nil
nil
end
end
def map_output_type(output_specified)
case output_specified
when :path
String
when :file
File
when :tempfile
Tempfile
when :stringio, :nil
StringIO
end
end
describe ActivePdftk::Wrapper do
before(:all) { @pdftk = ActivePdftk::Wrapper.new }
context "new" do
it "should instantiate the object." do
@pdftk.should be_an_instance_of(ActivePdftk::Wrapper)
end
it "should pass the defaults statements to the call instance." do
path = ActivePdftk::Call.new.locate_pdftk
@pdftk_opt = ActivePdftk::Wrapper.new(:path => path, :operation => {:fill_form => 'a.fdf'}, :options => { :flatten => false, :owner_pw => 'bar', :user_pw => 'baz', :encrypt => :'40bit'})
@pdftk_opt.default_statements.should == {:path => path, :operation => {:fill_form => 'a.fdf'}, :options => { :flatten => false, :owner_pw => 'bar', :user_pw => 'baz', :encrypt => :'40bit'}}
end
end
shared_examples "a working command" do
it "should return a #{@output.nil? ? StringIO : @output.class}" do
@call_output.should be_kind_of(@output.nil? ? StringIO : @output.class)
end
it "should return expected data" do
case @call_output
when String then File.new(@call_output).read.should == @example_expect
else
#Debugging only
@call_output.rewind
puts @call_output.read.inspect
puts @example_expect.inspect
# ====================
@call_output.rewind
@call_output.read.should == @example_expect
end
end
after(:all) do
case @output
when String then File.unlink(@output)
when File, Tempfile then File.unlink(@output.path)
end
@call_output = nil
end
end
inputs.each do |input_type|
outputs.each do |output_type|
context "(Input:#{input_type}|Output:#{output_type})" do
before :each do
@input = get_input(input_type)
@input.rewind rescue nil # rewind if possible.
@output = get_output(output_type)
end
describe "#fill_form from fdf" do
it_behaves_like "a working command" do
before(:all) do
@example_expect = File.new(path_to_pdf('fields.fill_form.pdf')).read
@call_output = @pdftk.fill_form(@input, path_to_pdf('fields.fdf.spec'), :output => @output)
end
end
end
describe "#fill_form from xfdf" do
it_behaves_like "a working command" do
before(:all) do
@example_expect = File.new(path_to_pdf('fields.fill_form.pdf')).read
@call_output = @pdftk.fill_form(@input, path_to_pdf('fields.xfdf.spec'), :output => @output)
end
end
end
end
end # each outputs
end # each inputs
end # Wrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment