require Record
defmodule RssParserTest do
Record.defrecordp :xmlElement, Record.extract(:xmlElement, from_lib: "xmerl/include/xmerl.hrl")
Record.defrecordp :xmlText, Record.extract(:xmlText, from_lib: "xmerl/include/xmerl.hrl")
Record.defrecordp :xmlAttribute, Record.extract(:xmlAttribute, from_lib: "xmerl/include/xmerl.hrl")
use ExUnit.Case
def sample_atom_xml do
"""
Example FeedA subtitle.urn:uuid:60a76c80-d399-11d9-b91C-0003939e0af62003-12-13T18:30:02ZItem 1urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a2003-12-13T18:30:02ZSome text.
This is the entry content.
John Doejohndoe@example.comItem 2urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a2003-12-13T18:30:02ZSome text.
This is the entry content.
John Doejohndoe@example.com
"""
end
def sample_rss_xml do
"""
Rss Feed Example
http://www.rssexample.com
Example description for an RSS feedItem 1
http://www.w3schools.com/rss
New RSS tutorial on W3SchoolsNews1PHP2Item 2
http://www.w3schools.com/xml
New XML tutorial on W3SchoolsNews1PHP2
"""
end
def sample_xml do
"""
Example TitleCustom DescriptionWed, 11 Jun 2014 18:00:41 +0200PHP RSSGen 1.0.0 - Seld.behttp://www.rssboard.org/rss-2-0-1060enTue, 27 May 2014 15:07:03 +0200PHPItem 1Test 1
http://feeds.seld.be/~r/php-blog-seld/~3/JIWnIXuFQBU/upcoming-conferences-2013
Tue, 19 Nov 2013 17:36:52 +0100http://seld.be/notes/upcoming-conferences-2013News1PHP2Item 2Test 2
http://feeds.seld.be/~r/php-blog-seld/~3/JIWnIXuFQBU/upcoming-conferences-2013
Tue, 19 Nov 2013 17:36:52 +0100http://seld.be/notes/upcoming-conferences-2013NewsPHP
"""
end
# Rss tests
test "parsing the title out of a rss feed" do
{xml, _rest} = :xmerl_scan.string(bitstring_to_list(sample_rss_xml))
[title_element] = :xmerl_xpath.string('/rss/channel/title', xml)
[title_text] = title_element.content
title = title_text.value
assert title == 'Rss Feed Example'
end
test "parsing the description out of a rss feed" do
{xml, _rest} = :xmerl_scan.string(bitstring_to_list(sample_rss_xml))
[description_element] = :xmerl_xpath.string('/rss/channel/description',xml)
[description_text] = description_element.content
description = description_text.value
assert description == 'Example description for an RSS feed'
end
test "parsing the title out of the items in the feed" do
{xml, _rest} = :xmerl_scan.string(bitstring_to_list(sample_rss_xml))
items = :xmerl_xpath.string('/rss/channel/item/title/text()', xml)
item_titles = items |> Enum.map(fn(x) -> x.value end)
assert item_titles == ['Item 1', 'Item 2']
end
test "parsing the description out of the items in the feed" do
{xml, _rest} = :xmerl_scan.string(bitstring_to_list(sample_rss_xml))
items = :xmerl_xpath.string('/rss/channel/item/description/text()', xml)
item_descriptions = items |> Enum.map(fn(x) -> x.value end)
assert item_descriptions == ['New RSS tutorial on W3Schools', 'New XML tutorial on W3Schools']
end
test "parsing the categories out of the items in the feed" do
{xml, _rest} = :xmerl_scan.string(bitstring_to_list(sample_rss_xml))
items = :xmerl_xpath.string('/rss/channel/item/category/text()', xml)
item_categories = items |> Enum.map(fn(x) -> x.value end)
assert item_categories == ['Test 1', 'Test 2']
end
test "parsing the title out of the items information in the feed" do
{xml, _rest} = :xmerl_scan.string(bitstring_to_list(sample_rss_xml))
items = :xmerl_xpath.string('/rss/channel/item', xml)
item_list = items |> Enum.map(fn(x) ->
[title_element] = :xmerl_xpath.string('/item/title', x)
[title_text] = title_element.content
[link_element] = :xmerl_xpath.string('/item/link', x)
[link_text] = link_element.content
[description_element] = :xmerl_xpath.string('/item/description', x)
[description_text] = description_element.content
value = xmlText(description_element.content, :value)
IO.inspect(value)
{title_text.value, description_text.value, link_text.value}
end)
assert item_list == [
{'Item 1', 'New RSS tutorial on W3Schools', 'http://www.w3schools.com/rss'},
{'Item 2', 'New XML tutorial on W3Schools', 'http://www.w3schools.com/xml'}
]
end
# Test concepts
#test "parsing the title out of the feed" do
# {xml, _rest} = :xmerl_scan.string(bitstring_to_list(sample_xml))
# [title_element] = :xmerl_xpath.string('/rss/channel/title', xml)
# [title_text] = title_element.content
# title = title_text.value
# assert title == 'Example Title'
#end
#test "parsing the description out of the feed" do
# {xml, _rest} = :xmerl_scan.string(bitstring_to_list(sample_xml))
# [description_element] = :xmerl_xpath.string('/rss/channel/description',xml)
# [description_text] = description_element.content
# description = description_text.value
# assert description == 'Custom Description'
#end
#test "parsing the title out of the items in the feed (alternate)" do
# {xml, _rest} = :xmerl_scan.string(bitstring_to_list(sample_xml))
# items = :xmerl_xpath.string('/rss/channel/item', xml)
# item_titles = items |> Enum.map(fn(x) ->
# [title_element] = :xmerl_xpath.string('/item/title', x)
# [title_text] = title_element.content
# title_text.value
# end)
# assert item_titles == ['Item 1', 'Item 2']
#end
#test "parsing the title out of the items in the feed" do
# {xml, _rest} = :xmerl_scan.string(bitstring_to_list(sample_xml))
# items = :xmerl_xpath.string('/rss/channel/item/title/text()', xml)
# item_titles = items |> Enum.map(fn(x) -> x.value end)
# assert item_titles == ['Item 1', 'Item 2']
#end
#test "parsing the description out of the items in the feed" do
# {xml, _rest} = :xmerl_scan.string(bitstring_to_list(sample_xml))
# items = :xmerl_xpath.string('/rss/channel/item/description/text()', xml)
# item_descriptions = items |> Enum.map(fn(x) -> x.value end)
# assert item_descriptions = ['Test 1', 'Test 2']
#end
#test "parsing the categories out of the items in the feed" do
# {xml, _rest} = :xmerl_scan.string(bitstring_to_list(sample_xml))
# items = :xmerl_xpath.string('/rss/channel/item/category/text()', xml)
# item_categories = items |> Enum.map(fn(x) -> x.value end)
# assert item_categories = ['1', 'Test 2']
#end
end