Permalink
Browse files

rspec tests added

  • Loading branch information...
Malykh Oleg
Malykh Oleg committed Jul 21, 2011
1 parent e8e7d65 commit 925ed0d7ca23277bbd3e3de4c7adff0cd60a12c3
@@ -0,0 +1,49 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe ImageDownloader::Download do
+
+ before do
+ @image = ImageDownloader::Images.new("www.test.com", "http://www.test.com/images", {
+ :catalog_path => 'tmp',
+ :file_name_prefix => ''})
+ @image1 = ImageDownloader::Images.new("www.test1.com", "http://www.test1.com/images", {
+ :catalog_path => 'tmp1',
+ :file_name_prefix => '1'})
+ @image.stub!(:download)
+ @image1.stub!(:download)
+ end
+
+
+ describe "parallel" do
+ it "should call threads new" do
+ thread = Thread.new(@image){|local_image| true}
+ Thread.should_receive(:new).with(@image).and_return(thread)
+ ImageDownloader::Download.parallel([@image], "ruby")
+ end
+
+ it "should call image download" do
+ @image.should_receive(:download)
+ ImageDownloader::Download.parallel([@image], "ruby")
+ end
+
+ it "should call image download for all images" do
+ @image.should_receive(:download)
+ @image1.should_receive(:download)
+ ImageDownloader::Download.parallel([@image, @image1], "ruby")
+ end
+ end
+
+ describe "consequentially" do
+ it "should not call threads new" do
+ Thread.should_not_receive(:new)
+ ImageDownloader::Download.consequentially([@image], "ruby")
+ end
+
+ it "should call image download for all images" do
+ @image.should_receive(:download)
+ @image1.should_receive(:download)
+ ImageDownloader::Download.consequentially([@image, @image1], "ruby")
+ end
+ end
+
+end
@@ -0,0 +1,56 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe ImageDownloader::Images do
+
+ before do
+ @test_host = "www.test.com"
+ @src = "http://www.test.com/images/pic.jpg"
+ @h = {:catalog_path => 'tmp', :file_name_prefix => ''}
+ end
+
+ context "when correct" do
+ before do
+ @image = ImageDownloader::Images.new(@test_host, @src, @h)
+ end
+ it "set file name" do
+ @image.file_name.should == 'pic.jpg'
+ end
+ it "set file_path_name" do
+ @image.file_path_name.should == 'tmp/pic.jpg'
+ end
+ it "set absolute_src" do
+ @image.absolute_src.should == 'http://www.test.com/images/pic.jpg'
+ end
+ it "set prefix if exist" do
+ @h[:file_name_prefix] = 'prefix_'
+ image = ImageDownloader::Images.new(@test_host, @src, @h)
+ image.file_name.should == 'prefix_pic.jpg'
+ end
+ it "set self host if not host in src"do
+ image = ImageDownloader::Images.new(@test_host, '/images/icon.ico', @h)
+ image.absolute_src.should == 'http://www.test.com/images/icon.ico'
+ end
+ end
+
+ context "when incorrect" do
+ it "generate file name" do
+ image = ImageDownloader::Images.new(@test_host, '', @h)
+ image.file_name.should be
+ end
+ it "file name like standart name" do
+ image = ImageDownloader::Images.new(@test_host, '', @h)
+ image.file_name.should =~ /#{ImageDownloader::Images::EMPTY_FILE_NAME}/
+ end
+ it "file name different for two empty files" do
+ image = ImageDownloader::Images.new(@test_host, '', @h)
+ image1 = ImageDownloader::Images.new(@test_host, '', @h)
+ image.file_name.should_not == image1.file_name
+ end
+ it "cut too long file_name" do
+ 1.upto(1000) {@src.insert(-7,'x')}
+ image = ImageDownloader::Images.new(@test_host, @src, @h)
+ image.file_name.length.should <= ImageDownloader::Images::MAX_FILE_NAME_LENGTH_ALLOWED
+ end
+ end
+
+end
@@ -0,0 +1,29 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe ImageDownloader::URL do
+
+ it "false for url without http" do
+ ImageDownloader::URL.contain_http?('www.test.com').should == false
+ end
+ it "true for url with http" do
+ ImageDownloader::URL.contain_http?('http://www.test.com').should == true
+ end
+ it "true for url with https" do
+ ImageDownloader::URL.contain_http?('https://www.test.com').should == true
+ end
+
+ it "should normalize url without http" do
+ ImageDownloader::URL.normalize('www.test.com').should == "http://www.test.com"
+ end
+ it "should not change url with http" do
+ ImageDownloader::URL.normalize('http://www.test.com').should == "http://www.test.com"
+ end
+ it "should not change url with https" do
+ ImageDownloader::URL.normalize('https://www.test.com').should == "https://www.test.com"
+ end
+
+ it "should remove \\n and \\r from string" do
+ ImageDownloader::URL.remove_new_line_symbols!("www.te\rst.c\nom").should == "www.test.com"
+ end
+
+end
@@ -0,0 +1,139 @@
+require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
+
+describe Process do
+
+ def mock_parser
+ parser = mock(ImageDownloader::Parser)
+ ImageDownloader::Parser.should_receive(:new).with("http://www.test.com", ImageDownloader::Process::DEFAULT_USER_AGENT).and_return(parser)
+ parser
+ end
+
+ def stub_parser_methods(parser)
+ parser.stub!(:get_content_raw)
+ parser.stub!(:get_images_raw)
+ parser.stub!(:get_content)
+ parser.stub!(:get_images)
+ parser.stub!(:get_images_regexp)
+ parser.stub!(:ignore_file_without)
+ parser.stub!(:images)
+ end
+
+ before do
+ @process = ImageDownloader::Process.new("www.test.com", "tmp/dir/")
+ end
+
+ it "should correct normalize url" do
+ @process.argument.url.should eq "http://www.test.com"
+ end
+
+ it "should correct normalize path" do
+ @process.argument.path.should eq "tmp/dir"
+ end
+
+ it "should not change correct url and path" do
+ process = ImageDownloader::Process.new("http://www.test.com", "/tmp/cc/dir")
+ process.argument.url.should eq "http://www.test.com"
+ process.argument.path.should eq "/tmp/cc/dir"
+ end
+
+ it "parse should call rebuild_collect_hash" do
+ @process.should_receive(:rebuild_collect_hash).with(:any_looks_like_image => true, :collect => {:all => true})
+ @process.parse(:any_looks_like_image => true, :collect => {:all => true})
+ end
+
+ it "user can set any user agent to get web page for parsing" do
+ parser = mock(ImageDownloader::Parser)
+ ImageDownloader::Parser.should_receive(:new).with("http://www.test.com", "ruby").and_return(parser)
+ stub_parser_methods(parser)
+ @process.parse(:user_agent => "ruby")
+ end
+
+ describe "parse" do
+
+ before do
+ @parser = mock_parser
+ stub_parser_methods(@parser)
+ end
+
+ it "rebuild_collect_hash should change :collect hash for :all and call get_content and get_images" do
+ @parser.should_receive(:get_content)
+ @parser.should_receive(:get_images).with("tmp/dir", {
+ :collect_from_a_href=>true,
+ :collect_from_style_url=>true,
+ :collect_from_link_icon=>true,
+ :collect_from_img_src=>true})
+ @process.parse(:collect => {:all => true})
+ end
+
+ it "rebuild_collect_hash should change :collect hash for :a_href" do
+ @parser.should_receive(:get_content)
+ @parser.should_receive(:get_images).with("tmp/dir", {:collect_from_a_href=>true})
+ @process.parse(:collect => {:a_href => true})
+ end
+
+ it "should parse with regexp if :regexp specified" do
+ @parser.should_receive(:get_content_raw)
+ @parser.should_receive(:get_images_regexp).with("tmp/dir", /[^'"]+\.jpg/i)
+ @process.parse(:regexp => /[^'"]+\.jpg/i)
+ end
+
+ it "should call get_content_raw and get_images_raw for :any_looks_like_image" do
+ @parser.should_receive(:get_content_raw)
+ @parser.should_receive(:get_images_raw)
+ @process.parse(:any_looks_like_image => true)
+ end
+
+ it "should call ignore and images methods" do
+ @parser.should_receive(:ignore_file_without)
+ @parser.should_receive(:images)
+ @process.parse()
+ end
+
+ end
+
+ describe "download" do
+
+ describe "should parallel" do
+
+ before do
+ parser = mock(ImageDownloader::Parser)
+ ImageDownloader::Download.should_receive(:parallel).and_return(parser)
+ end
+
+ it "if no arguments, by default" do
+ @process.download
+ end
+ it "if :parallel as argument" do
+ @process.download(:parallel)
+ end
+ it "if :parallel => true as argument" do
+ @process.download(:parallel => true)
+ end
+
+ end
+
+ describe "should consequentially" do
+
+ before do
+ parser = mock(ImageDownloader::Parser)
+ ImageDownloader::Download.should_receive(:consequentially).and_return(parser)
+ end
+
+ it "if :consequentially as argument" do
+ @process.download(:consequentially)
+ end
+ it "if :consequentially => true as argument" do
+ @process.download(:consequentially => true)
+ end
+
+ end
+
+ it "no download if incorrect argument" do
+ ImageDownloader::Download.should_not_receive(:consequentially)
+ ImageDownloader::Download.should_not_receive(:parallel)
+ @process.download(:dab => true)
+ end
+
+ end
+
+end
View
@@ -0,0 +1,12 @@
+$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
+$LOAD_PATH.unshift(File.dirname(__FILE__))
+require 'rspec'
+require 'image_downloader'
+
+# Requires supporting files with custom matchers and macros, etc,
+# in ./support/ and its subdirectories.
+Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
+
+RSpec.configure do |config|
+
+end

0 comments on commit 925ed0d

Please sign in to comment.