Skip to content

Commit

Permalink
split groups of test samples into several files
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Apr 26, 2008
1 parent 320fc66 commit 2382acb
Show file tree
Hide file tree
Showing 8 changed files with 1,036 additions and 967 deletions.
8 changes: 4 additions & 4 deletions spec/markdown_spec.rb
Expand Up @@ -6,16 +6,16 @@
extend SampleLoader

it "should render HTML without trailing newline" do
BlueCloth.new('Foo').to_html.should == '<p>Foo</p>'
markdown('Foo').should == '<p>Foo</p>'
end

it "should not swallow trailing newline" do
pending
BlueCloth.new("Foo\n").to_html.should == "<p>Foo</p>\n"
markdown("Foo\n").should == "<p>Foo</p>\n"
end

describe 'sample:' do
load_samples('all') do |sample|
load_samples('all', 'code', 'titles', 'emphasis', 'links', 'lists') do |sample|
it(sample.comment) { sample.should render }
end

Expand Down Expand Up @@ -90,7 +90,7 @@ def matches?(sample)
end

def failure_message
"%s (line %d):\n<<<\n%s\n---\n%s\n>>>\n%s\n===" % [
"%s (line %d):\n<<< input:\n%s\n--- expected:\n%s\n>>> actual:\n%s\n===" % [
@sample.comment, @sample.line,
@sample.input,
@sample.output,
Expand Down
123 changes: 62 additions & 61 deletions spec/sample_loader.rb
@@ -1,77 +1,78 @@
module SampleLoader
protected
def load_samples(name, &block)
samples_file = File.dirname(__FILE__) + '/samples/' + name
raise ArgumentError, %[no samples file for "#{name}"] unless File.exists? samples_file
def load_samples(*names, &block)
@sections ||= Hash.new { |h, k| h[k] = [] }
loaded_samples = []

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

@sections = Hash.new { |h, k| h[k] = [] }
for name in names
samples_file = File.dirname(__FILE__) + '/samples/' + name
unless File.exists? samples_file
$stderr.puts %[WARNING: no samples file for "#{name}"]
next
end

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

begin
File.foreach(samples_file) do |line|
linenum += 1
begin
File.foreach(samples_file) do |line|
linenum += 1

# Start off in the meta section, which has sections and
# descriptions.
if meta_space
case line
# Left angles switch into data section for the current section
# and description.
when /^<<</
meta_space = false
unless sample
sample = Sample.new(nil, section_name, linenum)
# Start off in the meta section, which has sections and
# descriptions.
if meta_space
case line
# Left angles switch into data section for the current section
# and description.
when /^<<</
meta_space = false
unless sample
sample = Sample.new(nil, section_name, linenum)
end
# Section headings look like:
# ### [Code blocks]
when /^### \[([^\]]+)\]/
section_name = $1.chomp
section = @sections[section_name]
# Descriptions look like:
# # Para plus code block
when /^# (.*)/
description = $1.chomp

unless sample
sample = Sample.new(description, section_name, linenum)
else
sample.comment << $1.chomp
end
end
# Section headings look like:
# ### [Code blocks]
when /^### \[([^\]]+)\]/
section_name = $1.chomp
section = @sections[section_name]
# Descriptions look like:
# # Para plus code block
when /^# (.*)/
description = $1.chomp

unless sample
sample = Sample.new(description, section_name, linenum)
# 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 /^>>>/
section = @sections['[no name]'] unless section
section << sample
loaded_samples << sample
sample = nil
meta_space = true
# 3-Dashed divider with text divides input from output
when /^--- (.+)/
sample.end_input
# Anything else adds to either input or output
else
sample.comment << $1.chomp
sample << line
end
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 /^>>>/
unless section
section = @sections['[no name]']
end
section << sample
sample = nil
meta_space = true
# 3-Dashed divider with text divides input from output
when /^--- (.+)/
sample.end_input
# Anything else adds to either input or output
else
sample << line
end
end
rescue
$stderr.puts "error while processing line #{linenum}"
raise $!
end
rescue
$stderr.puts "error while processing line #{linenum}"
raise $!
end

if block_given?
@sections.values.each do |samples|
samples.each &block
end
end
loaded_samples.each &block if block_given?
end

class Sample
Expand Down

0 comments on commit 2382acb

Please sign in to comment.