05_markdown.tests.rb
来自「用ruby on rails写的一个博客程序,还不错..ruby on rail」· RB 代码 · 共 1,528 行 · 第 1/3 页
RB
1,528 行
#!/usr/bin/ruby## Test case for BlueCloth Markdown transforms.# $Id: TEMPLATE.rb.tpl,v 1.2 2003/09/11 04:59:51 deveiant Exp $## Copyright (c) 2004 The FaerieMUD Consortium.# if !defined?( BlueCloth ) || !defined?( BlueCloth::TestCase ) basedir = File::dirname( __FILE__ ) require File::join( basedir, 'bctestcase' )end### This test case tests ...class SubfunctionsTestCase < BlueCloth::TestCase ### Test email address output Emails = %w[ address@example.com foo-list-admin@bar.com fu@bar.COM baz@ruby-lang.org foo-tim-bazzle@bar-hop.co.uk littlestar@twinkle.twinkle.band.CO.ZA ll@lll.lllll.ll Ull@Ulll.Ulllll.ll UUUU1@UU1.UU1UUU.UU l@ll.ll Ull.Ullll@llll.ll Ulll-Ull.Ulllll@ll.ll 1@111.ll ] # I can't see a way to handle IDNs clearly yet, so these will have to wait. # info@鰇o.de # jemand@b黵o.de # irgendwo-interre遖nt@d舋ta.se #] def test_10_email_address printTestHeader "BlueCloth: Inline email address" rval = match = nil Emails.each {|addr| assert_nothing_raised { rval = BlueCloth::new( "<#{addr}>" ).to_html } match = %r{<p><a href="([^\"]+)">[^<]+</a></p>}.match( rval ) assert_not_nil match, "Match against output #{rval}" assert_equal "mailto:#{addr}", decode( match[1] ) } end def decode( str ) str.gsub( /&#(x[a-f0-9]+|\d{3});/i ) {|match| code = $1 debugMsg "Decoding %p" % code case code when /^x([a-f0-9]+)/i debugMsg " (hex) = %p" % $1.to_i(16).chr $1.to_i(16).chr when /\d{3}/ debugMsg " (oct) = %p" % code.to_i.chr code.to_i.chr else raise "Hmmm... malformed entity %p" % code end } end ################################################################# ### A U T O - G E N E R A T E D T E S T S ################################################################# # Parse the data section into a hash of test specifications TestSets = {} begin seenEnd = false inMetaSection = true inInputSection = true section, description, input, output = '', '', '', '' linenum = 0 # Read this file, skipping lines until the __END__ token. Then start # reading the tests. File::foreach( __FILE__ ) {|line| linenum += 1 if /^__END__/ =~ line then seenEnd = true; next end debugMsg "#{linenum}: #{line.chomp}" next unless seenEnd # Start off in the meta section, which has sections and # descriptions. if inMetaSection case line # Left angles switch into data section for the current section # and description. when /^<<</ inMetaSection = false next # Section headings look like: # ### [Code blocks] when /^### \[([^\]]+)\]/ section = $1.chomp TestSets[ section ] ||= {} # Descriptions look like: # # Para plus code block when /^# (.*)/ description = $1.chomp TestSets[ section ][ description ] ||= { :line => linenum, :sets => [], } end # Data section has input and expected output parts else case line # Right angles terminate a data section, at which point we # should have enough data to add a test. when /^>>>/ TestSets[ section ][ description ][:sets] << [ input.chomp, output.chomp ] inMetaSection = true inInputSection = true input = ''; output = '' # 3-Dashed divider with text divides input from output when /^--- (.+)/ inInputSection = false # Anything else adds to either input or output else if inInputSection input += line else output += line end end end } end debugMsg "Test sets: %p" % TestSets # Auto-generate tests out of the test specifications TestSets.each {|sname, section| # Generate a test method for each section section.each do |desc, test| methname = "test_%03d_%s" % [ test[:line], desc.gsub(/\W+/, '_').downcase ] # Header code = %{ def #{methname} printTestHeader "BlueCloth: #{desc}" rval = nil } # An assertion for each input/output pair test[:sets].each {|input, output| code << %{ assert_nothing_raised { obj = BlueCloth::new(%p) rval = obj.to_html } assert_equal %p, rval } % [ input, output ] } code << %{ end } debugMsg "--- %s [%s]:\n%s\n---\n" % [sname, desc, code] eval code end }end__END__### [Paragraphs and Line Breaks]# Paragraphs<<<This is some stuff that should all be put in one paragrapheven though it occurs over several lines.And this is a anotherone.--- Should become:<p>This is some stuff that should all be put in one paragrapheven though it occurs over several lines.</p><p>And this is a anotherone.</p>>>># Line breaks<<<Mostly the same kind of thing with two spaces at the end of each line should result in line breaks, though.And this is a another one.--- Should become:<p>Mostly the same kind of thing<br/>with two spaces at the end<br/>of each line<br/>should result in<br/>line breaks, though.</p><p>And this is a another<br/>one.</p>>>># Escaping special characters<<<The left shift operator, which is written as <<, is often used & greatly admired.--- Should become:<p>The left shift operator, which is written as <<, is often used & greatly admired.</p>>>># Preservation of named entities<<<The left shift operator, which is written as <<, is often used & greatly admired.--- Should become:<p>The left shift operator, which is written as <<, is often used & greatly admired.</p>>>># Preservation of decimal-encoded entities<<<The left shift operator, which is written as <<, is often used & greatly admired.--- Should become:<p>The left shift operator, which is written as <<, is often used & greatly admired.</p>>>># Preservation of hex-encoded entities<<<The left shift operator, which is written as <<, is often used & greatly admired.--- Should become:<p>The left shift operator, which is written as <<, is often used & greatly admired.</p>>>># Inline HTML - table tags<<<This is a regular paragraph.<table> <tr> <td>Foo</td> </tr></table>This is another regular paragraph.--- Should become:<p>This is a regular paragraph.</p><table> <tr> <td>Foo</td> </tr></table><p>This is another regular paragraph.</p>>>># Inline HTML - div tags<<<This is a regular paragraph.<div> Something</div>Something else.--- Should become:<p>This is a regular paragraph.</p><div> Something</div><p>Something else.</p>>>># Inline HTML - Plain HR<<<This is a regular paragraph.<hr />Something else.--- Should become:<p>This is a regular paragraph.</p><hr /><p>Something else.</p>>>># Inline HTML - Fancy HR<<<This is a regular paragraph.<hr class="publishers-mark" id="first-hrule" />Something else.--- Should become:<p>This is a regular paragraph.</p><hr class="publishers-mark" id="first-hrule" /><p>Something else.</p>>>># Inline HTML - Iframe<<<This is a regular paragraph.<iframe src="foo.html" id="foo-frame"></iframe>Something else.--- Should become:<p>This is a regular paragraph.</p><iframe src="foo.html" id="foo-frame"></iframe><p>Something else.</p>>>># Inline HTML - mathml<<<Examples--------Now that we have met some of the key players, it is time to see what we cando. Here are some examples and comments which illustrate the use of the basiclayout and token elements. Consider the expression x2 + 4x + 4 = 0. A basicMathML presentation encoding for this would be:<math> <mrow> <msup> <mi>x</mi> <mn>2</mn> </msup> <mo>+</mo> <mn>4</mn> <mi>x</mi> <mo>+</mo> <mn>4</mn> <mo>=</mo> <mn>0</mn> </mrow></math>This encoding will display as you would expect. However, if we were interestedin reusing this expression in unknown situations, we would likely want to spenda little more effort analyzing and encoding the logical expression structure.--- Should become:<h2>Examples</h2><p>Now that we have met some of the key players, it is time to see what we cando. Here are some examples and comments which illustrate the use of the basiclayout and token elements. Consider the expression x2 + 4x + 4 = 0. A basicMathML presentation encoding for this would be:</p><math> <mrow> <msup> <mi>x</mi> <mn>2</mn> </msup> <mo>+</mo> <mn>4</mn> <mi>x</mi> <mo>+</mo> <mn>4</mn> <mo>=</mo> <mn>0</mn> </mrow></math><p>This encoding will display as you would expect. However, if we were interestedin reusing this expression in unknown situations, we would likely want to spenda little more effort analyzing and encoding the logical expression structure.</p>>>># Span-level HTML<<<This is some stuff with a <span class="foo">spanned bit of text</span> init. And <del>this *should* be a bit of deleted text</del> which should bepreserved, and part of it emphasized.--- Should become:<p>This is some stuff with a <span class="foo">spanned bit of text</span> init. And <del>this <em>should</em> be a bit of deleted text</del> which should bepreserved, and part of it emphasized.</p>>>># Inline HTML (Case-sensitivity)<<<This is a regular paragraph.<TABLE> <TR> <TD>Foo</TD> </TR></TABLE>This is another regular paragraph.--- Should become:<p>This is a regular paragraph.</p><TABLE> <TR> <TD>Foo</TD> </TR></TABLE><p>This is another regular paragraph.</p>>>># Span-level HTML (Case-sensitivity)<<<This is some stuff with a <SPAN CLASS="foo">spanned bit of text</SPAN> init. And <DEL>this *should* be a bit of deleted text</DEL> which should bepreserved, and part of it emphasized.--- Should become:<p>This is some stuff with a <SPAN CLASS="foo">spanned bit of text</SPAN> init. And <DEL>this <em>should</em> be a bit of deleted text</DEL> which should bepreserved, and part of it emphasized.</p>>>>### [Code spans]# Single backtick<<<Making `code` work for you--- Should become:<p>Making <code>code</code> work for you</p>>>># Literal backtick with doubling<<<Making `` `code` `` work for you--- Should become:<p>Making <code>`code`</code> work for you</p>>>># Many repetitions<<<Making `````code````` work for you--- Should become:<p>Making <code>code</code> work for you</p>>>># Two in a row<<<This `thing` should be `two` spans.--- Should become:<p>This <code>thing</code> should be <code>two</code> spans.</p>>>># At the beginning of a newline<<<I should think that the`tar` command would be universal.--- Should become:<p>I should think that the<code>tar</code> command would be universal.</p>>>># Entity escaping<<<The left angle-bracket (`<`) can also be written as a decimal-encoded(`<`) or hex-encoded (`<`) entity.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?