Commit a6016198 authored by aboxhall@chromium.org's avatar aboxhall@chromium.org

Add stream operators for URLPattern and URLPatternSet and unit test for URLPatternSet

BUG=

Review URL: https://codereview.chromium.org/300573002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@282150 0039d316-1c4b-4281-b951-d872f2087c98
parent 69a3301c
......@@ -4,6 +4,8 @@
#include "extensions/common/url_pattern.h"
#include <ostream>
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_piece.h"
#include "base/strings/string_split.h"
......@@ -159,6 +161,10 @@ bool URLPattern::operator==(const URLPattern& other) const {
return GetAsString() == other.GetAsString();
}
std::ostream& operator<<(std::ostream& out, const URLPattern& url_pattern) {
return out << '"' << url_pattern.GetAsString() << '"';
}
URLPattern::ParseResult URLPattern::Parse(const std::string& pattern) {
spec_.clear();
SetMatchAllURLs(false);
......
......@@ -5,6 +5,7 @@
#define EXTENSIONS_COMMON_URL_PATTERN_H_
#include <functional>
#include <iosfwd>
#include <string>
#include <vector>
......@@ -248,6 +249,8 @@ class URLPattern {
mutable std::string spec_;
};
std::ostream& operator<<(std::ostream& out, const URLPattern& url_pattern);
typedef std::vector<URLPattern> URLPatternList;
#endif // EXTENSIONS_COMMON_URL_PATTERN_H_
......@@ -5,6 +5,7 @@
#include "extensions/common/url_pattern_set.h"
#include <iterator>
#include <ostream>
#include "base/logging.h"
#include "base/memory/linked_ptr.h"
......@@ -99,6 +100,27 @@ bool URLPatternSet::operator==(const URLPatternSet& other) const {
return patterns_ == other.patterns_;
}
std::ostream& operator<<(std::ostream& out,
const URLPatternSet& url_pattern_set) {
out << "{ ";
std::set<URLPattern>::const_iterator iter =
url_pattern_set.patterns().begin();
if (!url_pattern_set.patterns().empty()) {
out << *iter;
++iter;
}
for (;iter != url_pattern_set.patterns().end(); ++iter)
out << ", " << *iter;
if (!url_pattern_set.patterns().empty())
out << " ";
out << "}";
return out;
}
bool URLPatternSet::is_empty() const {
return patterns_.empty();
}
......
......@@ -5,6 +5,7 @@
#ifndef EXTENSIONS_COMMON_URL_PATTERN_SET_H_
#define EXTENSIONS_COMMON_URL_PATTERN_SET_H_
#include <iosfwd>
#include <set>
#include "base/memory/scoped_ptr.h"
......@@ -100,6 +101,9 @@ class URLPatternSet {
std::set<URLPattern> patterns_;
};
std::ostream& operator<<(std::ostream& out,
const URLPatternSet& url_pattern_set);
} // namespace extensions
#endif // EXTENSIONS_COMMON_URL_PATTERN_SET_H_
......@@ -4,6 +4,8 @@
#include "extensions/common/url_pattern_set.h"
#include <sstream>
#include "base/values.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
......@@ -60,6 +62,34 @@ TEST(URLPatternSetTest, Two) {
EXPECT_FALSE(set.MatchesURL(GURL("https://www.apple.com/monkey")));
}
TEST(URLPatternSetTest, StreamOperatorEmpty) {
URLPatternSet set;
std::ostringstream stream;
stream << set;
EXPECT_EQ("{ }", stream.str());
}
TEST(URLPatternSetTest, StreamOperatorOne) {
URLPatternSet set;
AddPattern(&set, "http://www.google.com/*");
std::ostringstream stream;
stream << set;
EXPECT_EQ("{ \"http://www.google.com/*\" }", stream.str());
}
TEST(URLPatternSetTest, StreamOperatorTwo) {
URLPatternSet set;
AddPattern(&set, "http://www.google.com/*");
AddPattern(&set, "http://www.yahoo.com/*");
std::ostringstream stream;
stream << set;
EXPECT_EQ("{ \"http://www.google.com/*\", \"http://www.yahoo.com/*\" }",
stream.str());
}
TEST(URLPatternSetTest, OverlapsWith) {
URLPatternSet set1;
AddPattern(&set1, "http://www.google.com/f*");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment