Skip to content

Commit

Permalink
Implement Rspec content matcher (for files, directories, strings, pat…
Browse files Browse the repository at this point in the history
…hnames)

Implement #fixture_path helper returning pathname object or array of pathnames
Implement attach_files specs
  • Loading branch information
elmatou committed Nov 15, 2011
1 parent e0ff870 commit 7e75c08
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 42 deletions.
54 changes: 17 additions & 37 deletions spec/active_pdftk/wrapper_spec.rb
Expand Up @@ -146,44 +146,7 @@ def cleanup_file_content(text)
end end
end end


describe "#attach_files" do
before(:all) { @attachment_size = File.size(path_to_pdf('attach_files/spec.txt')) }
before(:each) { @call_output = @pdftk.attach_files(@input, [path_to_pdf('attach_files/spec.txt')], :output => @output) }
it "should bind the file ine the pdf" do
if @call_output.is_a?(String)
output_size = File.size(@call_output)
else
@call_output.rewind
t = Tempfile.new('attachment_output')
t.write(@call_output.read)
output_size = File.size(t.path)
t.close
end
if @input.is_a?(String)
input_size = File.size(@input)
elsif @input.is_a?(Hash)
input_size = 0
@input.each do |file_path, name|
input_size += File.size(file_path)
end
else
@input.rewind
t = Tempfile.new('attachment_input')
t.write(@input.read)
input_size = File.size(t.path)
t.close
end
total_size = input_size + @attachment_size
output_size.should >= total_size
end

it "should output the correct type" do
@call_output.should be_kind_of(map_output_type(output_type))
end
end

describe "#unpack_files" do describe "#unpack_files" do

before(:all) { @example_expect = open_or_rewind(path_to_pdf('unpack_files/expect.txt')) } before(:all) { @example_expect = open_or_rewind(path_to_pdf('unpack_files/expect.txt')) }


context "to path", :if => output_type == :path do context "to path", :if => output_type == :path do
Expand Down Expand Up @@ -215,6 +178,23 @@ def cleanup_file_content(text)
end end
end end


describe "#attach_files", :focus => true do
before(:each) do
@call_output = @pdftk.attach_files(@input, fixtures_path('attach_files/expect', true).collect(&:to_s), :output => @output)
end

it "should bind the file in the pdf" do
Dir.mktmpdir do |directory|
@pdftk.unpack_files(@call_output, directory)
Pathname.new(directory).should have_the_content_of(fixtures_path('attach_files/expect'))
end
end

it "should output the correct type" do
@call_output.should be_kind_of(map_output_type(output_type))
end
end

describe "#background" do describe "#background" do
it_behaves_like "a working command" do it_behaves_like "a working command" do
before(:all) { @example_expect = File.new(path_to_pdf('background/expect.pdf')).read } before(:all) { @example_expect = File.new(path_to_pdf('background/expect.pdf')).read }
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/.gitignore
@@ -0,0 +1 @@
/output.spec
Binary file removed spec/fixtures/attach_files/expect.pdf
Binary file not shown.
@@ -1,5 +1,5 @@
Testing pdftk_forms Testing active_pdftk
https://github.com/tcocca/active_pdftk https://github.com/tcocca/active_pdftk


This file can be attached to any PDF. This file can be attached to any PDF.
And any files can be attached to this PDF. And any files can be attached to this PDF.
4 changes: 4 additions & 0 deletions spec/fixtures/attach_files/expect/second.txt
@@ -0,0 +1,4 @@
Testing active_pdftk
https://github.com/tcocca/active_pdftk

Second attached file !!
13 changes: 10 additions & 3 deletions spec/support/inputs_helper.rb
@@ -1,7 +1,14 @@
def path_to_pdf(filename) def path_to_pdf(filename)
file = File.join(File.dirname(__FILE__), '../', 'fixtures') File.expand_path(File.join(File.dirname(__FILE__), '../', 'fixtures', "#{filename}"))
file = File.join(file, "#{filename}") unless filename.nil? end
file
def fixtures_path(entry, expand = false)
entry_path = Pathname.new(File.expand_path(File.join(File.dirname(__FILE__), '../', 'fixtures', "#{entry}")))
if expand && entry_path.directory?
(entry_path.entries - [Pathname.new('.'), Pathname.new('..')]).collect { |obj| entry_path + obj}
else
entry_path
end
end end


# Because with Ruby 1.8 Hashes are unordered, and options in cli are unordered too, # Because with Ruby 1.8 Hashes are unordered, and options in cli are unordered too,
Expand Down
40 changes: 40 additions & 0 deletions spec/support/matchers/content_matcher.rb
@@ -0,0 +1,40 @@
RSpec::Matchers.define :have_the_content_of do |expected|
match do |actual|
sha256_hash_of(actual) == sha256_hash_of(expected)
end

failure_message_for_should do |actual|
"expected that #{actual} would have the content of #{expected}"
end

failure_message_for_should_not do |actual|
"expected that #{actual} would differ from #{expected}"
end
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|
# !(sha256_hash_of(expected) & sha256_hash_of(actual)).empty?
# end
#end

def sha256_hash_of(entry)
case entry
when File, Tempfile then Digest::SHA256.file(entry.path).hexdigest
when Dir then (entry.entries - ['.', '..']).collect { |filename| sha256_hash_of(Pathname.new(File.join(entry.path, filename))) }.compact.sort
when StringIO then sha256_hash_of(entry.read)
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(Pathname.new(entry))
else
Digest::SHA256.hexdigest(entry)
end
when Pathname then
if entry.directory?
sha256_hash_of(Dir.new(entry))
elsif entry.file?
sha256_hash_of(File.new(entry))
end
end
end

0 comments on commit 7e75c08

Please sign in to comment.