public
Rubygem
Description: Rails Plugin - a Ruby way to manage your stylesheets and javascripts. Don't put all your assets in your layout; define what you need where you need them.
Homepage: http://6brand.com
Clone URL: git://github.com/JackDanger/sweet_assets.git
studioda (author)
Tue Dec 04 12:43:16 -0800 2007
commit  221b7e822738579b1867b531b4c53f9e79cc3a29
tree    42f326acf240d29242fc3d8c039684d2153d22c0
sweet_assets / test / sweet_assets_test.rb
100644 167 lines (143 sloc) 6.49 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
RAILS_ENV='test'
 
require 'test/unit'
require File.expand_path(File.dirname(__FILE__) + '/../../../../config/environment')
require File.expand_path(File.dirname(__FILE__) + '/../../../../test/test_helper')
 
 
# Re-raise errors caught by the controller.
class WebController < ActionController::Base
  before_filter :style_like_home
  before_filter :style_like_users!
  before_filter :style_like_extra
  before_filter :script_like_home
  before_filter :script_like_users!
  before_filter :script_like_extra
  def index
    render :text => '<html><head><title>SweetAssets</title></head><body></body></html>'
  end
end
 
class ShortcutController < ActionController::Base
  style_like :home!, :users
  script_like :extra!, :home
end
 
class SweetAssetsTest < Test::Unit::TestCase
 
  def setup
    ActionController::Base.perform_caching = false
    @controller = WebController.new
    @request = ActionController::TestRequest.new
    @response = ActionController::TestResponse.new
  end
  
  def teardown
    delete_empty_recent_files
  end
  
  def test_each_stylesheet_should_appear
    get :index
    assert_response :success
    assert_tag :link, :attributes => {:href => /stylesheets\/web.css/, :rel => 'stylesheet'}
    assert_tag :link, :attributes => {:href => /stylesheets\/home.css/, :rel => 'stylesheet'}
    assert_tag :link, :attributes => {:href => /stylesheets\/extra.css/, :rel => 'stylesheet'}
    assert_tag :link, :attributes => {:href => /stylesheets\/users.css/, :rel => 'stylesheet'}
    assert_select 'head > link', :count => 4
  end
  
  def test_bottom_stylesheets_should_appear_last
    get :index
    assert_response :success
    assert @response.body =~ /<link href="\/stylesheets\/users.css(\?\d*)?" media="screen" rel="stylesheet" type="text\/css" \/>\n*?<\/head>/
  end
 
  def test_caching_stylesheets
    ActionController::Base.perform_caching = true
    with_stylesheets :home, :web, :users, :extra do
      with_javascripts :home, :web, :users, :extra do
        get :index
      end
    end
    assert_response :success
    assert_no_tag :link, :attributes => {:href => /stylesheets\/web.css/, :rel => 'stylesheet'}
    assert_no_tag :link, :attributes => {:href => /stylesheets\/home.css/, :rel => 'stylesheet'}
    assert_no_tag :link, :attributes => {:href => /stylesheets\/extra.css/, :rel => 'stylesheet'}
    assert_no_tag :link, :attributes => {:href => /stylesheets\/users.css/, :rel => 'stylesheet'}
    assert_tag :link, :attributes => {:href => /sweet_stylesheets_home,extra.css(\?\d*)?/, :rel => 'stylesheet'}
    assert_tag :link, :attributes => {:href => /sweet_stylesheets_web,users.css(\?\d*)?/, :rel => 'stylesheet'}
    # and check the placement
    location_of_head = @response.body =~ /<head>/
    location_of_title = @response.body =~ /<title>/
    location_of_home_extra_css = @response.body =~ /<link href="\/stylesheets\/sweet_stylesheets_home,extra.css/
    location_of_web_users_css = @response.body =~ /<link href="\/stylesheets\/sweet_stylesheets_web,users.css/
    assert location_of_head < location_of_home_extra_css
    assert location_of_home_extra_css < location_of_title
    assert location_of_title < location_of_web_users_css
    ActionController::Base.perform_caching = false
  end
 
  def test_caching_javascripts
    ActionController::Base.perform_caching = true
    with_stylesheets :home, :web, :users, :extra do
      with_javascripts :home, :web, :users, :extra do
        get :index
      end
    end
    assert_response :success
    assert_no_tag :script, :attributes => {:src => /javascripts\/web.js/}
    assert_no_tag :script, :attributes => {:src => /javascripts\/home.js/}
    assert_no_tag :script, :attributes => {:src => /javascripts\/extra.js/}
    assert_no_tag :script, :attributes => {:src => /javascripts\/users.js/}
    assert_tag :script, :attributes => {:src => /sweet_javascripts_home,extra.js(\?\d*)?/}
    assert_tag :script, :attributes => {:src => /sweet_javascripts_web,users.js(\?\d*)?/}
    # and check the placement
    location_of_head = @response.body =~ /<head>/
    location_of_title = @response.body =~ /<title>/
    location_of_home_extra_js = @response.body =~ /<script src="\/javascripts\/sweet_javascripts_home,extra.js/
    location_of_web_users_js = @response.body =~ /<script src="\/javascripts\/sweet_javascripts_web,users.js/
    assert location_of_head < location_of_home_extra_js
    assert location_of_home_extra_js < location_of_title
    assert location_of_title < location_of_web_users_js
    ActionController::Base.perform_caching = false
  end
  
  def test_shortcuts
    @controller = ShortcutController.new
    with_stylesheets :home, :web, :users, :extra do
      with_javascripts :home, :web, :users, :extra do
        get :index
      end
    end
    assert_tag :link, :attributes => {:href => /home.css/, :rel => 'stylesheet'}
    assert_tag :link, :attributes => {:href => /users.css/, :rel => 'stylesheet'}
    assert_tag :script, :attributes => {:src => /extra.js/}
    assert_tag :script, :attributes => {:src => /home.js/}
    @controller = WebController.new
  end
 
  def delete_empty_recent_files
    dir = "#{RAILS_ROOT}/public/stylesheets"
    Dir.entries(dir).each do |file|
      if file =~ /.css$/
        filename = File.join(dir, file)
        if File.exists?(filename) && File.read(filename).blank? && (File.ctime(filename) + 10.minutes) > Time.now
          File.delete(filename)
        end
      end
    end
  end
  
  # create some temporary css files for the duration of the block
  def with_stylesheets(*files, &block)
    @temp_stylesheet_files = []
    files.each do |file|
      filename = "#{RAILS_ROOT}/public/stylesheets/#{file}.css"
      unless File.exists?(filename)
        @temp_stylesheet_files << "#{RAILS_ROOT}/public/stylesheets/#{file}.css"
        File.open(filename, 'w') {|f| f.write('') }
      end
    end
    
    yield
    
    @temp_stylesheet_files.each {|filename| File.delete(filename) }
 
  end
  
  # create some temporary javascript files for the duration of the block
  def with_javascripts(*files, &block)
    @temp_javascript_files = []
    files.each do |file|
      filename = "#{RAILS_ROOT}/public/javascripts/#{file}.js"
      unless File.exists?(filename)
        @temp_javascript_files << "#{RAILS_ROOT}/public/javascripts/#{file}.js"
        File.open(filename, 'w') {|f| f.write('') }
      end
    end
    
    yield
 
    @temp_javascript_files.each {|filename| File.delete(filename) }
 
  end
  
end