rack-less 1.5.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. data/.bundle/config +2 -0
  2. data/.gitignore +5 -0
  3. data/Gemfile +4 -0
  4. data/Gemfile.lock +53 -0
  5. data/Rakefile +5 -43
  6. data/lib/rack/less.rb +6 -12
  7. data/lib/rack/less/config.rb +13 -25
  8. data/lib/rack/less/request.rb +36 -22
  9. data/lib/rack/less/source.rb +7 -7
  10. data/lib/rack/less/version.rb +3 -11
  11. data/rack-less.gemspec +29 -0
  12. data/test/app_helper.rb +25 -0
  13. data/test/config_test.rb +215 -0
  14. data/test/env.rb +9 -0
  15. data/test/fixtures/mock_options.rb +9 -0
  16. data/test/fixtures/sinatra/app.rb +9 -0
  17. data/test/fixtures/sinatra/app/stylesheets/all_compiled.css +7 -0
  18. data/test/fixtures/sinatra/app/stylesheets/all_one.less +10 -0
  19. data/test/fixtures/sinatra/app/stylesheets/all_two.less +4 -0
  20. data/test/fixtures/sinatra/app/stylesheets/css.css +4 -0
  21. data/test/fixtures/sinatra/app/stylesheets/css_compiled.css +4 -0
  22. data/test/fixtures/sinatra/app/stylesheets/nested/file.css +10 -0
  23. data/test/fixtures/sinatra/app/stylesheets/nested/file_compiled.css +2 -0
  24. data/test/fixtures/sinatra/app/stylesheets/nested/really/really.less +10 -0
  25. data/test/fixtures/sinatra/app/stylesheets/nested/really/really_compiled.css +2 -0
  26. data/test/fixtures/sinatra/app/stylesheets/normal.less +10 -0
  27. data/test/fixtures/sinatra/app/stylesheets/normal_compiled.css +2 -0
  28. data/test/fixtures/sinatra/app/stylesheets/some-styles.less +8 -0
  29. data/test/fixtures/sinatra/app/stylesheets/some_styles.less +8 -0
  30. data/test/fixtures/sinatra/app/stylesheets/styles1.less +8 -0
  31. data/test/helper.rb +77 -0
  32. data/test/options_test.rb +60 -0
  33. data/test/request_test.rb +142 -0
  34. data/test/response_test.rb +41 -0
  35. data/test/sinatra_test.rb +54 -0
  36. data/test/source_test.rb +158 -0
  37. metadata +93 -57
@@ -0,0 +1,25 @@
1
+ require 'rack/test'
2
+ require 'webrat'
3
+
4
+ class Test::Unit::TestCase
5
+ include Rack::Test::Methods
6
+ include Webrat::Methods
7
+ include Webrat::Matchers
8
+
9
+ Webrat.configure do |config|
10
+ config.mode = :rack
11
+ end
12
+
13
+ class << self
14
+
15
+ def should_respond_with_compiled_css
16
+ should "return compiled css" do
17
+ assert_equal 200, @response.status, "status is not '#{Rack::Utils::HTTP_STATUS_CODES[200]}'"
18
+ assert @response.headers["Content-Type"].include?(Rack::Less::MIME_TYPE), "content type is not '#{Rack::Less::MIME_TYPE}'"
19
+ assert_equal @compiled.strip, @response.body.strip, "the compiled css is incorrect"
20
+ end
21
+ end
22
+
23
+ end
24
+
25
+ end
@@ -0,0 +1,215 @@
1
+ require "test/helper"
2
+ require 'rack/less/config'
3
+
4
+ class ConfigTest < Test::Unit::TestCase
5
+ context 'Rack::Less::Config' do
6
+ setup do
7
+ @config = Rack::Less::Config.new
8
+ end
9
+
10
+ { :cache => false,
11
+ :compress => false,
12
+ :combinations => {},
13
+ :cache_bust => false
14
+ }.each do |k,v|
15
+ should "default #{k} correctly" do
16
+ assert_equal v, @config.send(k)
17
+ end
18
+
19
+ should "have an accessor for #{k}" do
20
+ assert_respond_to @config, k, "no reader for #{k}"
21
+ assert_respond_to @config, "#{k}=".to_sym, "no writer for #{k}"
22
+ end
23
+ end
24
+
25
+ should "provide boolean readers" do
26
+ assert_respond_to @config, :cache?, "no reader for :cache?"
27
+ assert_equal !!@config.cache, @config.cache?
28
+ assert_respond_to @config, :compress?, "no reader for :compress?"
29
+ assert_equal !!@config.compress, @config.compress?
30
+ end
31
+
32
+ should "allow init with setting hash" do
33
+ settings = {
34
+ :cache => true,
35
+ :compress => true,
36
+ :combinations => {
37
+ 'all' => ['one', 'two']
38
+ },
39
+ :cache_bust => false
40
+ }
41
+ config = Rack::Less::Config.new settings
42
+
43
+ assert_equal true, config.cache
44
+ assert_equal true, config.compress
45
+ combinations = {'all' => ['one', 'two']}
46
+ assert_equal combinations, config.combinations
47
+ end
48
+
49
+ should "be accessible at Rack::Less class level" do
50
+ assert_respond_to Rack::Less, :configure
51
+ assert_respond_to Rack::Less, :config
52
+ assert_respond_to Rack::Less, :config=
53
+ assert_respond_to Rack::Less, :combinations
54
+ assert_respond_to Rack::Less, :cache_bust
55
+ assert_respond_to Rack::Less, :stylesheet
56
+ end
57
+
58
+ context "given a new configuration" do
59
+ setup do
60
+ @old_config = Rack::Less.config
61
+ @settings = {
62
+ :cache => true,
63
+ :compress => true,
64
+ :combinations => {
65
+ 'all' => ['one', 'two']
66
+ },
67
+ :cache_bust => false
68
+ }
69
+ @traditional_config = Rack::Less::Config.new @settings
70
+ end
71
+ teardown do
72
+ Rack::Less.config = @old_config
73
+ end
74
+
75
+ should "allow Rack::Less to directly apply settings" do
76
+ Rack::Less.config = @traditional_config.dup
77
+
78
+ assert_equal @traditional_config.cache, Rack::Less.config.cache
79
+ assert_equal @traditional_config.compress, Rack::Less.config.compress
80
+ assert_equal @traditional_config.combinations, Rack::Less.config.combinations
81
+ assert_equal @traditional_config.cache_bust, Rack::Less.config.cache_bust
82
+ end
83
+
84
+ should "allow Rack::Less to apply settings using a block" do
85
+ Rack::Less.configure do |config|
86
+ config.cache = true
87
+ config.compress = true
88
+ config.combinations = {
89
+ 'all' => ['one', 'two']
90
+ }
91
+ config.cache_bust = false
92
+ end
93
+
94
+ assert_equal @traditional_config.cache, Rack::Less.config.cache
95
+ assert_equal @traditional_config.compress, Rack::Less.config.compress
96
+ assert_equal @traditional_config.combinations, Rack::Less.config.combinations
97
+ assert_equal @traditional_config.cache_bust, Rack::Less.config.cache_bust
98
+ end
99
+
100
+ context "helpers" do
101
+ setup do
102
+ @settings = {
103
+ :combinations => {
104
+ 'all' => ['one', 'two']
105
+ },
106
+ :cache_bust => false
107
+ }
108
+ end
109
+
110
+ context "#combinations" do
111
+ should "should be able to access it's values with a parameter" do
112
+ config = Rack::Less::Config.new @settings
113
+
114
+ assert_equal [], config.combinations('one')
115
+ assert_equal [], config.combinations('wtf')
116
+ assert_equal ['one.css', 'two.css'], config.combinations('all')
117
+ end
118
+
119
+ context "if cache setting is true" do
120
+ setup do
121
+ @settings[:cache] = true
122
+ end
123
+
124
+ should "use the lookup parameter instead of the value" do
125
+ config = Rack::Less::Config.new @settings
126
+
127
+ assert_equal 'all.css', config.combinations('all')
128
+ end
129
+ end
130
+
131
+ end
132
+
133
+ context "#stylesheet" do
134
+ should "should be able to stylesheet references" do
135
+ config = Rack::Less::Config.new @settings
136
+
137
+ assert_equal 'one.css', config.stylesheet('one')
138
+ assert_equal 'wtf.css', config.stylesheet('wtf')
139
+ end
140
+
141
+ should "should be able to access combination values with a parameter" do
142
+ config = Rack::Less::Config.new @settings
143
+
144
+ assert_equal ['one.css', 'two.css'], config.stylesheet('all')
145
+ end
146
+
147
+ context "if cache setting is true" do
148
+ setup do
149
+ @settings[:cache] = true
150
+ end
151
+
152
+ should "use the lookup parameter instead of the value" do
153
+ config = Rack::Less::Config.new @settings
154
+
155
+ assert_equal 'one.css', config.stylesheet('one')
156
+ assert_equal 'all.css', config.stylesheet('all')
157
+ end
158
+ end
159
+
160
+ context "when cache_bust is false" do
161
+ setup do
162
+ @settings[:cache_bust] = false
163
+ end
164
+
165
+ should "should not put in a cache bust value" do
166
+ config = Rack::Less::Config.new @settings
167
+
168
+ assert_equal 'one.css', config.stylesheet('one')
169
+ end
170
+ end
171
+
172
+ context "when cache_bust is nil" do
173
+ setup do
174
+ @settings[:cache_bust] = nil
175
+ end
176
+
177
+ should "should not put in a cache bust value" do
178
+ config = Rack::Less::Config.new @settings
179
+
180
+ assert_equal 'one.css', config.stylesheet('one')
181
+ end
182
+ end
183
+
184
+ context "when cache_bust is true" do
185
+ setup do
186
+ @settings[:cache_bust] = true
187
+ end
188
+
189
+ should "always put a timestamp value on the end of the href" do
190
+ config = Rack::Less::Config.new @settings
191
+
192
+ assert_match /one.css\?[0-9]+/, config.stylesheet('one')
193
+ end
194
+ end
195
+
196
+ context "when timestamp specified" do
197
+ setup do
198
+ @stamp = Time.now.to_i - 100_000
199
+ @settings[:cache_bust] = @stamp
200
+ end
201
+
202
+ should "always use that timestamp" do
203
+ config = Rack::Less::Config.new @settings
204
+
205
+ assert_equal ["one.css?#{@stamp}", "two.css?#{@stamp}"], config.stylesheet('all')
206
+ end
207
+
208
+ end
209
+ end
210
+
211
+ end
212
+ end
213
+
214
+ end
215
+ end
@@ -0,0 +1,9 @@
1
+ # Add test and lib paths to the $LOAD_PATH
2
+ [ File.dirname(__FILE__),
3
+ File.join(File.dirname(__FILE__), '..', 'lib')
4
+ ].each do |path|
5
+ full_path = File.expand_path(path)
6
+ $LOAD_PATH.unshift(full_path) unless $LOAD_PATH.include?(full_path)
7
+ end
8
+
9
+ require 'rack/less'
@@ -0,0 +1,9 @@
1
+ class MockOptions
2
+ include Rack::Less::Options
3
+
4
+ def initialize
5
+ @env = nil
6
+ initialize_options
7
+ end
8
+ end
9
+
@@ -0,0 +1,9 @@
1
+ require 'sinatra/base'
2
+
3
+ class SinatraApp < Sinatra::Base
4
+
5
+ configure do
6
+ set :root, File.expand_path(File.dirname(__FILE__))
7
+ end
8
+
9
+ end
@@ -0,0 +1,7 @@
1
+ #header { color: #6c94be; }
2
+ div { width: 2; }
3
+
4
+ div {
5
+ width: 20px;
6
+ height: 30px;
7
+ }
@@ -0,0 +1,10 @@
1
+ @nice-blue: #5B83AD;
2
+ @light-blue: @nice-blue + #111;
3
+
4
+ #header {
5
+ color: @light-blue;
6
+ }
7
+
8
+ div {
9
+ width: 1 + 1
10
+ }
@@ -0,0 +1,4 @@
1
+ div {
2
+ width: 20px;
3
+ height: 30px;
4
+ }
@@ -0,0 +1,4 @@
1
+ SPAN {
2
+ width: 20px;
3
+ height: 30px;
4
+ }
@@ -0,0 +1,4 @@
1
+ SPAN {
2
+ width: 20px;
3
+ height: 30px;
4
+ }
@@ -0,0 +1,10 @@
1
+ @nice-blue: #5B83AD;
2
+ @light-blue: @nice-blue + #111;
3
+
4
+ #header {
5
+ color: @light-blue;
6
+ }
7
+
8
+ div {
9
+ width: 1 + 1
10
+ }
@@ -0,0 +1,2 @@
1
+ #header { color: #6c94be; }
2
+ div { width: 2; }
@@ -0,0 +1,10 @@
1
+ @bad-blue: #5B83FF;
2
+ @bad-light-blue: @bad-blue + #111;
3
+
4
+ #header {
5
+ color: @bad-light-blue;
6
+ }
7
+
8
+ div {
9
+ width: 2 + 2
10
+ }
@@ -0,0 +1,2 @@
1
+ #header { color: #6c94ff; }
2
+ div { width: 4; }
@@ -0,0 +1,10 @@
1
+ @nice-blue: #5B83AD;
2
+ @light-blue: @nice-blue + #111;
3
+
4
+ #header {
5
+ color: @light-blue;
6
+ }
7
+
8
+ div {
9
+ width: 1 + 1
10
+ }
@@ -0,0 +1,2 @@
1
+ #header { color: #6c94be; }
2
+ div { width: 2; }
@@ -0,0 +1,8 @@
1
+ SPAN {
2
+ width: 20px;
3
+ height: 30px;
4
+ }
5
+
6
+ DIV {
7
+ color: #fff;
8
+ }
@@ -0,0 +1,8 @@
1
+ SPAN {
2
+ width: 20px;
3
+ height: 30px;
4
+ }
5
+
6
+ DIV {
7
+ color: #fff;
8
+ }
@@ -0,0 +1,8 @@
1
+ SPAN {
2
+ width: 20px;
3
+ height: 30px;
4
+ }
5
+
6
+ DIV {
7
+ color: #fff;
8
+ }
@@ -0,0 +1,77 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler.setup
4
+
5
+ require 'test_belt'
6
+ require 'test/env'
7
+
8
+ class Test::Unit::TestCase
9
+
10
+ def file_path(*segments)
11
+ segs = segments.unshift([File.dirname(__FILE__), '..']).flatten
12
+ File.expand_path(segs.join(File::SEPARATOR))
13
+ end
14
+
15
+ def self.should_compile_source(name, desc)
16
+ context desc do
17
+ setup do
18
+ @compiled = File.read(File.join(@source_folder, "#{name}_compiled.css"))
19
+ @source = Rack::Less::Source.new(name, :folder => @source_folder)
20
+ end
21
+
22
+ should "compile LESS" do
23
+ assert_equal @compiled.strip, @source.compiled.strip, '.compiled is incorrect'
24
+ assert_equal @compiled.strip, @source.to_css.strip, '.to_css is incorrect'
25
+ assert_equal @compiled.strip, @source.css.strip, '.css is incorrect'
26
+ end
27
+ end
28
+ end
29
+
30
+ def env_defaults
31
+ Rack::Less::Base.defaults.merge({
32
+ Rack::Less::Base.option_name(:root) => file_path('test','fixtures','sinatra')
33
+ })
34
+ end
35
+
36
+ def less_request(method, path_info)
37
+ Rack::Less::Request.new(@defaults.merge({
38
+ 'REQUEST_METHOD' => method,
39
+ 'PATH_INFO' => path_info
40
+ }))
41
+ end
42
+
43
+ def less_response(css)
44
+ Rack::Less::Response.new(@defaults, css)
45
+ end
46
+
47
+ def self.should_not_be_a_valid_rack_less_request(args)
48
+ context "to #{args[:method].upcase} #{args[:resource]} (#{args[:description]})" do
49
+ setup do
50
+ @request = less_request(args[:method], args[:resource])
51
+ end
52
+
53
+ should "not be a valid endpoint for Rack::Less" do
54
+ not_valid = !@request.get?
55
+ not_valid ||= !@request.for_css?
56
+ not_valid ||= @request.source.files.empty?
57
+ assert not_valid, 'request is a GET for .css format and has source'
58
+ assert !@request.for_less?, 'the request is for less'
59
+ end
60
+ end
61
+ end
62
+ def self.should_be_a_valid_rack_less_request(args)
63
+ context "to #{args[:method].upcase} #{args[:resource]} (#{args[:description]})" do
64
+ setup do
65
+ @request = less_request(args[:method], args[:resource])
66
+ end
67
+
68
+ should "be a valid endpoint for Rack::Less" do
69
+ assert @request.get?, 'the request is not a GET'
70
+ assert @request.for_css?, 'the request is not for css'
71
+ assert !@request.source.files.empty?, 'the request resource has no source'
72
+ assert @request.for_less?, 'the request is not for less'
73
+ end
74
+ end
75
+ end
76
+
77
+ end