require 'helper'
class TestAttribute < TestCase
context "attribute" do
setup do
setup_fixtures
end
should "generate output" do
expected = html <<-EOHTML
Full name
Doe, John
EOHTML
actual = @user_builder.attribute(:full_name)
assert_equal expected, actual
end
should "not show attribute when value is blank" do
actual = @user_builder.attribute(:title)
assert_nil actual
end
should "show attribute with :display_empty => true" do
expected = html <<-EOHTML
Title
EOHTML
actual = @user_builder.attribute(:title, :display_empty => true)
assert_equal expected, actual
end
context "with default formating" do
should "properly format a String" do
expected = html <<-EOHTML
Author
Doe, John
EOHTML
actual = @blog_builder.attribute(:author_full_name, :label => "Author")
assert_equal expected, actual
end
should "properly format a Date" do
expected = html <<-EOHTML
Birthday
1953-06-03
EOHTML
actual = @user_builder.attribute(:birthday)
assert_equal expected, actual
end
should "properly format a DateTime" do
expected = html <<-EOHTML
Created at
Thu, 02 Jun 2011 12:06:42 +0000
EOHTML
actual = @user_builder.attribute(:created_at)
assert_equal expected, actual
end
should "properly format a Time" do
expected = html <<-EOHTML
Time
Sat, 01 Jan 2000 06:00:00 +0100
EOHTML
actual = @user_builder.attribute(:time)
assert_equal expected, actual
end
should "properly format a Float" do
expected = html <<-EOHTML
Float
54424.220
EOHTML
actual = @user_builder.attribute(:float)
assert_equal expected, actual
end
should "properly format a Decimal" do
expected = html <<-EOHTML
Decimal
4454.344
EOHTML
actual = @user_builder.attribute(:decimal)
assert_equal expected, actual
end
should "properly format a Integer" do
expected = html <<-EOHTML
Integer
45,453
EOHTML
actual = @user_builder.attribute(:integer)
assert_equal expected, actual
end
end
context "with default formating disabled" do
should "properly format a String" do
expected = html <<-EOHTML
Author
Doe, John
EOHTML
actual = @blog_builder.attribute(:author_full_name, :label => "Author", :format => false)
assert_equal expected, actual
end
should "properly format a Date" do
expected = html <<-EOHTML
Birthday
1953-06-03
EOHTML
actual = @user_builder.attribute(:birthday, :format => false)
assert_equal expected, actual
end
should "properly format a DateTime" do
expected = html <<-EOHTML
Created at
2011-06-02T12:06:42+00:00
EOHTML
actual = @user_builder.attribute(:created_at, :format => false)
assert_equal expected, actual
end
should "properly format a Time" do
expected = html <<-EOHTML
Time
2000-01-01 06:00:00 +0100
EOHTML
actual = @user_builder.attribute(:time, :format => false)
assert_equal expected, actual
end
should "properly format a Float" do
expected = html <<-EOHTML
Float
54424.22
EOHTML
actual = @user_builder.attribute(:float, :format => false)
assert_equal expected, actual
end
should "properly format a Decimal" do
expected = html <<-EOHTML
Decimal
4454.3435
EOHTML
actual = @user_builder.attribute(:decimal, :format => false)
assert_equal expected, actual
end
should "properly format a Integer" do
expected = html <<-EOHTML
Integer
45453
EOHTML
actual = @user_builder.attribute(:integer, :format => false)
assert_equal expected, actual
end
end
context "with custom formating" do
should "output the return value of called template's method" do
expected = html <<-EOHTML
Author
Hello, my name is Doe, John
EOHTML
def @template.hello(name)
"Hello, my name is #{name}"
end
actual = @blog_builder.attribute(:author_full_name, :label => "Author", :format => :hello)
assert_equal expected, actual
end
end
should "show custom label" do
expected = html <<-EOHTML
Name
Doe, John
EOHTML
actual = @user_builder.attribute(:full_name, :label => "Name")
assert_equal expected, actual
end
should "show custom value" do
expected = html <<-EOHTML
Full name
Sir Doe, John
EOHTML
actual = @user_builder.attribute(:full_name, :value => "Sir #{@user.full_name}")
assert_equal expected, actual
end
should "use th custome value as hash key if it's a symbol and the attribute is a hash" do
expected = html <<-EOHTML
Address
Hellway 13
EOHTML
actual = @user_builder.attribute(:address, :value => :street)
assert_equal expected, actual
end
should "use th custome value as a method it's a symbol and the attribute is not a hash" do
expected = html <<-EOHTML
Blog
IT Pro Blog
EOHTML
actual = @user_builder.attribute(:blog, :value => :name)
assert_equal expected, actual
end
should "work with custom value blank" do
assert_nil @user_builder.attribute(:full_name, :value => nil)
assert_nil @user_builder.attribute(:full_name, :value => "")
expected = html <<-EOHTML
Full name
EOHTML
actual = @user_builder.attribute(:full_name, :value => nil, :display_empty => true)
assert_equal expected, actual
actual = @user_builder.attribute(:full_name, :value => "", :display_empty => true)
assert_equal expected, actual
end
context "with block" do
should "yield block" do
block_run = false
@user_builder.attribute :full_name do
block_run = true
end
assert block_run
end
should "generate output" do
expected = html <<-EOHTML
Full name
John Doe!!!
EOHTML
actual = @user_builder.attribute :full_name do
@user_builder.template.output_buffer << "John Doe"
3.times do
@user_builder.template.output_buffer << "!"
end
end
assert_equal expected, actual
end
should "show custom label" do
expected = html <<-EOHTML
Full name
John Doe!!!
EOHTML
actual = @user_builder.attribute :label => "Full name" do
@user_builder.template.output_buffer << "John Doe"
3.times do
@user_builder.template.output_buffer << "!"
end
end
assert_equal expected, actual
end
end
end
end