sprite-factory 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. data/LICENSE +20 -0
  2. data/README.md +207 -0
  3. data/Rakefile +67 -0
  4. data/bin/sf +46 -0
  5. data/lib/sprite_factory.rb +51 -0
  6. data/lib/sprite_factory/layout.rb +89 -0
  7. data/lib/sprite_factory/library/chunky_png.rb +31 -0
  8. data/lib/sprite_factory/library/rmagick.rb +32 -0
  9. data/lib/sprite_factory/runner.rb +204 -0
  10. data/lib/sprite_factory/style.rb +58 -0
  11. data/sprite_factory.gemspec +24 -0
  12. data/test/images/custom/custom.css +4 -0
  13. data/test/images/custom/running.png +0 -0
  14. data/test/images/custom/stopped.png +0 -0
  15. data/test/images/empty/readme.txt +1 -0
  16. data/test/images/formats/alice.gif +0 -0
  17. data/test/images/formats/monkey.gif +0 -0
  18. data/test/images/formats/spies.jpg +0 -0
  19. data/test/images/formats/thief.png +0 -0
  20. data/test/images/irregular/irregular1.png +0 -0
  21. data/test/images/irregular/irregular2.png +0 -0
  22. data/test/images/irregular/irregular3.png +0 -0
  23. data/test/images/irregular/irregular4.png +0 -0
  24. data/test/images/irregular/irregular5.png +0 -0
  25. data/test/images/irregular/readme.txt +2 -0
  26. data/test/images/reference/custom.css +22 -0
  27. data/test/images/reference/custom.png +0 -0
  28. data/test/images/reference/formats.css +22 -0
  29. data/test/images/reference/formats.png +0 -0
  30. data/test/images/reference/index.html +135 -0
  31. data/test/images/reference/irregular.css +24 -0
  32. data/test/images/reference/irregular.fixed.css +24 -0
  33. data/test/images/reference/irregular.fixed.png +0 -0
  34. data/test/images/reference/irregular.horizontal.css +24 -0
  35. data/test/images/reference/irregular.horizontal.png +0 -0
  36. data/test/images/reference/irregular.padded.css +24 -0
  37. data/test/images/reference/irregular.padded.png +0 -0
  38. data/test/images/reference/irregular.png +0 -0
  39. data/test/images/reference/irregular.sassy.css +38 -0
  40. data/test/images/reference/irregular.sassy.png +0 -0
  41. data/test/images/reference/irregular.sassy.sass +40 -0
  42. data/test/images/reference/irregular.vertical.css +24 -0
  43. data/test/images/reference/irregular.vertical.png +0 -0
  44. data/test/images/reference/regular.css +24 -0
  45. data/test/images/reference/regular.custom.css +24 -0
  46. data/test/images/reference/regular.custom.png +0 -0
  47. data/test/images/reference/regular.fixed.css +24 -0
  48. data/test/images/reference/regular.fixed.png +0 -0
  49. data/test/images/reference/regular.horizontal.css +24 -0
  50. data/test/images/reference/regular.horizontal.png +0 -0
  51. data/test/images/reference/regular.padded.css +24 -0
  52. data/test/images/reference/regular.padded.png +0 -0
  53. data/test/images/reference/regular.png +0 -0
  54. data/test/images/reference/regular.sassy.css +38 -0
  55. data/test/images/reference/regular.sassy.png +0 -0
  56. data/test/images/reference/regular.sassy.sass +40 -0
  57. data/test/images/reference/regular.vertical.css +24 -0
  58. data/test/images/reference/regular.vertical.png +0 -0
  59. data/test/images/reference/s.gif +0 -0
  60. data/test/images/regular/regular1.png +0 -0
  61. data/test/images/regular/regular2.png +0 -0
  62. data/test/images/regular/regular3.png +0 -0
  63. data/test/images/regular/regular4.png +0 -0
  64. data/test/images/regular/regular5.png +0 -0
  65. data/test/integration_test.rb +100 -0
  66. data/test/layout_test.rb +228 -0
  67. data/test/library_test.rb +57 -0
  68. data/test/runner_test.rb +156 -0
  69. data/test/style_test.rb +64 -0
  70. data/test/test_case.rb +127 -0
  71. metadata +159 -0
@@ -0,0 +1,64 @@
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_sass
23
+ expected = "img.foo\n width: 30px\n height: 40px\n background: url(path/to/image.png) -10px -20px no-repeat\n"
24
+ actual = Style.sass("img.", "foo", TEST_ATTRIBUTES)
25
+ assert_equal(expected, actual)
26
+ end
27
+
28
+ #--------------------------------------------------------------------------
29
+
30
+ def test_generate
31
+
32
+ style = :css
33
+ selector = 'img.'
34
+ path = '/path/to/sprite.png'
35
+ images = [
36
+ {:filename => '/path/to/sprite1.png', :name => 'sprite1', :cssx => 1, :cssy => 10, :cssw => 100, :cssh => 1000},
37
+ {:filename => '/path/to/sprite2.png', :name => 'sprite2', :cssx => 2, :cssy => 20, :cssw => 200, :cssh => 2000},
38
+ {:filename => '/path/to/sprite3.png', :name => 'sprite3', :cssx => 3, :cssy => 30, :cssw => 300, :cssh => 3000},
39
+ {:filename => '/path/to/sprite4.png', :name => 'sprite4', :cssx => 4, :cssy => 40, :cssw => 400, :cssh => 4000},
40
+ {:filename => '/path/to/sprite5.png', :name => 'sprite5', :cssx => 5, :cssy => 50, :cssw => 500, :cssh => 5000}
41
+ ]
42
+
43
+ lines = Style.generate(style, selector, path, images).split("\n")
44
+
45
+ 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")
46
+ 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")
47
+ 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")
48
+ 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")
49
+ 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")
50
+
51
+ assert_equal("img.sprite1 { #{images[0][:style]}; }", lines[0], "appropriate rule should have been generated using the pure style stashed away in image[:style]")
52
+ assert_equal("img.sprite2 { #{images[1][:style]}; }", lines[1], "appropriate rule should have been generated using the pure style stashed away in image[:style]")
53
+ assert_equal("img.sprite3 { #{images[2][:style]}; }", lines[2], "appropriate rule should have been generated using the pure style stashed away in image[:style]")
54
+ assert_equal("img.sprite4 { #{images[3][:style]}; }", lines[3], "appropriate rule should have been generated using the pure style stashed away in image[:style]")
55
+ assert_equal("img.sprite5 { #{images[4][:style]}; }", lines[4], "appropriate rule should have been generated using the pure style stashed away in image[:style]")
56
+
57
+ end
58
+
59
+ #--------------------------------------------------------------------------
60
+
61
+ end
62
+ end
63
+
64
+
@@ -0,0 +1,127 @@
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
+
18
+ REGULAR = Dir[File.join(REGULAR_PATH, '*.png')].sort
19
+ IRREGULAR = Dir[File.join(IRREGULAR_PATH, '*.png')].sort
20
+
21
+ REGULAR_INFO = [
22
+ { :filename => REGULAR[0], :width => 64, :height => 64 },
23
+ { :filename => REGULAR[1], :width => 64, :height => 64 },
24
+ { :filename => REGULAR[2], :width => 64, :height => 64 },
25
+ { :filename => REGULAR[3], :width => 64, :height => 64 },
26
+ { :filename => REGULAR[4], :width => 64, :height => 64 }
27
+ ]
28
+
29
+ IRREGULAR_INFO = [
30
+ { :filename => IRREGULAR[0], :width => 60, :height => 60 },
31
+ { :filename => IRREGULAR[1], :width => 16, :height => 16 },
32
+ { :filename => IRREGULAR[2], :width => 48, :height => 48 },
33
+ { :filename => IRREGULAR[3], :width => 34, :height => 14 },
34
+ { :filename => IRREGULAR[4], :width => 46, :height => 25 }
35
+ ]
36
+
37
+ def output_path(name)
38
+ File.join(IMAGES_PATH, name)
39
+ end
40
+
41
+ def reference_path(name)
42
+ File.join(REFERENCE_PATH, name)
43
+ end
44
+
45
+ #----------------------------------------------------------------------------
46
+
47
+ def integration_test(input, options = {}, &block)
48
+ output = options[:output] || input
49
+ with_clean_output do
50
+ SpriteFactory.run!(input, options, &block)
51
+ assert_reference_image(File.basename(output) + "." + ( :png).to_s)
52
+ assert_reference_style(File.basename(output) + "." + (options[:style] || :css).to_s)
53
+ end
54
+ end
55
+
56
+ #----------------------------------------------------------------------------
57
+
58
+ def with_clean_output
59
+ begin
60
+ clean_output
61
+ yield
62
+ ensure
63
+ clean_output
64
+ end
65
+ end
66
+
67
+ def clean_output
68
+ Dir[File.join(IMAGES_PATH, "*.png"),
69
+ File.join(IMAGES_PATH, "*.css"),
70
+ File.join(IMAGES_PATH, "*.sass"),
71
+ File.join(IMAGES_PATH, "*.scss")].each {|f| File.delete(f)}
72
+ end
73
+
74
+ #----------------------------------------------------------------------------
75
+
76
+ def assert_runtime_error(msg = nil)
77
+ e = assert_raise RuntimeError do
78
+ yield
79
+ end
80
+ assert_match(msg, e.message) if msg
81
+ end
82
+
83
+ #----------------------------------------------------------------------------
84
+
85
+ def assert_reference_image(name)
86
+ #
87
+ # images generated with different libraries (or different versions of same library)
88
+ # might have different headers, so use rmagick to actually compare image bytes
89
+ #
90
+ actual = output_path(name)
91
+ expected = reference_path(name)
92
+ actual_image = Magick::Image.read(actual)[0]
93
+ expected_image = Magick::Image.read(expected)[0]
94
+ img, val = expected_image.compare_channel(actual_image, Magick::MeanAbsoluteErrorMetric)
95
+ assert_equal(0.0, val, "generated image does not match pregenerated reference:\n actual: #{actual}\n expected: #{expected}")
96
+ end
97
+
98
+ def assert_reference_style(name)
99
+ actual = output_path(name)
100
+ expected = reference_path(name)
101
+ diff = `cmp #{expected} #{actual}`
102
+ assert(diff.empty?, "generated styles do not match pregenerated reference:\n#{diff}")
103
+ end
104
+
105
+ #----------------------------------------------------------------------------
106
+
107
+ end # class TestCase
108
+ end # module SpriteFactory
109
+
110
+
111
+ #==============================================================================
112
+
113
+ class Class
114
+
115
+ # allow tests to call private methods without ugly #send syntax
116
+ def publicize_methods
117
+ methods = private_instance_methods
118
+ send(:public, *methods)
119
+ yield
120
+ send(:private, *methods)
121
+ end
122
+
123
+ end
124
+
125
+ #==============================================================================
126
+
127
+
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sprite-factory
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 0
9
+ version: 1.0.0
10
+ platform: ruby
11
+ authors:
12
+ - Jake Gordon
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-04-29 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rmagick
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :development
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: chunky_png
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ version: "0"
44
+ type: :development
45
+ version_requirements: *id002
46
+ description: Combines individual images from a directory into a single sprite image file and creates an appropriate CSS stylesheet
47
+ email:
48
+ - jake@codeincomplete.com
49
+ executables:
50
+ - sf
51
+ extensions: []
52
+
53
+ extra_rdoc_files: []
54
+
55
+ files:
56
+ - LICENSE
57
+ - README.md
58
+ - Rakefile
59
+ - bin/sf
60
+ - lib/sprite_factory.rb
61
+ - lib/sprite_factory/layout.rb
62
+ - lib/sprite_factory/library/chunky_png.rb
63
+ - lib/sprite_factory/library/rmagick.rb
64
+ - lib/sprite_factory/runner.rb
65
+ - lib/sprite_factory/style.rb
66
+ - sprite_factory.gemspec
67
+ - test/images/custom/custom.css
68
+ - test/images/custom/running.png
69
+ - test/images/custom/stopped.png
70
+ - test/images/empty/readme.txt
71
+ - test/images/formats/alice.gif
72
+ - test/images/formats/monkey.gif
73
+ - test/images/formats/spies.jpg
74
+ - test/images/formats/thief.png
75
+ - test/images/irregular/irregular1.png
76
+ - test/images/irregular/irregular2.png
77
+ - test/images/irregular/irregular3.png
78
+ - test/images/irregular/irregular4.png
79
+ - test/images/irregular/irregular5.png
80
+ - test/images/irregular/readme.txt
81
+ - test/images/reference/custom.css
82
+ - test/images/reference/custom.png
83
+ - test/images/reference/formats.css
84
+ - test/images/reference/formats.png
85
+ - test/images/reference/index.html
86
+ - test/images/reference/irregular.css
87
+ - test/images/reference/irregular.fixed.css
88
+ - test/images/reference/irregular.fixed.png
89
+ - test/images/reference/irregular.horizontal.css
90
+ - test/images/reference/irregular.horizontal.png
91
+ - test/images/reference/irregular.padded.css
92
+ - test/images/reference/irregular.padded.png
93
+ - test/images/reference/irregular.png
94
+ - test/images/reference/irregular.sassy.css
95
+ - test/images/reference/irregular.sassy.png
96
+ - test/images/reference/irregular.sassy.sass
97
+ - test/images/reference/irregular.vertical.css
98
+ - test/images/reference/irregular.vertical.png
99
+ - test/images/reference/regular.css
100
+ - test/images/reference/regular.custom.css
101
+ - test/images/reference/regular.custom.png
102
+ - test/images/reference/regular.fixed.css
103
+ - test/images/reference/regular.fixed.png
104
+ - test/images/reference/regular.horizontal.css
105
+ - test/images/reference/regular.horizontal.png
106
+ - test/images/reference/regular.padded.css
107
+ - test/images/reference/regular.padded.png
108
+ - test/images/reference/regular.png
109
+ - test/images/reference/regular.sassy.css
110
+ - test/images/reference/regular.sassy.png
111
+ - test/images/reference/regular.sassy.sass
112
+ - test/images/reference/regular.vertical.css
113
+ - test/images/reference/regular.vertical.png
114
+ - test/images/reference/s.gif
115
+ - test/images/regular/regular1.png
116
+ - test/images/regular/regular2.png
117
+ - test/images/regular/regular3.png
118
+ - test/images/regular/regular4.png
119
+ - test/images/regular/regular5.png
120
+ - test/integration_test.rb
121
+ - test/layout_test.rb
122
+ - test/library_test.rb
123
+ - test/runner_test.rb
124
+ - test/style_test.rb
125
+ - test/test_case.rb
126
+ has_rdoc: true
127
+ homepage: https://github.com/jakesgordon/sprite-factory
128
+ licenses: []
129
+
130
+ post_install_message:
131
+ rdoc_options: []
132
+
133
+ require_paths:
134
+ - lib
135
+ required_ruby_version: !ruby/object:Gem::Requirement
136
+ none: false
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ segments:
141
+ - 0
142
+ version: "0"
143
+ required_rubygems_version: !ruby/object:Gem::Requirement
144
+ none: false
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ segments:
149
+ - 0
150
+ version: "0"
151
+ requirements: []
152
+
153
+ rubyforge_project:
154
+ rubygems_version: 1.3.7
155
+ signing_key:
156
+ specification_version: 3
157
+ summary: Automatic CSS sprite generator
158
+ test_files: []
159
+