sprite-factory-custom 1.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/Gemfile +12 -0
- data/LICENSE +20 -0
- data/README.md +351 -0
- data/RELEASE_NOTES.md +60 -0
- data/Rakefile +73 -0
- data/bin/sf +57 -0
- data/lib/sprite_factory/layout/horizontal.rb +44 -0
- data/lib/sprite_factory/layout/packed.rb +118 -0
- data/lib/sprite_factory/layout/vertical.rb +44 -0
- data/lib/sprite_factory/library/chunky_png.rb +31 -0
- data/lib/sprite_factory/library/image_magick.rb +74 -0
- data/lib/sprite_factory/library/rmagick.rb +32 -0
- data/lib/sprite_factory/runner.rb +287 -0
- data/lib/sprite_factory/style.rb +74 -0
- data/lib/sprite_factory.rb +85 -0
- data/sprite_factory.gemspec +27 -0
- data/test/images/custom/custom.css +4 -0
- data/test/images/custom/running.png +0 -0
- data/test/images/custom/stopped.png +0 -0
- data/test/images/empty/readme.txt +1 -0
- data/test/images/formats/alice.gif +0 -0
- data/test/images/formats/codeincomplete.ico +0 -0
- data/test/images/formats/github.ico +0 -0
- data/test/images/formats/monkey.gif +0 -0
- data/test/images/formats/spies.jpg +0 -0
- data/test/images/formats/stackoverflow.ico +0 -0
- data/test/images/formats/thief.png +0 -0
- data/test/images/hover/div.bar__img.icon--active.png +0 -0
- data/test/images/hover/div.bar__img.icon--focus.png +0 -0
- data/test/images/hover/div.bar__img.icon--hover.png +0 -0
- data/test/images/hover/div.bar__img.icon--link.png +0 -0
- data/test/images/hover/div.bar__img.icon--visited.png +0 -0
- data/test/images/hover/div.bar__img.icon.png +0 -0
- data/test/images/hover/div.foo__img.icon--active.png +0 -0
- data/test/images/hover/div.foo__img.icon--focus.png +0 -0
- data/test/images/hover/div.foo__img.icon--hover.png +0 -0
- data/test/images/hover/div.foo__img.icon--link.png +0 -0
- data/test/images/hover/div.foo__img.icon--visited.png +0 -0
- data/test/images/hover/div.foo__img.icon.png +0 -0
- data/test/images/irregular/irregular1.png +0 -0
- data/test/images/irregular/irregular2.png +0 -0
- data/test/images/irregular/irregular3.png +0 -0
- data/test/images/irregular/irregular4.png +0 -0
- data/test/images/irregular/irregular5.png +0 -0
- data/test/images/irregular/readme.txt +2 -0
- data/test/images/reference/custom.css +22 -0
- data/test/images/reference/custom.png +0 -0
- data/test/images/reference/formats.css +28 -0
- data/test/images/reference/formats.png +0 -0
- data/test/images/reference/hover.css +38 -0
- data/test/images/reference/hover.png +0 -0
- data/test/images/reference/index.html +182 -0
- data/test/images/reference/irregular.css +24 -0
- data/test/images/reference/irregular.fixed.css +24 -0
- data/test/images/reference/irregular.fixed.png +0 -0
- data/test/images/reference/irregular.horizontal.css +24 -0
- data/test/images/reference/irregular.horizontal.png +0 -0
- data/test/images/reference/irregular.margin.css +24 -0
- data/test/images/reference/irregular.margin.png +0 -0
- data/test/images/reference/irregular.packed.css +24 -0
- data/test/images/reference/irregular.packed.png +0 -0
- data/test/images/reference/irregular.padded.css +24 -0
- data/test/images/reference/irregular.padded.png +0 -0
- data/test/images/reference/irregular.png +0 -0
- data/test/images/reference/irregular.sassy.css +38 -0
- data/test/images/reference/irregular.sassy.png +0 -0
- data/test/images/reference/irregular.sassy.sass +40 -0
- data/test/images/reference/irregular.vertical.css +24 -0
- data/test/images/reference/irregular.vertical.png +0 -0
- data/test/images/reference/regular.css +24 -0
- data/test/images/reference/regular.custom.css +24 -0
- data/test/images/reference/regular.custom.png +0 -0
- data/test/images/reference/regular.fixed.css +24 -0
- data/test/images/reference/regular.fixed.png +0 -0
- data/test/images/reference/regular.horizontal.css +24 -0
- data/test/images/reference/regular.horizontal.png +0 -0
- data/test/images/reference/regular.margin.css +24 -0
- data/test/images/reference/regular.margin.png +0 -0
- data/test/images/reference/regular.nocomments.css +5 -0
- data/test/images/reference/regular.nocomments.png +0 -0
- data/test/images/reference/regular.packed.css +24 -0
- data/test/images/reference/regular.packed.png +0 -0
- data/test/images/reference/regular.padded.css +24 -0
- data/test/images/reference/regular.padded.png +0 -0
- data/test/images/reference/regular.png +0 -0
- data/test/images/reference/regular.sassy.css +38 -0
- data/test/images/reference/regular.sassy.png +0 -0
- data/test/images/reference/regular.sassy.sass +40 -0
- data/test/images/reference/regular.vertical.css +24 -0
- data/test/images/reference/regular.vertical.png +0 -0
- data/test/images/reference/s.gif +0 -0
- data/test/images/reference/subfolders.css +24 -0
- data/test/images/reference/subfolders.png +0 -0
- data/test/images/regular/regular1.PNG +0 -0
- data/test/images/regular/regular2.PNG +0 -0
- data/test/images/regular/regular3.PNG +0 -0
- data/test/images/regular/regular4.PNG +0 -0
- data/test/images/regular/regular5.PNG +0 -0
- data/test/images/subfolders/england/amy.png +0 -0
- data/test/images/subfolders/england/bob.png +0 -0
- data/test/images/subfolders/france/bob.png +0 -0
- data/test/images/subfolders/usa/amy.png +0 -0
- data/test/images/subfolders/usa/bob.png +0 -0
- data/test/integration_test.rb +167 -0
- data/test/layout/horizontal_test.rb +156 -0
- data/test/layout/packed_test.rb +283 -0
- data/test/layout/test_case.rb +56 -0
- data/test/layout/vertical_test.rb +156 -0
- data/test/library_test.rb +58 -0
- data/test/runner_test.rb +229 -0
- data/test/style_test.rb +72 -0
- data/test/test_case.rb +138 -0
- metadata +286 -0
@@ -0,0 +1,58 @@
|
|
1
|
+
require File.expand_path('test_case', File.dirname(__FILE__))
|
2
|
+
|
3
|
+
class SpriteFactory::LibraryTest < SpriteFactory::TestCase
|
4
|
+
|
5
|
+
#--------------------------------------------------------------------------
|
6
|
+
|
7
|
+
LIBRARIES = {
|
8
|
+
:rmagick => SpriteFactory::Library::RMagick,
|
9
|
+
:chunkypng => SpriteFactory::Library::ChunkyPng,
|
10
|
+
:image_magick => SpriteFactory::Library::ImageMagick
|
11
|
+
}
|
12
|
+
|
13
|
+
#--------------------------------------------------------------------------
|
14
|
+
|
15
|
+
LIBRARIES.each do |name, library| # use metaprogramming to define independent tests for each library
|
16
|
+
|
17
|
+
define_method "test_load_regular_using_#{name}" do
|
18
|
+
assert_images(REGULAR_INFO, library.load(REGULAR))
|
19
|
+
end
|
20
|
+
|
21
|
+
define_method "test_load_irregular_using_#{name}" do
|
22
|
+
assert_images(IRREGULAR_INFO, library.load(IRREGULAR))
|
23
|
+
end
|
24
|
+
|
25
|
+
define_method "test_create_using_#{name}" do
|
26
|
+
with_clean_output do
|
27
|
+
images = library.load(REGULAR)
|
28
|
+
x = 0
|
29
|
+
images.each do |image|
|
30
|
+
image[:x] = x
|
31
|
+
image[:y] = 0
|
32
|
+
x = x + image[:width]
|
33
|
+
end
|
34
|
+
width = images.map{|i| i[:width]}.inject(0){|n,w| n = n + w }
|
35
|
+
height = images.map{|i| i[:height]}.max
|
36
|
+
library.create(output_path('regular.horizontal.png'), images, width, height)
|
37
|
+
assert_reference_image('regular.horizontal.png')
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
#--------------------------------------------------------------------------
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def assert_images(expected, actual, msg = nil)
|
48
|
+
assert_equal(expected.length, actual.length, "#{msg} - expected the same number of images")
|
49
|
+
expected.length.times do |n|
|
50
|
+
assert_equal(expected[n][:filename], actual[n][:filename], "#{msg} - unexpected filename at index #{n}")
|
51
|
+
assert_equal(expected[n][:width], actual[n][:width], "#{msg} - unexpected width at index #{n}")
|
52
|
+
assert_equal(expected[n][:height], actual[n][:height], "#{msg} - unexpected height at index #{n}")
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
#----------------------------------------------------------------------------
|
57
|
+
|
58
|
+
end
|
data/test/runner_test.rb
ADDED
@@ -0,0 +1,229 @@
|
|
1
|
+
require File.expand_path('test_case', File.dirname(__FILE__))
|
2
|
+
|
3
|
+
module SpriteFactory
|
4
|
+
class RunnerTest < SpriteFactory::TestCase
|
5
|
+
|
6
|
+
#----------------------------------------------------------------------------
|
7
|
+
|
8
|
+
def test_defaults
|
9
|
+
|
10
|
+
Runner.publicize_methods do
|
11
|
+
|
12
|
+
r = Runner.new(REGULAR_PATH)
|
13
|
+
assert_equal(REGULAR_PATH, r.input)
|
14
|
+
assert_equal(REGULAR_PATH, r.output)
|
15
|
+
assert_equal(REGULAR_PATH + ".png", r.output_image_file)
|
16
|
+
assert_equal(REGULAR_PATH + ".css", r.output_style_file)
|
17
|
+
assert_equal(REGULAR, r.image_files)
|
18
|
+
assert_equal(:horizontal, r.layout_name)
|
19
|
+
assert_equal(:css, r.style_name)
|
20
|
+
assert_equal(:rmagick, r.library_name)
|
21
|
+
|
22
|
+
r = Runner.new(IRREGULAR_PATH)
|
23
|
+
assert_equal(IRREGULAR_PATH, r.input)
|
24
|
+
assert_equal(IRREGULAR_PATH, r.output)
|
25
|
+
assert_equal(IRREGULAR_PATH + ".png", r.output_image_file)
|
26
|
+
assert_equal(IRREGULAR_PATH + ".css", r.output_style_file)
|
27
|
+
assert_equal(IRREGULAR, r.image_files)
|
28
|
+
assert_equal(:horizontal, r.layout_name)
|
29
|
+
assert_equal(:css, r.style_name)
|
30
|
+
assert_equal(:rmagick, r.library_name)
|
31
|
+
|
32
|
+
r = Runner.new(REGULAR_PATH, :output => IRREGULAR_PATH)
|
33
|
+
assert_equal(REGULAR_PATH, r.input)
|
34
|
+
assert_equal(IRREGULAR_PATH, r.output)
|
35
|
+
assert_equal(IRREGULAR_PATH + ".png", r.output_image_file)
|
36
|
+
assert_equal(IRREGULAR_PATH + ".css", r.output_style_file)
|
37
|
+
assert_equal(REGULAR, r.image_files)
|
38
|
+
assert_equal(:horizontal, r.layout_name)
|
39
|
+
assert_equal(:css, r.style_name)
|
40
|
+
assert_equal(:rmagick, r.library_name)
|
41
|
+
|
42
|
+
r = Runner.new(REGULAR_PATH, :output_image => "foo.png", :output_style => "bar.css.sass.erb")
|
43
|
+
assert_equal(REGULAR_PATH, r.input)
|
44
|
+
assert_equal(REGULAR_PATH, r.output)
|
45
|
+
assert_equal("foo.png", r.output_image_file)
|
46
|
+
assert_equal("bar.css.sass.erb", r.output_style_file)
|
47
|
+
assert_equal(REGULAR, r.image_files)
|
48
|
+
assert_equal(:horizontal, r.layout_name)
|
49
|
+
assert_equal(:css, r.style_name)
|
50
|
+
assert_equal(:rmagick, r.library_name)
|
51
|
+
|
52
|
+
r = Runner.new(REGULAR_PATH, :layout => :vertical, :library => :chunkypng, :style => :sass)
|
53
|
+
assert_equal(REGULAR_PATH, r.input)
|
54
|
+
assert_equal(REGULAR_PATH, r.output)
|
55
|
+
assert_equal(REGULAR_PATH + ".png", r.output_image_file)
|
56
|
+
assert_equal(REGULAR_PATH + ".sass", r.output_style_file)
|
57
|
+
assert_equal(REGULAR, r.image_files)
|
58
|
+
assert_equal(:vertical, r.layout_name)
|
59
|
+
assert_equal(:sass, r.style_name)
|
60
|
+
assert_equal(:chunkypng, r.library_name)
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
#----------------------------------------------------------------------------
|
67
|
+
|
68
|
+
def test_default_padding
|
69
|
+
Runner.publicize_methods do
|
70
|
+
r = Runner.new(REGULAR_PATH)
|
71
|
+
assert_equal(0, r.hpadding)
|
72
|
+
assert_equal(0, r.vpadding)
|
73
|
+
|
74
|
+
r = Runner.new(REGULAR_PATH, :padding => 10)
|
75
|
+
assert_equal(10, r.hpadding)
|
76
|
+
assert_equal(10, r.vpadding)
|
77
|
+
|
78
|
+
r = Runner.new(REGULAR_PATH, :hpadding => 10, :vpadding => 20)
|
79
|
+
assert_equal(10, r.hpadding)
|
80
|
+
assert_equal(20, r.vpadding)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_default_margin
|
85
|
+
Runner.publicize_methods do
|
86
|
+
r = Runner.new(REGULAR_PATH)
|
87
|
+
assert_equal(0, r.hmargin)
|
88
|
+
assert_equal(0, r.vmargin)
|
89
|
+
|
90
|
+
r = Runner.new(REGULAR_PATH, :margin => 10)
|
91
|
+
assert_equal(10, r.hmargin)
|
92
|
+
assert_equal(10, r.vmargin)
|
93
|
+
|
94
|
+
r = Runner.new(REGULAR_PATH, :hmargin => 10, :vmargin => 20)
|
95
|
+
assert_equal(10, r.hmargin)
|
96
|
+
assert_equal(20, r.vmargin)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
#----------------------------------------------------------------------------
|
101
|
+
|
102
|
+
def test_default_css_url
|
103
|
+
Runner.publicize_methods do
|
104
|
+
r1 = Runner.new(REGULAR_PATH)
|
105
|
+
r2 = Runner.new(REGULAR_PATH, :cssurl => "http://s3.amazonaws.com/sf")
|
106
|
+
r3 = Runner.new(REGULAR_PATH, :cssurl => "foo(<%= image_path('$IMAGE') %>)")
|
107
|
+
r4 = Runner.new(REGULAR_PATH, :cssurl => lambda{|image| "foo(/very/dynamic/path/#{image})" })
|
108
|
+
|
109
|
+
assert_equal("url(regular.png)", r1.css_url, "by default, css_url should be basename of the generated sprite image")
|
110
|
+
assert_equal("url(http://s3.amazonaws.com/sf/regular.png)", r2.css_url, "allow customization by prepending to basename of the generated sprite image")
|
111
|
+
assert_equal("foo(<%= image_path('regular.png') %>)", r3.css_url, "allow customization by providing custom format string with $IMAGE token to be replaced with basename of the generated sprite image")
|
112
|
+
assert_equal("foo(/very/dynamic/path/regular.png)", r4.css_url, "allow customization by lambda function - allow caller to decide how to generate css url to sprite image")
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
#----------------------------------------------------------------------------
|
117
|
+
|
118
|
+
def test_trailing_slash_on_input_is_ignored
|
119
|
+
Runner.publicize_methods do
|
120
|
+
r = Runner.new("#{REGULAR_PATH}/")
|
121
|
+
assert_equal(REGULAR_PATH, r.input)
|
122
|
+
assert_equal(REGULAR_PATH, r.output)
|
123
|
+
assert_equal(REGULAR_PATH + ".png", r.output_image_file)
|
124
|
+
assert_equal(REGULAR_PATH + ".css", r.output_style_file)
|
125
|
+
assert_equal(REGULAR, r.image_files)
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
#----------------------------------------------------------------------------
|
130
|
+
|
131
|
+
def test_invalid_config
|
132
|
+
|
133
|
+
assert_runtime_error "input must be a single directory" do
|
134
|
+
SpriteFactory.run!("")
|
135
|
+
end
|
136
|
+
|
137
|
+
assert_runtime_error "input must be a single directory" do
|
138
|
+
SpriteFactory.run!(REGULAR.first)
|
139
|
+
end
|
140
|
+
|
141
|
+
assert_runtime_error "no output file specified" do
|
142
|
+
SpriteFactory.run!(REGULAR_PATH, :output => "")
|
143
|
+
end
|
144
|
+
|
145
|
+
assert_runtime_error "no image files found" do
|
146
|
+
SpriteFactory.run!(EMPTY_PATH)
|
147
|
+
end
|
148
|
+
|
149
|
+
assert_runtime_error "unknown layout diagonal" do
|
150
|
+
SpriteFactory.run!(REGULAR_PATH, :layout => :diagonal)
|
151
|
+
end
|
152
|
+
|
153
|
+
assert_runtime_error "unknown style funky" do
|
154
|
+
SpriteFactory.run!(REGULAR_PATH, :style => :funky)
|
155
|
+
end
|
156
|
+
|
157
|
+
assert_runtime_error "unknown library hogwarts" do
|
158
|
+
SpriteFactory.run!(REGULAR_PATH, :library => :hogwarts)
|
159
|
+
end
|
160
|
+
|
161
|
+
assert_runtime_error "set :width for fixed width, or :hpadding for horizontal padding, but not both." do
|
162
|
+
SpriteFactory.run!(REGULAR_PATH, :width => 50, :padding => 10)
|
163
|
+
end
|
164
|
+
|
165
|
+
assert_runtime_error "set :height for fixed height, or :vpadding for vertical padding, but not both." do
|
166
|
+
SpriteFactory.run!(REGULAR_PATH, :height => 50, :padding => 10)
|
167
|
+
end
|
168
|
+
|
169
|
+
assert_runtime_error "set :width for fixed width, or :hmargin for horizontal margin, but not both." do
|
170
|
+
SpriteFactory.run!(REGULAR_PATH, :width => 50, :margin => 10)
|
171
|
+
end
|
172
|
+
|
173
|
+
assert_runtime_error "set :height for fixed height, or :vmargin for vertical margin, but not both." do
|
174
|
+
SpriteFactory.run!(REGULAR_PATH, :height => 50, :margin => 10)
|
175
|
+
end
|
176
|
+
|
177
|
+
assert_runtime_error "image regular1 does not fit within a fixed width of 10" do
|
178
|
+
SpriteFactory.run!(REGULAR_PATH, :width => 10)
|
179
|
+
end
|
180
|
+
|
181
|
+
assert_runtime_error "image regular1 does not fit within a fixed height of 10" do
|
182
|
+
SpriteFactory.run!(REGULAR_PATH, :height => 10)
|
183
|
+
end
|
184
|
+
|
185
|
+
end
|
186
|
+
|
187
|
+
#----------------------------------------------------------------------------
|
188
|
+
|
189
|
+
def test_images_are_sorted_in_classname_order
|
190
|
+
Runner.publicize_methods do
|
191
|
+
expected = [
|
192
|
+
"alice",
|
193
|
+
"codeincomplete",
|
194
|
+
"github",
|
195
|
+
"monkey",
|
196
|
+
"spies",
|
197
|
+
"stackoverflow",
|
198
|
+
"thief"
|
199
|
+
]
|
200
|
+
actual = Runner.new(FORMATS_PATH).load_images.map{|i| i[:name]}
|
201
|
+
assert_equal(expected, actual)
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
def test_images_are_secondary_sorted_on_psuedoclass
|
206
|
+
Runner.publicize_methods do
|
207
|
+
expected = [
|
208
|
+
"div.bar img.icon",
|
209
|
+
"div.bar img.icon:link",
|
210
|
+
"div.bar img.icon:visited",
|
211
|
+
"div.bar img.icon:focus",
|
212
|
+
"div.bar img.icon:hover",
|
213
|
+
"div.bar img.icon:active",
|
214
|
+
"div.foo img.icon",
|
215
|
+
"div.foo img.icon:link",
|
216
|
+
"div.foo img.icon:visited",
|
217
|
+
"div.foo img.icon:focus",
|
218
|
+
"div.foo img.icon:hover",
|
219
|
+
"div.foo img.icon:active",
|
220
|
+
]
|
221
|
+
actual = Runner.new(HOVER_PATH).load_images.map{|i| i[:name]}
|
222
|
+
assert_equal(expected, actual)
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
#----------------------------------------------------------------------------
|
227
|
+
|
228
|
+
end
|
229
|
+
end
|
data/test/style_test.rb
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
require File.expand_path('test_case', File.dirname(__FILE__))
|
2
|
+
|
3
|
+
module SpriteFactory
|
4
|
+
class StyleTest < SpriteFactory::TestCase
|
5
|
+
|
6
|
+
TEST_ATTRIBUTES = [
|
7
|
+
"width: 30px",
|
8
|
+
"height: 40px",
|
9
|
+
"background: url(path/to/image.png) -10px -20px no-repeat"
|
10
|
+
]
|
11
|
+
|
12
|
+
#--------------------------------------------------------------------------
|
13
|
+
|
14
|
+
def test_css
|
15
|
+
expected = "img.foo { width: 30px; height: 40px; background: url(path/to/image.png) -10px -20px no-repeat; }"
|
16
|
+
actual = Style.css("img.", "foo", TEST_ATTRIBUTES)
|
17
|
+
assert_equal(expected, actual)
|
18
|
+
end
|
19
|
+
|
20
|
+
#--------------------------------------------------------------------------
|
21
|
+
|
22
|
+
def test_scss
|
23
|
+
expected = "img.foo { width: 30px; height: 40px; background: url(path/to/image.png) -10px -20px no-repeat; }"
|
24
|
+
actual = Style.scss("img.", "foo", TEST_ATTRIBUTES)
|
25
|
+
assert_equal(expected, actual)
|
26
|
+
end
|
27
|
+
|
28
|
+
#--------------------------------------------------------------------------
|
29
|
+
|
30
|
+
def test_sass
|
31
|
+
expected = "img.foo\n width: 30px\n height: 40px\n background: url(path/to/image.png) -10px -20px no-repeat\n"
|
32
|
+
actual = Style.sass("img.", "foo", TEST_ATTRIBUTES)
|
33
|
+
assert_equal(expected, actual)
|
34
|
+
end
|
35
|
+
|
36
|
+
#--------------------------------------------------------------------------
|
37
|
+
|
38
|
+
def test_generate
|
39
|
+
|
40
|
+
style = :css
|
41
|
+
selector = 'img.'
|
42
|
+
url = 'url(/path/to/sprite.png)'
|
43
|
+
images = [
|
44
|
+
{:filename => '/path/to/sprite1.png', :name => 'sprite1', :cssx => 1, :cssy => 10, :cssw => 100, :cssh => 1000},
|
45
|
+
{:filename => '/path/to/sprite2.png', :name => 'sprite2', :cssx => 2, :cssy => 20, :cssw => 200, :cssh => 2000},
|
46
|
+
{:filename => '/path/to/sprite3.png', :name => 'sprite3', :cssx => 3, :cssy => 30, :cssw => 300, :cssh => 3000},
|
47
|
+
{:filename => '/path/to/sprite4.png', :name => 'sprite4', :cssx => 4, :cssy => 40, :cssw => 400, :cssh => 4000},
|
48
|
+
{:filename => '/path/to/sprite5.png', :name => 'sprite5', :cssx => 5, :cssy => 50, :cssw => 500, :cssh => 5000}
|
49
|
+
]
|
50
|
+
|
51
|
+
lines = Style.generate(style, selector, url, images).split("\n")
|
52
|
+
|
53
|
+
assert_equal("width: 100px; height: 1000px; background: url(/path/to/sprite.png) -1px -10px no-repeat", images[0][:style], "pure style should have been stashed away in image[:style] for use by custom rule builders")
|
54
|
+
assert_equal("width: 200px; height: 2000px; background: url(/path/to/sprite.png) -2px -20px no-repeat", images[1][:style], "pure style should have been stashed away in image[:style] for use by custom rule builders")
|
55
|
+
assert_equal("width: 300px; height: 3000px; background: url(/path/to/sprite.png) -3px -30px no-repeat", images[2][:style], "pure style should have been stashed away in image[:style] for use by custom rule builders")
|
56
|
+
assert_equal("width: 400px; height: 4000px; background: url(/path/to/sprite.png) -4px -40px no-repeat", images[3][:style], "pure style should have been stashed away in image[:style] for use by custom rule builders")
|
57
|
+
assert_equal("width: 500px; height: 5000px; background: url(/path/to/sprite.png) -5px -50px no-repeat", images[4][:style], "pure style should have been stashed away in image[:style] for use by custom rule builders")
|
58
|
+
|
59
|
+
assert_equal("img.sprite1 { #{images[0][:style]}; }", lines[0], "appropriate rule should have been generated using the pure style stashed away in image[:style]")
|
60
|
+
assert_equal("img.sprite2 { #{images[1][:style]}; }", lines[1], "appropriate rule should have been generated using the pure style stashed away in image[:style]")
|
61
|
+
assert_equal("img.sprite3 { #{images[2][:style]}; }", lines[2], "appropriate rule should have been generated using the pure style stashed away in image[:style]")
|
62
|
+
assert_equal("img.sprite4 { #{images[3][:style]}; }", lines[3], "appropriate rule should have been generated using the pure style stashed away in image[:style]")
|
63
|
+
assert_equal("img.sprite5 { #{images[4][:style]}; }", lines[4], "appropriate rule should have been generated using the pure style stashed away in image[:style]")
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
#--------------------------------------------------------------------------
|
68
|
+
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
|
data/test/test_case.rb
ADDED
@@ -0,0 +1,138 @@
|
|
1
|
+
require File.expand_path('../lib/sprite_factory', File.dirname(__FILE__))
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
module SpriteFactory
|
5
|
+
class TestCase < Test::Unit::TestCase
|
6
|
+
|
7
|
+
#----------------------------------------------------------------------------
|
8
|
+
|
9
|
+
IMAGES_PATH = 'test/images'
|
10
|
+
|
11
|
+
REFERENCE_PATH = 'test/images/reference'
|
12
|
+
REGULAR_PATH = 'test/images/regular'
|
13
|
+
IRREGULAR_PATH = 'test/images/irregular'
|
14
|
+
CUSTOM_PATH = 'test/images/custom'
|
15
|
+
FORMATS_PATH = 'test/images/formats'
|
16
|
+
EMPTY_PATH = 'test/images/empty'
|
17
|
+
SUBFOLDERS_PATH = 'test/images/subfolders'
|
18
|
+
HOVER_PATH = 'test/images/hover'
|
19
|
+
|
20
|
+
REGULAR = SpriteFactory.find_files(File.join(REGULAR_PATH, '*.png'))
|
21
|
+
IRREGULAR = SpriteFactory.find_files(File.join(IRREGULAR_PATH, '*.png'))
|
22
|
+
|
23
|
+
REGULAR_INFO = [
|
24
|
+
{ :filename => REGULAR[0], :width => 64, :height => 64 },
|
25
|
+
{ :filename => REGULAR[1], :width => 64, :height => 64 },
|
26
|
+
{ :filename => REGULAR[2], :width => 64, :height => 64 },
|
27
|
+
{ :filename => REGULAR[3], :width => 64, :height => 64 },
|
28
|
+
{ :filename => REGULAR[4], :width => 64, :height => 64 }
|
29
|
+
]
|
30
|
+
|
31
|
+
IRREGULAR_INFO = [
|
32
|
+
{ :filename => IRREGULAR[0], :width => 60, :height => 60 },
|
33
|
+
{ :filename => IRREGULAR[1], :width => 16, :height => 16 },
|
34
|
+
{ :filename => IRREGULAR[2], :width => 48, :height => 48 },
|
35
|
+
{ :filename => IRREGULAR[3], :width => 34, :height => 14 },
|
36
|
+
{ :filename => IRREGULAR[4], :width => 46, :height => 25 }
|
37
|
+
]
|
38
|
+
|
39
|
+
def output_path(name)
|
40
|
+
File.join(IMAGES_PATH, name)
|
41
|
+
end
|
42
|
+
|
43
|
+
def reference_path(name)
|
44
|
+
File.join(REFERENCE_PATH, name)
|
45
|
+
end
|
46
|
+
|
47
|
+
#----------------------------------------------------------------------------
|
48
|
+
|
49
|
+
def integration_test(input, options = {}, &block)
|
50
|
+
output = options[:output] || input
|
51
|
+
with_clean_output do
|
52
|
+
SpriteFactory.run!(input, options, &block)
|
53
|
+
assert_reference_image(File.basename(output) + "." + ( :png).to_s)
|
54
|
+
assert_reference_style(File.basename(output) + "." + (options[:style] || :css).to_s)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
#----------------------------------------------------------------------------
|
59
|
+
|
60
|
+
def with_clean_output
|
61
|
+
begin
|
62
|
+
clean_output
|
63
|
+
yield
|
64
|
+
ensure
|
65
|
+
clean_output
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def clean_output
|
70
|
+
SpriteFactory.find_files(File.join(IMAGES_PATH, "*.png"),
|
71
|
+
File.join(IMAGES_PATH, "*.css"),
|
72
|
+
File.join(IMAGES_PATH, "*.sass"),
|
73
|
+
File.join(IMAGES_PATH, "*.scss")).each do |f|
|
74
|
+
File.delete(f)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
#----------------------------------------------------------------------------
|
79
|
+
|
80
|
+
def assert_runtime_error(msg = nil)
|
81
|
+
e = assert_raise RuntimeError do
|
82
|
+
yield
|
83
|
+
end
|
84
|
+
assert_match(msg, e.message) if msg
|
85
|
+
end
|
86
|
+
|
87
|
+
def assert_not_implemented(msg = nil)
|
88
|
+
e = assert_raise NotImplementedError do
|
89
|
+
yield
|
90
|
+
end
|
91
|
+
assert_match(msg, e.message) if msg
|
92
|
+
end
|
93
|
+
|
94
|
+
#----------------------------------------------------------------------------
|
95
|
+
|
96
|
+
def assert_reference_image(name)
|
97
|
+
#
|
98
|
+
# images generated with different libraries (or different versions of same library)
|
99
|
+
# might have different headers, so use rmagick to actually compare image bytes
|
100
|
+
#
|
101
|
+
actual = output_path(name)
|
102
|
+
expected = reference_path(name)
|
103
|
+
actual_image = Magick::Image.read(actual)[0]
|
104
|
+
expected_image = Magick::Image.read(expected)[0]
|
105
|
+
img, val = expected_image.compare_channel(actual_image, Magick::MeanAbsoluteErrorMetric)
|
106
|
+
assert_equal(0.0, val, "generated image does not match pregenerated reference:\n actual: #{actual}\n expected: #{expected}")
|
107
|
+
end
|
108
|
+
|
109
|
+
def assert_reference_style(name)
|
110
|
+
actual = output_path(name)
|
111
|
+
expected = reference_path(name)
|
112
|
+
diff = `cmp #{expected} #{actual}`
|
113
|
+
assert(diff.empty?, "generated styles do not match pregenerated reference:\n#{diff}")
|
114
|
+
end
|
115
|
+
|
116
|
+
#----------------------------------------------------------------------------
|
117
|
+
|
118
|
+
end # class TestCase
|
119
|
+
end # module SpriteFactory
|
120
|
+
|
121
|
+
|
122
|
+
#==============================================================================
|
123
|
+
|
124
|
+
class Class
|
125
|
+
|
126
|
+
# allow tests to call private methods without ugly #send syntax
|
127
|
+
def publicize_methods
|
128
|
+
methods = private_instance_methods
|
129
|
+
send(:public, *methods)
|
130
|
+
yield
|
131
|
+
send(:private, *methods)
|
132
|
+
end
|
133
|
+
|
134
|
+
end
|
135
|
+
|
136
|
+
#==============================================================================
|
137
|
+
|
138
|
+
|