Skip to content
This repository was archived by the owner on Aug 19, 2019. It is now read-only.

Add the first unit test#53

Merged
igorpeshansky merged 8 commits intoStackdriver:masterfrom
supriyagarg:master
Mar 2, 2018
Merged

Add the first unit test#53
igorpeshansky merged 8 commits intoStackdriver:masterfrom
supriyagarg:master

Conversation

@supriyagarg
Copy link
Copy Markdown
Contributor

This PR sets up the googletest framework, and adds a unit test for format.cc

Copy link
Copy Markdown
Contributor

@bmoyles0117 bmoyles0117 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great overall, excited to see tests! One minor comment.

Comment thread src/Makefile Outdated
$(CPP_NETLIB_DIR) $(YAML_CPP_DIR)
git submodule deinit --all
$(RM) -r init-submodules build-cpp-netlib build-yaml-cpp
git submodule deinit -f $(CPP_NETLIB_DIR) && \
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these commands need to be chained together? If so, can we indent the second line? Same elsewhere.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I edited the existing Makefile, so have kept the same format.

I have updated the indents in the new Makefile though. PTAL.

@bmoyles0117
Copy link
Copy Markdown
Contributor

Just providing some context around an offline conversation. Igor confirmed the issues with the Makefile on xenial so he is going to work on fixing up the Makefile issues in a separate pull request, and then we'll rebase your changes so that they can focus on the unit test functionality.

Update the Makefile to load specific submodules.
Also add a Makefile that is easy to extend as we add tests for the
remaining code. Only the SRCS needs to be extended in most cases.
Copy link
Copy Markdown
Contributor

@bmoyles0117 bmoyles0117 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Copy Markdown
Contributor

@igorpeshansky igorpeshansky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A preliminary review.

Comment thread test/Makefile Outdated
CPPFLAGS+=-isystem $(GTEST_DIR)/include
CXXFLAGS=-std=c++11 -g -pthread

# Where to find user code.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/user code/code under test/

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment thread src/Makefile Outdated
purge: clean
$(RM) -r init-submodules build-cpp-netlib build-yaml-cpp \
$(CPP_NETLIB_DIR) $(YAML_CPP_DIR)
git submodule deinit --all
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused. Is this just do we don't touch the googletest submodule during regular builds?

Comment thread src/Makefile Outdated
git submodule deinit --all
$(RM) -r init-submodules build-cpp-netlib build-yaml-cpp
git submodule deinit -f $(CPP_NETLIB_DIR) && \
git submodule deinit -f $(YAML_CPP_DIR)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just list both of them on the same line:

git submodule deinit -f $(CPP_NETLIB_DIR) $(YAML_CPP_DIR)

Comment thread src/Makefile Outdated
touch init-submodules
git submodule update --init $(CPP_NETLIB_DIR) && \
git submodule update --init $(YAML_CPP_DIR)
touch ../init-submodules
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Careful! init-submodules is an actual file target. You can't just move it to the parent directory.

Comment thread src/Makefile Outdated
git submodule init && \
git submodule update
touch init-submodules
git submodule update --init $(CPP_NETLIB_DIR) && \
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, you can just put both of them on the same line:

git submodule update --init $(CPP_NETLIB_DIR) $(YAML_CPP_DIR)

Comment thread test/Makefile
purge: clean
git submodule deinit -f --all

init-submodules:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless you want to re-init the submodules all the time, you shouldn't make init-submodules a phony target.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to also touch it, like this.

Comment thread test/Makefile Outdated
$(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c \
$(GTEST_SOURCEDIR)/gtest_main.cc

$(GTEST_LIBS): build-googletest
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why have a phony target in the middle? This will re-archive every time. Just make $(GTEST_LIBS) depend on $(GTEST_DIR)/Makefile, gtest_main.a and the object files. Or make the target an actual file.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made the target an actual file - now the only phony targets are clean, purge and all

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If that file is never created (and cleaned up), it might as well be a phony target... I was proposing this:

$(GTEST_LIBS): $(GTEST_DIR)/Makefile
	$(AR) $(ARFLAGS) gtest_main.a gtest-all.o gtest_main.o

Comment thread test/format_unittest.cc Outdated

TEST(SubstituteTest, ValidFormat) {
const std::string endpoint_format =
"https://stackdriver.googleapis.com/v1beta2/projects/{{project_id}}";
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we need to keep the original format string here. How about something like:

  const std::string format_str("prefix|{{subst}}|postfix");
  EXPECT_EQ("prefix|value|postfix",
            format::Substitute(format_str, {{"subst", "value"}}));

? Ditto for other tests.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't have to be. But it makes the test more descriptive of the use cases.
Updated.

Comment thread test/format_unittest.cc Outdated

namespace {

TEST(SubstituteTest, ValidFormat) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about SingleSubstitution?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds better. Done.

Comment thread test/format_unittest.cc Outdated
);
}

TEST(SubstituteTest, ValidFormatMultipleSubstitution) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about just MultipleSubstitutions?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Copy Markdown
Contributor

@igorpeshansky igorpeshansky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not done with this round, but some early comments.

Comment thread test/Makefile Outdated
# Where to find user code.
SRC_DIR=../src
SRCS=\
$(SRC_DIR)/format.cc
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I meant something like:

../src/%.o: ../src/%.cc
	cd ../src && $(MAKE) $(subst $@,../src/,)

Alternatively, you could just have:

$(OBJS): ../src/metadatad
../src/metadatad:
	cd ../src && $(MAKE) all

Comment thread test/Makefile
purge: clean
git submodule deinit -f --all

init-submodules:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to also touch it, like this.

Comment thread test/Makefile Outdated

$(OBJS): build-source
$(TEST_OBJS): build-test
unittest: $(GTEST_LIBS) $(OBJS) $(TEST_OBJS)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I'm saying is: should we name this format-test instead of unittest? Because we'll have tests for other modules.

Comment thread test/Makefile Outdated
$(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c \
$(GTEST_SOURCEDIR)/gtest_main.cc

$(GTEST_LIBS): build-googletest
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If that file is never created (and cleaned up), it might as well be a phony target... I was proposing this:

$(GTEST_LIBS): $(GTEST_DIR)/Makefile
	$(AR) $(ARFLAGS) gtest_main.a gtest-all.o gtest_main.o

Comment thread test/Makefile
$(GTEST_DIR)/include/gtest/internal/*.h
GTEST_SRCS_=$(GTEST_SOURCEDIR)/*.cc $(GTEST_SOURCEDIR)/*.h $(GTEST_HEADERS)

CPP_NETLIB_DIR=$(LIBDIR)/cpp-netlib
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, let's add a TODO, please?

Comment thread test/format_unittest.cc Outdated
}

TEST(SubstituteTest, MultipleSubstitutions) {
const std::string format_str("prefix|{{subst}}|{{another_subst}}|postfix");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Optional] "prefix|{{first}}|middle|{{second}}|suffix".

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment thread test/format_unittest.cc Outdated
namespace {

TEST(SubstituteTest, SingleSubstitution) {
const std::string format_str("prefix|{{subst}}|postfix");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Optional] I know I suggested postfix in the first place, but suffix might read slightly better... Up to you.

Comment thread test/format_unittest.cc Outdated
EXPECT_EQ(
"prefix|value|value2|postfix",
format::Substitute(
format_str, {{"subst", "value"}, {"another_subst", "value2"}})
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Optional] {{"first", "one"}, {"second", "two"}}.

Comment thread test/format_unittest.cc Outdated
TEST(SubstituteTest, UnknownParameter) {
const std::string format_str("prefix|{{subst}}|postfix");
EXPECT_THROW(
format::Substitute(format_str, {{"another_subst", "value"}}),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Optional] {{"unknown", "value"}}.

Copy link
Copy Markdown
Contributor

@bmoyles0117 bmoyles0117 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Copy Markdown
Contributor

@igorpeshansky igorpeshansky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of stragglers.

Comment thread test/Makefile Outdated

TEST_DIR=.
TEST_SOURCES=$(wildcard $(TEST_DIR)/*_unittest.cc)
TEST_OBJS=$(TEST_SOURCES:%.cc=%.o)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs to be:

TEST_OBJS=$(TEST_SOURCES:$(TEST_DIR)/%.cc=%.o)

Otherwise all TEST_OBJS are going to be in $(TEST_DIR)...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed offline. TEST_DIR is '.', so this works fine.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Optional] I realized later that $(TEST_DIR) is ., so this is optional, but may be worth doing in case we end up putting tests in a subdirectory, lest we forget to override this (since the test executables do depend on the .o files being in the current directory).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that we already have the $(TEST_DIR) variable, makes sense to use it explicitly. Done.

Comment thread test/Makefile Outdated

gtest-all.o: $(GTEST_SOURCEDIR)/gtest-all.cc
$(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c -o $@ \
$(GTEST_SOURCEDIR)/gtest-all.cc
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can now be $^, right? And will fit on the previous line.
Ditto for gtest_main.cc

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment thread test/Makefile Outdated


$(SRC_DIR)/%.o: $(SRC_DIR)/%.cc
cd $(SRC_DIR) && $(MAKE) $(subst $@,$(SRC_DIR)/,)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I may have gotten this wrong. The following works: $(MAKE) $(@:$(SRC_DIR)/%=%).

Copy link
Copy Markdown
Contributor

@igorpeshansky igorpeshansky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM :shipit:
But see some optional comments.

Comment thread test/Makefile Outdated

TEST_DIR=.
TEST_SOURCES=$(wildcard $(TEST_DIR)/*_unittest.cc)
TEST_OBJS=$(TEST_SOURCES:%.cc=%.o)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Optional] I realized later that $(TEST_DIR) is ., so this is optional, but may be worth doing in case we end up putting tests in a subdirectory, lest we forget to override this (since the test executables do depend on the .o files being in the current directory).

Comment thread test/Makefile Outdated
$(GTEST_SOURCEDIR)/gtest-all.cc $(GTEST_SOURCEDIR)/gtest_main.cc: init-submodules

gtest-all.o: $(GTEST_SOURCEDIR)/gtest-all.cc
$(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c -o $@ $^
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Optional] We could make it

	$(CXX) -c $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -o $@ $^

to conform to the implicit rule. WDYT? Also below.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done - moved CXXFLAGS up as well.

Comment thread test/Makefile Outdated
$(AR) $(ARFLAGS) $@ $^

format_unittest: $(GTEST_LIB) format_unittest.o $(SRC_DIR)/format.o
$(CXX) $(CPPFLAGS) $(CXXFLAGS) $^ -lpthread -o $@
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Optional] We could make it:

	$(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@

and set LDLIBS to -lpthread (again, to ~ conform to the implicit rule).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about LDFLAGS? set that to CPPFLAGS?

Copy link
Copy Markdown
Contributor

@igorpeshansky igorpeshansky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor.

Comment thread test/Makefile Outdated
$(GTEST_SOURCEDIR)/gtest-all.cc $(GTEST_SOURCEDIR)/gtest_main.cc: init-submodules

gtest-all.o: $(GTEST_SOURCEDIR)/gtest-all.cc
$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) -I$(GTEST_DIR) -o $@ $^
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This -I$(GTEST_DIR) is part of $(CPPFLAGS), so let's put it right after (before $(CXXFLAGS)).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment thread test/Makefile Outdated

CPPFLAGS+=-isystem $(GTEST_DIR)/include
CXXFLAGS=-std=c++11 -g -pthread
LDFLAGS=$(CPPFLAGS)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for this -- the linker ignores the -isystem arguments. Just let $(LDFLAGS) pick up the default (i.e., don't set it).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment thread test/Makefile
CPPFLAGS+=-isystem $(GTEST_DIR)/include
CXXFLAGS=-std=c++11 -g -pthread
LDFLAGS=$(CPPFLAGS)
LDLIBS=-lpthread
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Optional] This may actually be implied by -pthread, but it's harmless to keep as-is.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is required.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may not have been required had we set LDFLAGS=$(CXXFLAGS), but it's not a big deal either way.

Copy link
Copy Markdown
Contributor

@igorpeshansky igorpeshansky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM :shipit:

@igorpeshansky igorpeshansky merged commit adde53f into Stackdriver:master Mar 2, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants