OSDN Git Service

add test
[pettanr/pettanr.git] / lib / test / local_picture_spec.rb
1 # -*- encoding: utf-8 -*-
2 #ファイル保存
3 require 'local_picture'
4 require 'spec_helper'
5
6 #テスト用ファイルをクリアしておく
7 @path = Rails.root + 'lib/test/temp/'
8 Dir.glob @path + '*' do |filename|
9   if File.directory?(filename)
10     Dir.glob filename do |filename2|
11       File.delete(filename2) unless File.directory?(filename2)
12     end
13   else
14     File.delete(filename)
15   end
16 end
17 describe PictureIO::LocalPicture do
18   before do
19     @path = Rails.root + 'lib/test/temp/'
20     @io = PictureIO::LocalPicture.new @path
21   end
22
23   describe 'サブディレクトリに於いて' do
24     it '配列が返る' do
25       PictureIO::LocalPicture.subdirs.is_a?(Array).should be_true 
26     end
27
28     it 'カレント、サムネ、水平反転、垂直反転、水平垂直反転が返る' do
29       PictureIO::LocalPicture.subdirs.size.should eq 5
30       PictureIO::LocalPicture.subdirs.include?('').should eq true
31       PictureIO::LocalPicture.subdirs.include?('v').should eq true
32       PictureIO::LocalPicture.subdirs.include?('h').should eq true
33       PictureIO::LocalPicture.subdirs.include?('vh').should eq true
34       PictureIO::LocalPicture.subdirs.include?('thumbnail').should eq true
35     end
36   end
37   
38   describe 'ファイル存在確認に於いて' do
39     before do
40     end
41     context 'ファイルがあるとき' do
42       it 'trueが返る' do
43         File.open @path + 'exist', 'w' do |f|
44           f.puts 'test data'
45         end
46         @io.exist?('exist').should be_true
47       end
48     end
49     context 'ファイルがないとき' do
50       it 'falseが返る' do
51         @io.exist?('not.exist').should be_false
52       end
53     end
54   end
55   
56   describe 'ファイル取得に於いて' do
57     before do
58       File.open @path + 'test.get', 'w' do |f|
59         f.puts 'basedata'
60       end
61       File.open @path + 'v/testsub.get', 'w' do |f|
62         f.puts 'subdata'
63       end
64     end
65     context 'ベースディレクトリから取るとき' do
66       it 'basedataが返る' do
67         @io.get('test.get').should match /basedata/
68       end
69     end
70     context 'サブディレクトリvから取るとき' do
71       it 'subdataが返る' do
72         @io.get('testsub.get', 'v').should match /subdata/
73       end
74     end
75     context '例外が発生するとき' do
76       it 'falseが返る' do
77         File.stub(:open).with(any_args()).and_raise(StandardError)
78         @io.get('test.get').should be_false
79       end
80     end
81   end
82   
83   describe 'ファイル作成に於いて' do
84     before do
85       File.open @path + 'test.put', 'w' do |f|
86         f.puts 'already'
87       end
88     end
89     context 'ベースディレクトリに作成するとき' do
90       before do
91         @io.put('created', 'create.put')
92       end
93       it 'ファイルが作成されている' do
94         File.exist?(@path + 'create.put').should be_true
95       end
96       it 'createdが書きこまれている' do
97         d = File.open(@path + 'create.put').read
98         d.should match /create/
99       end
100     end
101     context '既存ファイルを更新するとき' do
102       before do
103         @io.put('overwrited', 'test.put')
104       end
105       it 'overwritedが書きこまれている' do
106         d = File.open(@path + 'test.put').read
107         d.should match /overwrited/
108       end
109       it '上書きされている' do
110         d = File.open(@path + 'test.put').read
111         d.should_not match /already/
112       end
113     end
114     context 'サブディレクトリhに作成するとき' do
115       before do
116         @io.put('subdirtest', 'testsub.put', 'h')
117       end
118       it 'ファイルが作成されている' do
119         File.exist?(@path + 'h/testsub.put').should be_true
120       end
121       it 'subdirtestが書きこまれている' do
122         d = File.open(@path + 'h/testsub.put').read
123         d.should match /subdirtest/
124       end
125     end
126     context '例外が発生するとき' do
127       it 'falseが返る' do
128         File.stub(:open).with(any_args()).and_raise(StandardError)
129         @io.put('bindata', 'test.put').should be_false
130       end
131     end
132   end
133   
134   describe 'ファイル削除に於いて' do
135     before do
136       File.open @path + 'test.del', 'w' do |f|
137         f.puts 'basedata'
138       end
139       File.open @path + 'thumbnail/testsub.del', 'w' do |f|
140         f.puts 'subdata'
141       end
142     end
143     context 'ベースディレクトリから削除するとき' do
144       it 'ファイルが削除されている' do
145         @io.delete('test.del')
146         File.exist?(@path + 'test.del').should be_false
147       end
148     end
149     context 'サブディレクトリthumbnailから削除するとき' do
150       it 'ファイルが削除されている' do
151         @io.delete('testsub.del', 'thumbnail')
152         File.exist?(@path + 'thumbnail/testsub.del').should be_false
153       end
154     end
155     context '例外が発生するとき' do
156       it 'falseが返る' do
157         File.stub(:delete).with(any_args()).and_raise(StandardError)
158         @io.delete('test.del.not.exist').should be_false
159       end
160     end
161   end
162 end