File: content_for_spec.rb

package info (click to toggle)
ruby-sinatra 4.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,932 kB
  • sloc: ruby: 17,700; sh: 25; makefile: 8
file content (280 lines) | stat: -rw-r--r-- 9,013 bytes parent folder | download | duplicates (3)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
require 'spec_helper'

RSpec.describe Sinatra::ContentFor do
  subject do
    Sinatra.new do
      helpers Sinatra::ContentFor
      set :views, File.expand_path("content_for", __dir__)
    end.new!
  end

  Tilt.prefer Tilt::ERBTemplate
  require 'hamlit'
  Tilt.register Tilt::HamlTemplate, :haml

  extend Forwardable
  def_delegators :subject, :content_for, :clear_content_for, :yield_content
  def render(engine, template)
    subject.send(:render, engine, template, :layout => false).gsub(/\s/, '')
  end

  describe "without templates" do
    it 'renders blocks declared with the same key you use when rendering' do
      content_for(:foo) { "foo" }
      expect(yield_content(:foo)).to eq("foo")
    end

    it 'renders blocks more than once' do
      content_for(:foo) { "foo" }
      3.times { expect(yield_content(:foo)).to eq("foo") }
    end

    it 'does not render a block with a different key' do
      content_for(:bar) { "bar" }
      expect(yield_content(:foo)).to be_empty
    end

    it 'renders default content if no block matches the key and a default block is specified' do
      expect(yield_content(:foo) {}).to be_nil
      expect(yield_content(:foo) { "foo" }).to eq("foo")
    end

    it 'renders multiple blocks with the same key' do
      content_for(:foo) { "foo" }
      content_for(:foo) { "bar" }
      content_for(:bar) { "WON'T RENDER ME" }
      content_for(:foo) { "baz" }
      expect(yield_content(:foo)).to eq("foobarbaz")
    end

    it 'renders multiple blocks more than once' do
      content_for(:foo) { "foo" }
      content_for(:foo) { "bar" }
      content_for(:bar) { "WON'T RENDER ME" }
      content_for(:foo) { "baz" }
      3.times { expect(yield_content(:foo)).to eq("foobarbaz") }
    end

    it 'passes values to the blocks' do
      content_for(:foo) { |a| a.upcase }
      expect(yield_content(:foo, 'a')).to eq("A")
      expect(yield_content(:foo, 'b')).to eq("B")
    end

    it 'clears named blocks with the specified key' do
      content_for(:foo) { "foo" }
      expect(yield_content(:foo)).to eq("foo")
      clear_content_for(:foo)
      expect(yield_content(:foo)).to be_empty
    end

    it 'takes an immediate value instead of a block' do
      content_for(:foo, "foo")
      expect(yield_content(:foo)).to eq("foo")
    end

    context 'when flush option was disabled' do
      it 'append content' do
        content_for(:foo, "foo")
        content_for(:foo, "bar")
        expect(yield_content(:foo)).to eq("foobar")
      end
    end

    context 'when flush option was enabled' do
      it 'flush first content' do
        content_for(:foo, "foo")
        content_for(:foo, "bar", flush: true)
        expect(yield_content(:foo)).to eq("bar")
      end
    end
  end

  # TODO: liquid markaby builder nokogiri
  engines = %w[erb erubi haml hamlit slim]

  engines.each do |inner|
    describe inner.capitalize do
      before :all do
        begin
          require inner
        rescue LoadError => e
          skip "Skipping: " << e.message
        end
      end

      describe "with yield_content in Ruby" do
        it 'renders blocks declared with the same key you use when rendering' do
          render inner, :same_key
          expect(yield_content(:foo).strip).to eq("foo")
        end

        it 'renders blocks more than once' do
          render inner, :same_key
          3.times { expect(yield_content(:foo).strip).to eq("foo") }
        end

        it 'does not render a block with a different key' do
          render inner, :different_key
          expect(yield_content(:foo)).to be_empty
        end

        it 'renders default content if no block matches the key and a default block is specified' do
          render inner, :different_key
          expect(yield_content(:foo) { "foo" }).to eq("foo")
        end

        it 'renders multiple blocks with the same key' do
          render inner, :multiple_blocks
          expect(yield_content(:foo).gsub(/\s/, '')).to eq("foobarbaz")
        end

        it 'renders multiple blocks more than once' do
          render inner, :multiple_blocks
          3.times { expect(yield_content(:foo).gsub(/\s/, '')).to eq("foobarbaz") }
        end

        it 'passes values to the blocks' do
          render inner, :takes_values
          expect(yield_content(:foo, 1, 2).gsub(/\s/, '')).to eq("<i>1</i>2")
        end
      end

      describe "with content_for in Ruby" do
        it 'renders blocks declared with the same key you use when rendering' do
          content_for(:foo) { "foo" }
          expect(render(inner, :layout)).to eq("foo")
        end

        it 'renders blocks more than once' do
          content_for(:foo) { "foo" }
          expect(render(inner, :multiple_yields)).to eq("foofoofoo")
        end

        it 'does not render a block with a different key' do
          content_for(:bar) { "foo" }
          expect(render(inner, :layout)).to be_empty
        end

        it 'renders multiple blocks with the same key' do
          content_for(:foo) { "foo" }
          content_for(:foo) { "bar" }
          content_for(:bar) { "WON'T RENDER ME" }
          content_for(:foo) { "baz" }
          expect(render(inner, :layout)).to eq("foobarbaz")
        end

        it 'renders multiple blocks more than once' do
          content_for(:foo) { "foo" }
          content_for(:foo) { "bar" }
          content_for(:bar) { "WON'T RENDER ME" }
          content_for(:foo) { "baz" }
          expect(render(inner, :multiple_yields)).to eq("foobarbazfoobarbazfoobarbaz")
        end

        it 'passes values to the blocks' do
          content_for(:foo) { |a,b| "<i>#{a}</i>#{b}" }
          expect(render(inner, :passes_values)).to eq("<i>1</i>2")
        end

        it 'clears named blocks with the specified key' do
          content_for(:foo) { "foo" }
          expect(render(inner, :layout)).to eq("foo")
          clear_content_for(:foo)
          expect(render(inner, :layout)).to be_empty
        end
      end

      describe "with content_for? in Ruby" do
        it 'renders block if key is set' do
          content_for(:foo) { "foot" }
          expect(render(inner, :footer)).to eq("foot")
        end

        it 'does not render a block if different key' do
          content_for(:different_key) { "foot" }
          expect(render(inner, :footer)).to be_empty
        end
      end

      engines.each do |outer|
        describe "with yield_content in #{outer.capitalize}" do
          def body
            last_response.body.gsub(/\s/, '')
          end

          before :all do
            begin
              require outer
            rescue LoadError => e
              skip "Skipping: " << e.message
            end
          end

          before do
            mock_app do
              helpers Sinatra::ContentFor
              set inner, :layout_engine => outer
              set :views, File.expand_path("content_for", __dir__)
              get('/:view') { render(inner, params[:view].to_sym) }
              get('/:layout/:view') do
                render inner, params[:view].to_sym, :layout => params[:layout].to_sym
              end
            end
          end

          describe 'with a default content block' do
            describe 'when content_for key exists' do
              it 'ignores default content and renders content' do
                expect(get('/yield_block/same_key')).to be_ok
                expect(body).to eq("foo")
              end
            end

            describe 'when content_for key is missing' do
              it 'renders default content block' do
                expect(get('/yield_block/different_key')).to be_ok
                expect(body).to eq("baz")
              end
            end
          end

          it 'renders content set as parameter' do
            expect(get('/parameter_value')).to be_ok
            expect(body).to eq("foo")
          end

          it 'renders blocks declared with the same key you use when rendering' do
            expect(get('/same_key')).to be_ok
            expect(body).to eq("foo")
          end

          it 'renders blocks more than once' do
            expect(get('/multiple_yields/same_key')).to be_ok
            expect(body).to eq("foofoofoo")
          end

          it 'does not render a block with a different key' do
            expect(get('/different_key')).to be_ok
            expect(body).to be_empty
          end

          it 'renders multiple blocks with the same key' do
            expect(get('/multiple_blocks')).to be_ok
            expect(body).to eq("foobarbaz")
          end

          it 'renders multiple blocks more than once' do
            expect(get('/multiple_yields/multiple_blocks')).to be_ok
            expect(body).to eq("foobarbazfoobarbazfoobarbaz")
          end

          it 'passes values to the blocks' do
            expect(get('/passes_values/takes_values')).to be_ok
            expect(body).to eq("<i>1</i>2")
          end
        end
      end
    end
  end
end