diff --git a/src/resource.h b/src/resource.h index c0db8ceb..cab6b5cb 100644 --- a/src/resource.h +++ b/src/resource.h @@ -36,9 +36,12 @@ class MonitoredResource { bool operator==(const MonitoredResource& other) const { return other.type_ == type_ && other.labels_ == labels_; } + bool operator!=(const MonitoredResource& other) const { + return !(other == *this); + } bool operator<(const MonitoredResource& other) const { - return other.type_ < type_ - || (other.type_ == type_ && other.labels_ < labels_); + return type_ < other.type_ + || (type_ == other.type_ && labels_ < other.labels_); } json::value ToJSON() const; diff --git a/test/Makefile b/test/Makefile index 0ce40f18..f1150410 100644 --- a/test/Makefile +++ b/test/Makefile @@ -18,7 +18,7 @@ CPPFLAGS+= \ -isystem $(GTEST_DIR)/include -I$(GMOCK_DIR)/include \ -I$(YAML_CPP_DIR)/include CXXFLAGS=-std=c++11 -g -pthread -LDLIBS=-lpthread -lboost_program_options -lyaml-cpp +LDLIBS=-lpthread -lboost_program_options -lyaml-cpp -lyajl LDFLAGS=-L$(YAML_CPP_LIBDIR) # Where to find code under test. @@ -31,6 +31,7 @@ TESTS=\ base64_unittest \ configuration_unittest \ format_unittest \ + resource_unittest \ time_unittest GTEST_LIB=gtest_lib.a @@ -79,6 +80,8 @@ base64_unittest: $(GTEST_LIB) base64_unittest.o $(SRC_DIR)/base64.o $(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@ configuration_unittest: $(GTEST_LIB) $(YAML_CPP_LIBS) configuration_unittest.o $(SRC_DIR)/configuration.o $(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@ +resource_unittest: $(GTEST_LIB) resource_unittest.o $(SRC_DIR)/resource.o $(SRC_DIR)/json.o + $(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@ time_unittest: $(GTEST_LIB) time_unittest.o $(SRC_DIR)/time.o $(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@ diff --git a/test/resource_unittest.cc b/test/resource_unittest.cc new file mode 100644 index 00000000..6ae903ce --- /dev/null +++ b/test/resource_unittest.cc @@ -0,0 +1,53 @@ +#include "../src/resource.h" +#include "../src/json.h" +#include "gtest/gtest.h" + +namespace { + +TEST(ResourceTest, Type) { + google::MonitoredResource mr("some_resource", {}); + EXPECT_EQ("some_resource", mr.type()); +} + +TEST(ResourceTest, Labels) { + google::MonitoredResource mr("", {{"foo", "bar"}, {"bar", "baz"}}); + EXPECT_EQ(2, mr.labels().size()); + EXPECT_EQ("bar", mr.labels().at("foo")); + EXPECT_EQ("baz", mr.labels().at("bar")); +} + +TEST(ResourceTest, BasicEquality) { + google::MonitoredResource mr1("", {}); + google::MonitoredResource mr2("", {}); + + EXPECT_EQ(mr1, mr2); +} + +TEST(ResourceTest, BasicTypeComparison) { + google::MonitoredResource mr1("1", {}); + google::MonitoredResource mr2("2", {}); + + EXPECT_LT(mr1, mr2); +} + +TEST(ResourceTest, BasicLabelComparison) { + google::MonitoredResource mr1("", {{"a", "a"}}); + google::MonitoredResource mr2("", {{"b", "b"}}); + + EXPECT_NE(mr1, mr2); + EXPECT_LT(mr1, mr2); +} +TEST(ResourceTest, BasicJson) { + json::value target = json::object({ + {"type", json::string("test")}, + {"labels", json::object({ + {"a", json::string("a")}, + {"b", json::string("b")} + })} + }); + + google::MonitoredResource mr("test", {{"a", "a"}, {"b", "b"}}); + + EXPECT_EQ(target->ToString(), mr.ToJSON()->ToString()); +} +} // namespace