cemeng-paperclip 2.3.6
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/LICENSE +26 -0
- data/README.rdoc +182 -0
- data/Rakefile +80 -0
- data/generators/paperclip/USAGE +5 -0
- data/generators/paperclip/paperclip_generator.rb +27 -0
- data/generators/paperclip/templates/paperclip_migration.rb.erb +19 -0
- data/init.rb +1 -0
- data/lib/generators/paperclip/USAGE +8 -0
- data/lib/generators/paperclip/paperclip_generator.rb +31 -0
- data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +19 -0
- data/lib/paperclip.rb +402 -0
- data/lib/paperclip/attachment.rb +347 -0
- data/lib/paperclip/callback_compatability.rb +61 -0
- data/lib/paperclip/command_line.rb +80 -0
- data/lib/paperclip/geometry.rb +115 -0
- data/lib/paperclip/interpolations.rb +114 -0
- data/lib/paperclip/iostream.rb +45 -0
- data/lib/paperclip/matchers.rb +33 -0
- data/lib/paperclip/matchers/have_attached_file_matcher.rb +57 -0
- data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +75 -0
- data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +54 -0
- data/lib/paperclip/matchers/validate_attachment_size_matcher.rb +95 -0
- data/lib/paperclip/processor.rb +58 -0
- data/lib/paperclip/railtie.rb +25 -0
- data/lib/paperclip/storage.rb +3 -0
- data/lib/paperclip/storage/database.rb +204 -0
- data/lib/paperclip/storage/filesystem.rb +73 -0
- data/lib/paperclip/storage/s3.rb +192 -0
- data/lib/paperclip/style.rb +91 -0
- data/lib/paperclip/thumbnail.rb +79 -0
- data/lib/paperclip/upfile.rb +55 -0
- data/lib/paperclip/version.rb +3 -0
- data/lib/tasks/paperclip.rake +79 -0
- data/rails/init.rb +2 -0
- data/shoulda_macros/paperclip.rb +118 -0
- data/test/attachment_test.rb +804 -0
- data/test/command_line_test.rb +133 -0
- data/test/database.yml +4 -0
- data/test/database_storage_test.rb +267 -0
- data/test/fixtures/12k.png +0 -0
- data/test/fixtures/50x50.png +0 -0
- data/test/fixtures/5k.png +0 -0
- data/test/fixtures/bad.png +1 -0
- data/test/fixtures/s3.yml +8 -0
- data/test/fixtures/text.txt +0 -0
- data/test/fixtures/twopage.pdf +0 -0
- data/test/geometry_test.rb +177 -0
- data/test/helper.rb +146 -0
- data/test/integration_test.rb +482 -0
- data/test/interpolations_test.rb +127 -0
- data/test/iostream_test.rb +71 -0
- data/test/matchers/have_attached_file_matcher_test.rb +24 -0
- data/test/matchers/validate_attachment_content_type_matcher_test.rb +47 -0
- data/test/matchers/validate_attachment_presence_matcher_test.rb +26 -0
- data/test/matchers/validate_attachment_size_matcher_test.rb +51 -0
- data/test/paperclip_test.rb +254 -0
- data/test/processor_test.rb +10 -0
- data/test/storage_test.rb +363 -0
- data/test/style_test.rb +141 -0
- data/test/thumbnail_test.rb +227 -0
- data/test/upfile_test.rb +36 -0
- metadata +186 -0
@@ -0,0 +1,133 @@
|
|
1
|
+
require './test/helper'
|
2
|
+
|
3
|
+
class CommandLineTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
Paperclip::CommandLine.path = nil
|
6
|
+
File.stubs(:exist?).with("/dev/null").returns(true)
|
7
|
+
end
|
8
|
+
|
9
|
+
should "take a command and parameters and produce a shell command for bash" do
|
10
|
+
cmd = Paperclip::CommandLine.new("convert", "a.jpg b.png", :swallow_stderr => false)
|
11
|
+
assert_equal "convert a.jpg b.png", cmd.command
|
12
|
+
end
|
13
|
+
|
14
|
+
should "be able to set a path and produce commands with that path" do
|
15
|
+
Paperclip::CommandLine.path = "/opt/bin"
|
16
|
+
cmd = Paperclip::CommandLine.new("convert", "a.jpg b.png", :swallow_stderr => false)
|
17
|
+
assert_equal "/opt/bin/convert a.jpg b.png", cmd.command
|
18
|
+
end
|
19
|
+
|
20
|
+
should "be able to interpolate quoted variables into the parameters" do
|
21
|
+
cmd = Paperclip::CommandLine.new("convert",
|
22
|
+
":one :{two}",
|
23
|
+
:one => "a.jpg",
|
24
|
+
:two => "b.png",
|
25
|
+
:swallow_stderr => false)
|
26
|
+
assert_equal "convert 'a.jpg' 'b.png'", cmd.command
|
27
|
+
end
|
28
|
+
|
29
|
+
should "quote command line options differently if we're on windows" do
|
30
|
+
File.stubs(:exist?).with("/dev/null").returns(false)
|
31
|
+
cmd = Paperclip::CommandLine.new("convert",
|
32
|
+
":one :{two}",
|
33
|
+
:one => "a.jpg",
|
34
|
+
:two => "b.png",
|
35
|
+
:swallow_stderr => false)
|
36
|
+
assert_equal 'convert "a.jpg" "b.png"', cmd.command
|
37
|
+
end
|
38
|
+
|
39
|
+
should "be able to quote and interpolate dangerous variables" do
|
40
|
+
cmd = Paperclip::CommandLine.new("convert",
|
41
|
+
":one :two",
|
42
|
+
:one => "`rm -rf`.jpg",
|
43
|
+
:two => "ha'ha.png",
|
44
|
+
:swallow_stderr => false)
|
45
|
+
assert_equal "convert '`rm -rf`.jpg' 'ha'\\''ha.png'", cmd.command
|
46
|
+
end
|
47
|
+
|
48
|
+
should "be able to quote and interpolate dangerous variables even on windows" do
|
49
|
+
File.stubs(:exist?).with("/dev/null").returns(false)
|
50
|
+
cmd = Paperclip::CommandLine.new("convert",
|
51
|
+
":one :two",
|
52
|
+
:one => "`rm -rf`.jpg",
|
53
|
+
:two => "ha'ha.png",
|
54
|
+
:swallow_stderr => false)
|
55
|
+
assert_equal %{convert "`rm -rf`.jpg" "ha'ha.png"}, cmd.command
|
56
|
+
end
|
57
|
+
|
58
|
+
should "add redirection to get rid of stderr in bash" do
|
59
|
+
File.stubs(:exist?).with("/dev/null").returns(true)
|
60
|
+
cmd = Paperclip::CommandLine.new("convert",
|
61
|
+
"a.jpg b.png",
|
62
|
+
:swallow_stderr => true)
|
63
|
+
|
64
|
+
assert_equal "convert a.jpg b.png 2>/dev/null", cmd.command
|
65
|
+
end
|
66
|
+
|
67
|
+
should "add redirection to get rid of stderr in cmd.exe" do
|
68
|
+
File.stubs(:exist?).with("/dev/null").returns(false)
|
69
|
+
cmd = Paperclip::CommandLine.new("convert",
|
70
|
+
"a.jpg b.png",
|
71
|
+
:swallow_stderr => true)
|
72
|
+
|
73
|
+
assert_equal "convert a.jpg b.png 2>NUL", cmd.command
|
74
|
+
end
|
75
|
+
|
76
|
+
should "raise if trying to interpolate :swallow_stderr or :expected_outcodes" do
|
77
|
+
cmd = Paperclip::CommandLine.new("convert",
|
78
|
+
":swallow_stderr :expected_outcodes",
|
79
|
+
:swallow_stderr => false,
|
80
|
+
:expected_outcodes => [0, 1])
|
81
|
+
assert_raise(Paperclip::PaperclipCommandLineError) do
|
82
|
+
cmd.command
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
should "run the #command it's given and return the output" do
|
87
|
+
cmd = Paperclip::CommandLine.new("convert", "a.jpg b.png", :swallow_stderr => false)
|
88
|
+
cmd.class.stubs(:"`").with("convert a.jpg b.png").returns(:correct_value)
|
89
|
+
with_exitstatus_returning(0) do
|
90
|
+
assert_equal :correct_value, cmd.run
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
should "raise a PaperclipCommandLineError if the result code isn't expected" do
|
95
|
+
cmd = Paperclip::CommandLine.new("convert", "a.jpg b.png", :swallow_stderr => false)
|
96
|
+
cmd.class.stubs(:"`").with("convert a.jpg b.png").returns(:correct_value)
|
97
|
+
with_exitstatus_returning(1) do
|
98
|
+
assert_raises(Paperclip::PaperclipCommandLineError) do
|
99
|
+
cmd.run
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
should "not raise a PaperclipCommandLineError if the result code is expected" do
|
105
|
+
cmd = Paperclip::CommandLine.new("convert",
|
106
|
+
"a.jpg b.png",
|
107
|
+
:expected_outcodes => [0, 1],
|
108
|
+
:swallow_stderr => false)
|
109
|
+
cmd.class.stubs(:"`").with("convert a.jpg b.png").returns(:correct_value)
|
110
|
+
with_exitstatus_returning(1) do
|
111
|
+
assert_nothing_raised do
|
112
|
+
cmd.run
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
should "log the command" do
|
118
|
+
cmd = Paperclip::CommandLine.new("convert", "a.jpg b.png", :swallow_stderr => false)
|
119
|
+
cmd.class.stubs(:'`')
|
120
|
+
Paperclip.expects(:log).with("convert a.jpg b.png")
|
121
|
+
cmd.run
|
122
|
+
end
|
123
|
+
|
124
|
+
should "detect that the system is unix or windows based on presence of /dev/null" do
|
125
|
+
File.stubs(:exist?).returns(true)
|
126
|
+
assert Paperclip::CommandLine.unix?
|
127
|
+
end
|
128
|
+
|
129
|
+
should "detect that the system is not unix or windows based on absence of /dev/null" do
|
130
|
+
File.stubs(:exist?).returns(false)
|
131
|
+
assert ! Paperclip::CommandLine.unix?
|
132
|
+
end
|
133
|
+
end
|
data/test/database.yml
ADDED
@@ -0,0 +1,267 @@
|
|
1
|
+
require './test/helper'
|
2
|
+
|
3
|
+
module ActionController
|
4
|
+
class Base
|
5
|
+
def self.relative_url_root
|
6
|
+
'/base'
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def rebuild_database_table_1_blob_column options = {}
|
12
|
+
ActiveRecord::Base.connection.create_table :dummies, :force => true do |table|
|
13
|
+
table.column :other, :string
|
14
|
+
table.column :avatar_file_name, :string
|
15
|
+
table.column :avatar_content_type, :string
|
16
|
+
table.column :avatar_file_size, :integer
|
17
|
+
table.column :avatar_updated_at, :datetime
|
18
|
+
table.column :avatar_file, :binary
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def rebuild_database_table_3_default_blob_columns options = {}
|
23
|
+
ActiveRecord::Base.connection.create_table :dummies, :force => true do |table|
|
24
|
+
table.column :other, :string
|
25
|
+
table.column :avatar_file_name, :string
|
26
|
+
table.column :avatar_content_type, :string
|
27
|
+
table.column :avatar_file_size, :integer
|
28
|
+
table.column :avatar_updated_at, :datetime
|
29
|
+
table.column :avatar_file, :binary
|
30
|
+
table.column :avatar_thumb_file, :binary
|
31
|
+
table.column :avatar_medium_file, :binary
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def rebuild_database_table_3_custom_blob_columns options = {}
|
36
|
+
ActiveRecord::Base.connection.create_table :dummies, :force => true do |table|
|
37
|
+
table.column :other, :string
|
38
|
+
table.column :avatar_file_name, :string
|
39
|
+
table.column :avatar_content_type, :string
|
40
|
+
table.column :avatar_file_size, :integer
|
41
|
+
table.column :avatar_updated_at, :datetime
|
42
|
+
table.column :avatar_file_data, :binary
|
43
|
+
table.column :thumb_file_data, :binary
|
44
|
+
table.column :medium_file_data, :binary
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
class DatabaseStorageTest < Test::Unit::TestCase
|
49
|
+
|
50
|
+
context "An attachment with database storage and default options" do
|
51
|
+
setup do
|
52
|
+
rebuild_database_table_1_blob_column
|
53
|
+
rebuild_class :storage => :database
|
54
|
+
end
|
55
|
+
|
56
|
+
should "be extended by the database storage module" do
|
57
|
+
assert Dummy.new.avatar.is_a?(Paperclip::Storage::Database)
|
58
|
+
end
|
59
|
+
|
60
|
+
should "return the meta data columns from select_without_file_columns_for" do
|
61
|
+
assert_equal Dummy.select_without_file_columns_for(:avatar),
|
62
|
+
{ :select=>"id,other,avatar_file_name,avatar_content_type,avatar_file_size,avatar_updated_at" }
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
context "An attachment with database storage and array style options" do
|
67
|
+
setup do
|
68
|
+
rebuild_database_table_1_blob_column
|
69
|
+
rebuild_class :storage => :database, :styles => {:original => ["200x200", :jpg ]}
|
70
|
+
end
|
71
|
+
|
72
|
+
should "be extended by the database storage module" do
|
73
|
+
assert Dummy.new.avatar.is_a?(Paperclip::Storage::Database)
|
74
|
+
end
|
75
|
+
|
76
|
+
should "return the meta data columns from select_without_file_columns_for" do
|
77
|
+
assert_equal Dummy.select_without_file_columns_for(:avatar),
|
78
|
+
{ :select=>"id,other,avatar_file_name,avatar_content_type,avatar_file_size,avatar_updated_at" }
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
context "An attachment with database storage, default options plus two styles" do
|
83
|
+
setup do
|
84
|
+
rebuild_database_table_3_default_blob_columns
|
85
|
+
rebuild_class :storage => :database,
|
86
|
+
:styles => {
|
87
|
+
:medium => {:geometry => "300x300>"},
|
88
|
+
:thumb => {:geometry => "100x100>"}
|
89
|
+
}
|
90
|
+
end
|
91
|
+
|
92
|
+
should "return the meta data columns from select_without_file_columns_for" do
|
93
|
+
assert_equal Dummy.select_without_file_columns_for(:avatar),
|
94
|
+
{ :select=>"id,other,avatar_file_name,avatar_content_type,avatar_file_size,avatar_updated_at" }
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
context "An attachment with database storage and incorrect original style column name" do
|
99
|
+
setup do
|
100
|
+
rebuild_database_table_1_blob_column
|
101
|
+
end
|
102
|
+
|
103
|
+
should "raise an exception" do
|
104
|
+
assert_raises(Paperclip::PaperclipError) do
|
105
|
+
rebuild_class :storage => :database, :column => 'missing'
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
context "An attachment with database storage and incorrect other style column name" do
|
111
|
+
setup do
|
112
|
+
rebuild_database_table_3_default_blob_columns
|
113
|
+
end
|
114
|
+
|
115
|
+
should "raise an exception" do
|
116
|
+
assert_raises(Paperclip::PaperclipError) do
|
117
|
+
rebuild_class :storage => :database,
|
118
|
+
:styles => {
|
119
|
+
:medium => {:geometry => "300x300>", :column => 'missing'},
|
120
|
+
:thumb => {:geometry => "100x100>"}
|
121
|
+
}
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
context "An attachment with database storage and original style column name set to attachment name" do
|
127
|
+
setup do
|
128
|
+
rebuild_database_table_3_default_blob_columns
|
129
|
+
end
|
130
|
+
|
131
|
+
should "raise an exception" do
|
132
|
+
assert_raises(Paperclip::PaperclipError) do
|
133
|
+
rebuild_class :storage => :database, :column => 'avatar',
|
134
|
+
:styles => {
|
135
|
+
:medium => {:geometry => "300x300>"},
|
136
|
+
:thumb => {:geometry => "100x100>"}
|
137
|
+
}
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
|
143
|
+
context "An attachment with database storage and other style column name set to attachment name" do
|
144
|
+
setup do
|
145
|
+
rebuild_database_table_3_default_blob_columns
|
146
|
+
end
|
147
|
+
|
148
|
+
should "raise an exception" do
|
149
|
+
assert_raises(Paperclip::PaperclipError) do
|
150
|
+
rebuild_class :storage => :database,
|
151
|
+
:styles => {
|
152
|
+
:medium => {:geometry => "300x300>", :column => 'avatar'},
|
153
|
+
:thumb => {:geometry => "100x100>"}
|
154
|
+
}
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
|
160
|
+
context "An attachment with database storage" do
|
161
|
+
setup do
|
162
|
+
rebuild_database_table_3_custom_blob_columns
|
163
|
+
rebuild_class :storage => :database, :column => 'avatar_file_data',
|
164
|
+
:styles => {
|
165
|
+
:medium => {:geometry => "300x300>", :column => 'medium_file_data'},
|
166
|
+
:thumb => {:geometry => "100x100>", :column => 'thumb_file_data'}
|
167
|
+
}
|
168
|
+
@dummy = Dummy.new
|
169
|
+
end
|
170
|
+
|
171
|
+
context "before assigned a file" do
|
172
|
+
should "return false when asked exists?" do
|
173
|
+
assert !@dummy.avatar.exists?
|
174
|
+
end
|
175
|
+
|
176
|
+
should "return its default_url" do
|
177
|
+
assert @dummy.avatar.to_file.nil?
|
178
|
+
assert_equal "/avatars/original/missing.png", @dummy.avatar.url
|
179
|
+
assert_equal "/avatars/blah/missing.png", @dummy.avatar.url(:blah)
|
180
|
+
end
|
181
|
+
|
182
|
+
should "return nil as path" do
|
183
|
+
assert @dummy.avatar.to_file.nil?
|
184
|
+
assert_equal nil, @dummy.avatar.path
|
185
|
+
assert_equal nil, @dummy.avatar.path(:blah)
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
context "and assigned a file" do
|
190
|
+
setup do
|
191
|
+
@file = File.new(File.join(File.dirname(__FILE__),
|
192
|
+
"fixtures",
|
193
|
+
"5k.png"), 'rb')
|
194
|
+
@contents = @file.read
|
195
|
+
@file.rewind
|
196
|
+
|
197
|
+
@dummy.avatar = @file
|
198
|
+
end
|
199
|
+
|
200
|
+
teardown { @file.close if @file }
|
201
|
+
|
202
|
+
should "be dirty" do
|
203
|
+
assert @dummy.avatar.dirty?
|
204
|
+
end
|
205
|
+
|
206
|
+
should "exist" do
|
207
|
+
assert @dummy.avatar.exists?
|
208
|
+
end
|
209
|
+
|
210
|
+
should "save the actual file contents to the original style column" do
|
211
|
+
assert_equal @contents, @dummy.avatar_file_data
|
212
|
+
assert !(file = @dummy.avatar.to_file).nil?
|
213
|
+
assert_equal Paperclip::Tempfile, file.class
|
214
|
+
file.close
|
215
|
+
end
|
216
|
+
|
217
|
+
should "save some value to the other style columns" do
|
218
|
+
assert !@dummy.medium_file_data.nil?
|
219
|
+
assert !@dummy.thumb_file_data.nil?
|
220
|
+
end
|
221
|
+
|
222
|
+
should "return the proper default url" do
|
223
|
+
assert_match %r{^/dummies/avatars/#{@dummy.id}\?style=original}, @dummy.avatar.url
|
224
|
+
assert_match %r{^/dummies/avatars/#{@dummy.id}\?style=medium}, @dummy.avatar.url(:medium)
|
225
|
+
assert_match %r{^/dummies/avatars/#{@dummy.id}\?style=thumb}, @dummy.avatar.url(:thumb)
|
226
|
+
end
|
227
|
+
|
228
|
+
should "return the column name as path" do
|
229
|
+
assert_equal "avatar_file_data", @dummy.avatar.path
|
230
|
+
assert_equal "avatar_file_data", @dummy.avatar.path(:original)
|
231
|
+
assert_equal "medium_file_data", @dummy.avatar.path(:medium)
|
232
|
+
assert_equal "thumb_file_data", @dummy.avatar.path(:thumb)
|
233
|
+
end
|
234
|
+
|
235
|
+
context "and assigned to another attachment" do
|
236
|
+
setup do
|
237
|
+
@dummy2 = Dummy.new
|
238
|
+
@dummy2.avatar = @dummy.avatar
|
239
|
+
end
|
240
|
+
|
241
|
+
should "have the proper attributes assigned to the other attachment" do
|
242
|
+
assert_equal @dummy.avatar_file_name, @dummy2.avatar_file_name
|
243
|
+
assert_equal @dummy.avatar_content_type, @dummy2.avatar_content_type
|
244
|
+
assert_equal @dummy.avatar_file_size, @dummy2.avatar_file_size
|
245
|
+
assert_equal @dummy.avatar_file_data, @dummy2.avatar.file_contents
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
249
|
+
context "and saved and assigned to another attachment" do
|
250
|
+
setup do
|
251
|
+
@dummy.save
|
252
|
+
|
253
|
+
@dummy2 = Dummy.new
|
254
|
+
@dummy2.avatar = @dummy.avatar
|
255
|
+
end
|
256
|
+
|
257
|
+
should "have the proper attributes assigned to the other attachment" do
|
258
|
+
assert_equal @dummy.avatar_file_name, @dummy2.avatar_file_name
|
259
|
+
assert_equal @dummy.avatar_content_type, @dummy2.avatar_content_type
|
260
|
+
assert_equal @dummy.avatar_file_size, @dummy2.avatar_file_size
|
261
|
+
assert_equal @dummy.avatar_file_data, @dummy2.avatar.file_contents
|
262
|
+
end
|
263
|
+
end
|
264
|
+
|
265
|
+
end
|
266
|
+
end
|
267
|
+
end
|
Binary file
|
Binary file
|
Binary file
|
@@ -0,0 +1 @@
|
|
1
|
+
This is not an image.
|
File without changes
|
Binary file
|
@@ -0,0 +1,177 @@
|
|
1
|
+
require './test/helper'
|
2
|
+
|
3
|
+
class GeometryTest < Test::Unit::TestCase
|
4
|
+
context "Paperclip::Geometry" do
|
5
|
+
should "correctly report its given dimensions" do
|
6
|
+
assert @geo = Paperclip::Geometry.new(1024, 768)
|
7
|
+
assert_equal 1024, @geo.width
|
8
|
+
assert_equal 768, @geo.height
|
9
|
+
end
|
10
|
+
|
11
|
+
should "set height to 0 if height dimension is missing" do
|
12
|
+
assert @geo = Paperclip::Geometry.new(1024)
|
13
|
+
assert_equal 1024, @geo.width
|
14
|
+
assert_equal 0, @geo.height
|
15
|
+
end
|
16
|
+
|
17
|
+
should "set width to 0 if width dimension is missing" do
|
18
|
+
assert @geo = Paperclip::Geometry.new(nil, 768)
|
19
|
+
assert_equal 0, @geo.width
|
20
|
+
assert_equal 768, @geo.height
|
21
|
+
end
|
22
|
+
|
23
|
+
should "be generated from a WxH-formatted string" do
|
24
|
+
assert @geo = Paperclip::Geometry.parse("800x600")
|
25
|
+
assert_equal 800, @geo.width
|
26
|
+
assert_equal 600, @geo.height
|
27
|
+
end
|
28
|
+
|
29
|
+
should "be generated from a xH-formatted string" do
|
30
|
+
assert @geo = Paperclip::Geometry.parse("x600")
|
31
|
+
assert_equal 0, @geo.width
|
32
|
+
assert_equal 600, @geo.height
|
33
|
+
end
|
34
|
+
|
35
|
+
should "be generated from a Wx-formatted string" do
|
36
|
+
assert @geo = Paperclip::Geometry.parse("800x")
|
37
|
+
assert_equal 800, @geo.width
|
38
|
+
assert_equal 0, @geo.height
|
39
|
+
end
|
40
|
+
|
41
|
+
should "be generated from a W-formatted string" do
|
42
|
+
assert @geo = Paperclip::Geometry.parse("800")
|
43
|
+
assert_equal 800, @geo.width
|
44
|
+
assert_equal 0, @geo.height
|
45
|
+
end
|
46
|
+
|
47
|
+
should "ensure the modifier is nil if not present" do
|
48
|
+
assert @geo = Paperclip::Geometry.parse("123x456")
|
49
|
+
assert_nil @geo.modifier
|
50
|
+
end
|
51
|
+
|
52
|
+
should "treat x and X the same in geometries" do
|
53
|
+
@lower = Paperclip::Geometry.parse("123x456")
|
54
|
+
@upper = Paperclip::Geometry.parse("123X456")
|
55
|
+
assert_equal 123, @lower.width
|
56
|
+
assert_equal 123, @upper.width
|
57
|
+
assert_equal 456, @lower.height
|
58
|
+
assert_equal 456, @upper.height
|
59
|
+
end
|
60
|
+
|
61
|
+
['>', '<', '#', '@', '%', '^', '!', nil].each do |mod|
|
62
|
+
should "ensure the modifier #{mod.inspect} is preserved" do
|
63
|
+
assert @geo = Paperclip::Geometry.parse("123x456#{mod}")
|
64
|
+
assert_equal mod, @geo.modifier
|
65
|
+
assert_equal "123x456#{mod}", @geo.to_s
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
['>', '<', '#', '@', '%', '^', '!', nil].each do |mod|
|
70
|
+
should "ensure the modifier #{mod.inspect} is preserved with no height" do
|
71
|
+
assert @geo = Paperclip::Geometry.parse("123x#{mod}")
|
72
|
+
assert_equal mod, @geo.modifier
|
73
|
+
assert_equal "123#{mod}", @geo.to_s
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
should "make sure the modifier gets passed during transformation_to" do
|
78
|
+
assert @src = Paperclip::Geometry.parse("123x456")
|
79
|
+
assert @dst = Paperclip::Geometry.parse("123x456>")
|
80
|
+
assert_equal ["123x456>", nil], @src.transformation_to(@dst)
|
81
|
+
end
|
82
|
+
|
83
|
+
should "generate correct ImageMagick formatting string for W-formatted string" do
|
84
|
+
assert @geo = Paperclip::Geometry.parse("800")
|
85
|
+
assert_equal "800", @geo.to_s
|
86
|
+
end
|
87
|
+
|
88
|
+
should "generate correct ImageMagick formatting string for Wx-formatted string" do
|
89
|
+
assert @geo = Paperclip::Geometry.parse("800x")
|
90
|
+
assert_equal "800", @geo.to_s
|
91
|
+
end
|
92
|
+
|
93
|
+
should "generate correct ImageMagick formatting string for xH-formatted string" do
|
94
|
+
assert @geo = Paperclip::Geometry.parse("x600")
|
95
|
+
assert_equal "x600", @geo.to_s
|
96
|
+
end
|
97
|
+
|
98
|
+
should "generate correct ImageMagick formatting string for WxH-formatted string" do
|
99
|
+
assert @geo = Paperclip::Geometry.parse("800x600")
|
100
|
+
assert_equal "800x600", @geo.to_s
|
101
|
+
end
|
102
|
+
|
103
|
+
should "be generated from a file" do
|
104
|
+
file = File.join(File.dirname(__FILE__), "fixtures", "5k.png")
|
105
|
+
file = File.new(file, 'rb')
|
106
|
+
assert_nothing_raised{ @geo = Paperclip::Geometry.from_file(file) }
|
107
|
+
assert @geo.height > 0
|
108
|
+
assert @geo.width > 0
|
109
|
+
end
|
110
|
+
|
111
|
+
should "be generated from a file path" do
|
112
|
+
file = File.join(File.dirname(__FILE__), "fixtures", "5k.png")
|
113
|
+
assert_nothing_raised{ @geo = Paperclip::Geometry.from_file(file) }
|
114
|
+
assert @geo.height > 0
|
115
|
+
assert @geo.width > 0
|
116
|
+
end
|
117
|
+
|
118
|
+
should "not generate from a bad file" do
|
119
|
+
file = "/home/This File Does Not Exist.omg"
|
120
|
+
assert_raise(Paperclip::NotIdentifiedByImageMagickError){ @geo = Paperclip::Geometry.from_file(file) }
|
121
|
+
end
|
122
|
+
|
123
|
+
[['vertical', 900, 1440, true, false, false, 1440, 900, 0.625],
|
124
|
+
['horizontal', 1024, 768, false, true, false, 1024, 768, 1.3333],
|
125
|
+
['square', 100, 100, false, false, true, 100, 100, 1]].each do |args|
|
126
|
+
context "performing calculations on a #{args[0]} viewport" do
|
127
|
+
setup do
|
128
|
+
@geo = Paperclip::Geometry.new(args[1], args[2])
|
129
|
+
end
|
130
|
+
|
131
|
+
should "#{args[3] ? "" : "not"} be vertical" do
|
132
|
+
assert_equal args[3], @geo.vertical?
|
133
|
+
end
|
134
|
+
|
135
|
+
should "#{args[4] ? "" : "not"} be horizontal" do
|
136
|
+
assert_equal args[4], @geo.horizontal?
|
137
|
+
end
|
138
|
+
|
139
|
+
should "#{args[5] ? "" : "not"} be square" do
|
140
|
+
assert_equal args[5], @geo.square?
|
141
|
+
end
|
142
|
+
|
143
|
+
should "report that #{args[6]} is the larger dimension" do
|
144
|
+
assert_equal args[6], @geo.larger
|
145
|
+
end
|
146
|
+
|
147
|
+
should "report that #{args[7]} is the smaller dimension" do
|
148
|
+
assert_equal args[7], @geo.smaller
|
149
|
+
end
|
150
|
+
|
151
|
+
should "have an aspect ratio of #{args[8]}" do
|
152
|
+
assert_in_delta args[8], @geo.aspect, 0.0001
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
[[ [1000, 100], [64, 64], "x64", "64x64+288+0" ],
|
158
|
+
[ [100, 1000], [50, 950], "x950", "50x950+22+0" ],
|
159
|
+
[ [100, 1000], [50, 25], "50x", "50x25+0+237" ]]. each do |args|
|
160
|
+
context "of #{args[0].inspect} and given a Geometry #{args[1].inspect} and sent transform_to" do
|
161
|
+
setup do
|
162
|
+
@geo = Paperclip::Geometry.new(*args[0])
|
163
|
+
@dst = Paperclip::Geometry.new(*args[1])
|
164
|
+
@scale, @crop = @geo.transformation_to @dst, true
|
165
|
+
end
|
166
|
+
|
167
|
+
should "be able to return the correct scaling transformation geometry #{args[2]}" do
|
168
|
+
assert_equal args[2], @scale
|
169
|
+
end
|
170
|
+
|
171
|
+
should "be able to return the correct crop transformation geometry #{args[3]}" do
|
172
|
+
assert_equal args[3], @crop
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|