Getting the tests functional and getting the plugin into functional shape.

Now... time to try with a real application!



git-svn-id: svn://hamptoncatlin.com/haml/branches/1.5dev@175 7063305b-7217-0410-af8c-cdc13e5119b9
This commit is contained in:
hcatlin 2006-11-28 20:33:22 +00:00
parent fa5048ba40
commit c94123be02
8 changed files with 121 additions and 14 deletions

View File

@ -1,3 +1,4 @@
require 'haml/template' require 'haml/template'
require 'sass/plugin'
ActionView::Base.register_template_handler('haml', Haml::Template) ActionView::Base.register_template_handler('haml', Haml::Template)

View File

@ -3,6 +3,12 @@ require File.dirname(__FILE__) + '/css/parser'
module Sass module Sass
class Engine class Engine
def render_file(location)
@result = ""
File.open(location).each_line { |l| @result += l }
render(@result)
end
def render(input) def render(input)
buffer, stack, last_level, first = "", [], 0, true buffer, stack, last_level, first = "", [], 0, true
input.each do |line| input.each do |line|

View File

@ -1,27 +1,35 @@
require File.dirname(__FILE__) + "/engine"
#Rails plugin stuff. For use with action_view #Rails plugin stuff. For use with action_view
module Sass module Sass
module Plugin module Plugin
@@options = {}
def options class << self
@@options def options; @@options; end
end
def stylesheet_location def _stylesheet_location
@@options[:stylesheet_location] || (RAILS_ROOT + "/public/stylesheets/") @@options[:stylesheet_location] || (RAILS_ROOT + "/public/stylesheets/")
end end
def sass_template(name) def _always_update
file_location = stylesheet_location + name @@options[:always_update] || false
if stylesheet_needs_update?(file_location)
file = File.open(file_location + ".css")
Sass::Engine.new.render(file_location + ".sass")
end
end end
def stylesheet_needs_update?(file_location) def stylesheet_needs_update?(file_location)
!File.exists?(file_location + ".css") || (File.mtime("#{file_location}.sass") - 60) > File.mtime("#{file_location}.css") !File.exists?(file_location + ".css") || (File.mtime("#{file_location}.sass") - 60) > File.mtime("#{file_location}.css")
end end
end end
def sass_template(name)
file_location = Plugin._stylesheet_location + "/" + name.to_s
if Plugin._always_update || Plugin.stylesheet_needs_update?(file_location)
output_file = File.open(file_location + ".css", "w+")
output_file << Sass::Engine.new.render_file(file_location + ".sass")
output_file.close
end
end
end
end end

11
test/css/basic.css Normal file
View File

@ -0,0 +1,11 @@
%body { font: Arial; background: blue; }
#page { width: 700px; height: 100%; }
#page #header { height: 300px; }
#page #header %h1 { font-size: 50px; color: blue; }
#page #header #content.user.show { }
#page #header #content.user.show #container.top { }
#page #header #content.user.show #container.top #column.left { width: 100px; }
#page #header #content.user.show #container.top #column.right { width: 600px; }
#page #header #content.user.show #container.top #container.bottom { background: brown; }

View File

@ -0,0 +1,6 @@
require File.dirname(__FILE__) + '/../../lib/sass/engine'
require File.dirname(__FILE__) + '/../../lib/sass/plugin'
class FakeController
include Sass::Plugin
end

23
test/sass/basic.sass Normal file
View File

@ -0,0 +1,23 @@
%body
:font Arial
:background blue
#page
:width 700px
:height 100%
#header
:height 300px
%h1
:font-size 50px
:color blue
#content.user.show
#container.top
#column.left
:width 100px
#column.right
:width 600px
#container.bottom
:background brown

28
test/sass_engine_test.rb Normal file
View File

@ -0,0 +1,28 @@
#!/usr/bin/env ruby
require 'test/unit'
require File.dirname(__FILE__) + '/../lib/sass/engine'
class SassEngineTest < Test::Unit::TestCase
def setup
@engine = Sass::Engine.new
end
def test_basic_render
renders_correctly "basic"
end
def renders_correctly(name)
sass_file = load_file(name, "sass")
css_file = load_file(name, "css")
css_result = @engine.render(sass_file)
#puts css_result.collect { |a| a.inspect }.join("\n ")
assert_equal css_file, css_result
end
def load_file(name, type = "sass")
@result = ''
File.new(File.dirname(__FILE__) + "/#{type}/#{name}.#{type}").each_line { |l| @result += l }
@result
end
end

24
test/sass_plugin_test.rb Normal file
View File

@ -0,0 +1,24 @@
#!/usr/bin/env ruby
require 'test/unit'
require File.dirname(__FILE__) + '/../lib/sass/engine'
require File.dirname(__FILE__) + '/../lib/sass/plugin'
require File.dirname(__FILE__) + '/mocks/fake_controller'
class SassPluginTest < Test::Unit::TestCase
def setup
Sass::Plugin.options[:stylesheet_location] = File.dirname(__FILE__) + '/sass'
Sass::Plugin.options[:always_update] = true
@controller = FakeController.new
end
def test_truth
@controller.instance_eval do
sass_template(:basic)
end
assert FileUtils.compare_file(File.dirname(__FILE__) + "/sass/basic.css",
File.dirname(__FILE__) + "/css/basic.css")
FileUtils.rm(File.dirname(__FILE__) + "/sass/basic.css")
end
end