Renamed helpers to correspond to renamed Controller classes.
[merb_radiant.git] / radiant_specs / spec / lib / radiant / .svn / text-base / taggable_spec.rb.svn-base
blob7f8f5a9ba4ba327e2e380c798bc5270a28e756b2
1 require File.dirname(__FILE__) + "/../../spec_helper"
2 require 'ostruct'
4 describe Radiant::Taggable, "when included in a class or module" do
6   class TaggedClass
7     include Radiant::Taggable
8   end
9   
10   module TaggedModule
11     include Radiant::Taggable
12   end
13   
14   it "should add tag definition methods to the class" do
15     [TaggedClass, TaggedModule].each do |c|
16       c.should respond_to(:tag)
17       c.should respond_to(:desc)
18     end
19   end
20   
21   it "should turn tag definitions into methods" do
22     [TaggedClass, TaggedModule].each do |c|
23       c.tag 'hello' do 
24         "hello world" 
25       end
26       c.instance_methods.should include("tag:hello")
27     end
28     TaggedClass.new.send("tag:hello").should == "hello world"
29   end
30   
31   it "should store tag descriptions filtered with Textile" do
32     [TaggedClass, TaggedModule].each do |c|
33       c.desc "A simple tag."
34       Radiant::Taggable.last_description.should == "<p>A simple tag.</p>"
35     end
36   end
37   
38   it "should associate a tag description with the tag definition that follows it" do
39     [TaggedClass, TaggedModule].each do |c|
40       c.desc "Bonjour!"
41       c.tag "hello" do
42         "hello world"
43       end
44       c.tag_descriptions['hello'].should =~ /Bonjour!/
45       Radiant::Taggable.last_description.should be_nil
46     end
47   end
48   
49   it "should normalize leading whitespace in a tag description" do
50     Radiant::Taggable::Util.should_receive(:strip_leading_whitespace).twice.with("   Blah blah\n blah blah").and_return("blah")
51     [TaggedClass, TaggedModule].each do |c|
52       c.desc "   Blah blah\n blah blah"
53     end
54   end
55 end
57 describe Radiant::Taggable, "when included in a module with defined tags" do
59   module MyTags
60     include Radiant::Taggable
61     
62     desc %{This tag renders the text "just a test".}
63     tag "test" do
64       "just a test"
65     end
67     desc %{This tag implements "Hello, world!".}    
68     tag "hello" do |tag|
69       "Hello, #{ tag.attr['name'] || 'world' }!"
70     end
71   end
73   class TestObject
74     include Radiant::Taggable
75     
76     desc %{Yet another test}
77     tag "test" do
78       "My new test"
79     end
80     
81     include MyTags
82   end
84   before :each do
85     @object = TestObject.new
86     @tag_binding = OpenStruct.new('attr' => {"name" => "John"})
87   end
89   it "should have a collection of defined tags" do
90     MyTags.should respond_to(:tags)
91     MyTags.tags.should == ['hello', 'test']
92   end
93   
94   it "should add tags to an included class" do
95     TestObject.should respond_to(:tags)
96     TestObject.tags.should == ['hello', 'test']
97   end
98   
99   it "should merge tag descriptions with an included class" do
100     TestObject.tag_descriptions["test"].should == MyTags.tag_descriptions["test"]
101   end
102   
103   it "should render a defined tag on an instance of an included class" do
104     @object.should respond_to(:render_tag)
105     @object.render_tag(:test, {}).should == "My new test"
106   end
108   it "should render a defined tag on an instance of an included class with a given tag binding" do
109     @object.render_tag(:hello, @tag_binding).should == "Hello, John!"
110   end
114 describe Radiant::Taggable::Util do
115   it "should normalize leading whitespace" do
116         markup = %{  
117   
118   I'm a really small paragraph that
119   happens to span two lines.
120   
121   * I'm just
122   * a simple
123   * list
125   Let's try a small code example:
126   
127     puts "Hello world!"
128   
129   Nice job! It really, really, really
130   works.
133 result = %{
135 I'm a really small paragraph that
136 happens to span two lines.
138 * I'm just
139 * a simple
140 * list
142 Let's try a small code example:
144   puts "Hello world!"
146 Nice job! It really, really, really
147 works.}
148     Radiant::Taggable::Util.strip_leading_whitespace(markup).should == result
149   end