Skip to content
This repository was archived by the owner on Aug 19, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -31,6 +31,7 @@ TESTS=\
base64_unittest \
configuration_unittest \
format_unittest \
resource_unittest \
time_unittest

GTEST_LIB=gtest_lib.a
Expand Down Expand Up @@ -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 $@

Expand Down
53 changes: 53 additions & 0 deletions test/resource_unittest.cc
Original file line number Diff line number Diff line change
@@ -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