OSDN Git Service

add test
[pettanr/pettanr.git] / lib / test / local_picture_spec.rb
diff --git a/lib/test/local_picture_spec.rb b/lib/test/local_picture_spec.rb
new file mode 100644 (file)
index 0000000..2be6b6a
--- /dev/null
@@ -0,0 +1,162 @@
+# -*- encoding: utf-8 -*-
+#ファイル保存
+require 'local_picture'
+require 'spec_helper'
+
+#テスト用ファイルをクリアしておく
+@path = Rails.root + 'lib/test/temp/'
+Dir.glob @path + '*' do |filename|
+  if File.directory?(filename)
+    Dir.glob filename do |filename2|
+      File.delete(filename2) unless File.directory?(filename2)
+    end
+  else
+    File.delete(filename)
+  end
+end
+describe PictureIO::LocalPicture do
+  before do
+    @path = Rails.root + 'lib/test/temp/'
+    @io = PictureIO::LocalPicture.new @path
+  end
+
+  describe 'サブディレクトリに於いて' do
+    it '配列が返る' do
+      PictureIO::LocalPicture.subdirs.is_a?(Array).should be_true 
+    end
+
+    it 'カレント、サムネ、水平反転、垂直反転、水平垂直反転が返る' do
+      PictureIO::LocalPicture.subdirs.size.should eq 5
+      PictureIO::LocalPicture.subdirs.include?('').should eq true
+      PictureIO::LocalPicture.subdirs.include?('v').should eq true
+      PictureIO::LocalPicture.subdirs.include?('h').should eq true
+      PictureIO::LocalPicture.subdirs.include?('vh').should eq true
+      PictureIO::LocalPicture.subdirs.include?('thumbnail').should eq true
+    end
+  end
+  
+  describe 'ファイル存在確認に於いて' do
+    before do
+    end
+    context 'ファイルがあるとき' do
+      it 'trueが返る' do
+        File.open @path + 'exist', 'w' do |f|
+          f.puts 'test data'
+        end
+        @io.exist?('exist').should be_true
+      end
+    end
+    context 'ファイルがないとき' do
+      it 'falseが返る' do
+        @io.exist?('not.exist').should be_false
+      end
+    end
+  end
+  
+  describe 'ファイル取得に於いて' do
+    before do
+      File.open @path + 'test.get', 'w' do |f|
+        f.puts 'basedata'
+      end
+      File.open @path + 'v/testsub.get', 'w' do |f|
+        f.puts 'subdata'
+      end
+    end
+    context 'ベースディレクトリから取るとき' do
+      it 'basedataが返る' do
+        @io.get('test.get').should match /basedata/
+      end
+    end
+    context 'サブディレクトリvから取るとき' do
+      it 'subdataが返る' do
+        @io.get('testsub.get', 'v').should match /subdata/
+      end
+    end
+    context '例外が発生するとき' do
+      it 'falseが返る' do
+        File.stub(:open).with(any_args()).and_raise(StandardError)
+        @io.get('test.get').should be_false
+      end
+    end
+  end
+  
+  describe 'ファイル作成に於いて' do
+    before do
+      File.open @path + 'test.put', 'w' do |f|
+        f.puts 'already'
+      end
+    end
+    context 'ベースディレクトリに作成するとき' do
+      before do
+        @io.put('created', 'create.put')
+      end
+      it 'ファイルが作成されている' do
+        File.exist?(@path + 'create.put').should be_true
+      end
+      it 'createdが書きこまれている' do
+        d = File.open(@path + 'create.put').read
+        d.should match /create/
+      end
+    end
+    context '既存ファイルを更新するとき' do
+      before do
+        @io.put('overwrited', 'test.put')
+      end
+      it 'overwritedが書きこまれている' do
+        d = File.open(@path + 'test.put').read
+        d.should match /overwrited/
+      end
+      it '上書きされている' do
+        d = File.open(@path + 'test.put').read
+        d.should_not match /already/
+      end
+    end
+    context 'サブディレクトリhに作成するとき' do
+      before do
+        @io.put('subdirtest', 'testsub.put', 'h')
+      end
+      it 'ファイルが作成されている' do
+        File.exist?(@path + 'h/testsub.put').should be_true
+      end
+      it 'subdirtestが書きこまれている' do
+        d = File.open(@path + 'h/testsub.put').read
+        d.should match /subdirtest/
+      end
+    end
+    context '例外が発生するとき' do
+      it 'falseが返る' do
+        File.stub(:open).with(any_args()).and_raise(StandardError)
+        @io.put('bindata', 'test.put').should be_false
+      end
+    end
+  end
+  
+  describe 'ファイル削除に於いて' do
+    before do
+      File.open @path + 'test.del', 'w' do |f|
+        f.puts 'basedata'
+      end
+      File.open @path + 'thumbnail/testsub.del', 'w' do |f|
+        f.puts 'subdata'
+      end
+    end
+    context 'ベースディレクトリから削除するとき' do
+      it 'ファイルが削除されている' do
+        @io.delete('test.del')
+        File.exist?(@path + 'test.del').should be_false
+      end
+    end
+    context 'サブディレクトリthumbnailから削除するとき' do
+      it 'ファイルが削除されている' do
+        @io.delete('testsub.del', 'thumbnail')
+        File.exist?(@path + 'thumbnail/testsub.del').should be_false
+      end
+    end
+    context '例外が発生するとき' do
+      it 'falseが返る' do
+        File.stub(:delete).with(any_args()).and_raise(StandardError)
+        @io.delete('test.del.not.exist').should be_false
+      end
+    end
+  end
+end