test files

This commit is contained in:
Scott Davis 2011-05-16 09:36:27 -04:00
parent 6ba72b9533
commit d9ca08f685
9 changed files with 117 additions and 5 deletions

View File

@ -1,6 +1,7 @@
require 'digest/md5' require 'digest/md5'
require 'compass/sass_extensions/sprites/sprite_map' require 'compass/sass_extensions/sprites/sprite_map'
require 'compass/sass_extensions/sprites/image' require 'compass/sass_extensions/sprites/image'
require 'compass/sass_extensions/sprites/image_row'
require 'compass/sass_extensions/sprites/base' require 'compass/sass_extensions/sprites/base'
require 'compass/sass_extensions/sprites/engines' require 'compass/sass_extensions/sprites/engines'

View File

@ -70,12 +70,36 @@ module Compass
# Calculates the overal image dimensions # Calculates the overal image dimensions
# collects image sizes and input parameters for each sprite # collects image sizes and input parameters for each sprite
def compute_image_positions! def compute_image_positions!
@images.each_with_index do |image, index| imgs = @images.sort { |a,b| a.width <=> b.width }
image.left = image.position.unit_str == "%" ? (@width - image.width) * (image.position.value / 100) : image.position.value rows = [::Compass::SassExtensions::Sprites::ImageRow.new(@width)]
next if index == 0 imgs.each do |image|
last_image = @images[index-1] next if rows.last.add(image)
image.top = last_image.top + last_image.height + [image.spacing, last_image.spacing].max
rows << ::Compass::SassExtensions::Sprites::ImageRow.new(@width)
unless rows.last.add(image)
raise "Image failed to be added"
end end
end
current_y = 0
rows.each do |row|
current_x = 0
row.images.each_with_index do |image, index|
image.left = current_x
image.top = current_y
current_x += image.width
image.left = image.position.unit_str == "%" ? (@width - image.width) * (image.position.value / 100) : image.position.value
end
current_y += row.height
end
# @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
# last_image = image
# end
end end
# Fetches the Sprite::Image object for the supplied name # Fetches the Sprite::Image object for the supplied name

View File

@ -0,0 +1,35 @@
module Compass
module SassExtensions
module Sprites
class ImageRow
attr_reader :images, :max_width
def initialize(max_width)
@images = []
@max_width = max_width
end
def add(image)
unless image.is_a?(Compass::SassExtensions::Sprites::Image)
raise ArgumentError, "Must be a SpriteImage"
end
if (images.inject(0) {|sum, img| sum + img.width} + image.width) > max_width
return false
end
@images << image
true
end
def height
images.map(&:height).max
end
def width
images.map(&:width).max
end
end
end
end
end

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

View File

@ -0,0 +1,52 @@
require 'test_helper'
class ImageRowTest < Test::Unit::TestCase
def setup
@filenames = %w(large.png large_square.png medium.png tall.png small.png)
Compass.configuration.stubs(:images_path).returns('./')
@images_src_path = File.join(File.dirname(__FILE__), '..', '..', 'fixtures', 'sprites', 'public', 'images')
@image_files = Dir["#{@images_src_path}/image_row/*.png"].sort
@images = @image_files.map do |img|
Compass::SassExtensions::Sprites::Image.new(nil, img, {})
end
image_row(1000)
end
def image_row(max)
@image_row = Compass::SassExtensions::Sprites::ImageRow.new(max)
end
def teardown
end
def populate_row
@images.each do |image|
assert @image_row.add(image)
end
end
it "should return false if image will not fit in row" do
image_row(100)
img = Compass::SassExtensions::Sprites::Image.new(nil, File.join(@images_src_path, 'image_row', 'large.png'), {})
assert !@image_row.add(img)
end
it "should have 5 images" do
populate_row
assert_equal 5, @image_row.images.size
end
it "should return max image width" do
populate_row
assert_equal 400, @image_row.width
end
it "should return max image height" do
populate_row
assert_equal 40, @image_row.height
end
end