uploadcolumn 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,195 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+ require File.join(File.dirname(__FILE__), '../lib/upload_column/manipulators/image_science')
3
+
4
+ describe UploadColumn::Manipulators::ImageScience, "#resize!" do
5
+
6
+ before(:each) do
7
+ @uploaded_file = class << self; self end # this is a singleton object
8
+ @uploaded_file.extend( UploadColumn::Manipulators::ImageScience )
9
+ @uploaded_file.load_manipulator_dependencies
10
+ @uploaded_file.stub!(:path).and_return('/some_path.png')
11
+ end
12
+
13
+ it "should preserve the aspect ratio if the image is too wide" do
14
+
15
+ image = mock('an image_science object')
16
+
17
+ ::ImageScience.should_receive(:with_image).with('/some_path.png').and_yield(image)
18
+
19
+ image.should_receive(:width).and_return(640)
20
+ image.should_receive(:height).and_return(480)
21
+
22
+ i2 = mock('another stupid mock')
23
+ i2.should_receive(:save).with('/some_path.png')
24
+
25
+ image.should_receive(:resize).with(160, 120).and_yield(i2)
26
+
27
+ @uploaded_file.resize!('400x120')
28
+ end
29
+
30
+ it "should preserve the aspect ratio if the image is too narrow" do
31
+
32
+ image = mock('an image_science object')
33
+
34
+ ::ImageScience.should_receive(:with_image).with('/some_path.png').and_yield(image)
35
+
36
+ image.should_receive(:width).and_return(640)
37
+ image.should_receive(:height).and_return(480)
38
+
39
+ i2 = mock('another stupid mock')
40
+ i2.should_receive(:save).with('/some_path.png')
41
+
42
+ image.should_receive(:resize).with(200, 150).and_yield(i2)
43
+
44
+ @uploaded_file.resize!('200x400')
45
+ end
46
+
47
+ it "should rescale to the exact size if the aspect ratio is the same" do
48
+
49
+ image = mock('an image_science object')
50
+
51
+ ::ImageScience.should_receive(:with_image).with('/some_path.png').and_yield(image)
52
+
53
+ image.should_receive(:width).and_return(640)
54
+ image.should_receive(:height).and_return(480)
55
+
56
+ i2 = mock('another stupid mock')
57
+ i2.should_receive(:save).with('/some_path.png')
58
+
59
+ image.should_receive(:resize).with(320, 240).and_yield(i2)
60
+
61
+ @uploaded_file.resize!('320x240')
62
+ end
63
+
64
+ it "should not exceed the dimensions if the image is a rather weird size" do
65
+
66
+ image = mock('an image_science object')
67
+
68
+ ::ImageScience.should_receive(:with_image).with('/some_path.png').and_yield(image)
69
+
70
+ image.should_receive(:width).and_return(737)
71
+ image.should_receive(:height).and_return(237)
72
+
73
+ i2 = mock('another stupid mock')
74
+ i2.should_receive(:save).with('/some_path.png')
75
+
76
+ image.should_receive(:resize).with(137, 44).and_yield(i2)
77
+
78
+ @uploaded_file.resize!('137x137')
79
+ end
80
+
81
+ end
82
+
83
+
84
+ describe UploadColumn::Manipulators::ImageScience, "#crop_resized!" do
85
+
86
+ before(:each) do
87
+ @uploaded_file = class << self; self end # this is a singleton object
88
+ @uploaded_file.extend( UploadColumn::Manipulators::ImageScience )
89
+ @uploaded_file.load_manipulator_dependencies
90
+ @uploaded_file.stub!(:path).and_return('/some_path.png')
91
+ end
92
+
93
+ it "should crop and resize an image that is too tall" do
94
+ image = mock('an image_science object')
95
+
96
+ ::ImageScience.should_receive(:with_image).with('/some_path.png').and_yield(image)
97
+
98
+ image.should_receive(:width).and_return(640)
99
+ image.should_receive(:height).and_return(480)
100
+
101
+ i2 = mock('another stupid mock')
102
+ image.should_receive(:resize).with(400, 300).and_yield(i2)
103
+
104
+ i3 = mock('image science is stupid')
105
+ i2.should_receive(:with_crop).with(0, 90, 400, 210).and_yield(i3)
106
+
107
+ i3.should_receive(:save).with('/some_path.png')
108
+
109
+ @uploaded_file.crop_resized!('400x120')
110
+ end
111
+
112
+ it "should crop and resize an image that is too tall" do
113
+ image = mock('an image_science object')
114
+
115
+ ::ImageScience.should_receive(:with_image).with('/some_path.png').and_yield(image)
116
+
117
+ image.should_receive(:width).and_return(640)
118
+ image.should_receive(:height).and_return(480)
119
+
120
+ i2 = mock('another stupid mock')
121
+ image.should_receive(:resize).with(560, 420).and_yield(i2)
122
+
123
+ i3 = mock('image science is stupid')
124
+ i2.should_receive(:with_crop).with(180, 0, 380, 420).and_yield(i3)
125
+
126
+ i3.should_receive(:save).with('/some_path.png')
127
+
128
+ @uploaded_file.crop_resized!('200x420')
129
+ end
130
+
131
+ it "should crop and resize an image with the correct aspect ratio" do
132
+ image = mock('an image_science object')
133
+
134
+ ::ImageScience.should_receive(:with_image).with('/some_path.png').and_yield(image)
135
+
136
+ image.should_receive(:width).and_return(640)
137
+ image.should_receive(:height).and_return(480)
138
+
139
+ i2 = mock('another stupid mock')
140
+ image.should_receive(:resize).with(320, 240).and_yield(i2)
141
+
142
+ i3 = mock('image science is stupid')
143
+ i2.should_receive(:with_crop).with(0, 0, 320, 240).and_yield(i3)
144
+
145
+ i3.should_receive(:save).with('/some_path.png')
146
+
147
+ @uploaded_file.crop_resized!('320x240')
148
+ end
149
+
150
+ it "should crop and resize an image with weird dimensions" do
151
+ image = mock('an image_science object')
152
+
153
+ ::ImageScience.should_receive(:with_image).with('/some_path.png').and_yield(image)
154
+
155
+ image.should_receive(:width).and_return(737)
156
+ image.should_receive(:height).and_return(967)
157
+
158
+ i2 = mock('another stupid mock')
159
+ image.should_receive(:resize).with(333, 437).and_yield(i2)
160
+
161
+ i3 = mock('image science is stupid')
162
+ i2.should_receive(:with_crop).with(0, 150, 333, 287).and_yield(i3)
163
+
164
+ i3.should_receive(:save).with('/some_path.png')
165
+
166
+ @uploaded_file.crop_resized!('333x137')
167
+ end
168
+ end
169
+
170
+ describe UploadColumn::Manipulators::ImageScience, "#process!" do
171
+
172
+ before(:each) do
173
+ @uploaded_file = class << self; self end
174
+ @uploaded_file.extend( UploadColumn::Manipulators::ImageScience )
175
+ @uploaded_file.load_manipulator_dependencies
176
+ @uploaded_file.stub!(:path).and_return('/some_path.png')
177
+ end
178
+
179
+ it "should resize the image if a string like '333x444' is passed" do
180
+ @uploaded_file.should_receive(:resize!).with('333x444')
181
+ @uploaded_file.process!('333x444')
182
+ end
183
+
184
+ it "should crop and resize the image if a string like 'c333x444' is passed" do
185
+ @uploaded_file.should_receive(:crop_resized!).with('333x444')
186
+ @uploaded_file.process!('c333x444')
187
+ end
188
+
189
+ it "should do nothing if :none is passed" do
190
+ @uploaded_file.should_not_receive(:manipulate!)
191
+ @uploaded_file.process!(:none)
192
+ end
193
+
194
+ end
195
+