Permalink
Browse files

refactor to using a before_filter, added controller tests, now loadin…

…g init.rb while testing
  • Loading branch information...
1 parent 1d1ed80 commit 682b27999621b9dcc4d2936eefddbd615bb0819e @grosser grosser committed Feb 14, 2010
Showing with 107 additions and 59 deletions.
  1. +0 −8 lib/controller_extension.rb
  2. +7 −0 lib/less/controller_extension.rb
  3. +1 −1 rails/init.rb
  4. +26 −0 test/controller_test.rb
  5. +4 −44 test/more_test.rb
  6. +69 −6 test/test_helper.rb
View
8 lib/controller_extension.rb
@@ -1,8 +0,0 @@
-class ActionController::Base
- def process_with_less(*args)
- Less::More.generate_all
- process_without_less(*args)
- end
-
- alias_method_chain :process, :less
-end
View
7 lib/less/controller_extension.rb
@@ -0,0 +1,7 @@
+class ActionController::Base
+ before_filter :generate_css_from_less
+
+ def generate_css_from_less
+ Less::More.generate_all
+ end
+end
View
2 rails/init.rb
@@ -1 +1 @@
-require File.join(File.dirname(__FILE__), '..', 'lib', 'controller_extension') if RAILS_ENV == 'development'
+require File.join(File.dirname(__FILE__), '..', 'lib', 'less', 'controller_extension') if RAILS_ENV == 'development'
View
26 test/controller_test.rb
@@ -0,0 +1,26 @@
+require 'test_helper'
+
+class ExampleController < ActionController::Base
+ def test
+ render :text => 'OK'
+ end
+end
+
+class ControllerTest < ActionController::TestCase
+ def setup
+ @controller = ExampleController.new
+ @request = ActionController::TestRequest.new
+ @response = ActionController::TestResponse.new
+ setup_for_generate_test
+ end
+
+ def teardown
+ teardown_for_generate_test
+ end
+
+ should "generate less files" do
+ write_less 'xxx.less', 'a{color:red}'
+ get :test
+ assert_equal 'a { color: red; }', read_css('xxx.css').strip
+ end
+end
View
48 test/more_test.rb
@@ -51,50 +51,13 @@ class MoreTest < ActiveSupport::TestCase
end
end
- def css_path
- "#{Rails.root}/public/css"
- end
-
- def less_path
- "#{Rails.root}/less_files"
- end
-
- def write_less file, content
- write_content File.join(less_path, file), content
- end
-
- def write_css file, content
- write_content File.join(css_path, file), content
- end
-
- def write_content file, content
- `mkdir -p #{File.dirname(file)}`
- File.open(file,'w'){|f| f.print content }
- end
-
- def read_css(file)
- File.read(File.join(css_path, file)) rescue nil
- end
-
- def assert_include(item, obj)
- assert_block("#{obj.inspect}\ndoes not include\n#{item.inspect}."){ obj.include? item }
- end
-
- def assert_not_include(item, obj)
- assert_block("#{obj.inspect}\ndoes include\n#{item.inspect}."){ !obj.include? item }
- end
-
context :generate do
setup do
- Less::More.source_path = 'less_files'
- Less::More.destination_path = 'css'
- Less::More.header = false
- `mkdir -p #{css_path}`
+ setup_for_generate_test
end
teardown do
- `rm -rf #{css_path}`
- `rm -rf #{less_path}`
+ teardown_for_generate_test
end
should 'generate css from .less files' do
@@ -210,14 +173,11 @@ def assert_not_include(item, obj)
context :remove_all_generated do
setup do
- Less::More.source_path = 'less_files'
- Less::More.destination_path = 'css'
- `mkdir -p #{css_path}`
+ setup_for_generate_test
end
teardown do
- `rm -rf #{css_path}`
- `rm -rf #{less_path}`
+ teardown_for_generate_test
end
should "remove all generated css" do
View
75 test/test_helper.rb
@@ -1,16 +1,79 @@
-require 'test/unit'
-
+# fake Rails with loaded plugin
require 'rubygems'
require 'active_support'
-require 'active_support/test_case'
-require 'shoulda'
+require 'action_pack'
+require 'action_controller'
-class Rails
+module Rails
def self.root
File.expand_path(File.dirname(__FILE__))
end
+
+ def self.backtrace_cleaner
+ ActiveSupport::BacktraceCleaner.new
+ end
end
+RAILS_ENV = 'development'
+
+# load plugin
$LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
+require 'less/more'
+load 'init.rb'
+
+# load testing libs
+require 'test/unit'
+require 'active_support/test_case'
+begin; require 'redgreen'; rescue LoadError; end
+require 'shoulda'
+
+# setup controller testing
+require 'action_controller/test_process'
+ActionController::Base.logger = nil
+ActionController::Routing::Routes.reload rescue nil
+
+# test helpers
+def css_path
+ "#{Rails.root}/public/css"
+end
+
+def less_path
+ "#{Rails.root}/less_files"
+end
+
+def write_less file, content
+ write_content File.join(less_path, file), content
+end
+
+def write_css file, content
+ write_content File.join(css_path, file), content
+end
+
+def write_content file, content
+ `mkdir -p #{File.dirname(file)}`
+ File.open(file,'w'){|f| f.print content }
+end
+
+def read_css(file)
+ File.read(File.join(css_path, file)) rescue nil
+end
+
+def assert_include(item, obj)
+ assert_block("#{obj.inspect}\ndoes not include\n#{item.inspect}."){ obj.include? item }
+end
+
+def assert_not_include(item, obj)
+ assert_block("#{obj.inspect}\ndoes include\n#{item.inspect}."){ !obj.include? item }
+end
+
+def setup_for_generate_test
+ Less::More.source_path = 'less_files'
+ Less::More.destination_path = 'css'
+ Less::More.header = false
+ `mkdir -p #{css_path}`
+end
-require 'less/more'
+def teardown_for_generate_test
+ `rm -rf #{css_path}`
+ `rm -rf #{less_path}`
+end

0 comments on commit 682b279

Please sign in to comment.