# encoding: utf-8
require 'bacon'
$:.unshift File.expand_path('../../lib', __FILE__)
require "fragment"
describe 'Fragment' do
def fragment(&block); Fragment.new(&block).to_s; end
it "Builds simple tags" do
fragment{ br }.should == ' '
fragment{ p }.should == '
'
end
it "Opens and closes tags" do
fragment{ p{} }.should == ''
fragment{ div{} }.should == ''
end
it "Nests tags" do
fragment{ p{ br } }.should == '
'
end
it "Builds deeply nested tags" do
fragment do
p do
div do
ol do
li
end
end
end
end.should == '
'
end
it "Builds deeply nested tags with repetition" do
fragment do
p do
div do
ol do
li
li
end
ol do
li
li
end
end
end
end.should == '
'
end
it "Builds deeply nested tags with strings" do
fragment do
p do
div {'Hello, World'}
end
end.should == '
Hello, World
'
end
it "Allows to write directly if needed" do
fragment do
write ""
end.should == ''
end
it "Builds a full HTML page" do
fragment do
doctype
html do
head do
title {"Hello World"}
end
body do
h1 {"Hello World"}
end
end
end.should == "\nHello World
Hello World
"
end
it "Builds with some ruby inside" do
fragment do
table do
tr do
%w[one two three].each do |s|
td{s}
end
end
end
end.should == '
one
two
three
'
end
it "Builds escapeable attributes" do
fragment {
a(:href => "http://example.org/?a=one&b=two") {
"Click here"
}
}.should == "Click here"
end
it "Should accept attributes in a string" do
fragment{ input("type='text'") }.should == ""
end
it 'Should accept symbols as attributes' do
input = fragment{ input(:type => :text, :value => :one) }
input.should =~ /type='text'/
input.should =~ /value='one'/
end
it 'Builds tags with prefix' do
fragment{ tag "prefix:local" }.should == ''
end
it 'Builds tags with a variety of characters' do
# with "-"
fragment{ tag "hello-world" }.should == ''
# with Hiragana
fragment{ tag "あいうえお" }.should == '<あいうえお />'
end
it "Has a practicle way to add attributes like 'selected' based on boolean" do
@selected = false
fragment do
option({:name => 'opt', :selected => @selected})
option(:name => 'opt', :selected => !@selected)
option(:name => 'opt', :selected => @i_am_nil)
end.should == ""
end
it "Builds a more complex HTML page with a variable in the outer scope" do
default = 'HTML'
html = Fragment.new do
doctype
html(:lang=>'en') do
head { title { "My Choice" } }
body do
comment "Here starts the body"
select(:name => 'language') do
['JS', 'HTML', 'CSS'].each do |l|
option(:value => l, :selected => l==default) { l }
end
end
write "\n\n"
write "\n\n"
end
end
end.to_s
html.should == "\nMy Choice\n\n\n\n\n\n"
end
it 'Can be used inside the scope of an object' do
class Stranger
attr_accessor :name, :comment
def show_comment
Fragment.new(true) do |b|
b.p{ self.comment }
end.to_s
end
def show_summary
Fragment.create_here do |b|
b.article do
b.h1{ self.name }
b.p{ self.comment }
end
end
end
def fail_summary
Fragment.create do
article do
h1{ self.name }
p{ self.comment }
end
end
end
end
s = Stranger.new
s.name = 'The Doors'
s.comment = 'Strange days'
s.show_comment.should=='