tests are cool

parslet
Jordan Orelli 9 years ago
parent bb0084e198
commit fcf1ef95ea

@ -15,11 +15,11 @@ module Bristlecode
rule(:bold_open) { str('[b]') | str('[B]') }
rule(:bold_close) { str('[/b]') | str('[/B]') | eof }
rule(:bold) { bold_open >> children.as(:bold) >> bold_close }
rule(:bold) { bold_open >> children.as(:bold) >> bold_close >> space? }
rule(:italic_open) { str('[i]') | str('[I]') }
rule(:italic_close) { str('[/i]') | str('[/I]') | eof }
rule(:italic) { italic_open >> children.as(:italic) >> italic_close }
rule(:italic) { italic_open >> children.as(:italic) >> italic_close >> space? }
rule(:eof) { any.absent? }
rule(:tag) { bold | italic }
@ -29,8 +29,8 @@ module Bristlecode
rule(:tag_delim) { tag_open | tag_close }
rule(:text) { (tag_delim.absent? >> any).repeat(1).as(:text) }
rule(:children) { space? >> elem.repeat }
rule(:doc) { space? >> elem.repeat.as(:doc) }
rule(:children) { space? >> elem.repeat >> space? }
rule(:doc) { space? >> elem.repeat.as(:doc) >> space? }
root(:doc)
end
@ -55,7 +55,7 @@ module Bristlecode
class Text
def initialize(text)
@text = text
@text = text.to_str.strip
end
def to_html

@ -2,6 +2,51 @@ require 'parslet/rig/rspec'
require_relative '../../bristlecode.rb'
module Bristlecode
describe '.to_html' do
def to_html(text)
Bristlecode.to_html(text)
end
it 'leaves an empty string unchanged' do
expect(to_html("")).to eq("")
end
it 'handles empty documents' do
expect(to_html(" \t \n \n \t")).to eq("")
end
it 'handles plain text just fine' do
expect(to_html("plaintext")).to eq("plaintext")
end
it 'can bold stuff' do
expect(to_html("[b]bold[/b]")).to eq("<b>bold</b>")
end
it 'can italic stuff' do
expect(to_html("[i]italic[/i]")).to eq("<i>italic</i>")
end
it 'can nest tags' do
doc = '[b] bold [i] italic [/i] bold [/b]'
expected = '<b>bold<i>italic</i>bold</b>'
out = to_html(doc)
expect(out).to eq(expected)
doc = '[i] italic [b] bold [/b] italic [/i]'
expected = '<i>italic<b>bold</b>italic</i>'
out = to_html(doc)
expect(out).to eq(expected)
end
it 'auto-closes tags at eof' do
expect(to_html("[b]bold")).to eq("<b>bold</b>")
expect(to_html("[i]italic")).to eq("<i>italic</i>")
end
end
describe Parser do
let(:parser) { Parser.new }

Loading…
Cancel
Save