require 'rubygems'
require 'bacon'
ROOT = ::File.dirname(__FILE__)+'/..'
$:.unshift ROOT+'/lib'
require 'fragment'
describe "Fragment" do
def fragment &block
Fragment.please(&block)
end
it "simple tag" do
fragment{ br }.should == ' '
fragment{ p }.should == '
'
end
it "open close tags" do
fragment{ p{} }.should == ''
fragment{ div{} }.should == ''
end
it "nested tags" do
fragment{ p{ br } }.should == '
'
end
it "deep nested tags" do
fragment do
p do
div do
ol do
li
end
end
end
end.should == '
'
end
it "deep 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 "deep 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 "some simple example" 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 "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 "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 'tags with prefix' do
fragment{ tag "prefix:local" }.should == ''
end
it '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 "Pass the Readme example" do
default = 'HTML'
html = Fragment.please 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
html.should == "\nMy Choice\n\n\n\n\n\n"
end
end