Skip to content
This repository has been archived by the owner on Oct 10, 2019. It is now read-only.

Commit

Permalink
Fix erroneous comments in rss_feed::set_rssurl
Browse files Browse the repository at this point in the history
cppcheck pointed out that I overwrite variable's value right after
assigning it, and this proved to be true. However, while looking into
it, I found that the comments I left in the function don't match the
reality as the input that it gets are a bit different from the examples
in comments. It's sheer luck that the code that I got by making
a mistake turned out to be *right*.

So I removed superfluous code, amended the comments and added a few
tests to make sure I never step into that particular trap ever again.
  • Loading branch information
Minoru committed Nov 19, 2016
1 parent 20925d5 commit a954765
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
9 changes: 3 additions & 6 deletions src/rss.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -476,13 +476,10 @@ void rss_feed::set_rssurl(const std::string& u) {
if (rssurl_.substr(0,6) == "query:") {
/* Query string looks like this:
*
* "query:Title:unread = \"yes\" and age between 0:7" tag1 "tag two"
* query:Title:unread = "yes" and age between 0:7
*
* At this point, we're only interested in the first part enclosed in
* the quotes. Thus, we first tokenize using space as delimiter... */
std::vector<std::string> tokens = utils::tokenize_quoted(u, " ");
// and then further split by colon, so as to extract title and query
tokens = utils::tokenize_quoted(u, ":");
* So we split by colons to get title and the query itself. */
std::vector<std::string> tokens = utils::tokenize_quoted(u, ":");

if (tokens.size() < 3) {
throw _s("too few arguments");
Expand Down
23 changes: 23 additions & 0 deletions test/rss.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

#include <rsspp.h>
#include <rsspp_internal.h>
#include <rss.h>
#include <cache.h>
#include <configcontainer.h>

TEST_CASE("Throws exception if file doesn't exist", "[rsspp::parser]") {
rsspp::parser p;
Expand Down Expand Up @@ -227,3 +230,23 @@ TEST_CASE("W3C DTF to RFC 822 conversion behaves correctly with different "
}
tzset();
}

namespace newsbeuter {

TEST_CASE("set_rssurl checks if query feed has a valid query", "[rss]") {
configcontainer cfg;
cache rsscache(":memory:", &cfg);
rss_feed f(&rsscache);

SECTION("invalid query results in exception") {
REQUIRE_THROWS(f.set_rssurl("query:a title:unread ="));
REQUIRE_THROWS(f.set_rssurl("query:a title:between 1:3"));
}

SECTION("valid query doesn't throw an exception") {
REQUIRE_NOTHROW(f.set_rssurl("query:a title:unread = \"yes\""));
REQUIRE_NOTHROW(f.set_rssurl("query:Title:unread = \"yes\" and age between 0:7"));
}
}

}

0 comments on commit a954765

Please sign in to comment.