Browse files

Setup Genesis methods. (generate expectation files for spec)

add :genesis => true to a describe, context, example to generate corresponding files
  • Loading branch information...
1 parent 5d49a45 commit ea98477b419f965918781059f819861eac1425cd @elmatou committed Nov 17, 2011
View
8 lib/active_pdftk/wrapper.rb
@@ -123,6 +123,10 @@ def background(template, background, options = {})
@call.pdftk(options.merge(command_options))
end
+ def multibackground(template, background, options = {})
+ background(template, background, options.merge(:multi => true))
+ end
+
# Add a stamp/foreground image/file to the template pdf.
# @param [String, File, Tempfile, StringIO] template is the file on which the form is based.
# @param [String] stamp the location of the file you wish to stamp onto the template file.
@@ -139,6 +143,10 @@ def stamp(template, stamp, options = {})
@call.pdftk(options.merge(command_options))
end
+ def multistamp(template, stamp, options = {})
+ stamp(template, stamp, options.merge(:multi => true))
+ end
+
# Dump the field data info from the template file.
# @param [String, File, Tempfile, StringIO] template is the file on which the form is based.
# @param [Hash] options is a hash containing statements for the wrapper.
View
42 spec/active_pdftk/call_spec.rb
@@ -53,8 +53,8 @@
end
it "should convert input" do
- @pdftk.set_cmd(:input => 'spec.a.pdf').should == "spec.a.pdf output -"
- inputs = {'spec.a.pdf' => 'foo', 'spec.b.pdf' => 'bar', 'spec.c.pdf' => nil}
+ @pdftk.set_cmd(:input => 'multi.pdf').should == "multi.pdf output -"
+ inputs = {'multi.pdf' => 'foo', 'poly.pdf' => 'bar', 'spec.c.pdf' => nil}
reconstruct_inputs(@pdftk.set_cmd(:input => inputs)).should == inputs
@pdftk.set_cmd(:input => File.new(path_to_pdf('spec.fields.pdf'))).should == "- output -"
@pdftk.set_cmd(:input => Tempfile.new('specs')).should == "- output -"
@@ -78,7 +78,7 @@
end
it "should convert output" do
- @pdftk.set_cmd(:output => 'spec.a.pdf').should == "output spec.a.pdf"
+ @pdftk.set_cmd(:output => 'multi.pdf').should == "output multi.pdf"
@pdftk.set_cmd(:output => File.new(path_to_pdf('spec.fields.pdf'))).should == "output -"
@pdftk.set_cmd(:output => Tempfile.new('specs')).should == "output -"
@pdftk.set_cmd(:output => StringIO.new('specs')).should == "output -"
@@ -101,43 +101,43 @@
it "should set the operation with arguments" do
cat_options = {
- :input => {'spec.a.pdf' => nil, 'spec.b.pdf' => nil, 'spec.c.pdf' => nil},
+ :input => {'multi.pdf' => nil, 'poly.pdf' => nil, 'spec.c.pdf' => nil},
:operation => {
:cat => [
- {:start => 1, :end => 'end', :pdf => 'spec.a.pdf'},
- {:pdf => 'spec.b.pdf', :start => 12, :end => 16, :orientation => 'E', :pages => 'even'}
+ {:start => 1, :end => 'end', :pdf => 'multi.pdf'},
+ {:pdf => 'poly.pdf', :start => 12, :end => 16, :orientation => 'E', :pages => 'even'}
]
}
}
cmd = @pdftk.set_cmd(cat_options)
input_pdfs = cmd.split(' cat ').first
input_map = map_inputs(input_pdfs)
- cmd.should == "#{input_pdfs} cat #{input_map['spec.a.pdf']}1-end #{input_map['spec.b.pdf']}12-16evenE output -"
+ cmd.should == "#{input_pdfs} cat #{input_map['multi.pdf']}1-end #{input_map['poly.pdf']}12-16evenE output -"
- @pdftk.set_cmd(:input => {'spec.a.pdf' => nil}, :operation => {:cat => [{:pdf => 'spec.a.pdf', :start => 1, :end => 'end'}]}).should == "B=spec.a.pdf cat B1-end output -"
- @pdftk.set_cmd(:input => {'spec.a.pdf' => nil}, :operation => {:cat => [{:pdf => 'spec.a.pdf'}]}).should == "B=spec.a.pdf cat B output -"
+ @pdftk.set_cmd(:input => {'multi.pdf' => nil}, :operation => {:cat => [{:pdf => 'multi.pdf', :start => 1, :end => 'end'}]}).should == "B=multi.pdf cat B1-end output -"
+ @pdftk.set_cmd(:input => {'multi.pdf' => nil}, :operation => {:cat => [{:pdf => 'multi.pdf'}]}).should == "B=multi.pdf cat B output -"
- cat_options = {:input => {'spec.a.pdf' => nil, 'spec.b.pdf' => nil}, :operation => {:cat => [{:pdf => 'spec.a.pdf'}, {:pdf => 'spec.b.pdf'}]}}
+ cat_options = {:input => {'multi.pdf' => nil, 'poly.pdf' => nil}, :operation => {:cat => [{:pdf => 'multi.pdf'}, {:pdf => 'poly.pdf'}]}}
cmd = @pdftk.set_cmd(cat_options)
input_pdfs = cmd.split(' cat ').first
input_map = map_inputs(input_pdfs)
- cmd.should == "#{input_pdfs} cat #{input_map['spec.a.pdf']} #{input_map['spec.b.pdf']} output -"
+ cmd.should == "#{input_pdfs} cat #{input_map['multi.pdf']} #{input_map['poly.pdf']} output -"
- @pdftk.set_cmd(:input => 'spec.a.pdf', :operation => {:cat => [{:pdf => 'spec.a.pdf', :start => 1, :end => 'end'}]}).should == "spec.a.pdf cat 1-end output -"
- @pdftk.set_cmd(:input => 'spec.a.pdf', :operation => {:cat => [{:pdf => 'spec.a.pdf', :end => 'end'}]}).should == "spec.a.pdf cat 1-end output -"
- @pdftk.set_cmd(:input => 'spec.a.pdf', :operation => {:cat => [{:pdf => 'spec.a.pdf', :start => '4', :orientation => 'N'}]}).should == "spec.a.pdf cat 4N output -"
+ @pdftk.set_cmd(:input => 'multi.pdf', :operation => {:cat => [{:pdf => 'multi.pdf', :start => 1, :end => 'end'}]}).should == "multi.pdf cat 1-end output -"
+ @pdftk.set_cmd(:input => 'multi.pdf', :operation => {:cat => [{:pdf => 'multi.pdf', :end => 'end'}]}).should == "multi.pdf cat 1-end output -"
+ @pdftk.set_cmd(:input => 'multi.pdf', :operation => {:cat => [{:pdf => 'multi.pdf', :start => '4', :orientation => 'N'}]}).should == "multi.pdf cat 4N output -"
end
it "should raise missing input errors" do
- expect { @pdftk.set_cmd(:input => {'spec.a.pdf' => nil}, :operation => {:cat => [{:pdf => 'spec.a.pdf'}, {:pdf => 'spec.b.pdf'}]}) }.to raise_error(ActivePdftk::MissingInput)
- expect { @pdftk.set_cmd(:input => 'spec.a.pdf', :operation => {:cat => [{:pdf => 'spec.a.pdf'}, {:pdf => 'spec.b.pdf'}]}) }.to raise_error(ActivePdftk::MissingInput)
- expect { @pdftk.set_cmd(:input => {'spec.a.pdf' => nil, 'spec.c.pdf' => 'foo'}, :operation => {:cat => [{:pdf => 'spec.a.pdf'}, {:pdf => 'spec.b.pdf'}]}) }.to raise_error(ActivePdftk::MissingInput, "Missing Input file, `spec.b.pdf`")
+ expect { @pdftk.set_cmd(:input => {'multi.pdf' => nil}, :operation => {:cat => [{:pdf => 'multi.pdf'}, {:pdf => 'poly.pdf'}]}) }.to raise_error(ActivePdftk::MissingInput)
+ expect { @pdftk.set_cmd(:input => 'multi.pdf', :operation => {:cat => [{:pdf => 'multi.pdf'}, {:pdf => 'poly.pdf'}]}) }.to raise_error(ActivePdftk::MissingInput)
+ expect { @pdftk.set_cmd(:input => {'multi.pdf' => nil, 'spec.c.pdf' => 'foo'}, :operation => {:cat => [{:pdf => 'multi.pdf'}, {:pdf => 'poly.pdf'}]}) }.to raise_error(ActivePdftk::MissingInput, "Missing Input file, `poly.pdf`")
end
it "should raise an invalid options error" do
- expect { @pdftk.set_cmd(:input => {'spec.a.pdf' => nil}, :operation => {:cat => nil}) }.to raise_error(ActivePdftk::InvalidOptions, "Invalid options passed to the command, `cat`, please see `$: pdftk --help`")
- expect { @pdftk.set_cmd(:input => {'spec.a.pdf' => nil}, :operation => {:cat => []}) }.to raise_error(ActivePdftk::InvalidOptions, "Invalid options passed to the command, `cat`, please see `$: pdftk --help`")
- expect { @pdftk.set_cmd(:input => {'spec.a.pdf' => nil}, :operation => {:cat => "test"}) }.to raise_error(ActivePdftk::InvalidOptions, "Invalid options passed to the command, `cat`, please see `$: pdftk --help`")
+ expect { @pdftk.set_cmd(:input => {'multi.pdf' => nil}, :operation => {:cat => nil}) }.to raise_error(ActivePdftk::InvalidOptions, "Invalid options passed to the command, `cat`, please see `$: pdftk --help`")
+ expect { @pdftk.set_cmd(:input => {'multi.pdf' => nil}, :operation => {:cat => []}) }.to raise_error(ActivePdftk::InvalidOptions, "Invalid options passed to the command, `cat`, please see `$: pdftk --help`")
+ expect { @pdftk.set_cmd(:input => {'multi.pdf' => nil}, :operation => {:cat => "test"}) }.to raise_error(ActivePdftk::InvalidOptions, "Invalid options passed to the command, `cat`, please see `$: pdftk --help`")
end
end
@@ -203,7 +203,7 @@
end
it "should raise a ActivePdftk::CommandError exception" do
- expect{ @pdftk.pdftk(:input => {'spec.a.pdf' => 'foo', 'spec.b.pdf' => 'bar', 'spec.c.pdf' => nil}, :operation => {}, :output => 'out.pdf',:options => { :flatten => false, :owner_pw => 'bar', :user_pw => 'baz', :encrypt => :'40bit'}) }.to raise_error(ActivePdftk::CommandError)
+ expect{ @pdftk.pdftk(:input => {'multi.pdf' => 'foo', 'poly.pdf' => 'bar', 'spec.c.pdf' => nil}, :operation => {}, :output => 'out.pdf',:options => { :flatten => false, :owner_pw => 'bar', :user_pw => 'baz', :encrypt => :'40bit'}) }.to raise_error(ActivePdftk::CommandError)
end
context "#burst" do
View
89 spec/active_pdftk/wrapper_spec.rb
@@ -48,13 +48,6 @@ def map_output_type(output_specified)
end
end
-def cleanup_file_content(text)
- text.force_encoding('ASCII-8BIT') if text.respond_to? :force_encoding # PDF embed some binary data breaking gsub with ruby 1.9.2
- text.gsub!(/\(D\:.*\)/, '') # Remove dates ex: /CreationDate (D:20111106104455-05'00')
- text.gsub!(/\/ID \[<\w*><\w*>\]/, '') # Remove ID values ex: /ID [<4ba02a4cf55b1fc842299e6f01eb838e><33bec7dc37839cadf7ab76f3be4d4306>]
- text
-end
-
describe ActivePdftk::Wrapper do
before(:all) { @pdftk = ActivePdftk::Wrapper.new }
@@ -76,7 +69,11 @@ def cleanup_file_content(text)
end
it "should return expected data" do
- @call_output.should have_the_content_of(@example_expect)
+ if example.metadata[:genesis] && @output.is_a?(String)
+ FileUtils.copy_entry(@output, @example_expect.to_s, true, false, true)
+ else
+ @call_output.should have_the_content_of(@example_expect)
+ end
end
after(:each) { remove_output(@call_output) }
@@ -88,10 +85,11 @@ def cleanup_file_content(text)
end
it "should return expected data" do
- cleanup_file_content(@example_expect)
- text = open_or_rewind(@call_output)
- cleanup_file_content(text)
- text.should == @example_expect
+ if example.metadata[:genesis] && @output.is_a?(String)
+ FileUtils.copy_entry(@output, @example_expect.to_s, true, false, true)
+ else
+ @call_output.should look_like_the_same_pdf_as(@example_expect)
+ end
end
after(:each) { remove_output(@call_output) }
@@ -205,69 +203,94 @@ def cleanup_file_content(text)
describe "#background" do
it_behaves_like "a working command" do
before(:all) { @example_expect = fixtures_path('background/expect.pdf') }
- before(:each) { @call_output = @pdftk.background(@input, path_to_pdf('spec.a.pdf'), :output => @output) }
+ before(:each) { @call_output = @pdftk.background(get_input(input_type, 'multi.pdf'), path_to_pdf('poly.pdf'), :output => @output) }
end
+ end
- pending "spec multibackground also"
+ describe "#multibackground" do
+ it_behaves_like "a working command" do
+ before(:all) { @example_expect = fixtures_path('multibackground/expect.pdf') }
+ before(:each) { @call_output = @pdftk.multibackground(get_input(input_type, 'multi.pdf'), path_to_pdf('poly.pdf'), :output => @output) }
+ end
end
describe "#stamp" do
it_behaves_like "a working command" do
before(:all) { @example_expect = fixtures_path('stamp/expect.pdf') }
- before(:each) { @call_output = @pdftk.stamp(@input, path_to_pdf('spec.a.pdf'), :output => @output) }
+ before(:each) { @call_output = @pdftk.stamp(get_input(input_type, 'multi.pdf'), path_to_pdf('poly.pdf'), :output => @output) }
end
+ end
- pending "check if the output is really a stamp & spec multistamp also"
+ describe "#multistamp" do
+ it_behaves_like "a working command" do
+ before(:all) { @example_expect = fixtures_path('multistamp/expect.pdf') }
+ before(:each) { @call_output = @pdftk.multistamp(get_input(input_type, 'multi.pdf'), path_to_pdf('poly.pdf'), :output => @output) }
+ end
end
describe "#cat" do
it_behaves_like "a combination command" do
- before(:all) { @example_expect = File.new(path_to_pdf('cat/expect.pdf')).read }
- before(:each) { @call_output = @pdftk.cat([{:pdf => path_to_pdf('spec.a.pdf')}, {:pdf => path_to_pdf('spec.b.pdf'), :start => 1, :end => 'end', :orientation => 'N', :pages => 'even'}], :output => @output) }
+ before(:all) { @example_expect = fixtures_path('cat/expect.pdf')}
+ before(:each) { @call_output = @pdftk.cat([{:pdf => path_to_pdf('multi.pdf')}, {:pdf => path_to_pdf('poly.pdf'), :start => 1, :end => 'end', :orientation => 'N', :pages => 'even'}], :output => @output) }
end
end
describe "#shuffle" do
it_behaves_like "a combination command" do
- before(:all) { @example_expect = File.new(path_to_pdf('shuffle/expect.pdf')).read }
- before(:each) { @call_output = @pdftk.shuffle([{:pdf => path_to_pdf('spec.a.pdf')}, {:pdf => path_to_pdf('spec.b.pdf'), :start => 1, :end => 'end', :orientation => 'N', :pages => 'even'}], :output => @output) }
+ before(:all) { @example_expect = fixtures_path('shuffle/expect.pdf')}
+ before(:each) { @call_output = @pdftk.shuffle([{:pdf => path_to_pdf('multi.pdf')}, {:pdf => path_to_pdf('poly.pdf'), :start => 1, :end => 'end', :orientation => 'N', :pages => 'even'}], :output => @output) }
end
end
describe "#burst" do
+ before(:all) { @example_expect = fixtures_path('burst/expect') }
+
context 'to path', :if => output_type == :path do
before(:each) do
- @input = get_input(input_type, 'spec.a.pdf')
+ @input = get_input(input_type, 'multi.pdf')
@input.rewind rescue nil # rewind if possible.
+
+ Dir.mkdir(out_path = fixtures_path('output'))
+ @output = Dir.new(out_path)
+
+ @call_output = @pdftk.burst(@input, :output => @output.path + '/pg_%04d.pdf')
end
+ after(:each) { FileUtils.remove_entry_secure @output.path }
+
it "should file into single pages" do
- output = path_to_pdf('burst/pg_%04d.pdf')
- @pdftk.burst(@input, :output => output).should == output
- File.unlink(path_to_pdf('burst/pg_0001.pdf')).should == 1
- File.unlink(path_to_pdf('burst/pg_0002.pdf')).should == 1
- File.unlink(path_to_pdf('burst/pg_0003.pdf')).should == 1
+ if example.metadata[:genesis]
+ FileUtils.copy_entry(@output.path, @example_expect.to_s, true, false, true)
+ else
+ @call_output.should == @output.path + '/pg_%04d.pdf'
+ fixtures_path('output').should look_like_the_same_pdf_as(@example_expect)
+ end
end
end
context "#to temporary directory", :if => output_type == :nil do
before(:each) do
- @input = get_input(input_type, 'spec.a.pdf')
+ @input = get_input(input_type, 'multi.pdf')
@input.rewind rescue nil # rewind if possible.
end
it "should file into single pages" do
@pdftk.burst(@input, :output => nil).should == Dir.tmpdir
- 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_0003.pdf')).should == 1
+
+ @example_expect.children(false).each do |file|
+ (Pathname.new(Dir.tmpdir) + file).should look_like_the_same_pdf_as(@example_expect + file)
+ FileUtils.remove_file(Pathname.new(Dir.tmpdir) + file)
+ 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
+
+ @example_expect.children(false).each do |file|
+ index = file.basename.to_s.match(/(\d+)/)[0].to_i
+ (Pathname.new(Dir.tmpdir) + ("page_%02d.pdf" % index)).should look_like_the_same_pdf_as(@example_expect + file)
+ FileUtils.remove_file(Pathname.new(Dir.tmpdir) + ("page_%02d.pdf" % index))
+ end
end
end
end
View
BIN spec/fixtures/background/expect.pdf
Binary file not shown.
View
BIN spec/fixtures/burst/expect/pg_0001.pdf
Binary file not shown.
View
BIN spec/fixtures/burst/expect/pg_0002.pdf
Binary file not shown.
View
BIN spec/fixtures/burst/expect/pg_0003.pdf
Binary file not shown.
View
BIN spec/fixtures/cat/expect.pdf
Binary file not shown.
View
BIN spec/fixtures/multi.pdf
Binary file not shown.
View
BIN spec/fixtures/multibackground/expect.pdf
Binary file not shown.
View
BIN spec/fixtures/multistamp/expect.pdf
Binary file not shown.
View
BIN spec/fixtures/poly.pdf
Binary file not shown.
View
BIN spec/fixtures/shuffle/expect.pdf
Binary file not shown.
View
BIN spec/fixtures/spec.a.pdf
Binary file not shown.
View
BIN spec/fixtures/spec.b.pdf
Binary file not shown.
View
BIN spec/fixtures/stamp/expect.pdf
Binary file not shown.
View
11 spec/support/inputs_helper.rb
@@ -56,4 +56,15 @@ def open_or_rewind(target)
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 # PDF embed some binary data breaking gsub with ruby 1.9.2
+ text.gsub!(/\(D\:.*\)/, '') # Remove dates ex: /CreationDate (D:20111106104455-05'00')
+ text.gsub!(/\/ID \[<\w*><\w*>\]/, '') # Remove ID values ex: /ID [<4ba02a4cf55b1fc842299e6f01eb838e><33bec7dc37839cadf7ab76f3be4d4306>]
+ text
+end
+
+def cleanup_file_content(text)
+ cleanup_file_content!(text.dup)
end
View
30 spec/support/matchers/content_matcher.rb
@@ -6,6 +6,15 @@
diffable
end
+RSpec::Matchers.define :look_like_the_same_pdf_as do |expected|
+ match do |actual|
+ sha256_hash_of_almost(actual) == sha256_hash_of_almost(expected)
+ end
+
+ diffable
+end
+
+
#TODO it would be great to implement an inclusion matcher, just for fun.
#RSpec::Matchers.define :include_the_content_of do |expected|
# match do |actual|
@@ -32,4 +41,25 @@ def sha256_hash_of(entry)
sha256_hash_of(File.new(entry))
end
end
+end
+
+def sha256_hash_of_almost(entry)
+ entry.rewind if entry.respond_to? :rewind
+ case entry
+ when File, Tempfile, StringIO then sha256_hash_of_almost(entry.read)
+ when Dir then (entry.entries - ['.', '..']).collect { |filename| sha256_hash_of_almost(Pathname.new(File.join(entry.path, filename))) }.compact.sort
+ when String then
+ if entry.size < 256 && (Pathname.new(entry).file? || Pathname.new(entry).directory?) # Would be deprecated in favor of Pathname object
+ sha256_hash_of_almost(Pathname.new(entry))
+ else
+ Digest::SHA256.hexdigest(cleanup_file_content(entry))
+ end
+
+ when Pathname then
+ if entry.directory?
+ sha256_hash_of_almost(Dir.new(entry))
+ elsif entry.file?
+ sha256_hash_of_almost(File.read(entry))
+ end
+ end
end

0 comments on commit ea98477

Please sign in to comment.