This commit is contained in:
Scott Davis 2011-06-27 01:07:53 -04:00
commit dfb7c70113
16 changed files with 276 additions and 149 deletions

View File

@ -1,13 +1,13 @@
GIT GIT
remote: git://github.com/johnbintz/fakefs.git remote: git://github.com/johnbintz/fakefs.git
revision: 005ddaaeb2b2881391c31ac9846a55ce5a42c206 revision: 7363b6f13bfcd9f583bbf7cd1e0d65c2dc656db7
specs: specs:
fakefs (0.3.1) fakefs (0.3.1)
PATH PATH
remote: . remote: .
specs: specs:
compass (0.11.3.rails.0.642d0b2) compass (0.11.3.rails.0.9da1d44)
chunky_png (~> 1.2) chunky_png (~> 1.2)
fssm (>= 0.2.7) fssm (>= 0.2.7)
sass (~> 3.1) sass (~> 3.1)
@ -17,12 +17,12 @@ GEM
specs: specs:
ZenTest (4.5.0) ZenTest (4.5.0)
abstract (1.0.0) abstract (1.0.0)
actionmailer (3.0.7) actionmailer (3.0.9.rc3)
actionpack (= 3.0.7) actionpack (= 3.0.9.rc3)
mail (~> 2.2.15) mail (~> 2.2.19)
actionpack (3.0.7) actionpack (3.0.9.rc3)
activemodel (= 3.0.7) activemodel (= 3.0.9.rc3)
activesupport (= 3.0.7) activesupport (= 3.0.9.rc3)
builder (~> 2.1.2) builder (~> 2.1.2)
erubis (~> 2.6.6) erubis (~> 2.6.6)
i18n (~> 0.5.0) i18n (~> 0.5.0)
@ -30,21 +30,21 @@ GEM
rack-mount (~> 0.6.14) rack-mount (~> 0.6.14)
rack-test (~> 0.5.7) rack-test (~> 0.5.7)
tzinfo (~> 0.3.23) tzinfo (~> 0.3.23)
activemodel (3.0.7) activemodel (3.0.9.rc3)
activesupport (= 3.0.7) activesupport (= 3.0.9.rc3)
builder (~> 2.1.2) builder (~> 2.1.2)
i18n (~> 0.5.0) i18n (~> 0.5.0)
activerecord (3.0.7) activerecord (3.0.9.rc3)
activemodel (= 3.0.7) activemodel (= 3.0.9.rc3)
activesupport (= 3.0.7) activesupport (= 3.0.9.rc3)
arel (~> 2.0.2) arel (~> 2.0.10)
tzinfo (~> 0.3.23) tzinfo (~> 0.3.23)
activeresource (3.0.7) activeresource (3.0.9.rc3)
activemodel (= 3.0.7) activemodel (= 3.0.9.rc3)
activesupport (= 3.0.7) activesupport (= 3.0.9.rc3)
activesupport (3.0.7) activesupport (3.0.9.rc3)
addressable (2.2.5) addressable (2.2.6)
arel (2.0.9) arel (2.0.10)
autotest (4.4.6) autotest (4.4.6)
ZenTest (>= 4.4.1) ZenTest (>= 4.4.1)
autotest-fsevent (0.2.5) autotest-fsevent (0.2.5)
@ -61,7 +61,7 @@ GEM
term-ansicolor (~> 1.0.5) term-ansicolor (~> 1.0.5)
diff-lcs (1.1.2) diff-lcs (1.1.2)
em-dir-watcher (0.9.4) em-dir-watcher (0.9.4)
em-websocket (0.2.1) em-websocket (0.3.0)
addressable (>= 2.1.1) addressable (>= 2.1.1)
eventmachine (>= 0.12.9) eventmachine (>= 0.12.9)
erubis (2.6.6) erubis (2.6.6)
@ -71,14 +71,14 @@ GEM
gherkin (2.2.9) gherkin (2.2.9)
json (~> 1.4.6) json (~> 1.4.6)
term-ansicolor (~> 1.0.5) term-ansicolor (~> 1.0.5)
haml (3.1.1) haml (3.1.2)
i18n (0.5.0) i18n (0.5.0)
json (1.4.6) json (1.4.6)
livereload (1.6) livereload (1.6)
em-dir-watcher (>= 0.1) em-dir-watcher (>= 0.1)
em-websocket (>= 0.1.2) em-websocket (>= 0.1.2)
ruby-json (>= 1.1.2) ruby-json (>= 1.1.2)
mail (2.2.17) mail (2.2.19)
activesupport (>= 2.3.6) activesupport (>= 2.3.6)
i18n (>= 0.4.0) i18n (>= 0.4.0)
mime-types (~> 1.16) mime-types (~> 1.16)
@ -86,27 +86,29 @@ GEM
mime-types (1.16) mime-types (1.16)
mocha (0.9.12) mocha (0.9.12)
polyglot (0.3.1) polyglot (0.3.1)
rack (1.2.2) rack (1.2.3)
rack-mount (0.6.14) rack-mount (0.6.14)
rack (>= 1.0.0) rack (>= 1.0.0)
rack-test (0.5.7) rack-test (0.5.7)
rack (>= 1.0) rack (>= 1.0)
rails (3.0.7) rails (3.0.9.rc3)
actionmailer (= 3.0.7) actionmailer (= 3.0.9.rc3)
actionpack (= 3.0.7) actionpack (= 3.0.9.rc3)
activerecord (= 3.0.7) activerecord (= 3.0.9.rc3)
activeresource (= 3.0.7) activeresource (= 3.0.9.rc3)
activesupport (= 3.0.7) activesupport (= 3.0.9.rc3)
bundler (~> 1.0) bundler (~> 1.0)
railties (= 3.0.7) railties (= 3.0.9.rc3)
railties (3.0.7) railties (3.0.9.rc3)
actionpack (= 3.0.7) actionpack (= 3.0.9.rc3)
activesupport (= 3.0.7) activesupport (= 3.0.9.rc3)
rake (>= 0.8.7) rake (>= 0.8.7)
rdoc (~> 3.4)
thor (~> 0.14.4) thor (~> 0.14.4)
rake (0.8.7) rake (0.9.2)
rb-fsevent (0.4.0) rb-fsevent (0.4.0)
rcov (0.9.9) rcov (0.9.9)
rdoc (3.6.1)
rspec (2.0.1) rspec (2.0.1)
rspec-core (~> 2.0.1) rspec-core (~> 2.0.1)
rspec-expectations (~> 2.0.1) rspec-expectations (~> 2.0.1)
@ -118,16 +120,16 @@ GEM
rspec-core (~> 2.0.1) rspec-core (~> 2.0.1)
rspec-expectations (~> 2.0.1) rspec-expectations (~> 2.0.1)
ruby-json (1.1.2) ruby-json (1.1.2)
ruby-prof (0.10.5) ruby-prof (0.10.7)
rubyzip (0.9.4) rubyzip (0.9.4)
sass (3.1.1) sass (3.1.2)
sys-uname (0.8.5) sys-uname (0.8.5)
term-ansicolor (1.0.5) term-ansicolor (1.0.5)
thor (0.14.6) thor (0.14.6)
timecop (0.3.5) timecop (0.3.5)
treetop (1.4.9) treetop (1.4.9)
polyglot (>= 0.3.1) polyglot (>= 0.3.1)
tzinfo (0.3.26) tzinfo (0.3.27)
PLATFORMS PLATFORMS
ruby ruby

View File

@ -29,6 +29,16 @@ To run with an alternate version of Rails, make test/rails a symlink to that ver
To run with an alternate version of Haml & Sass, make test/haml a symlink to that version. To run with an alternate version of Haml & Sass, make test/haml a symlink to that version.
END END
Rake::TestTask.new :units do |t|
t.libs << 'lib'
t.libs << 'test'
test_files = FileList['test/units/**/*_test.rb']
test_files.exclude('test/rails/*', 'test/haml/*')
t.test_files = test_files
t.verbose = true
end
desc "Compile Examples into HTML and CSS" desc "Compile Examples into HTML and CSS"
task :examples do task :examples do
linked_haml = "tests/haml" linked_haml = "tests/haml"

View File

@ -40,7 +40,7 @@ layout: site
## Manual setup using the Blueprint Framework ## Manual setup using the Blueprint Framework
When starting a project from scrath: When starting a project from scratch:
compass create /path/to/project --using blueprint compass create /path/to/project --using blueprint

View File

@ -52,7 +52,7 @@ module Compass
def check_for_double_boot! def check_for_double_boot!
if booted? if booted?
Compass::Util.compass_warn("Warning: Compass was booted twice. If you're using Rails 3, Compass has a Railtie now; please remove your compass intializer.") Compass::Util.compass_warn("Warning: Compass was booted twice. Compass has a Railtie now; please remove your initializer.")
else else
booted! booted!
end end

View File

@ -39,10 +39,11 @@ module Compass
def perform def perform
relative_uri = options[:uri].gsub(/^#{Compass.configuration.images_dir}\//, '') relative_uri = options[:uri].gsub(/^#{Compass.configuration.images_dir}\//, '')
sprites = Compass::SpriteImporter.new(:uri => relative_uri, :options => Compass.sass_engine_options) name = Compass::SpriteImporter.sprite_name(relative_uri)
options[:output_file] ||= File.join(Compass.configuration.sass_path, "sprites", "_#{sprites.name}.#{Compass.configuration.preferred_syntax}") sprites = Compass::SpriteImporter.new
options[:output_file] ||= File.join(Compass.configuration.sass_path, "sprites", "_#{name}.#{Compass.configuration.preferred_syntax}")
options[:skip_overrides] ||= false options[:skip_overrides] ||= false
contents = sprites.content_for_images(options[:skip_overrides]) contents = Compass::SpriteImporter.content_for_images(relative_uri, name, options[:skip_overrides])
if options[:output_file][-4..-1] != "scss" if options[:output_file][-4..-1] != "scss"
contents = Sass::Engine.new(contents, Compass.sass_engine_options.merge(:syntax => :scss)).to_tree.to_sass contents = Sass::Engine.new(contents, Compass.sass_engine_options.merge(:syntax => :scss)).to_tree.to_sass
end end

View File

@ -9,6 +9,7 @@ module Compass
end end
require 'compass/sass_extensions/sprites/image' require 'compass/sass_extensions/sprites/image'
require 'compass/sass_extensions/sprites/layout_methods'
require 'compass/sass_extensions/sprites/sprite_methods' require 'compass/sass_extensions/sprites/sprite_methods'
require 'compass/sass_extensions/sprites/image_methods' require 'compass/sass_extensions/sprites/image_methods'
require 'compass/sass_extensions/sprites/sprite_map' require 'compass/sass_extensions/sprites/sprite_map'

View File

@ -0,0 +1,85 @@
module Compass
module SassExtensions
module Sprites
module LayoutMethods
HORIZONTAL = 'horizontal'
def horizontal?
@kwargs.get_var('layout').value == HORIZONTAL
end
# Calculates the overal image dimensions
# collects image sizes and input parameters for each sprite
def compute_image_positions!
if horizontal?
calculate_height
calculate_horizontal_positions
calculate_width
else
calculate_width
calulate_vertical_postions
calculate_height
end
end
def calculate_horizontal_positions
@images.each_with_index do |image, index|
image.top = image.position.unit_str == '%' ? (@height - image.height) * (image.position.value / 100.0) : image.position.value
next if index == 0
last_image = @images[index-1]
image.left = last_image.left + last_image.width + [image.offset, last_image.offset].max
end
end
def calulate_vertical_postions
@images.each_with_index do |image, index|
image.left = image.position.unit_str == "%" ? (@width - image.width) * (image.position.value / 100.0) : image.position.value
next if index == 0
last_image = @images[index-1]
image.top = last_image.top + last_image.height + [image.spacing, last_image.spacing].max
end
end
def calculate_dimensions!
calculate_width
calculate_height
end
def calculate_height
@height = if horizontal?
height_for_horizontal_layout
else
height_for_vertical_layout
end
end
def height_for_vertical_layout
last = @images.last
last.top + last.height
end
def height_for_horizontal_layout
@height = @images.map {|image| image.height + image.spacing}.max
end
def calculate_width
@width = if horizontal?
width_for_horizontal_layout
else
width_for_vertical_layout
end
end
def width_for_horizontal_layout
@images.inject(0) { |sum, image| sum += (image.width + image.offset) }
end
def width_for_vertical_layout
@images.map { |image| image.width + image.offset }.max
end
end
end
end
end

View File

@ -7,16 +7,19 @@ module Compass
include SpriteMethods include SpriteMethods
include ImageMethods include ImageMethods
include LayoutMethods
# Initialize a new sprite object from a relative file path # Initialize a new sprite object from a relative file path
# the path is relative to the <tt>images_path</tt> confguration option # the path is relative to the <tt>images_path</tt> confguration option
def self.from_uri(uri, context, kwargs) def self.from_uri(uri, context, kwargs)
importer = ::Compass::SpriteImporter.new(:uri => uri.value, :options => {}) uri = uri.value
sprites = importer.files.map do |sprite| name, path = Compass::SpriteImporter.path_and_name(uri)
sprite.gsub(Compass.configuration.images_path+"/", "") files = Compass::SpriteImporter.files(uri)
sprites = files.map do |sprite|
sprite.gsub("#{Compass.configuration.images_path}/", "")
end end
new(sprites, importer.path, importer.name, context, kwargs) new(sprites, path, name, context, kwargs)
end end
def initialize(sprites, path, name, context, kwargs) def initialize(sprites, path, name, context, kwargs)
@ -25,6 +28,7 @@ module Compass
@name = name @name = name
@kwargs = kwargs @kwargs = kwargs
@kwargs['cleanup'] ||= Sass::Script::Bool.new(true) @kwargs['cleanup'] ||= Sass::Script::Bool.new(true)
@kwargs['layout'] ||= Sass::Script::String.new('vertical')
@images = nil @images = nil
@width = nil @width = nil
@height = nil @height = nil

View File

@ -14,7 +14,6 @@ module Compass
@width = 0 @width = 0
init_images init_images
compute_image_positions! compute_image_positions!
@height = @images.last.top + @images.last.height
init_engine init_engine
end end
@ -29,22 +28,10 @@ module Compass
def init_images def init_images
@images = image_names.collect do |relative_file| @images = image_names.collect do |relative_file|
image = Compass::SassExtensions::Sprites::Image.new(self, relative_file, kwargs) image = Compass::SassExtensions::Sprites::Image.new(self, relative_file, kwargs)
@width = [ @width, image.width + image.offset ].max
image image
end end
end end
# Calculates the overal image dimensions
# collects image sizes and input parameters for each sprite
def compute_image_positions!
@images.each_with_index do |image, index|
image.left = image.position.unit_str == "%" ? (@width - image.width) * (image.position.value / 100) : image.position.value
next if index == 0
last_image = @images[index-1]
image.top = last_image.top + last_image.height + [image.spacing, last_image.spacing].max
end
end
# Validates that the sprite_names are valid sass # Validates that the sprite_names are valid sass
def validate! def validate!
for sprite_name in sprite_names for sprite_name in sprite_names

View File

@ -1,6 +1,5 @@
module Compass module Compass
class SpriteImporter < Sass::Importers::Base class SpriteImporter < Sass::Importers::Base
attr_accessor :uri, :options
VAILD_FILE_NAME = /\A#{Sass::SCSS::RX::IDENT}\Z/ VAILD_FILE_NAME = /\A#{Sass::SCSS::RX::IDENT}\Z/
SPRITE_IMPORTER_REGEX = %r{((.+/)?([^\*.]+))/(.+?)\.png} SPRITE_IMPORTER_REGEX = %r{((.+/)?([^\*.]+))/(.+?)\.png}
VALID_EXTENSIONS = ['.png'] VALID_EXTENSIONS = ['.png']
@ -12,28 +11,14 @@ module Compass
Dir.glob(File.join(path, "**", glob)) Dir.glob(File.join(path, "**", glob))
end end
def self.load(uri, options)
klass = Compass::SpriteImporter.new
klass.uri, klass.options = uri, options
klass
end
def initialize(options ={})
@uri, @options = '', {}
options.each do |key, value|
send("#{key}=", value)
end
end
def find(uri, options) def find(uri, options)
@uri, @options = uri, options
if uri =~ SPRITE_IMPORTER_REGEX if uri =~ SPRITE_IMPORTER_REGEX
return sass_engine return self.class.sass_engine(uri, self.class.sprite_name(uri), self, options)
end end
nil
end end
def find_relative(uri, base, options) def find_relative(uri, base, options)
@uri, @options = uri, options
find(File.join(base, uri), options) find(File.join(base, uri), options)
end end
@ -50,45 +35,43 @@ module Compass
end end
def mtime(uri, options) def mtime(uri, options)
@uri, @options = uri, options self.class.files(uri).sort.inject(Time.at(0)) do |max_time, file|
files.sort.inject(Time.at(0)) do |max_time, file|
(t = File.mtime(file)) > max_time ? t : max_time (t = File.mtime(file)) > max_time ? t : max_time
end end
end end
def key(uri, options={}) def key(uri, options={})
@uri, @options = uri, options [self.class.name + ":sprite:" + File.dirname(File.expand_path(uri)), File.basename(uri)]
[self.class.name + ":" + File.dirname(File.expand_path(uri)), File.basename(uri)]
end end
def self.path_and_name(uri) def self.path_and_name(uri)
if uri =~ SPRITE_IMPORTER_REGEX if uri =~ SPRITE_IMPORTER_REGEX
[$1, $3] [$1, $3]
else else
raise Compass::Error "invalid sprite path" raise Compass::Error, "invalid sprite path"
end end
end end
# Name of this spite # Name of this spite
def name def self.sprite_name(uri)
ensure_path_and_name! _, name = path_and_name(uri)
@name name
end end
# The on-disk location of this sprite # The on-disk location of this sprite
def path def self.path(uri)
ensure_path_and_name! path, _ = path_and_name(uri)
@path path
end end
# Returns the Glob of image files for this sprite # Returns the Glob of image files for the uri
def files def self.files(uri)
Dir[File.join(Compass.configuration.images_path, uri)].sort Dir[File.join(Compass.configuration.images_path, uri)].sort
end end
# Returns an Array of image names without the file extension # Returns an Array of image names without the file extension
def sprite_names def self.sprite_names(uri)
files.collect do |file| files(uri).collect do |file|
filename = File.basename(file, '.png') filename = File.basename(file, '.png')
unless VAILD_FILE_NAME =~ filename unless VAILD_FILE_NAME =~ filename
raise Compass::Error, "Sprite file names must be legal css identifiers. Please rename #{File.basename(file)}" raise Compass::Error, "Sprite file names must be legal css identifiers. Please rename #{File.basename(file)}"
@ -97,31 +80,18 @@ module Compass
end end
end end
def validate_sprites! # Returns the sass _options for this sprite
files.each do |file| def self.sass_options(name, importer, options)
unless VALID_EXTENSIONS.include? File.extname(file) options.merge!(:filename => name, :syntax => :scss, :importer => importer)
raise Compass::Error, "Invalid sprite extension only: #{VALID_EXTENSIONS.join(',')} images are allowed"
end
end
end
# Returns the sass options for this sprite
def sass_options
options.merge(:filename => name, :syntax => :scss, :importer => self)
end end
# Returns a Sass::Engine for this sprite object # Returns a Sass::Engine for this sprite object
def sass_engine def self.sass_engine(uri, name, importer, options)
validate_sprites! Sass::Engine.new(content_for_images(uri, name, options[:skip_overrides]), sass_options(name, importer, options))
Sass::Engine.new(content_for_images, sass_options)
end
def ensure_path_and_name!
@path, @name = self.class.path_and_name(uri)
end end
# Generates the Sass for this sprite file # Generates the Sass for this sprite file
def content_for_images(skip_overrides = false) def self.content_for_images(uri, name, skip_overrides = false)
<<-SCSS <<-SCSS
@import "compass/utilities/sprites/base"; @import "compass/utilities/sprites/base";
@ -134,8 +104,9 @@ $#{name}-spacing: 0 !default;
$#{name}-repeat: no-repeat !default; $#{name}-repeat: no-repeat !default;
$#{name}-prefix: '' !default; $#{name}-prefix: '' !default;
$#{name}-clean-up: true !default; $#{name}-clean-up: true !default;
$#{name}-layout:vertical !default;
#{skip_overrides ? "$#{name}-sprites: sprite-map(\"#{uri}\", $cleanup: $#{name}-clean-up);" : generate_overrides } #{skip_overrides ? "$#{name}-sprites: sprite-map(\"#{uri}\", $layout: $#{name}-layout, $cleanup: $#{name}-clean-up);" : generate_overrides(uri, name) }
// All sprites should extend this class // All sprites should extend this class
// The #{name}-sprite mixin will do so for you. // The #{name}-sprite mixin will do so for you.
@ -167,7 +138,7 @@ $#{name}-clean-up: true !default;
// Generates a class for each sprited image. // Generates a class for each sprited image.
@mixin all-#{name}-sprites($dimensions: $#{name}-sprite-dimensions, $prefix: sprite-map-name($#{name}-sprites)) { @mixin all-#{name}-sprites($dimensions: $#{name}-sprite-dimensions, $prefix: sprite-map-name($#{name}-sprites)) {
@include #{name}-sprites(#{sprite_names.join(" ")}, $dimensions, $prefix); @include #{name}-sprites(#{sprite_names(uri).join(" ")}, $dimensions, $prefix);
} }
SCSS SCSS
end end
@ -176,12 +147,13 @@ SCSS
# <tt>$#{name}-#{sprite_name}-position </tt> # <tt>$#{name}-#{sprite_name}-position </tt>
# <tt> $#{name}-#{sprite_name}-spacing </tt> # <tt> $#{name}-#{sprite_name}-spacing </tt>
# <tt> #{name}-#{sprite_name}-repeat: </tt> # <tt> #{name}-#{sprite_name}-repeat: </tt>
def generate_overrides def self.generate_overrides(uri, name)
sprites = sprite_names(uri)
content = <<-TXT content = <<-TXT
// These variables control the generated sprite output // These variables control the generated sprite output
// You can override them selectively before you import this file. // You can override them selectively before you import this file.
TXT TXT
sprite_names.map do |sprite_name| sprites.map do |sprite_name|
content += <<-SCSS content += <<-SCSS
$#{name}-#{sprite_name}-position: $#{name}-position !default; $#{name}-#{sprite_name}-position: $#{name}-position !default;
$#{name}-#{sprite_name}-spacing: $#{name}-spacing !default; $#{name}-#{sprite_name}-spacing: $#{name}-spacing !default;
@ -189,8 +161,8 @@ $#{name}-#{sprite_name}-repeat: $#{name}-repeat !default;
SCSS SCSS
end.join end.join
content += "\n$#{name}-sprites: sprite-map(\"#{uri}\", \n$cleanup: $#{name}-clean-up,\n" content += "\n$#{name}-sprites: sprite-map(\"#{uri}\", \n$layout: $#{name}-layout, \n$cleanup: $#{name}-clean-up,\n"
content += sprite_names.map do |sprite_name| content += sprites.map do |sprite_name|
%Q{ $#{sprite_name}-position: $#{name}-#{sprite_name}-position, %Q{ $#{sprite_name}-position: $#{name}-#{sprite_name}-position,
$#{sprite_name}-spacing: $#{name}-#{sprite_name}-spacing, $#{sprite_name}-spacing: $#{name}-#{sprite_name}-spacing,
$#{sprite_name}-repeat: $#{name}-#{sprite_name}-repeat} $#{sprite_name}-repeat: $#{name}-#{sprite_name}-repeat}

View File

@ -604,4 +604,32 @@ class SpritesTest < Test::Unit::TestCase
CSS CSS
end end
it "should calculate corret sprite demsions when givin spacing via issue#253" do
css = render <<-SCSS
$squares-layout:horizontal;
@import "squares/*.png";
.foo {
@include sprite-background-position($squares-sprites, "twenty-by-twenty");
}
.bar {
@include sprite-background-position($squares-sprites, "ten-by-ten");
}
SCSS
assert_equal [30, 20], image_size('squares-s*.png')
other_css = <<-CSS
.squares-sprite {
background: url('/squares-s161c60ad78.png') no-repeat;
}
.foo {
background-position: -10px 0;
}
.bar {
background-position: 0 0;
}
CSS
assert_correct css.gsub("\n", '').gsub(' ', ''), other_css.gsub("\n", '').gsub(' ', '')
end
end end

View File

@ -35,3 +35,16 @@ class Test::Unit::TestCase
extend Compass::TestCaseHelper::ClassMethods extend Compass::TestCaseHelper::ClassMethods
end end
module SpriteHelper
URI = "selectors/*.png"
def sprite_map_test(options)
importer = Compass::SpriteImporter.new
path, name = Compass::SpriteImporter.path_and_name(URI)
sprite_names = Compass::SpriteImporter.sprite_names(URI)
sass_engine = Compass::SpriteImporter.sass_engine(URI, name, importer, options)
Compass::SassExtensions::Sprites::SpriteMap.new(sprite_names.map{|n| "selectors/#{n}.png"}, path, name, sass_engine, options)
end
end

View File

@ -1,9 +1,9 @@
require 'test_helper' require 'test_helper'
require 'mocha' require 'mocha'
require 'ostruct' require 'ostruct'
class SpritesImageTest < Test::Unit::TestCase class SpritesImageTest < Test::Unit::TestCase
include SpriteHelper
def setup def setup
@images_src_path = File.join(File.dirname(__FILE__), '..', '..', 'fixtures', 'sprites', 'public', 'images') @images_src_path = File.join(File.dirname(__FILE__), '..', '..', 'fixtures', 'sprites', 'public', 'images')
file = StringIO.new("images_path = #{@images_src_path.inspect}\n") file = StringIO.new("images_path = #{@images_src_path.inspect}\n")
@ -18,18 +18,15 @@ class SpritesImageTest < Test::Unit::TestCase
let(:sprite_path) { File.join(@images_src_path, sprite_filename) } let(:sprite_path) { File.join(@images_src_path, sprite_filename) }
let(:sprite_name) { File.basename(sprite_filename, '.png') } let(:sprite_name) { File.basename(sprite_filename, '.png') }
def parent
importer = Compass::SpriteImporter.new(:uri => "selectors/*.png", :options => options)
@parent ||= Compass::SassExtensions::Sprites::SpriteMap.new(importer.sprite_names.map{|n| "selectors/#{n}.png"}, importer.path, importer.name, importer.sass_engine, importer.options)
end
let(:options) do def options
options = {:offset => @offset} options = {:offset => @offset}
options.stubs(:get_var).with(anything).returns(nil) options.stubs(:get_var).with(anything).returns(nil)
::OpenStruct.any_instance.stubs(:unitless?).returns(true) ::OpenStruct.any_instance.stubs(:unitless?).returns(true)
options.stubs(:get_var).with("#{sprite_name}-repeat").returns(::OpenStruct.new(:value => @repeat)) options.stubs(:get_var).with("#{sprite_name}-repeat").returns(::OpenStruct.new(:value => @repeat))
options.stubs(:get_var).with("#{sprite_name}-spacing").returns(::OpenStruct.new(:value => @spacing)) options.stubs(:get_var).with("#{sprite_name}-spacing").returns(::OpenStruct.new(:value => @spacing))
options.stubs(:get_var).with("#{sprite_name}-position").returns(::OpenStruct.new(:value => @position)) options.stubs(:get_var).with("#{sprite_name}-position").returns(::OpenStruct.new(:value => @position))
options.stubs(:get_var).with("layout").returns(::OpenStruct.new(:value => 'vertical'))
options options
end end
@ -38,7 +35,7 @@ class SpritesImageTest < Test::Unit::TestCase
let(:digest) { Digest::MD5.file(sprite_path).hexdigest } let(:digest) { Digest::MD5.file(sprite_path).hexdigest }
let(:image) { Compass::SassExtensions::Sprites::Image.new(parent, File.join(sprite_filename), options)} let(:image) { Compass::SassExtensions::Sprites::Image.new(sprite_map_test(options), File.join(sprite_filename), options)}
test 'initialize' do test 'initialize' do
assert_equal sprite_name, image.name assert_equal sprite_name, image.name

View File

@ -7,7 +7,7 @@ class ImporterTest < Test::Unit::TestCase
@images_src_path = File.join(File.dirname(__FILE__), '..', '..', 'fixtures', 'sprites', 'public', 'images') @images_src_path = File.join(File.dirname(__FILE__), '..', '..', 'fixtures', 'sprites', 'public', 'images')
file = StringIO.new("images_path = #{@images_src_path.inspect}\n") file = StringIO.new("images_path = #{@images_src_path.inspect}\n")
Compass.add_configuration(file, "sprite_config") Compass.add_configuration(file, "sprite_config")
@importer = Compass::SpriteImporter.new(:uri => URI, :options => options) @importer = Compass::SpriteImporter.new
end end
def teardown def teardown
@ -18,20 +18,16 @@ class ImporterTest < Test::Unit::TestCase
{:foo => 'bar'} {:foo => 'bar'}
end end
test "load should return an instance of SpriteImporter" do
assert Compass::SpriteImporter.load(URI, options).is_a?(Compass::SpriteImporter)
end
test "name should return the sprite name" do test "name should return the sprite name" do
assert_equal 'selectors', @importer.name assert_equal 'selectors', Compass::SpriteImporter.sprite_name(URI)
end end
test "path should return the sprite path" do test "path should return the sprite path" do
assert_equal 'selectors', @importer.path assert_equal 'selectors', Compass::SpriteImporter.path(URI)
end end
test "should return all the sprite names" do test "should return all the sprite names" do
assert_equal ["ten-by-ten", "ten-by-ten_active", "ten-by-ten_hover", "ten-by-ten_target"], @importer.sprite_names assert_equal ["ten-by-ten", "ten-by-ten_active", "ten-by-ten_hover", "ten-by-ten_target"], Compass::SpriteImporter.sprite_names(URI)
end end
test "should have correct mtime" do test "should have correct mtime" do
@ -47,19 +43,21 @@ class ImporterTest < Test::Unit::TestCase
end end
test "sass options should contain options" do test "sass options should contain options" do
assert_equal 'bar', @importer.sass_options[:foo] opts = Compass::SpriteImporter.sass_options('foo', @importer, options)
assert_equal 'bar', opts[:foo]
end end
test "should fail given bad sprite extensions" do test "should fail given bad sprite extensions" do
@images_src_path = File.join(File.dirname(__FILE__), '..', '..', 'fixtures', 'sprites', 'public', 'images') @images_src_path = File.join(File.dirname(__FILE__), '..', '..', 'fixtures', 'sprites', 'public', 'images')
file = StringIO.new("images_path = #{@images_src_path.inspect}\n") file = StringIO.new("images_path = #{@images_src_path.inspect}\n")
Compass.add_configuration(file, "sprite_config") Compass.add_configuration(file, "sprite_config")
importer = Compass::SpriteImporter.new(:uri => 'bad_extensions/*.jpg', :options => options) importer = Compass::SpriteImporter.new
uri = "bad_extensions/*.jpg"
begin begin
importer.sass_engine Compass::SpriteImporter.sass_engine(uri, Compass::SpriteImporter.sprite_name(uri), importer, {})
assert false, "An invalid sprite file made it past validation." assert false, "An invalid sprite file made it past validation."
rescue Compass::Error => e rescue Compass::Error => e
assert e.message.include?('.png') assert e.message.include?("invalid sprite path")
end end
end end

View File

@ -1,6 +1,7 @@
require 'test_helper' require 'test_helper'
class SpriteMapTest < Test::Unit::TestCase class SpriteMapTest < Test::Unit::TestCase
include SpriteHelper
def setup def setup
Hash.send(:include, Compass::SassExtensions::Functions::Sprites::VariableReader) Hash.send(:include, Compass::SassExtensions::Functions::Sprites::VariableReader)
@ -11,17 +12,13 @@ class SpriteMapTest < Test::Unit::TestCase
config.images_path = @images_tmp_path config.images_path = @images_tmp_path
Compass.add_configuration(config) Compass.add_configuration(config)
Compass.configure_sass_plugin! Compass.configure_sass_plugin!
@options = {'cleanup' => Sass::Script::Bool.new(true)} @options = {'cleanup' => Sass::Script::Bool.new(true), 'layout' => Sass::Script::String.new('vertical')}
setup_map @base = sprite_map_test(@options)
end
def setup_map
@importer = Compass::SpriteImporter.new(:uri => "selectors/*.png", :options => @options)
@base = Compass::SassExtensions::Sprites::SpriteMap.new(@importer.sprite_names.map{|n| "selectors/#{n}.png"}, @importer.path, @importer.name, @importer.sass_engine, @importer.options)
end end
def teardown def teardown
FileUtils.rm_r @images_tmp_path FileUtils.rm_r @images_tmp_path
@base = nil
end end
it "should have the correct size" do it "should have the correct size" do
@ -29,7 +26,7 @@ class SpriteMapTest < Test::Unit::TestCase
end end
it "should have the sprite names" do it "should have the sprite names" do
assert_equal @importer.sprite_names, @base.sprite_names assert_equal Compass::SpriteImporter.sprite_names(URI), @base.sprite_names
end end
it 'should have image filenames' do it 'should have image filenames' do
@ -82,9 +79,41 @@ class SpriteMapTest < Test::Unit::TestCase
file_to_remove = File.join(@images_tmp_path, 'selectors', 'ten-by-ten.png') file_to_remove = File.join(@images_tmp_path, 'selectors', 'ten-by-ten.png')
FileUtils.rm file_to_remove FileUtils.rm file_to_remove
assert !File.exists?(file_to_remove), "Failed to remove sprite file" assert !File.exists?(file_to_remove), "Failed to remove sprite file"
setup_map @base = sprite_map_test(@options)
@base.generate @base.generate
assert !File.exists?(file), "Sprite file did not get removed" assert !File.exists?(file), "Sprite file did not get removed"
end end
it "should have a vertical layout" do
assert_equal [0, 10, 20, 30], @base.images.map(&:top)
assert_equal [0, 0, 0, 0], @base.images.map(&:left)
end
# Horizontal tests
def horizontal
opts = @options.merge("layout" => Sass::Script::String.new('horizontal'))
sprite_map_test(opts)
end
it "should have a horizontal layout" do
base = horizontal
assert_equal 10, base.height
assert_equal 40, base.width
end
it "should layout images horizontaly" do
base = horizontal
assert_equal [0, 10, 20, 30], base.images.map(&:left)
assert_equal [0, 0, 0, 0], base.images.map(&:top)
end
it "should generate a horrizontal sprite" do
base = horizontal
base.generate
assert File.exists?(base.filename)
FileUtils.rm base.filename
end
end end