parser_test.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package opml
  5. import (
  6. "bytes"
  7. "testing"
  8. )
  9. func TestParseOpmlWithoutCategories(t *testing.T) {
  10. data := `<?xml version="1.0" encoding="ISO-8859-1"?>
  11. <opml version="2.0">
  12. <head>
  13. <title>mySubscriptions.opml</title>
  14. </head>
  15. <body>
  16. <outline text="CNET News.com" description="Tech news and business reports by CNET News.com. Focused on information technology, core topics include computers, hardware, software, networking, and Internet media." htmlUrl="http://news.com.com/" language="unknown" title="CNET News.com" type="rss" version="RSS2" xmlUrl="http://news.com.com/2547-1_3-0-5.xml"/>
  17. <outline text="washingtonpost.com - Politics" description="Politics" htmlUrl="http://www.washingtonpost.com/wp-dyn/politics?nav=rss_politics" language="unknown" title="washingtonpost.com - Politics" type="rss" version="RSS2" xmlUrl="http://www.washingtonpost.com/wp-srv/politics/rssheadlines.xml"/>
  18. <outline text="Scobleizer: Microsoft Geek Blogger" description="Robert Scoble's look at geek and Microsoft life." htmlUrl="http://radio.weblogs.com/0001011/" language="unknown" title="Scobleizer: Microsoft Geek Blogger" type="rss" version="RSS2" xmlUrl="http://radio.weblogs.com/0001011/rss.xml"/>
  19. <outline text="Yahoo! News: Technology" description="Technology" htmlUrl="http://news.yahoo.com/news?tmpl=index&amp;cid=738" language="unknown" title="Yahoo! News: Technology" type="rss" version="RSS2" xmlUrl="http://rss.news.yahoo.com/rss/tech"/>
  20. <outline text="Workbench" description="Programming and publishing news and comment" htmlUrl="http://www.cadenhead.org/workbench/" language="unknown" title="Workbench" type="rss" version="RSS2" xmlUrl="http://www.cadenhead.org/workbench/rss.xml"/>
  21. <outline text="Christian Science Monitor | Top Stories" description="Read the front page stories of csmonitor.com." htmlUrl="http://csmonitor.com" language="unknown" title="Christian Science Monitor | Top Stories" type="rss" version="RSS" xmlUrl="http://www.csmonitor.com/rss/top.rss"/>
  22. <outline text="Dictionary.com Word of the Day" description="A new word is presented every day with its definition and example sentences from actual published works." htmlUrl="http://dictionary.reference.com/wordoftheday/" language="unknown" title="Dictionary.com Word of the Day" type="rss" version="RSS" xmlUrl="http://www.dictionary.com/wordoftheday/wotd.rss"/>
  23. <outline text="The Motley Fool" description="To Educate, Amuse, and Enrich" htmlUrl="http://www.fool.com" language="unknown" title="The Motley Fool" type="rss" version="RSS" xmlUrl="http://www.fool.com/xml/foolnews_rss091.xml"/>
  24. <outline text="InfoWorld: Top News" description="The latest on Top News from InfoWorld" htmlUrl="http://www.infoworld.com/news/index.html" language="unknown" title="InfoWorld: Top News" type="rss" version="RSS2" xmlUrl="http://www.infoworld.com/rss/news.xml"/>
  25. <outline text="NYT &gt; Business" description="Find breaking news &amp; business news on Wall Street, media &amp; advertising, international business, banking, interest rates, the stock market, currencies &amp; funds." htmlUrl="http://www.nytimes.com/pages/business/index.html?partner=rssnyt" language="unknown" title="NYT &gt; Business" type="rss" version="RSS2" xmlUrl="http://www.nytimes.com/services/xml/rss/nyt/Business.xml"/>
  26. <outline text="NYT &gt; Technology" description="" htmlUrl="http://www.nytimes.com/pages/technology/index.html?partner=rssnyt" language="unknown" title="NYT &gt; Technology" type="rss" version="RSS2" xmlUrl="http://www.nytimes.com/services/xml/rss/nyt/Technology.xml"/>
  27. <outline text="Scripting News" description="It's even worse than it appears." htmlUrl="http://www.scripting.com/" language="unknown" title="Scripting News" type="rss" version="RSS2" xmlUrl="http://www.scripting.com/rss.xml"/>
  28. <outline text="Wired News" description="Technology, and the way we do business, is changing the world we know. Wired News is a technology - and business-oriented news service feeding an intelligent, discerning audience. What role does technology play in the day-to-day living of your life? Wired News tells you. How has evolving technology changed the face of the international business world? Wired News puts you in the picture." htmlUrl="http://www.wired.com/" language="unknown" title="Wired News" type="rss" version="RSS" xmlUrl="http://www.wired.com/news_drop/netcenter/netcenter.rdf"/>
  29. </body>
  30. </opml>
  31. `
  32. var expected SubcriptionList
  33. expected = append(expected, &Subcription{Title: "CNET News.com", FeedURL: "http://news.com.com/2547-1_3-0-5.xml", SiteURL: "http://news.com.com/"})
  34. subscriptions, err := Parse(bytes.NewBufferString(data))
  35. if err != nil {
  36. t.Error(err)
  37. }
  38. if len(subscriptions) != 13 {
  39. t.Errorf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 13)
  40. }
  41. if !subscriptions[0].Equals(expected[0]) {
  42. t.Errorf(`Subscription are different: "%v" vs "%v"`, subscriptions[0], expected[0])
  43. }
  44. }
  45. func TestParseOpmlWithCategories(t *testing.T) {
  46. data := `<?xml version="1.0" encoding="utf-8"?>
  47. <opml version="2.0">
  48. <head>
  49. <title>mySubscriptions.opml</title>
  50. </head>
  51. <body>
  52. <outline text="My Category 1">
  53. <outline text="Feed 1" xmlUrl="http://example.org/feed1/" htmlUrl="http://example.org/1"/>
  54. <outline text="Feed 2" xmlUrl="http://example.org/feed2/" htmlUrl="http://example.org/2"/>
  55. </outline>
  56. <outline text="My Category 2">
  57. <outline text="Feed 3" xmlUrl="http://example.org/feed3/" htmlUrl="http://example.org/3"/>
  58. </outline>
  59. </body>
  60. </opml>
  61. `
  62. var expected SubcriptionList
  63. expected = append(expected, &Subcription{Title: "Feed 1", FeedURL: "http://example.org/feed1/", SiteURL: "http://example.org/1", CategoryName: "My Category 1"})
  64. expected = append(expected, &Subcription{Title: "Feed 2", FeedURL: "http://example.org/feed2/", SiteURL: "http://example.org/2", CategoryName: "My Category 1"})
  65. expected = append(expected, &Subcription{Title: "Feed 3", FeedURL: "http://example.org/feed3/", SiteURL: "http://example.org/3", CategoryName: "My Category 2"})
  66. subscriptions, err := Parse(bytes.NewBufferString(data))
  67. if err != nil {
  68. t.Error(err)
  69. }
  70. if len(subscriptions) != 3 {
  71. t.Errorf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 3)
  72. }
  73. for i := 0; i < len(subscriptions); i++ {
  74. if !subscriptions[i].Equals(expected[i]) {
  75. t.Errorf(`Subscription are different: "%v" vs "%v"`, subscriptions[i], expected[i])
  76. }
  77. }
  78. }
  79. func TestParseOpmlWithEmptyTitleAndEmptySiteURL(t *testing.T) {
  80. data := `<?xml version="1.0" encoding="ISO-8859-1"?>
  81. <opml version="2.0">
  82. <head>
  83. <title>mySubscriptions.opml</title>
  84. </head>
  85. <body>
  86. <outline xmlUrl="http://example.org/feed1/" htmlUrl="http://example.org/1"/>
  87. <outline xmlUrl="http://example.org/feed2/"/>
  88. </body>
  89. </opml>
  90. `
  91. var expected SubcriptionList
  92. expected = append(expected, &Subcription{Title: "http://example.org/1", FeedURL: "http://example.org/feed1/", SiteURL: "http://example.org/1", CategoryName: ""})
  93. expected = append(expected, &Subcription{Title: "http://example.org/feed2/", FeedURL: "http://example.org/feed2/", SiteURL: "http://example.org/feed2/", CategoryName: ""})
  94. subscriptions, err := Parse(bytes.NewBufferString(data))
  95. if err != nil {
  96. t.Error(err)
  97. }
  98. if len(subscriptions) != 2 {
  99. t.Errorf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 2)
  100. }
  101. for i := 0; i < len(subscriptions); i++ {
  102. if !subscriptions[i].Equals(expected[i]) {
  103. t.Errorf(`Subscription are different: "%v" vs "%v"`, subscriptions[i], expected[i])
  104. }
  105. }
  106. }
  107. func TestParseInvalidXML(t *testing.T) {
  108. data := `garbage`
  109. _, err := Parse(bytes.NewBufferString(data))
  110. if err == nil {
  111. t.Error("Parse should generate an error")
  112. }
  113. }