Last active
April 19, 2016 22:55
-
-
Save amacgregor/6b42634b42716dca62ae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
""" | |
<?xml version="1.0" encoding="utf-8"?> | |
<feed xmlns="http://www.w3.org/2005/Atom"> | |
<title>Example Feed</title> | |
<subtitle>A subtitle.</subtitle> | |
<link href="http://example.org/feed/" rel="self" /> | |
<link href="http://example.org/" /> | |
<id>urn:uuid:60a76c80-d399-11d9-b91C-0003939e0af6</id> | |
<updated>2003-12-13T18:30:02Z</updated> | |
<entry> | |
<title>Item 1</title> | |
<link href="http://example.org/2003/12/13/atom03" /> | |
<link rel="alternate" type="text/html" href="http://example.org/2003/12/13/atom03.html"/> | |
<link rel="edit" href="http://example.org/2003/12/13/atom03/edit"/> | |
<id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id> | |
<updated>2003-12-13T18:30:02Z</updated> | |
<summary>Some text.</summary> | |
<content type="xhtml"> | |
<div xmlns="http://www.w3.org/1999/xhtml"> | |
<p>This is the entry content.</p> | |
</div> | |
</content> | |
<author> | |
<name>John Doe</name> | |
<email>[email protected]</email> | |
</author> | |
</entry> | |
<entry> | |
<title>Item 2</title> | |
<link href="http://example.org/2003/12/13/atom03" /> | |
<link rel="alternate" type="text/html" href="http://example.org/2003/12/13/atom03.html"/> | |
<link rel="edit" href="http://example.org/2003/12/13/atom03/edit"/> | |
<id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id> | |
<updated>2003-12-13T18:30:02Z</updated> | |
<summary>Some text.</summary> | |
<content type="xhtml"> | |
<div xmlns="http://www.w3.org/1999/xhtml"> | |
<p>This is the entry content.</p> | |
</div> | |
</content> | |
<author> | |
<name>John Doe</name> | |
<email>[email protected]</email> | |
</author> | |
</entry> | |
</feed> | |
""" | |
end | |
def sample_rss_xml do | |
""" | |
<?xml version="1.0" encoding="UTF-8" ?> | |
<rss version="2.0"> | |
<channel> | |
<title>Rss Feed Example</title> | |
<link>http://www.rssexample.com</link> | |
<description>Example description for an RSS feed</description> | |
<item> | |
<title>Item 1</title> | |
<link>http://www.w3schools.com/rss</link> | |
<description>New RSS tutorial on W3Schools</description> | |
<category>News1</category> | |
<category>PHP2</category> | |
</item> | |
<item> | |
<title>Item 2</title> | |
<link>http://www.w3schools.com/xml</link> | |
<description>New XML tutorial on W3Schools</description> | |
<category>News1</category> | |
<category>PHP2</category> | |
</item> | |
</channel> | |
</rss> | |
""" | |
end | |
def sample_xml do | |
""" | |
<rss> | |
<channel> | |
<title>Example Title</title> | |
<description>Custom Description</description> | |
<pubDate>Wed, 11 Jun 2014 18:00:41 +0200</pubDate> | |
<generator>PHP RSSGen 1.0.0 - Seld.be</generator> | |
<docs>http://www.rssboard.org/rss-2-0-10</docs> | |
<ttl>60</ttl> | |
<language>en</language> | |
<lastBuildDate>Tue, 27 May 2014 15:07:03 +0200</lastBuildDate> | |
<category>PHP</category> | |
<item> | |
<title>Item 1</title> | |
<description>Test 1</description> | |
<link>http://feeds.seld.be/~r/php-blog-seld/~3/JIWnIXuFQBU/upcoming-conferences-2013</link> | |
<pubDate>Tue, 19 Nov 2013 17:36:52 +0100</pubDate> | |
<guid isPermaLink="false">http://seld.be/notes/upcoming-conferences-2013</guid> | |
<category>News1</category> | |
<category>PHP2</category> | |
</item> | |
<item> | |
<title>Item 2</title> | |
<description>Test 2</description> | |
<link>http://feeds.seld.be/~r/php-blog-seld/~3/JIWnIXuFQBU/upcoming-conferences-2013</link> | |
<pubDate>Tue, 19 Nov 2013 17:36:52 +0100</pubDate> | |
<guid isPermaLink="false">http://seld.be/notes/upcoming-conferences-2013</guid> | |
<category>News</category> | |
<category>PHP</category> | |
</item> | |
</channel> | |
</rss> | |
""" | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment