From 01d296acb19c2f00d1868c2c74c2a3fd3a80fb53 Mon Sep 17 00:00:00 2001 From: John Conway Date: Wed, 8 May 2019 15:57:35 +0100 Subject: [Atom] Writing tests, fixing bugs --- src/feednim/atom.nim | 66 +++++++++++++++++++++++++++++---------------- tests/__test1.nim | 12 +++++++++ tests/test1.nim | 12 --------- tests/test_atom | Bin 0 -> 409560 bytes tests/test_atom.nim | 15 ++++++++--- tests/test_atom.xml | 74 ++++++++++++++++++++++++++++----------------------- 6 files changed, 106 insertions(+), 73 deletions(-) create mode 100644 tests/__test1.nim delete mode 100644 tests/test1.nim create mode 100755 tests/test_atom diff --git a/src/feednim/atom.nim b/src/feednim/atom.nim index 3699e20..9fce652 100644 --- a/src/feednim/atom.nim +++ b/src/feednim/atom.nim @@ -11,6 +11,8 @@ import xmltree import streams import sugar +import marshal + type Atom* = object author: AtomAuthor # Sugar, not in Atom spec. Returns the first author. @@ -18,7 +20,7 @@ type title*: string # Required Atom field updated*: string # Required Atom field authors*: seq[AtomAuthor] # Pleuralised because the Atom spec allows more than one - categories*: seq[string] + categories*: seq[AtomCategory] contributors*: seq[AtomAuthor] generator*: string icon*: string @@ -26,13 +28,18 @@ type logo*: string rights*: string subtitle*: string - entrys*: seq[AtomEntry] + entries*: seq[AtomEntry] AtomAuthor* = object name*: string # Required Atom field uri*: string email*: string + AtomCategory* = object + term*: string + label*: string + scheme*: string + AtomLink* = object href*: string rel*: string @@ -47,7 +54,7 @@ type updated*: string # Required Atom field author: AtomAuthor # Sugar, not in Atom spec. Returns the first author. authors*: seq[AtomAuthor] # Pleuralised because the Atom spec allows more than one - categories*: seq[string] + categories*: seq[AtomCategory] content*: string contentSrc*: string contentType*: string @@ -61,7 +68,7 @@ type AtomSource* = object author: AtomAuthor # Sugar, not in Atom spec. Returns the first author. authors: seq[AtomAuthor] - categories*: seq[string] + categories*: seq[AtomCategory] contributors*: seq[AtomAuthor] generator*: string icon*: string @@ -82,19 +89,32 @@ proc parseAuthors ( node: XmlNode, mode="author") : seq[AtomAuthor] = if athr_node.child("uri") != nil: author.uri = athr_node.child("uri").innerText if athr_node.child("email") != nil: author.email = athr_node.child("email").innerText authors.add(author) - + if authors.len == 0: return @[] return authors +proc parseCategories ( node: XmlNode ) : seq[AtomCategory] = + var categories:seq[AtomCategory] + if node.child("category") != nil: + for cat_node in node.findAll("category"): + var category: AtomCategory = AtomCategory() + if cat_node.attr("term") != "": category.term = cat_node.attr("term") + if cat_node.attr("label") != "": category.label = cat_node.attr("label") + if cat_node.attr("scheme") != "": category.scheme = cat_node.attr("scheme") + + categories.add(category) + + if categories.len == 0: return @[] + return categories + proc parseLink ( node: XmlNode ): AtomLink = var link: AtomLink = AtomLink() if node.attrs != nil: - if node.attr("href") != "": link.href = node.attr("href") + if node.attr("href") != "": link.href = node.attr("rel") if node.attr("rel") != "": link.rel = node.attr("rel") if node.attr("type") != "": link.linktype = node.attr("type") - if node.attr("hreflang") != "": link.rel = node.attr("hreflang") - if node.attr("title") != "": link.rel = node.attr("title") - if node.attr("length") != "": link.rel = node.attr("length") - + if node.attr("hreflang") != "": link.hreflang = node.attr("hreflang") + if node.attr("title") != "": link.title = node.attr("title") + if node.attr("length") != "": link.length = node.attr("length") return link proc parseEntry( node: XmlNode) : AtomEntry = @@ -108,8 +128,7 @@ proc parseEntry( node: XmlNode) : AtomEntry = # Fill the optinal fields entry.authors = node.parseAuthors() - if node.child("category") != nil: - entry.categories = map(node.findAll("category"), (x: XmlNode) -> string => x.innerText) + if node.child("category") != nil: entry.categories = node.parseCategories() if node.child("content") != nil: entry.content = node.child("content").innerText @@ -128,19 +147,21 @@ proc parseEntry( node: XmlNode) : AtomEntry = if node.child("source") != nil: let source = node.child("source") - if node.child("author") != nil: entry.source.authors = source.parseAuthors() - if node.child("category") != nil: entry.source.categories = map(source.findAll("category"), (x: XmlNode) -> string => x.innerText) - if node.child("contributor") != nil: entry.source.contributors = source.parseAuthors(mode="contributor") + if source.child("author") != nil: entry.source.authors = source.parseAuthors() + if source.child("category") != nil: entry.source.categories = source.parseCategories() + if source.child("contributor") != nil: entry.source.contributors = source.parseAuthors(mode="contributor") if source.child("generator") != nil: entry.source.generator = source.child("generator").innerText - if source.child("icon") != nil: entry.source.generator = source.child("icon").innerText + if source.child("icon") != nil: entry.source.icon = source.child("icon").innerText if source.child("id") != nil: entry.source.id = source.child("id").innerText - if source.child("link") != nil: entry.source.link = parseLink( source ) + if source.child("link") != nil: entry.source.link = source.child("link").parseLink() if source.child("logo") != nil: entry.source.logo = source.child("logo").innerText if source.child("rights") != nil: entry.source.rights = source.child("rights").innerText if source.child("subtitle") != nil: entry.source.subtitle = source.child("subtitle").innerText if source.child("title") != nil: entry.source.title = source.child("title").innerText if source.child("updated") != nil: entry.source.updated = source.child("updated").innerText + entry.source.author = entry.source.authors[0] + if node.child("summary") != nil: entry.summary = node.child("summary").innerText # SUGAR an easy way to access an author @@ -168,8 +189,7 @@ proc parseAtom*(data: string): Atom = # Fill in the optional fields if node.child("author") != nil: atom.authors = node.parseAuthors() - if node.child("category") != nil: - atom.categories = map(node.findAll("category"), (x: XmlNode) -> string => x.innerText) + if node.child("category") != nil: atom.categories = node.parseCategories() if node.child("contributor") != nil: atom.contributors = node.parseAuthors(mode="contributor") @@ -190,14 +210,14 @@ proc parseAtom*(data: string): Atom = else: atom.author = AtomAuthor() - # If there are no entrys: + # If there are no entries: if node.child("entry") == nil: - atom.entrys = @[] + atom.entries = @[] return atom - # Otherwise, add the entrys. + # Otherwise, add the entries. if node.child("entry") != nil: - atom.entrys = map( node.findAll("entry"), parseEntry ) + atom.entries = map( node.findAll("entry"), parseEntry ) # Return the Atom data. return atom diff --git a/tests/__test1.nim b/tests/__test1.nim new file mode 100644 index 0000000..185df0e --- /dev/null +++ b/tests/__test1.nim @@ -0,0 +1,12 @@ +# This is just an example to get you started. You may wish to put all of your +# tests into a single file, or separate them into multiple `test1`, `test2` +# etc. files (better names are recommended, just make sure the name starts with +# the letter 't'). +# +# To run these tests, simply execute `nimble test`. + +import unittest + +import feednim +test "can add": + check add(5, 5) == 10 diff --git a/tests/test1.nim b/tests/test1.nim deleted file mode 100644 index 0ff07eb..0000000 --- a/tests/test1.nim +++ /dev/null @@ -1,12 +0,0 @@ -# This is just an example to get you started. You may wish to put all of your -# tests into a single file, or separate them into multiple `test1`, `test2` -# etc. files (better names are recommended, just make sure the name starts with -# the letter 't'). -# -# To run these tests, simply execute `nimble test`. - -import unittest - -import FeedNim -test "can add": - check add(5, 5) == 10 diff --git a/tests/test_atom b/tests/test_atom new file mode 100755 index 0000000..991e0e6 Binary files /dev/null and b/tests/test_atom differ diff --git a/tests/test_atom.nim b/tests/test_atom.nim index 3d563da..c4c65f9 100644 --- a/tests/test_atom.nim +++ b/tests/test_atom.nim @@ -5,12 +5,19 @@ # # To run these tests, simply execute `nimble test`. +import unittest +import marshal +import feednim +test "Read Valid Atom Feed": + let feed = "./tests/test_atom.xml".loadAtom() -import unittest + echo $$feed -import FeedNim -test "can add": - check add(5, 5) == 10 + check feed.title != "" + check feed.generator != "" + check feed.authors[0].name == "Joe Bloggs" + check feed.authors[0].uri == "http://joe.bloggs" + check feed.authors[0].email == "mail@joe.bloggs" \ No newline at end of file diff --git a/tests/test_atom.xml b/tests/test_atom.xml index 64f443d..5ea44a2 100644 --- a/tests/test_atom.xml +++ b/tests/test_atom.xml @@ -3,30 +3,37 @@ urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6 Bloggs's Planes Trains and Automobiles 2003-12-13T18:30:02Z + Joe Bloggs http://joe.bloggs mail@joe.bloggs + Jane Bloggs - Planes - Trains - Automobiles - nim + + + + + + Jester + http://joe.bloggs/mug,jpg + - © Joe and Jane Bloggs - © Joe and Jane Bloggs - + + http://joe.bloggs/logo.jpeg + Copyright Joe and Jane Bloggs + About Trains, Planes, and Autonmobiles. urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a @@ -34,38 +41,29 @@ 2003-12-13T18:30:02Z Joe Bloggs - http://joe.bloggs + http://joe.bloggs mail@joe.bloggs - planes - - Aero- not air-, fools! + + +
+

Aero- not air-, fools!

+
- 2003-12-13T18:30:02Z - © Joe Bloggs + Copyright Joe Bloggs - urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a + urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6d Aeroplane Aeroplanes 1755-04-15T18:30:00Z Samuel Johnson - http://dictionary.com + http://dictionary.com sjohnson@dictionary.com - words - © Samual Johnson - - - + + Copyright Samual Johnson
@@ -76,10 +74,18 @@ Jane Bloggs - trains + + 2003-12-13T18:20:02Z - © Jane Bloggs + Copyright Jane Bloggs Trains! -- cgit v1.2.3