here = File.dirname(__FILE__) require File.expand_path("#{here}/../spec_helper") $: << File.expand_path("#{here}/../../web/") require "article" describe Article do it "requires a name" do lambda { Article.new }.should raise_error(ArgumentError) a = Article.new(:name => "Foo") assert { a.instance_variable_get(:@name) == "Foo" } end it "renders a name" do a = Article.new(:name => "Foo") a.to_html.should == "

Foo

" end it "defaults to empty sections" do a = Article.new(:name => "Foo") assert { a.instance_variable_get(:@sections) == [] } end it "allows sections" do a = Article.new(:name => "Foo", :sections => [w = Erector::Widget.new]) assert { a.instance_variable_get(:@sections) == [w] } end it "allows adding sections later" do a = Article.new(:name => "Foo") a << (w = Section.new(:name => "Bar")) assert { a.instance_variable_get(:@sections) == [w] } end it "returns self from <<, to allow chaining" do a = Article.new(:name => "Foo") val = a << (w = Erector::Widget.new) assert { val == a } end it "allows adding sections via a method call" do a = Article.new(:name => "Foo") a.add(:name => "Bar") do p.bar! end s = a.instance_variable_get(:@sections) assert { s.size == 1 } assert { s.first.name == "Bar" } assert { s.first.to_html == "

" } end describe "with contents" do before do @article = Article.new(:name => "Gnome Plan") @article << Section.new(:name => "Steal Underpants") do p "very important" end @article << Section.new(:name => "?", :href => "question") do p "todo" end @article << Section.new(:name => "Profit!") do p "victory" end end it "shows a table of contents" do @article.to_html =~ /
/ end it "toc contains each section's name and a link to its href" do @article.to_html(:content_method_name => "table_of_contents", :prettyprint => true).should == <<-HTML

Table of Contents

  1. Steal Underpants
  2. ?
  3. Profit!
HTML end it "shows each section" do @article.to_html(:content_method_name => "emit_sections", :prettyprint => true).should == <<-HTML

1. Steal Underpants

very important

2. ?

todo

3. Profit!

victory

HTML end end describe "with subsections" do before do @article = Article.new(:name => "Food") @article << Section.new(:name => "Breakfast") { p "very important" }.tap do |s| s << Section.new(:name => "Coffee") { p "french roast" } s << Section.new(:name => "Eggs") { p "scrambled" } end end it "toc contains each section's name and a link to its href" do @article.to_html(:content_method_name => "table_of_contents", :prettyprint => true).should == <<-HTML

Table of Contents

  1. Breakfast
    1. Coffee
    2. Eggs
HTML end it "shows each section" do @article.to_html(:content_method_name => "emit_sections", :prettyprint => true).should == <<-HTML

1. Breakfast

very important

1.1. Coffee

french roast

1.2. Eggs

scrambled

HTML end end it "can be created using nested taps and adds" do article = Article.new(:name => "food").tap { |a| a.add(:name => "fruit") { text "sweet and fibrous" }.tap { |s| s.add(:name => "apple") { text "crunchy" }.tap { |s| s.add(:name => "granny smith") { text "green" } s.add(:name => "red delicious") { text "red" } } s.add(:name => "banana") { text "yellow" } } a.add(:name => "bread") { text "chewy and grainy" }.tap { |s| s.add(:name => "sourdough") { text "tangy" } } } article.to_html(:prettyprint => true).should == <<-HTML

food

1. fruit

sweet and fibrous

1.1. apple

crunchy

1.1.1. granny smith

green

1.1.2. red delicious

red

1.2. banana

yellow

2. bread

chewy and grainy

2.1. sourdough

tangy
HTML end end