feature a day: baisc template rendering
This commit is contained in:
parent
5e36e4e8c9
commit
5b8a746e24
16
features/build_runner_html.feature
Normal file
16
features/build_runner_html.feature
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
Feature: Build Runner HTML
|
||||||
|
Scenario: All JS files
|
||||||
|
Given I have the file "template.erb" with the content:
|
||||||
|
"""
|
||||||
|
<html>
|
||||||
|
<%= resource_tags %>
|
||||||
|
</html>
|
||||||
|
"""
|
||||||
|
When I instantiate a template renderer with the template "template.erb" and the files:
|
||||||
|
| src/source.js |
|
||||||
|
| spec/spec.js |
|
||||||
|
And I render the template
|
||||||
|
Then I should have script tags for the following files in the template:
|
||||||
|
| src/source.js |
|
||||||
|
| spec/spec.js |
|
||||||
|
|
3
features/step_definitions/given/i_have_file.rb
Normal file
3
features/step_definitions/given/i_have_file.rb
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
Given /^I have the file "([^"]*)" with the content:$/ do |filename, string|
|
||||||
|
File.open(filename, 'wb') { |fh| fh.print(string) }
|
||||||
|
end
|
@ -0,0 +1,9 @@
|
|||||||
|
require 'nokogiri'
|
||||||
|
|
||||||
|
Then /^I should have script tags for the following files in the template:$/ do |table|
|
||||||
|
doc = Nokogiri::XML(@result)
|
||||||
|
|
||||||
|
table.raw.collect(&:first).each do |file|
|
||||||
|
doc.at_css("script[src='#{file}']").should_not be_nil
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,3 @@
|
|||||||
|
When /^I instantiate a template renderer with the template "([^"]*)" and the files:$/ do |template, table|
|
||||||
|
@template_renderer = Flowerbox::Delivery::TemplateRenderer.new(:files => table.raw.collect(&:first), :template => template)
|
||||||
|
end
|
3
features/step_definitions/when/i_render_template.rb
Normal file
3
features/step_definitions/when/i_render_template.rb
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
When /^I render the template$/ do
|
||||||
|
@result = @template_renderer.render
|
||||||
|
end
|
@ -1,11 +1,14 @@
|
|||||||
require 'flowerbox-delivery'
|
require 'flowerbox-delivery'
|
||||||
|
|
||||||
require 'mocha'
|
require 'mocha'
|
||||||
|
require 'fakefs/safe'
|
||||||
|
|
||||||
World(Mocha::API)
|
World(Mocha::API)
|
||||||
|
|
||||||
Before do
|
Before do
|
||||||
mocha_setup
|
mocha_setup
|
||||||
|
|
||||||
|
FakeFS.activate!
|
||||||
end
|
end
|
||||||
|
|
||||||
After do
|
After do
|
||||||
@ -15,6 +18,8 @@ After do
|
|||||||
mocha_teardown
|
mocha_teardown
|
||||||
end
|
end
|
||||||
|
|
||||||
|
FakeFS.deactivate!
|
||||||
|
|
||||||
if @running_server
|
if @running_server
|
||||||
@running_server[:server].shutdown
|
@running_server[:server].shutdown
|
||||||
@running_server = nil
|
@running_server = nil
|
||||||
|
@ -19,6 +19,8 @@ Gem::Specification.new do |gem|
|
|||||||
gem.add_development_dependency 'rspec'
|
gem.add_development_dependency 'rspec'
|
||||||
gem.add_development_dependency 'jquery-rails', '~> 1.0.0'
|
gem.add_development_dependency 'jquery-rails', '~> 1.0.0'
|
||||||
gem.add_development_dependency 'mocha'
|
gem.add_development_dependency 'mocha'
|
||||||
|
gem.add_development_dependency 'fakefs'
|
||||||
|
gem.add_development_dependency 'nokogiri'
|
||||||
|
|
||||||
gem.add_runtime_dependency 'rack'
|
gem.add_runtime_dependency 'rack'
|
||||||
gem.add_runtime_dependency 'sprockets'
|
gem.add_runtime_dependency 'sprockets'
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
module Flowerbox
|
module Flowerbox
|
||||||
module Delivery
|
module Delivery
|
||||||
autoload :Server, 'flowerbox/delivery/server'
|
autoload :Server, 'flowerbox/delivery/server'
|
||||||
|
autoload :TemplateRenderer, 'flowerbox/delivery/template_renderer'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -38,7 +38,7 @@ module Flowerbox
|
|||||||
def port
|
def port
|
||||||
return @port if @port ||= options[:port]
|
return @port if @port ||= options[:port]
|
||||||
|
|
||||||
attempts = 100
|
attempts = 20
|
||||||
|
|
||||||
begin
|
begin
|
||||||
attempts -= 1
|
attempts -= 1
|
||||||
|
35
lib/flowerbox/delivery/template_renderer.rb
Normal file
35
lib/flowerbox/delivery/template_renderer.rb
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
require 'erb'
|
||||||
|
|
||||||
|
module Flowerbox::Delivery
|
||||||
|
class TemplateRenderer
|
||||||
|
class FileTypeError < StandardError ; end
|
||||||
|
|
||||||
|
attr_reader :options
|
||||||
|
|
||||||
|
def initialize(options)
|
||||||
|
@options = options
|
||||||
|
end
|
||||||
|
|
||||||
|
def render
|
||||||
|
ERB.new(template).result(binding)
|
||||||
|
end
|
||||||
|
|
||||||
|
def template
|
||||||
|
File.read(options[:template])
|
||||||
|
end
|
||||||
|
|
||||||
|
def resource_tags
|
||||||
|
options[:files].collect do |file|
|
||||||
|
case File.extname(file)
|
||||||
|
when '.js'
|
||||||
|
%{<script src="#{file}" type="text/javascript"></script>}
|
||||||
|
when '.css'
|
||||||
|
%{<link rel="stylesheet" href="#{file}" type="text/css" />}
|
||||||
|
else
|
||||||
|
raise FileTypeError.new("Unknown file type: #{File.extname(file)} for #{file}")
|
||||||
|
end
|
||||||
|
end.join
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
@ -41,31 +41,59 @@ describe Flowerbox::Delivery::Server do
|
|||||||
|
|
||||||
describe '#port' do
|
describe '#port' do
|
||||||
let(:interface) { '127.0.0.1' }
|
let(:interface) { '127.0.0.1' }
|
||||||
|
let(:base) { 25000 }
|
||||||
|
let(:initial) { base + @offset }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
server.stubs(:interface).returns(interface)
|
server.stubs(:interface).returns(interface)
|
||||||
|
|
||||||
|
@offset = 0
|
||||||
|
ok = true
|
||||||
|
|
||||||
|
begin
|
||||||
|
[ 0, 1 ].each do |index|
|
||||||
|
begin
|
||||||
|
TCPSocket.new(interface, base + @offset + index)
|
||||||
|
@offset += 1
|
||||||
|
ok = false
|
||||||
|
rescue Errno::ECONNREFUSED => e
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end while !ok
|
||||||
end
|
end
|
||||||
|
|
||||||
subject { server.port }
|
subject { server.port }
|
||||||
|
|
||||||
context 'no running service' do
|
context 'no running service' do
|
||||||
before do
|
before do
|
||||||
Kernel.stubs(:rand).returns(0)
|
Kernel.stubs(:rand).returns(@offset)
|
||||||
end
|
end
|
||||||
|
|
||||||
it { should == 25000 }
|
it { should == initial }
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'running service' do
|
context 'running service' do
|
||||||
before do
|
before do
|
||||||
@server = Thread.new do
|
@server = Thread.new do
|
||||||
TCPServer.new(interface, 25000)
|
TCPServer.new(interface, initial)
|
||||||
end
|
end
|
||||||
|
|
||||||
server.stubs(:random_port).returns(25000, 25001)
|
server.stubs(:random_port).returns(initial, initial + 1)
|
||||||
|
|
||||||
|
while true
|
||||||
|
begin
|
||||||
|
TCPSocket.new(interface, initial)
|
||||||
|
break
|
||||||
|
rescue Errno::ECONNREFUSED
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it { should == 25001 }
|
it { should == initial + 1 }
|
||||||
|
|
||||||
|
after do
|
||||||
|
@server.kill
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
59
spec/flowerbox/delivery/template_renderer_spec.rb
Normal file
59
spec/flowerbox/delivery/template_renderer_spec.rb
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
describe Flowerbox::Delivery::TemplateRenderer do
|
||||||
|
let(:template_renderer) { described_class.new(:template => template, :files => files) }
|
||||||
|
let(:template) { 'template' }
|
||||||
|
let(:files) { 'files' }
|
||||||
|
|
||||||
|
let(:rendered_template) { 'rendered template' }
|
||||||
|
let(:erb_template) { "#{rendered_template} <%= resource_tags %>" }
|
||||||
|
|
||||||
|
describe '#render' do
|
||||||
|
subject { template_renderer.render }
|
||||||
|
|
||||||
|
let(:rendered_files) { 'with files' }
|
||||||
|
let(:result) { "#{rendered_template} #{rendered_files}" }
|
||||||
|
|
||||||
|
before do
|
||||||
|
template_renderer.expects(:resource_tags).returns(rendered_files)
|
||||||
|
template_renderer.expects(:template).returns(erb_template)
|
||||||
|
end
|
||||||
|
|
||||||
|
it { should == result }
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#template' do
|
||||||
|
include FakeFS::SpecHelpers
|
||||||
|
|
||||||
|
before do
|
||||||
|
File.open(template, 'wb') { |fh| fh.print erb_template }
|
||||||
|
end
|
||||||
|
|
||||||
|
subject { template_renderer.template }
|
||||||
|
|
||||||
|
it { should == erb_template }
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#resource_tags' do
|
||||||
|
subject { template_renderer.resource_tags }
|
||||||
|
|
||||||
|
context 'success' do
|
||||||
|
let(:files) { [ js, css ] }
|
||||||
|
let(:js) { 'file.js' }
|
||||||
|
let(:css) { 'file.css' }
|
||||||
|
|
||||||
|
it { should == [
|
||||||
|
%{<script src="#{js}" type="text/javascript"></script>},
|
||||||
|
%{<link rel="stylesheet" href="#{css}" type="text/css" />}
|
||||||
|
].join }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'failure' do
|
||||||
|
let(:files) { [ 'what.ever' ] }
|
||||||
|
|
||||||
|
it 'should raise error' do
|
||||||
|
expect { subject }.to raise_error(Flowerbox::Delivery::TemplateRenderer::FileTypeError)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -1,5 +1,6 @@
|
|||||||
require 'flowerbox-delivery'
|
require 'flowerbox-delivery'
|
||||||
require 'mocha'
|
require 'mocha'
|
||||||
|
require 'fakefs/spec_helpers'
|
||||||
|
|
||||||
RSpec.configure do |c|
|
RSpec.configure do |c|
|
||||||
c.mock_with :mocha
|
c.mock_with :mocha
|
||||||
|
Loading…
Reference in New Issue
Block a user