2011-11-10 11:32:25 -05:00
|
|
|
|
# -*- coding: utf-8 -*-
|
2011-05-12 11:47:59 -04:00
|
|
|
|
require 'slim'
|
2014-05-09 02:39:16 -04:00
|
|
|
|
require 'spec_helper'
|
2011-05-12 11:47:59 -04:00
|
|
|
|
|
2022-02-05 07:27:48 -05:00
|
|
|
|
RSpec.describe Sinatra::Capture do
|
2011-05-12 11:47:59 -04:00
|
|
|
|
subject do
|
|
|
|
|
Sinatra.new do
|
|
|
|
|
enable :inline_templates
|
|
|
|
|
helpers Sinatra::Capture
|
|
|
|
|
end.new!
|
|
|
|
|
end
|
|
|
|
|
Tilt.prefer Tilt::ERBTemplate
|
|
|
|
|
|
|
|
|
|
extend Forwardable
|
|
|
|
|
def_delegators :subject, :capture, :capture_later
|
|
|
|
|
|
|
|
|
|
def render(engine, template)
|
|
|
|
|
subject.send(:render, engine, template.to_sym).strip.gsub(/\s+/, ' ')
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
shared_examples_for "a template language" do |engine|
|
2018-11-05 23:14:14 -05:00
|
|
|
|
lang = engine
|
2022-02-26 06:26:50 -05:00
|
|
|
|
if engine == :erubi
|
2018-11-05 23:14:14 -05:00
|
|
|
|
lang = :erb
|
|
|
|
|
end
|
2019-11-28 20:26:16 -05:00
|
|
|
|
if engine == :hamlit
|
|
|
|
|
lang = :haml
|
|
|
|
|
end
|
2012-06-11 18:45:05 -04:00
|
|
|
|
require "#{engine}"
|
2011-05-12 11:47:59 -04:00
|
|
|
|
|
|
|
|
|
it "captures content" do
|
2016-05-10 10:08:54 -04:00
|
|
|
|
expect(render(engine, "simple_#{lang}")).to eq("Say Hello World!")
|
2011-05-12 11:47:59 -04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "allows nested captures" do
|
2016-05-10 10:08:54 -04:00
|
|
|
|
expect(render(engine, "nested_#{lang}")).to eq("Say Hello World!")
|
2011-05-12 11:47:59 -04:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe('haml') { it_behaves_like "a template language", :haml }
|
2019-11-28 20:26:16 -05:00
|
|
|
|
describe('hamlit') { it_behaves_like "a template language", :hamlit }
|
2011-05-12 11:47:59 -04:00
|
|
|
|
describe('slim') { it_behaves_like "a template language", :slim }
|
2018-11-04 11:41:24 -05:00
|
|
|
|
describe('erubi') { it_behaves_like "a template language", :erubi }
|
2011-11-10 11:32:25 -05:00
|
|
|
|
|
|
|
|
|
describe 'erb' do
|
|
|
|
|
it_behaves_like "a template language", :erb
|
|
|
|
|
|
|
|
|
|
it "handles utf-8 encoding" do
|
2016-05-10 10:08:54 -04:00
|
|
|
|
expect(render(:erb, "utf_8")).to eq("UTF-8 –")
|
2011-11-10 11:32:25 -05:00
|
|
|
|
end
|
2015-05-22 17:22:25 -04:00
|
|
|
|
|
|
|
|
|
it "handles ISO-8859-1 encoding" do
|
2016-07-20 04:53:24 -04:00
|
|
|
|
expect(render(:erb, "iso_8859_1")).to eq("ISO-8859-1 -")
|
2016-05-07 05:57:06 -04:00
|
|
|
|
end
|
2011-11-10 11:32:25 -05:00
|
|
|
|
end
|
2018-12-09 01:15:52 -05:00
|
|
|
|
|
|
|
|
|
describe 'without templates' do
|
|
|
|
|
it 'captures empty blocks' do
|
|
|
|
|
expect(capture {}).to be_nil
|
|
|
|
|
end
|
|
|
|
|
end
|
2011-05-12 11:47:59 -04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
__END__
|
|
|
|
|
|
|
|
|
|
@@ simple_erb
|
|
|
|
|
Say
|
|
|
|
|
<% a = capture do %>World<% end %>
|
|
|
|
|
Hello <%= a %>!
|
|
|
|
|
|
|
|
|
|
@@ nested_erb
|
|
|
|
|
Say
|
|
|
|
|
<% a = capture do %>
|
|
|
|
|
<% b = capture do %>World<% end %>
|
|
|
|
|
<%= b %>!
|
|
|
|
|
<% end %>
|
|
|
|
|
Hello <%= a.strip %>
|
|
|
|
|
|
|
|
|
|
@@ simple_slim
|
|
|
|
|
| Say
|
|
|
|
|
- a = capture do
|
|
|
|
|
| World
|
|
|
|
|
| Hello #{a.strip}!
|
|
|
|
|
|
|
|
|
|
@@ nested_slim
|
|
|
|
|
| Say
|
|
|
|
|
- a = capture do
|
|
|
|
|
- b = capture do
|
|
|
|
|
| World
|
|
|
|
|
| #{b.strip}!
|
|
|
|
|
| Hello #{a.strip}
|
|
|
|
|
|
|
|
|
|
@@ simple_haml
|
|
|
|
|
Say
|
|
|
|
|
- a = capture do
|
|
|
|
|
World
|
|
|
|
|
Hello #{a.strip}!
|
|
|
|
|
|
|
|
|
|
@@ nested_haml
|
|
|
|
|
Say
|
|
|
|
|
- a = capture do
|
|
|
|
|
- b = capture do
|
|
|
|
|
World
|
|
|
|
|
#{b.strip}!
|
|
|
|
|
Hello #{a.strip}
|
2011-11-10 11:32:25 -05:00
|
|
|
|
|
|
|
|
|
@@ utf_8
|
|
|
|
|
<% a = capture do %>–<% end %>
|
|
|
|
|
UTF-8 <%= a %>
|
2015-05-22 17:22:25 -04:00
|
|
|
|
|
|
|
|
|
@@ iso_8859_1
|
|
|
|
|
<% a = capture do %>-<% end %>
|
|
|
|
|
ISO-8859-1 <%= a.force_encoding("iso-8859-1") %>
|