Skip to content

Commit

Permalink
Know what a method raises. Spec conformance!
Browse files Browse the repository at this point in the history
  • Loading branch information
defunkt committed Apr 13, 2010
1 parent 7c6d862 commit e2f2f3b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/tomdoc/source_parser.rb
Expand Up @@ -14,7 +14,7 @@ def self.parse(text)

# Each instance of SourceParser accumulates scopes with each
# parse, making it easy to parse an entire project in chunks but
# more difficult to parse disperate files in one go. Create
# more difficult to parse disparate files in one go. Create
# separate instances for separate global scopes.
#
# Returns an instance of TomDoc::SourceParser
Expand Down
22 changes: 21 additions & 1 deletion lib/tomdoc/tomdoc.rb
Expand Up @@ -84,7 +84,7 @@ def args
end

def examples
if tomdoc =~ /(\s*Examples\s*(.+?)\s*Returns)/m
if tomdoc =~ /(\s*Examples\s*(.+?)\s*(?:Returns|Raises))/m
$2.split("\n\n")
else
[]
Expand All @@ -96,6 +96,26 @@ def returns
lines = $1.split("\n")
statements = []

lines.each do |line|
next if line =~ /^\s*Raises/
if line =~ /^\s+/
statements.last << line.squeeze(' ')
else
statements << line
end
end

statements
else
[]
end
end

def raises
if tomdoc =~ /^\s*(Raises.+)/m
lines = $1.split("\n")
statements = []

lines.each do |line|
if line =~ /^\s+/
statements.last << line.squeeze(' ')
Expand Down
6 changes: 6 additions & 0 deletions test/tomdoc_parser_test.rb
Expand Up @@ -25,6 +25,8 @@ def setup
# Returns the atomic mass of the element as a Float. The value is in
# unified atomic mass units.
# Returns nil when the count is < 1.
# Raises ExpectedString if the first argument is not a String.
# Raises ExpectedInteger if the second argument is not an Integer.
comment

@comment2 = TomDoc::TomDoc.new(<<comment2)
Expand Down Expand Up @@ -102,6 +104,10 @@ def setup
assert_equal 3, @comment.returns.size
end

test "knows if the method raises anything" do
assert_equal 2, @comment.raises.size
end

test "knows each return example" do
assert_equal "Returns the duplicated String when the count is > 1.",
@comment.returns.first.to_s
Expand Down

0 comments on commit e2f2f3b

Please sign in to comment.