require 'helper' class TestAttributes < Test::Unit::TestCase context "attributes" do setup do setup_fixtures end should "run block" do block_run = false @user_builder.attributes do block_run = true end assert block_run end should "generate output even when no block given" do expected = html <<-EOHTML
EOHTML @user_builder.attributes actual = @template.output_buffer.to_s assert_equal expected, actual end should "generate output with block given" do expected = html <<-EOHTML
EOHTML @user_builder.attributes do end actual = @template.output_buffer.to_s assert_equal expected, actual end should "show header" do expected = html <<-EOHTML
Legend
EOHTML @user_builder.attributes "Legend" do end actual = @template.output_buffer.to_s assert_equal expected, actual @template.output_buffer.clear @user_builder.attributes :name => "Legend" do end actual = @template.output_buffer.to_s assert_equal expected, actual end context "with fields list" do should "generate output" do expected = html <<-EOHTML
  1. Full name Doe, John
  2. Email john@doe.com
EOHTML @user_builder.attributes :full_name, :email actual = @template.output_buffer.to_s assert_equal expected, actual end should "show header" do expected = html <<-EOHTML
Contact
  1. Full name Doe, John
  2. Title
  3. Email john@doe.com
EOHTML @user_builder.attributes "Contact", :full_name, :title, :email, :html => {:class => "contact"}, :display_empty => true actual = @template.output_buffer.to_s assert_equal expected, actual end end context "with :for option" do should "yield block" do block_run = false @blog_builder.attributes :for => nil do |author| block_run = true end assert block_run end end context "with :for => :method_name pointing to single object" do should "allow to access inner object" do @blog_builder.attributes :for => :author do |author| assert_equal @blog.author, author.record end end should "generate output for given inner object" do @blog_builder.attributes :for => :author do |author| expected = html <<-EOHTML
  • Full name Doe, John
  • EOHTML actual = author.attribute :full_name assert_equal expected, actual end expected = html <<-EOHTML
    EOHTML actual = @template.output_buffer assert_equal expected, actual end should "show header" do @blog_builder.attributes "Author", :for => :author do |author| end expected = html <<-EOHTML
    Author
    EOHTML actual = @template.output_buffer assert_equal expected, actual end should "work with field list" do expected = html <<-EOHTML
    1. Full name Doe, John
    EOHTML @blog_builder.attributes :full_name, :for => :author actual = @template.output_buffer assert_equal expected, actual end end context "with :for => object" do should "allow to acces given object" do @blog_builder.attributes :for => @user do |author| assert_equal @user, author.record end end should "generate output for given inner object" do @blog_builder.attributes :for => @user do |author| expected = html <<-EOHTML
  • Full name Doe, John
  • EOHTML actual = author.attribute :full_name assert_equal expected, actual end expected = html <<-EOHTML
    EOHTML actual = @template.output_buffer assert_equal expected, actual end should "show header" do @blog_builder.attributes "Author", :for => @user do |author| end expected = html <<-EOHTML
    Author
    EOHTML actual = @template.output_buffer assert_equal expected, actual end should "work with field list" do expected = html <<-EOHTML
    1. Full name Doe, John
    EOHTML @blog_builder.attributes :full_name, :for => @user actual = @template.output_buffer assert_equal expected, actual end end context "with :for => :method_name pointing to collection" do should "allow to access inner objects one by one" do posts = [] @blog_builder.attributes :for => :posts do |post| posts << post.record end assert_equal @blog.posts, posts end should "generate output for given objects" do @blog_builder.attributes :for => :posts do |post| end expected = html <<-EOHTML
    EOHTML actual = @template.output_buffer assert_equal expected, actual end should "show header" do @blog_builder.attributes "Post", :for => :posts do |post| end expected = html <<-EOHTML
    Post
    Post
    EOHTML actual = @template.output_buffer assert_equal expected, actual end should "work with field list" do expected = html <<-EOHTML
    1. Title Hello World!
    1. Title Sorry
    EOHTML @blog_builder.attributes :title, :for => :posts actual = @template.output_buffer assert_equal expected, actual end end context "with :for => collection" do should "allow to access inner objects one by one" do posts = [] @blog_builder.attributes :for => @blog.posts do |post| posts << post.record end assert_equal @blog.posts, posts end should "generate output for given objects" do @blog_builder.attributes :for => @blog.posts do |post| end expected = html <<-EOHTML
    EOHTML actual = @template.output_buffer assert_equal expected, actual end should "show header" do @blog_builder.attributes "Post", :for => @blog.posts do |post| end expected = html <<-EOHTML
    Post
    Post
    EOHTML actual = @template.output_buffer assert_equal expected, actual end should "work with field list" do expected = html <<-EOHTML
    1. Title Hello World!
    1. Title Sorry
    EOHTML @blog_builder.attributes :title, :for => @blog.posts actual = @template.output_buffer assert_equal expected, actual end end end end