Skip to content

Commit

Permalink
Wrapper :
Browse files Browse the repository at this point in the history
Fix 1.9.2 string encoding issue
Remove duplicate tests (from the end of the file)
Improve #burst test coverage
  • Loading branch information
elmatou committed Nov 7, 2011
1 parent 8d92496 commit 66a0ce5
Showing 1 changed file with 53 additions and 98 deletions.
151 changes: 53 additions & 98 deletions spec/active_pdftk/wrapper_spec.rb
Expand Up @@ -3,20 +3,20 @@
inputs = [:path, :hash, :file, :tempfile, :stringio] inputs = [:path, :hash, :file, :tempfile, :stringio]
outputs = [:path, :file, :tempfile, :stringio, :nil] outputs = [:path, :file, :tempfile, :stringio, :nil]


def get_input(input_type, file_name = nil) def get_input(input_type, file_name = 'fields.pdf')
case input_type case input_type
when :path when :path
path_to_pdf(file_name || 'fields.pdf') path_to_pdf(file_name)
when :hash when :hash
{path_to_pdf(file_name || 'fields.pdf') => nil} {path_to_pdf(file_name) => nil}
when :file when :file
File.new(path_to_pdf(file_name || 'fields.pdf')) File.new(path_to_pdf(file_name))
when :tempfile when :tempfile
t = Tempfile.new('specs') t = Tempfile.new('input.spec')
t.write(File.read(path_to_pdf(file_name || 'fields.pdf'))) t.write(File.read(path_to_pdf(file_name)))
t t
when :stringio when :stringio
StringIO.new(File.read(path_to_pdf(file_name || 'fields.pdf'))) StringIO.new(File.read(path_to_pdf(file_name)))
end end
end end


Expand All @@ -27,7 +27,7 @@ def get_output(output_type)
when :file when :file
File.new(path_to_pdf('output.spec'), 'w+') File.new(path_to_pdf('output.spec'), 'w+')
when :tempfile when :tempfile
Tempfile.new('specs2') Tempfile.new('output.spec')
when :stringio when :stringio
StringIO.new() StringIO.new()
when :nil when :nil
Expand Down Expand Up @@ -56,6 +56,22 @@ def remove_output(output)
end end
end end


def open_or_rewind(target)
if target.is_a? String
File.new(target).read
else
target.rewind if target.respond_to? :rewind
target.read
end
end

def cleanup_file_content(text)
text.force_encoding('ASCII-8BIT') if text.respond_to? :force_encoding
text.gsub!(/\(D\:.*\)/, '')
text.gsub!(/\[<[a-z0-9]*><[a-z0-9]*>\]/, '')
text
end

describe ActivePdftk::Wrapper do describe ActivePdftk::Wrapper do
before(:all) { @pdftk = ActivePdftk::Wrapper.new } before(:all) { @pdftk = ActivePdftk::Wrapper.new }


Expand All @@ -77,12 +93,7 @@ def remove_output(output)
end end


it "should return expected data" do it "should return expected data" do
if @call_output.is_a? String open_or_rewind(@call_output).should == @example_expect
File.new(@call_output).read.should == @example_expect
else
@call_output.rewind
@call_output.read.should == @example_expect
end
end end


after(:each) { remove_output(@call_output) } after(:each) { remove_output(@call_output) }
Expand All @@ -94,18 +105,11 @@ def remove_output(output)
end end


it "should return expected data" do it "should return expected data" do
@example_expect.gsub!(/\(D\:.*\)/, '') cleanup_file_content(@example_expect)
@example_expect.gsub!(/\[<[a-z0-9]*><[a-z0-9]*>\]/, '')
if @call_output.is_a?(String) text = open_or_rewind(@call_output)
text = File.read(@call_output) cleanup_file_content(text)
text.gsub!(/\(D\:.*\)/, '')
text.gsub!(/\[<[a-z0-9]*><[a-z0-9]*>\]/, '')
else
@call_output.rewind
text = @call_output.read
text.gsub!(/\(D\:.*\)/, '')
text.gsub!(/\[<[a-z0-9]*><[a-z0-9]*>\]/, '')
end
text.should == @example_expect text.should == @example_expect
end end


Expand Down Expand Up @@ -191,6 +195,7 @@ def remove_output(output)
total_size = input_size + @attachment_size total_size = input_size + @attachment_size
output_size.should >= total_size output_size.should >= total_size
end end

it "should output the correct type" do it "should output the correct type" do
@call_output.should be_kind_of(map_output_type(output_type)) @call_output.should be_kind_of(map_output_type(output_type))
end end
Expand All @@ -203,6 +208,7 @@ def remove_output(output)
@output = path_to_pdf('') @output = path_to_pdf('')
@call_output = @pdftk.unpack_files(@input, @output) @call_output = @pdftk.unpack_files(@input, @output)
end end

it "should unpack the files" do it "should unpack the files" do
@call_output.should == @output @call_output.should == @output
File.unlink(path_to_pdf('unpacked_file.txt')).should == 1 File.unlink(path_to_pdf('unpacked_file.txt')).should == 1
Expand All @@ -215,6 +221,7 @@ def remove_output(output)
@input.rewind rescue nil # rewind if possible. @input.rewind rescue nil # rewind if possible.
@call_output = @pdftk.unpack_files(@input, @output) @call_output = @pdftk.unpack_files(@input, @output)
end end

it "should unpack the files" do it "should unpack the files" do
@call_output.should == Dir.tmpdir @call_output.should == Dir.tmpdir
File.unlink(File.join(Dir.tmpdir, 'unpacked_file.txt')).should == 1 File.unlink(File.join(Dir.tmpdir, 'unpacked_file.txt')).should == 1
Expand Down Expand Up @@ -257,11 +264,12 @@ def remove_output(output)
before(:each) do before(:each) do
@input = get_input(input_type, 'a.pdf') @input = get_input(input_type, 'a.pdf')
@input.rewind rescue nil # rewind if possible. @input.rewind rescue nil # rewind if possible.
@output = path_to_pdf('pg_%04d.pdf')
@call_output = @pdftk.burst(@input, :output => @output)
end end

it "should file into single pages" do it "should file into single pages" do
@call_output.should == @output output = path_to_pdf('pg_%04d.pdf')

@pdftk.burst(@input, :output => output).should == output
File.unlink(path_to_pdf('pg_0001.pdf')).should == 1 File.unlink(path_to_pdf('pg_0001.pdf')).should == 1
File.unlink(path_to_pdf('pg_0002.pdf')).should == 1 File.unlink(path_to_pdf('pg_0002.pdf')).should == 1
File.unlink(path_to_pdf('pg_0003.pdf')).should == 1 File.unlink(path_to_pdf('pg_0003.pdf')).should == 1
Expand All @@ -272,14 +280,21 @@ def remove_output(output)
before(:each) do before(:each) do
@input = get_input(input_type, 'a.pdf') @input = get_input(input_type, 'a.pdf')
@input.rewind rescue nil # rewind if possible. @input.rewind rescue nil # rewind if possible.
@call_output = @pdftk.burst(@input, :output => @output)
end end

it "should file into single pages" do it "should file into single pages" do
@call_output.should == Dir.tmpdir @pdftk.burst(@input).should == Dir.tmpdir
File.unlink(File.join(Dir.tmpdir, 'pg_0001.pdf')).should == 1 File.unlink(File.join(Dir.tmpdir, 'pg_0001.pdf')).should == 1
File.unlink(File.join(Dir.tmpdir, 'pg_0002.pdf')).should == 1 File.unlink(File.join(Dir.tmpdir, 'pg_0002.pdf')).should == 1
File.unlink(File.join(Dir.tmpdir, 'pg_0003.pdf')).should == 1 File.unlink(File.join(Dir.tmpdir, 'pg_0003.pdf')).should == 1
end end

it "should put a file in the system tmpdir when no output location given but a page name format given" do
@pdftk.burst(@input, :output => 'page_%02d.pdf').should == 'page_%02d.pdf'
File.unlink(File.join(Dir.tmpdir, 'page_01.pdf')).should == 1
File.unlink(File.join(Dir.tmpdir, 'page_02.pdf')).should == 1
File.unlink(File.join(Dir.tmpdir, 'page_03.pdf')).should == 1
end
end end
end end


Expand All @@ -288,72 +303,12 @@ def remove_output(output)


context "burst" do context "burst" do
it "should call #pdtk on @call" do it "should call #pdtk on @call" do
ActivePdftk::Call.any_instance.should_receive(:pdftk).with({:input => path_to_pdf('fields.pdf'), :operation => :burst}) pending "integration of Call receiver tests in looping strategy for all operations."
@pdftk.burst(path_to_pdf('fields.pdf')) #ActivePdftk::Call.any_instance.should_receive(:pdftk).with({:input => path_to_pdf('fields.pdf'), :operation => :burst})
@pdftk = ActivePdftk::Wrapper.new #@pdftk.burst(path_to_pdf('fields.pdf'))
ActivePdftk::Call.any_instance.should_receive(:pdftk).with({:input => path_to_pdf('fields.pdf'), :operation => :burst, :options => {:encrypt => :'40bit'}}) #@pdftk = ActivePdftk::Wrapper.new
@pdftk.burst(path_to_pdf('fields.pdf'), :options => {:encrypt => :'40bit'}) #ActivePdftk::Call.any_instance.should_receive(:pdftk).with({:input => path_to_pdf('fields.pdf'), :operation => :burst, :options => {:encrypt => :'40bit'}})
end #@pdftk.burst(path_to_pdf('fields.pdf'), :options => {:encrypt => :'40bit'})

it "should put a file in the system tmpdir when no output location given" do
@pdftk = ActivePdftk::Wrapper.new
@pdftk.burst(path_to_pdf('fields.pdf'))
File.unlink(File.join(Dir.tmpdir, 'pg_0001.pdf')).should == 1
end

it "should put a file in the system tmpdir when no output location given but a page name format given" do
@pdftk = ActivePdftk::Wrapper.new
@pdftk.burst(path_to_pdf('fields.pdf'), :output => 'page_%02d.pdf')
File.unlink(File.join(Dir.tmpdir, 'page_01.pdf')).should == 1
end

it "should put a file in the specified path" do
@pdftk = ActivePdftk::Wrapper.new
@pdftk.burst(path_to_pdf('fields.pdf'), :output => path_to_pdf('page_%02d.pdf').to_s)
File.unlink(path_to_pdf('page_01.pdf')).should == 1
end
end

context "cat" do
it "should call #pdftk on @call" do
ActivePdftk::Call.any_instance.should_receive(:pdftk).with({:input => {'a.pdf' => 'foo', 'b.pdf' => nil}, :operation => {:cat => [{:pdf => 'a.pdf'}, {:pdf => 'b.pdf', :start => 1, :end => 'end', :orientation => 'N', :pages => 'even'}]}})
@pdftk.cat([{:pdf => 'a.pdf', :pass => 'foo'}, {:pdf => 'b.pdf', :start => 1, :end => 'end', :orientation => 'N', :pages => 'even'}])
end

it "should output the generated pdf" do
@pdftk = ActivePdftk::Wrapper.new
@pdftk.cat([{:pdf => path_to_pdf('a.pdf'), :pass => 'foo'}, {:pdf => path_to_pdf('b.pdf'), :start => 1, :end => 'end', :orientation => 'N', :pages => 'even'}], :output => path_to_pdf('cat.pdf'))
File.unlink(path_to_pdf('cat.pdf')).should == 1
end
end

context "shuffle" do
it "should call #pdftk on @call" do
ActivePdftk::Call.any_instance.should_receive(:pdftk).with({:input => {'a.pdf' => 'foo', 'b.pdf' => nil}, :operation => {:shuffle => [{:pdf => 'a.pdf'}, {:pdf => 'b.pdf', :start => 1, :end => 'end', :orientation => 'N', :pages => 'even'}]}})
@pdftk.shuffle([{:pdf => 'a.pdf', :pass => 'foo'}, {:pdf => 'b.pdf', :start => 1, :end => 'end', :orientation => 'N', :pages => 'even'}])
end

it "should output the generated pdf" do
@pdftk = ActivePdftk::Wrapper.new
@pdftk.shuffle([{:pdf => path_to_pdf('a.pdf'), :pass => 'foo'}, {:pdf => path_to_pdf('b.pdf'), :start => 1, :end => 'end', :orientation => 'N', :pages => 'even'}], :output => path_to_pdf('shuffle.pdf'))
File.unlink(path_to_pdf('shuffle.pdf')).should == 1
end
end

context "unpack_files" do
it "should return Dir.tmpdir" do
@pdftk = ActivePdftk::Wrapper.new
@pdftk.attach_files(path_to_pdf('fields.pdf'), [path_to_pdf('attached_file.txt')], :output => path_to_pdf('attached.pdf'))
@pdftk.unpack_files(path_to_pdf('attached.pdf')).should == Dir.tmpdir
File.unlink(path_to_pdf('attached.pdf')).should == 1
end

it "should return the specified output directory" do
@pdftk = ActivePdftk::Wrapper.new
@pdftk.attach_files(path_to_pdf('fields.pdf'), [path_to_pdf('attached_file.txt')], :output => path_to_pdf('attached.pdf'))
@pdftk.unpack_files(path_to_pdf('attached.pdf'), path_to_pdf(nil)).should == path_to_pdf(nil)
File.unlink(path_to_pdf('attached.pdf')).should == 1
end end
end end

end # Wrapper
end # Wrapper

0 comments on commit 66a0ce5

Please sign in to comment.