citrusbyte-milton 0.1.1 → 0.1.2
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.
- data/spec/milton/is_resizeable_spec.rb +131 -0
- metadata +1 -1
@@ -0,0 +1,131 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
describe Citrusbyte::Milton::IsResizeable do
|
4
|
+
describe "building the filename from options" do
|
5
|
+
before :each do
|
6
|
+
@image = Image.create :file => upload('milton.jpg')
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "options as hash" do
|
10
|
+
it "should coalesce size into filename" do
|
11
|
+
File.basename(@image.path(:size => '40x40')).should eql('milton.size=40x40.jpg')
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should raise unless a size is given" do
|
15
|
+
lambda {
|
16
|
+
File.basename(@image.path(:crop => true))
|
17
|
+
}.should raise_error
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should coalesce crop into filename" do
|
21
|
+
File.basename(@image.path(:size => '40x40', :crop => true)).should eql('milton.crop=true_size=40x40.jpg')
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should coalesce gravity into filename" do
|
25
|
+
File.basename(@image.path(:size => '40x40', :gravity => 'north')).should eql('milton.gravity=north_size=40x40.jpg')
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should coalese all options together" do
|
29
|
+
File.basename(@image.path(:size => '40x40', :gravity => 'north', :crop => true)).should eql('milton.crop=true_gravity=north_size=40x40.jpg')
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "options as string" do
|
34
|
+
it "should parse size" do
|
35
|
+
File.basename(@image.path('size=40x40')).should eql('milton.size=40x40.jpg')
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should parse crop" do
|
39
|
+
File.basename(@image.path('size=40x40_crop=true')).should eql('milton.crop=true_size=40x40.jpg')
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should parse gravity" do
|
43
|
+
File.basename(@image.path('size=40x40_gravity=north')).should eql('milton.gravity=north_size=40x40.jpg')
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should parse them all together" do
|
47
|
+
File.basename(@image.path('size=40x40_crop=true_gravity=north')).should eql('milton.crop=true_gravity=north_size=40x40.jpg')
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
# milton.jpg is 320x300
|
53
|
+
describe "resizing" do
|
54
|
+
before :each do
|
55
|
+
@image = Image.create :file => upload('milton.jpg')
|
56
|
+
end
|
57
|
+
|
58
|
+
describe "checking errors" do
|
59
|
+
it "should raise a MissingFileError if source file does not exist" do
|
60
|
+
FileUtils.rm(@image.path)
|
61
|
+
lambda {
|
62
|
+
@image.path(:size => '50x50')
|
63
|
+
}.should raise_error(Citrusbyte::Milton::MissingFileError)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
describe "when cropped" do
|
68
|
+
before :each do
|
69
|
+
@info = Citrusbyte::Milton::IsResizeable::Image.from_path(@image.reload.path(:size => '50x50', :crop => true))
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should have width of 50px" do
|
73
|
+
@info.width.should eql(50)
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should have height of 50px" do
|
77
|
+
@info.height.should eql(50)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
# 300/320 = 0.9375
|
82
|
+
# 50*0.9375 = 47
|
83
|
+
describe "when not cropped" do
|
84
|
+
before :each do
|
85
|
+
@info = Citrusbyte::Milton::IsResizeable::Image.from_path(@image.reload.path(:size => '50x50'))
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should have width of 47px" do
|
89
|
+
@info.width.should eql(47)
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should have height of 50px" do
|
93
|
+
@info.height.should eql(50)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
describe "smarter thumbnails" do
|
99
|
+
before :each do
|
100
|
+
@image = Image.create :file => upload('big-milton.jpg')
|
101
|
+
end
|
102
|
+
|
103
|
+
it "should generate 640px wide version when image is wider than 640px wide and generating an image smaller than 640px wide" do
|
104
|
+
path = @image.path(:crop => true, :size => '40x40')
|
105
|
+
File.exists?(path.gsub(/\.crop=true_size=40x40/, '.size=640x')).should be_true
|
106
|
+
end
|
107
|
+
|
108
|
+
it "should generate images smaller than 640px wide from the existing 640px one" do
|
109
|
+
# TODO: how can i test this?
|
110
|
+
@image.path(:crop => true, :size => '40x40')
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
describe "fetching thumbnails" do
|
115
|
+
before :each do
|
116
|
+
@image = Image.create :file => upload('milton.jpg')
|
117
|
+
end
|
118
|
+
|
119
|
+
it "should use the partitioned path when grabbing the original file" do
|
120
|
+
@image.path.should =~ /\/\d+\/\d+\/milton.jpg$/
|
121
|
+
end
|
122
|
+
|
123
|
+
it "should use the partitioned path and derivative path when grabbing a thubmnail" do
|
124
|
+
@image.path(:size => '10x10', :crop => true).should =~ /\/milton\/milton.crop=true_size=10x10.jpg$/
|
125
|
+
end
|
126
|
+
|
127
|
+
it "should append source filename to thumbnail path" do
|
128
|
+
@image.path(:size => '10x10').should =~ /\/milton\/milton.size=10x10.jpg$/
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|