OSDN Git Service

fix: bin tool
[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     before do
25     end
26     context 'ファイルがあるとき' do
27       it 'trueが返る' do
28         File.open @path + 'exist', 'w' do |f|
29           f.puts 'test data'
30         end
31         @io.exist?('exist').should be_true
32       end
33     end
34     context 'ファイルがないとき' do
35       it 'PictureIO::Error例外が発生する' do
36         File.stub(:exist?).with(any_args()).and_raise(StandardError)
37         lambda {
38           @io.exist?('not.exist')
39         }.should raise_error(PictureIO::Error)
40       end
41     end
42   end
43   
44   describe 'ファイル取得に於いて' do
45     before do
46       File.open @path + 'test.get', 'w' do |f|
47         f.puts 'basedata'
48       end
49       File.open @path + 'v/testsub.get', 'w' do |f|
50         f.puts 'subdata'
51       end
52     end
53     context 'ベースディレクトリから取るとき' do
54       it 'basedataが返る' do
55         @io.get('test.get').should match /basedata/
56       end
57     end
58     context 'サブディレクトリvから取るとき' do
59       it 'subdataが返る' do
60         @io.get('testsub.get', 'v').should match /subdata/
61       end
62     end
63     context '例外が発生するとき' do
64       it 'PictureIO::Error例外が発生する' do
65         File.stub(:open).with(any_args()).and_raise(StandardError)
66         lambda {
67           r = @io.get('test.get')
68         }.should raise_error(PictureIO::Error)
69       end
70     end
71   end
72   
73   describe 'ファイル作成に於いて' do
74     before do
75       File.open @path + 'test.put', 'w' do |f|
76         f.puts 'already'
77       end
78     end
79     context 'ベースディレクトリに作成するとき' do
80       before do
81         @io.put('created', 'create.put')
82       end
83       it 'ファイルが作成されている' do
84         File.exist?(@path + 'create.put').should be_true
85       end
86       it 'createdが書きこまれている' do
87         d = File.open(@path + 'create.put').read
88         d.should match /create/
89       end
90     end
91     context '既存ファイルを更新するとき' do
92       before do
93         @io.put('overwrited', 'test.put')
94       end
95       it 'overwritedが書きこまれている' do
96         d = File.open(@path + 'test.put').read
97         d.should match /overwrited/
98       end
99       it '上書きされている' do
100         d = File.open(@path + 'test.put').read
101         d.should_not match /already/
102       end
103     end
104     context 'サブディレクトリhに作成するとき' do
105       before do
106         @io.put('subdirtest', 'testsub.put', 'h')
107       end
108       it 'ファイルが作成されている' do
109         File.exist?(@path + 'h/testsub.put').should be_true
110       end
111       it 'subdirtestが書きこまれている' do
112         d = File.open(@path + 'h/testsub.put').read
113         d.should match /subdirtest/
114       end
115     end
116     context '例外が発生するとき' do
117       it 'PictureIO::Error例外が発生する' do
118         File.stub(:open).with(any_args()).and_raise(StandardError)
119         lambda {
120           @io.put('bindata', 'test.put')
121         }.should raise_error(PictureIO::Error)
122       end
123     end
124   end
125   
126   describe 'ファイル削除に於いて' do
127     before do
128       File.open @path + 'test.del', 'w' do |f|
129         f.puts 'basedata'
130       end
131       File.open @path + 'thumbnail/testsub.del', 'w' do |f|
132         f.puts 'subdata'
133       end
134     end
135     context 'ベースディレクトリから削除するとき' do
136       it 'ファイルが削除されている' do
137         @io.delete('test.del')
138         File.exist?(@path + 'test.del').should be_false
139       end
140     end
141     context 'サブディレクトリthumbnailから削除するとき' do
142       it 'ファイルが削除されている' do
143         @io.delete('testsub.del', 'thumbnail')
144         File.exist?(@path + 'thumbnail/testsub.del').should be_false
145       end
146     end
147     context '例外が発生するとき' do
148       it 'PictureIO::Error例外が発生する' do
149         File.stub(:delete).with(any_args()).and_raise(StandardError)
150         lambda {
151           @io.delete('test.del.not.exist')
152         }.should raise_error(PictureIO::Error)
153       end
154     end
155   end
156 end