Permalink
Browse files

Added unit tests for convert command. Modified where Shellwords is us…

…ed to only wrap the input file.
  • Loading branch information...
1 parent af953e1 commit 71c19be5914993b2497214d48714a0886baebaa5 @masom masom committed Dec 28, 2011
Showing with 92 additions and 25 deletions.
  1. +2 −2 Rakefile
  2. +41 −9 lib/tesseract/process.rb
  3. +49 −14 test/tesseract_test.rb
View
@@ -1,9 +1,9 @@
require 'rake'
require 'rake/testtask'
-require 'rake/rdoctask'
+require 'rdoc/task'
Rake::TestTask.new do |t|
t.libs << "tesseract"
t.libs << "test"
t.test_files = FileList['test/*_test.rb']
t.verbose = true
-end
+end
View
@@ -27,10 +27,39 @@ def initialize(image_name, options = {})
@out = nil
@image = Pathname.new(image_name)
@hash = Digest::MD5.hexdigest("#{@image}-#{Time.now}")
- @options = defaults.merge! options
+
+ merge_options! defaults, options
DependencyChecker.check! if @options[:check_deps]
end
+ def merge_options!(defaults, options)
+ @options = {}
+
+ if options.has_key? :tesseract_options
+ @options[:tesseract_options] = defaults[:tesseract_options].merge!(options[:tesseract_options]) if options.has_key? :tesseract_options
+ [:tesseract_options, :convert_options].each do |k|
+ options.delete(k) if options.has_key? k
+ end
+ end
+
+
+ if options.has_key? :convert_options
+ @options[:convert_options] = defaults[:convert_options]
+ defaults[:convert_options].each do |k,v|
+ next unless options[:convert_options].has_key? k
+ @options[:convert_options][k] = v | options[:convert_options][k]
+ end
+ options.delete :convert_options
+ end
+ @options = defaults.merge options
+ end
+
+ def lang=(lang)
+ @options[:lang]
+ end
+ def lang
+ @options[:lang]
+ end
def to_s
@out ||= process!
end
@@ -48,20 +77,23 @@ def process!
text.gsub(/^\//, '')
end
- # Converts the source image to a tiff file.
- def to_tiff
- temp_file = FileHandler.create_temp_file("#{@hash}.tif")
-
+ # Generates the convert command.
+ def generate_convert_command(temp_file)
cmd = [@options[:convert_command]]
input_opt = @options[:convert_options][:input]
output_opt = @options[:convert_options][:output]
- cmd << input_opt unless input_opt.empty?
- cmd << @image.to_s
- cmd << output_opt unless output_opt.empty?
+ cmd += input_opt unless input_opt.empty?
+ cmd << Shellwords.shellescape(@image.to_s)
+ cmd += output_opt unless output_opt.empty?
cmd << temp_file.to_s
+ cmd.join(" ")
+ end
- system Shellwords.join(cmd)
+ # Converts the source image to a tiff file.
+ def to_tiff
+ temp_file = FileHandler.create_temp_file("#{@hash}.tif")
+ system generate_convert_command(temp_file)
temp_file
end
View
@@ -16,7 +16,7 @@ class TesseractTest < Test::Unit::TestCase
silence_stream(STDERR) { Object.const_set("RUBY_PLATFORM", @old_val) }
end
end
-
+
context "dependency imagemagic fails" do
setup do
Tesseract::DependencyChecker.expects(:run_cmd).with("which tesseract").returns('foo').once
@@ -28,7 +28,7 @@ class TesseractTest < Test::Unit::TestCase
end
end
end
-
+
context "dependency tesseract fails" do
setup do
Tesseract::DependencyChecker.expects(:run_cmd).with("which tesseract").returns('').once
@@ -39,38 +39,73 @@ class TesseractTest < Test::Unit::TestCase
end
end
end
-
+
context "tesseract" do
setup do
@tess = Tesseract::Process.new(TEST_FILE)
end
should "return text" do
assert !@tess.to_s.empty?
end
- should "hanve lang of eng" do
- assert_equal 'eng', @tess.lang
+ should "have lang of eng" do
+ assert_equal :eng, @tess.lang
+ end
+ should "generate a valid convert command" do
+ expected = "convert #{TEST_FILE} derp"
+ result = @tess.generate_convert_command('derp')
+ assert_equal expected, result
+ end
+ end
+
+ context "tesseract convert options" do
+ should "generate a valid convert command with input options" do
+ options = {:convert_options => {:input => ['-size 120x120']}}
+ tess = Tesseract::Process.new(TEST_FILE, options)
+ expected = "convert -size 120x120 #{TEST_FILE} derp"
+ result = tess.generate_convert_command('derp')
+ assert_equal expected, result
+ end
+ should "generate a valid convert command with output options" do
+ options = {:convert_options => {:output => ['-resize 120x120']}}
+ tess = Tesseract::Process.new(TEST_FILE, options)
+ expected = "convert #{TEST_FILE} -resize 120x120 derp"
+ result = tess.generate_convert_command('derp')
+ assert_equal expected, result
+ end
+ should "generate a valid convert command with input and output options" do
+ options = {
+ :convert_options => {
+ :input => ['-size 120x120'],
+ :output => ['-resize 140x140']
+ }
+ }
+ tess = Tesseract::Process.new(TEST_FILE, options)
+ expected = "convert -size 120x120 #{TEST_FILE} -resize 140x140 derp"
+ result = tess.generate_convert_command('derp')
+ assert_equal expected, result
end
+
end
-
+
context "tesseract diff lang" do
setup do
- @tess = Tesseract::Process.new(TEST_FILE, {:lang => 'butts'})
+ @tess = Tesseract::Process.new(TEST_FILE, {:lang => :butts})
end
should "have lang of butts" do
- assert_equal 'butts', @tess.lang
+ assert_equal :butts, @tess.lang
end
end
-
+
context "tesseract configs" do
setup do
- @tess = Tesseract::Process.new(TEST_FILE, {:chop_enable=>0})
+ config = {:chop_enable => 0}
+ @tess = Tesseract::Process.new(TEST_FILE, {:tesseract_options => config})
end
should "return text" do
assert !@tess.to_s.empty?
end
- should "hanve lang of eng" do
- assert_equal 'eng', @tess.lang
+ should "have lang of eng" do
+ assert_equal :eng, @tess.lang
end
end
-
-end
+end

0 comments on commit 71c19be

Please sign in to comment.