test/test_attributes.rb in attrtastic-0.1.3 vs test/test_attributes.rb in attrtastic-0.2.0

- old
+ new

@@ -1,105 +1,433 @@ require 'helper' class TestAttributes < Test::Unit::TestCase - def setup - setup_fixtures - end + context "attributes" do - def test__attributes__shoul_run_block - block_run = false - @user_builder.attributes do - block_run = true + setup do + setup_fixtures end - assert block_run - end + should "run block" do + block_run = false + @user_builder.attributes do + block_run = true + end - def test__attributes__output_without_block - expected = html <<-EOHTML - <div class="attributes"> - </div> - EOHTML + assert block_run + end - @user_builder.attributes - actual = @template.output_buffer.to_s + should "generate output even when no block given" do + expected = html <<-EOHTML + <div class="attributes"> + </div> + EOHTML - assert_equal expected, actual - end + @user_builder.attributes + actual = @template.output_buffer.to_s - def test__attributes__output - expected = html <<-EOHTML - <div class="attributes"> - <ol> - </ol> - </div> - EOHTML + assert_equal expected, actual + end - @user_builder.attributes do end - actual = @template.output_buffer.to_s + should "generate output with block given" do + expected = html <<-EOHTML + <div class="attributes"> + <ol> + </ol> + </div> + EOHTML - assert_equal expected, actual - end + @user_builder.attributes do end + actual = @template.output_buffer.to_s - def test__attributes__with_header - expected = html <<-EOHTML - <div class="attributes"> - <div class="legend">Legend</div> - <ol> - </ol> - </div> - EOHTML + assert_equal expected, actual + end - @user_builder.attributes "Legend" do end - actual = @template.output_buffer.to_s - assert_equal expected, actual - end + should "show header" do + expected = html <<-EOHTML + <div class="attributes"> + <div class="legend">Legend</div> + <ol> + </ol> + </div> + EOHTML - def test__attributes__with_fields_list - expected = html <<-EOHTML - <div class="attributes"> - <ol> - <li class="attribute"> - <span class="label">Full name</span> - <span class="value">Doe, John</span> - </li> - <li class="attribute"> - <span class="label">Email</span> - <span class="value">john@doe.com</span> - </li> - </ol> - </div> - EOHTML + @user_builder.attributes "Legend" do end + actual = @template.output_buffer.to_s + assert_equal expected, actual - @user_builder.attributes :full_name, :email - actual = @template.output_buffer.to_s - assert_equal expected, actual - end + @template.output_buffer.clear + @user_builder.attributes :name => "Legend" do end + actual = @template.output_buffer.to_s + assert_equal expected, actual + end - def test__attributes__with_fields_list_header_and_options - expected = html <<-EOHTML - <div class="attributes contact"> - <div class="legend">Contact</div> - <ol> - <li class="attribute"> - <span class="label">Full name</span> - <span class="value">Doe, John</span> - </li> - <li class="attribute"> - <span class="label">Title</span> - <span class="value"></span> - </li> - <li class="attribute"> - <span class="label">Email</span> - <span class="value">john@doe.com</span> - </li> - </ol> - </div> - EOHTML + context "with fields list" do - @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 + should "generate output" do + expected = html <<-EOHTML + <div class="attributes"> + <ol> + <li class="attribute"> + <span class="label">Full name</span> + <span class="value">Doe, John</span> + </li> + <li class="attribute"> + <span class="label">Email</span> + <span class="value">john@doe.com</span> + </li> + </ol> + </div> + 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 + <div class="attributes contact"> + <div class="legend">Contact</div> + <ol> + <li class="attribute"> + <span class="label">Full name</span> + <span class="value">Doe, John</span> + </li> + <li class="attribute"> + <span class="label">Title</span> + <span class="value"></span> + </li> + <li class="attribute"> + <span class="label">Email</span> + <span class="value">john@doe.com</span> + </li> + </ol> + </div> + 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 + <li class="attribute"> + <span class="label">Full name</span> + <span class="value">Doe, John</span> + </li> + EOHTML + + actual = author.attribute :full_name + assert_equal expected, actual + + end + + expected = html <<-EOHTML + <div class="attributes"> + <ol> + </ol> + </div> + 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 + <div class="attributes"> + <div class="legend">Author</div> + <ol> + </ol> + </div> + EOHTML + + actual = @template.output_buffer + assert_equal expected, actual + end + + should "work with field list" do + expected = html <<-EOHTML + <div class="attributes"> + <ol> + <li class="attribute"> + <span class="label">Full name</span> + <span class="value">Doe, John</span> + </li> + </ol> + </div> + 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 + <li class="attribute"> + <span class="label">Full name</span> + <span class="value">Doe, John</span> + </li> + EOHTML + + actual = author.attribute :full_name + assert_equal expected, actual + + end + + expected = html <<-EOHTML + <div class="attributes"> + <ol> + </ol> + </div> + 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 + <div class="attributes"> + <div class="legend">Author</div> + <ol> + </ol> + </div> + EOHTML + + actual = @template.output_buffer + assert_equal expected, actual + end + + should "work with field list" do + expected = html <<-EOHTML + <div class="attributes"> + <ol> + <li class="attribute"> + <span class="label">Full name</span> + <span class="value">Doe, John</span> + </li> + </ol> + </div> + 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 + <div class="attributes"> + <ol> + </ol> + </div> + <div class="attributes"> + <ol> + </ol> + </div> + 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 + <div class="attributes"> + <div class="legend">Post</div> + <ol> + </ol> + </div> + <div class="attributes"> + <div class="legend">Post</div> + <ol> + </ol> + </div> + EOHTML + + actual = @template.output_buffer + assert_equal expected, actual + end + + should "work with field list" do + expected = html <<-EOHTML + <div class="attributes"> + <ol> + <li class="attribute"> + <span class="label">Title</span> + <span class="value">Hello World!</span> + </li> + </ol> + </div> + <div class="attributes"> + <ol> + <li class="attribute"> + <span class="label">Title</span> + <span class="value">Sorry</span> + </li> + </ol> + </div> + 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 + <div class="attributes"> + <ol> + </ol> + </div> + <div class="attributes"> + <ol> + </ol> + </div> + 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 + <div class="attributes"> + <div class="legend">Post</div> + <ol> + </ol> + </div> + <div class="attributes"> + <div class="legend">Post</div> + <ol> + </ol> + </div> + EOHTML + + actual = @template.output_buffer + assert_equal expected, actual + end + + should "work with field list" do + expected = html <<-EOHTML + <div class="attributes"> + <ol> + <li class="attribute"> + <span class="label">Title</span> + <span class="value">Hello World!</span> + </li> + </ol> + </div> + <div class="attributes"> + <ol> + <li class="attribute"> + <span class="label">Title</span> + <span class="value">Sorry</span> + </li> + </ol> + </div> + EOHTML + + @blog_builder.attributes :title, :for => @blog.posts + + actual = @template.output_buffer + assert_equal expected, actual + end + end + + end end