Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implement Rspec content matcher (for files, directories, strings, pat…

…hnames)

Implement #fixture_path helper returning pathname object or array of pathnames
Implement attach_files specs
  • Loading branch information...
commit 7e75c088bfaee69b3811e244b9c1a78090746476 1 parent e0ff870
@elmatou elmatou authored
View
54 spec/active_pdftk/wrapper_spec.rb
@@ -146,44 +146,7 @@ def cleanup_file_content(text)
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
-
before(:all) { @example_expect = open_or_rewind(path_to_pdf('unpack_files/expect.txt')) }
context "to path", :if => output_type == :path do
@@ -215,6 +178,23 @@ def cleanup_file_content(text)
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
it_behaves_like "a working command" do
before(:all) { @example_expect = File.new(path_to_pdf('background/expect.pdf')).read }
View
1  spec/fixtures/.gitignore
@@ -0,0 +1 @@
+/output.spec
View
BIN  spec/fixtures/attach_files/expect.pdf
Binary file not shown
View
4 spec/fixtures/attach_files/spec.txt → spec/fixtures/attach_files/expect/first.txt
@@ -1,5 +1,5 @@
-Testing pdftk_forms
+Testing active_pdftk
https://github.com/tcocca/active_pdftk
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.
View
4 spec/fixtures/attach_files/expect/second.txt
@@ -0,0 +1,4 @@
+Testing active_pdftk
+https://github.com/tcocca/active_pdftk
+
+Second attached file !!
View
13 spec/support/inputs_helper.rb
@@ -1,7 +1,14 @@
def path_to_pdf(filename)
- file = File.join(File.dirname(__FILE__), '../', 'fixtures')
- file = File.join(file, "#{filename}") unless filename.nil?
- file
+ File.expand_path(File.join(File.dirname(__FILE__), '../', 'fixtures', "#{filename}"))
+end
+
+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
# Because with Ruby 1.8 Hashes are unordered, and options in cli are unordered too,
View
40 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
Please sign in to comment.
Something went wrong with that request. Please try again.