Skip to content

Commit

Permalink
change sample loader to load named sample files
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Apr 9, 2008
1 parent bf4c098 commit 053df9f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
37 changes: 24 additions & 13 deletions spec/markdown_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@

include SampleLoader

before(:all) do
load_samples
end

it "should render HTML without trailing newline" do
BlueCloth.new('Foo').to_html.should == '<p>Foo</p>'
end
Expand All @@ -18,16 +14,28 @@
end

it "should render all the samples correctly" do
@sections.values.each do |samples|
samples.each do |sample|
sample.should render
end
end
load_samples('all')
render_samples
end

def render
SampleMatcher.new

it "should render all the failing samples" do
load_samples('failing')
render_samples
end

protected

def render
SampleMatcher.new
end

def render_samples
@sections.values.each do |samples|
samples.each do |sample|
sample.should render
end
end
end
end

class SampleMatcher
Expand All @@ -42,13 +50,16 @@ def matches?(sample)
end

def failure_message
<<-MSG
msg = <<-MSG
#{@sample.comment} (line #{@sample.line}):
<<<
#{@sample.input}---
#{@sample.output}>>>
#{@result}===
MSG
indent = ' ' * 4
msg.gsub!(/\n(\S)/, "\n#{indent}\\1")
indent + msg
end

def negative_failure_message
Expand Down
10 changes: 8 additions & 2 deletions spec/sample_loader.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
module SampleLoader
protected
def load_samples
def load_samples(name)
samples_file = File.dirname(__FILE__) + '/samples/' + name
raise ArgumentError, %[no samples file for "#{name}"] unless File.exists? samples_file

meta_space = true
section = section_name = sample = nil
linenum = 0

@sections = Hash.new { |h, k| h[k] = [] }

begin
File.foreach(File.dirname(__FILE__) + '/samples/all') do |line|
File.foreach(samples_file) do |line|
linenum += 1

# Start off in the meta section, which has sections and
Expand Down Expand Up @@ -44,6 +47,9 @@ def load_samples
# Right angles terminate a data section, at which point we
# should have enough data to add a test.
when /^>>>/
unless section
section = @sections['[no name]']
end
section << sample
sample = nil
meta_space = true
Expand Down

0 comments on commit 053df9f

Please sign in to comment.