textfilter_controller_spec.rb
上传用户:netsea168
上传日期:2022-07-22
资源大小:4652k
文件大小:8k
源码类别:

Ajax

开发平台:

Others

  1. require File.dirname(__FILE__) + '/../spec_helper'
  2. require 'flickr_mock'
  3. describe TextfilterController do
  4.   before do
  5.     reset_whiteboard
  6.     get :test_action # set up @url; In Rails 1.0, we can't do url_for without it.
  7.   end
  8.   def blog
  9.     blogs(:default)
  10.   end
  11.   def filter_text(text, filters, filterparams={})
  12.     TextFilter.filter_text(blog, text, self, filters, filterparams)
  13.   end
  14.   def whiteboard
  15.     @whiteboard ||= Hash.new
  16.   end
  17.   def reset_whiteboard
  18.     @whiteboard = nil
  19.   end
  20.   it "test_unknown" do
  21.     text = filter_text('*foo*',[:unknowndoesnotexist])
  22.     assert_equal '*foo*', text
  23.   end
  24.   it "test_smartypants" do
  25.     text = filter_text('"foo"',[:smartypants])
  26.     assert_equal '“foo”', text
  27.   end
  28.   it "test_markdown" do
  29.     text = filter_text('*foo*', [:markdown])
  30.     assert_equal '<p><em>foo</em></p>', text
  31.     text = filter_text("foonnbar",[:markdown])
  32.     assert_equal "<p>foo</p>nn<p>bar</p>", text
  33.   end
  34.   it "test_filterchain" do
  35.     assert_equal '<p><em>&#8220;foo&#8221;</em></p>',
  36.       filter_text('*"foo"*',[:markdown,:smartypants])
  37.     assert_equal '<p><em>&#8220;foo&#8221;</em></p>',
  38.       filter_text('*"foo"*',[:doesntexist1,:markdown,"doesn't exist 2",:smartypants,:nopenotmeeither])
  39.   end
  40.   it "test_flickr" do
  41.     assert_equal "<div style="float:left" class="flickrplugin"><a href="http://www.flickr.com/users/scottlaird/31366117"><img src="http://photos23.flickr.com/31366117_b1a791d68e_s.jpg" width="75" height="75" alt="Matz" title="Matz"/></a><p class="caption" style="width:75px">This is Matz, Ruby's creator</p></div>",
  42.       filter_text('<typo:flickr img="31366117" size="Square" style="float:left"/>',
  43.         [:macropre,:macropost],
  44.         {'flickr-user' => 'scott@sigkill.org'})
  45.     # Test default image size
  46.     assert_equal "<div style="" class="flickrplugin"><a href="http://www.flickr.com/users/scottlaird/31366117"><img src="http://photos23.flickr.com/31366117_b1a791d68e_s.jpg" width="75" height="75" alt="Matz" title="Matz"/></a><p class="caption" style="width:75px">This is Matz, Ruby's creator</p></div>",
  47.       filter_text('<typo:flickr img="31366117"/>',
  48.         [:macropre,:macropost],
  49.         {'flickr-user' => 'scott@sigkill.org'})
  50.     # Test with caption=""
  51.     assert_equal "<div style="" class="flickrplugin"><a href="http://www.flickr.com/users/scottlaird/31366117"><img src="http://photos23.flickr.com/31366117_b1a791d68e_s.jpg" width="75" height="75" alt="Matz" title="Matz"/></a></div>",
  52.       filter_text('<typo:flickr img="31366117" caption=""/>',
  53.         [:macropre,:macropost],
  54.         {'flickr-user' => 'scott@sigkill.org'})
  55.   end
  56.   it "test_broken_flickr_link" do
  57.     assert_equal %{<div class='broken_flickr_link'>`notaflickrid' could not be displayed because: <br />Photo not found</div>},
  58.       filter_text('<typo:flickr img="notaflickrid" />',
  59.         [:macropre, :macropost],
  60.         { 'flickr-user' => 'scott@sigkill.org' })
  61.   end
  62.   describe 'code textfilter' do
  63.     
  64.     describe 'single line' do
  65.       it 'should made nothin if no args' do
  66.         filter_text('<typo:code>foo-code</typo:code>', [:macropre,:macropost]).should == %{<div class="CodeRay"><pre><notextile>foo-code</notextile></pre></div>}
  67.       end
  68.       it 'should parse ruby lang' do
  69.         filter_text('<typo:code lang="ruby">foo-code</typo:code>', [:macropre,:macropost]).should == %{<div class="CodeRay"><pre><notextile><span class="CodeRay">foo-code</span></notextile></pre></div>}
  70.       end
  71.       it 'should parse ruby and xml in same sentence but not in same place' do
  72.         filter_text('<typo:code lang="ruby">foo-code</typo:code> blah blah <typo:code lang="xml">zzz</typo:code>',[:macropre,:macropost]).should == %{<div class="CodeRay"><pre><notextile><span class="CodeRay">foo-code</span></notextile></pre></div> blah blah <div class="CodeRay"><pre><notextile><span class="CodeRay">zzz</span></notextile></pre></div>}
  73.       end
  74.     end
  75.     describe 'multiline' do
  76.       it 'should render ruby' do
  77.       filter_text(%{
  78. <typo:code lang="ruby">
  79. class Foo
  80.   def bar
  81.     @a = "zzz"
  82.   end
  83. end
  84. </typo:code>
  85. },[:macropre,:macropost]).should == %{
  86. <div class="CodeRay"><pre><notextile><span class="CodeRay"><span class="r">class</span> <span class="cl">Foo</span>
  87.   <span class="r">def</span> <span class="fu">bar</span>
  88.     <span class="iv">@a</span> = <span class="s"><span class="dl">&quot;</span><span class="k">zzz</span><span class="dl">&quot;</span></span>
  89.   <span class="r">end</span>
  90. <span class="r">end</span></span></notextile></pre></div>
  91. }
  92.       end
  93.     end
  94.   end
  95.   it "test_named_filter" do
  96.     assert_equal '<p><em>&#8220;foo&#8221;</em></p>',
  97.       TextFilter.filter_text_by_name(blog, '*"foo"*', 'markdown smartypants')
  98.   end
  99.   it "test_code_plus_markup_chain" do
  100.     text = <<-EOF
  101. *header text here*
  102. <typo:code lang="ruby">
  103. class test
  104.   def method
  105.     "foo"
  106.   end
  107. end
  108. </typo:code>
  109. _footer text here_
  110. EOF
  111.     expects_markdown = <<-EOF
  112. <p><em>header text here</em></p>
  113. <div class="CodeRay"><pre><span class="CodeRay"><span class="r">class</span> <span class="cl">test</span>
  114.   <span class="r">def</span> <span class="fu">method</span>
  115.     <span class="s"><span class="dl">&quot;</span><span class="k">foo</span><span class="dl">&quot;</span></span>
  116.   <span class="r">end</span>
  117. <span class="r">end</span></span></pre></div>
  118. <p><em>footer text here</em></p>
  119. EOF
  120.     expects_textile = <<-EOF
  121. <p><strong>header text here</strong></p>
  122. <div class="CodeRay"><pre><span class="CodeRay"><span class="r">class</span> <span class="cl">test</span>
  123.   <span class="r">def</span> <span class="fu">method</span>
  124.     <span class="s"><span class="dl">&quot;</span><span class="k">foo</span><span class="dl">&quot;</span></span>
  125.   <span class="r">end</span>
  126. <span class="r">end</span></span></pre></div>
  127. <p><em>footer text here</em></p>
  128. EOF
  129.     assert_equal expects_markdown.strip, TextFilter.filter_text_by_name(blog, text, 'markdown')
  130.     assert_equal expects_textile.strip, TextFilter.filter_text_by_name(blog, text, 'textile')
  131.   end
  132.   it "test_lightbox" do
  133.     assert_equal "<div style="float:left" class="lightboxplugin"><a href="http://photos23.flickr.com/31366117_b1a791d68e_b.jpg" rel="lightbox" title="Matz"><img src="http://photos23.flickr.com/31366117_b1a791d68e_t.jpg" width="67" height="100" alt="Matz" title="Matz"/></a><p class="caption" style="width:67px">This is Matz, Ruby's creator</p></div>",
  134.       filter_text('<typo:lightbox img="31366117" thumbsize="Thumbnail" displaysize="Large" style="float:left"/>',
  135.         [:macropre,:macropost],
  136.         {})
  137. #     Test default thumb image size
  138.     assert_equal "<div style="" class="lightboxplugin"><a href="http://photos23.flickr.com/31366117_b1a791d68e_b.jpg" rel="lightbox" title="Matz"><img src="http://photos23.flickr.com/31366117_b1a791d68e_s.jpg" width="75" height="75" alt="Matz" title="Matz"/></a><p class="caption" style="width:75px">This is Matz, Ruby's creator</p></div>",
  139.       filter_text('<typo:lightbox img="31366117" displaysize="Large"/>',
  140.         [:macropre,:macropost],
  141.         {})
  142. #     Test default display image size
  143.     assert_equal "<div style="" class="lightboxplugin"><a href="http://photos23.flickr.com/31366117_b1a791d68e_o.jpg" rel="lightbox" title="Matz"><img src="http://photos23.flickr.com/31366117_b1a791d68e_s.jpg" width="75" height="75" alt="Matz" title="Matz"/></a><p class="caption" style="width:75px">This is Matz, Ruby's creator</p></div>",
  144.       filter_text('<typo:lightbox img="31366117"/>',
  145.         [:macropre,:macropost],
  146.         {})
  147. #     Test with caption=""
  148.     assert_equal "<div style="" class="lightboxplugin"><a href="http://photos23.flickr.com/31366117_b1a791d68e_o.jpg" rel="lightbox" title="Matz"><img src="http://photos23.flickr.com/31366117_b1a791d68e_s.jpg" width="75" height="75" alt="Matz" title="Matz"/></a></div>",
  149.       filter_text('<typo:lightbox img="31366117" caption=""/>',
  150.         [:macropre,:macropost],
  151.         {})
  152.   end
  153. end