Skip to content

Commit

Permalink
testing cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Aeon committed Jul 6, 2010
1 parent 13aeb63 commit d07dcc0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 44 deletions.
12 changes: 6 additions & 6 deletions slashdot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def build_tree(buffer)
# get next tag, and its contents
match = $open_tag_rx.match(buffer)
if match
puts 'parsing ' + match[1]
# puts 'parsing ' + match[1]
tag[:tag] = match[1]
unless match[2].nil?
tag[:attributes] = parse_attributes(match[2])
Expand All @@ -26,19 +26,19 @@ def build_tree(buffer)
close_match = Regexp.new("</#{tag[:tag]}>").match(buffer)
if close_match
# close_match.pre_match.strip!
tag[:content] = get_branch(close_match.pre_match)
tag[:content] = build_tree(close_match.pre_match)
branch << tag
# close_match.post_match.strip!
buffer = close_match.post_match
else
debugger
# debugger
raise Exception.new("Parse error: No closing tag for #{tag[:tag]} found!")
end
else
# check for self-closing tags
match = $self_tag_rx.match(buffer)
if match
puts 'parsing ' + match[1]
# puts 'parsing ' + match[1]
tag[:tag] = match[1]
unless match[2].nil?
tag[:attributes] = parse_attributes(match[2])
Expand Down Expand Up @@ -89,5 +89,5 @@ def parse_file(filename)
return build_tree(buffer)
end

tree = parse_file('slashdot.rss')
puts tree.inspect
# tree = parse_file('slashdot.rss')
# puts tree.inspect
55 changes: 17 additions & 38 deletions test/parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,6 @@ class ParserTest < Test::Unit::TestCase
# def teardown
# end

def test_get_items
item_count = get_items('slashdot.rss').size
assert(item_count == 15, "Got #{item_count} items instead of 15")
end

def test_get_tag
str = '<item>Description</item>'
tag = get_next_tag(str)
assert(tag[:tag] == 'item', "tag name is #{tag[:tag]}")
assert(tag[:text] == 'Description', "tag text is incorrect #{tag[:text]}")
end

def test_parse_attributes
str = ' link="http://foobar.com" bar="baz"'
attributes = parse_attributes(str)
Expand All @@ -28,59 +16,50 @@ def test_parse_attributes
assert(attributes[:bar] == 'baz', "bar attribute is #{attributes[:bar]}")
end

def test_get_tag_with_attributes
str = '<item link="http://foobar.com">Description</item>'
tag = get_next_tag(str)
assert(tag[:tag] == 'item', "tag name is #{tag[:tag]}")
assert(!tag[:attributes].nil?, "tag attributes are missing")
assert(tag[:attributes][:link] == 'http://foobar.com', "tag link attribute is incorrect #{tag[:attributes][:link]}")
end

def test_get_branch
def test_parse_multiple_items
str = '<item>Description</item><item value="foo" link="bar://">Second</item>'
branch = get_branch(str)
branch = build_tree(str)
# puts branch.inspect
assert(branch.size == 2, "didn't get two items in branch")
assert(branch[0][:content] == 'Description', "tag text is incorrect #{branch[0][:content]}")
assert(branch[1][:content] == 'Second', "tag text is incorrect #{branch[1][:content]}")
assert(branch[0][:attributes].empty?, "tag attributes present #{branch[0][:attributes].inspect}")
assert(branch[0][:attributes].nil?, "tag attributes present #{branch[0][:attributes].inspect}")
assert(branch[1][:attributes] == {:value => 'foo', :link => 'bar://'}, "tag attributes incorrect")
end

def test_parse_self_closing_tag
str = '<item value="foo"/>'
branch = get_branch(str)
branch = build_tree(str)
assert(branch[0][:tag] == 'item', "tag name is #{branch[0][:tag]}")
assert(branch[0][:attributes][:value] == 'foo', "tag attribute is incorrect #{branch[0][:attributes][:value]}")
end

def test_get_branch_with_self_closing_tag
str = '<item>Description</item><item value="foo" link="bar://"/><item>Description</item>'
branch = get_branch(str)
branch = build_tree(str)
assert(branch.size == 3, "didn't get 3 items in branch")
assert(branch[0][:content] == 'Description', "tag text is incorrect #{branch[0][:content]}")
assert(branch[1][:content].nil?, "tag text is incorrect #{branch[1][:content]}")
assert(branch[0][:attributes].empty?, "tag attributes present #{branch[0][:attributes].inspect}")
assert(branch[0][:attributes].nil?, "tag attributes present #{branch[0][:attributes].inspect}")
assert(branch[1][:attributes] == {:value => 'foo', :link => 'bar://'}, "tag attributes incorrect")
end

def test_parse_nested_branch
str = '<bar><item>Description</item><item value="foo" link="bar://"/><item>Description</item></bar>'
branch = get_branch(str)
puts branch.inspect
branch = build_tree(str)
assert(branch.size == 1, "didn't get 1 items in branch")
assert(branch[0][:tag] == 'bar', "didn't get bar items as parent")
assert(branch[0][:content][0][:content] == 'Description', "tag text is incorrect #{branch[0][:content][0][:content]}")
assert(branch[0][:content][1][:content].nil?, "tag text is incorrect #{branch[0][:content][1][:content]}")
assert(branch[0][:content][0][:attributes].empty?, "tag attributes present #{branch[0][:content][0][:attributes].inspect}")
assert(branch[0][:content][0][:attributes].nil?, "tag attributes present #{branch[0][:content][0][:attributes].inspect}")
assert(branch[0][:content][1][:attributes] == {:value => 'foo', :link => 'bar://'}, "tag attributes incorrect")
end

# def test_parse_simple_tree
# str = '<item>Description</item>'
# tree = parse_tree(str, {})
# assert(tree[:tag] == 'item', 'tag name is incorrect')
# assert(tree[:text] == 'Description', 'tag text is incorrect')
# assert(tree[:children].empty?, 'tag children are not empty')
# assert(tree[:attributes].empty?, 'tag attributes are not empty')
# end


def test_parse_missing_closing_tag
str = '<bar><item>Description<item value="foo" link="bar://"/><item>Description</item></bar>'
assert_raise Exception do
branch = build_tree(str)
end
end
end

0 comments on commit d07dcc0

Please sign in to comment.