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

Add the first unit test #53

Merged
merged 8 commits into from
Mar 2, 2018
Merged

Conversation

supriyagarg
Copy link
Contributor

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

Copy link
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.

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
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
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
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
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
Member

@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.

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

# Where to find user code.
Copy link
Member

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
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
Contributor Author

Choose a reason for hiding this comment

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

Done

src/Makefile Outdated
@@ -123,15 +123,14 @@ clean:
$(RM) metadatad $(OBJS)

purge: clean
$(RM) -r init-submodules build-cpp-netlib build-yaml-cpp \
$(CPP_NETLIB_DIR) $(YAML_CPP_DIR)
git submodule deinit --all
Copy link
Member

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?

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
Member

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)

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
Member

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.

src/Makefile Outdated
git submodule init && \
git submodule update
touch init-submodules
git submodule update --init $(CPP_NETLIB_DIR) && \
Copy link
Member

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)

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

init-submodules:
Copy link
Member

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
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
Member

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.

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

$(GTEST_LIBS): build-googletest
Copy link
Member

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
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
Member

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


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

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
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.


namespace {

TEST(SubstituteTest, ValidFormat) {
Copy link
Member

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
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.

);
}

TEST(SubstituteTest, ValidFormatMultipleSubstitution) {
Copy link
Member

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
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
Member

@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.

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

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

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

init-submodules:
Copy link
Member

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.

test/Makefile Outdated

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

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.

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

$(GTEST_LIBS): build-googletest
Copy link
Member

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

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

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

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?

}

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

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
Contributor Author

Choose a reason for hiding this comment

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

Done

namespace {

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

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.

EXPECT_EQ(
"prefix|value|value2|postfix",
format::Substitute(
format_str, {{"subst", "value"}, {"another_subst", "value2"}})
Copy link
Member

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"}}.

TEST(SubstituteTest, UnknownParameter) {
const std::string format_str("prefix|{{subst}}|postfix");
EXPECT_THROW(
format::Substitute(format_str, {{"another_subst", "value"}}),
Copy link
Member

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
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
Member

@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.

test/Makefile Outdated

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

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
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
Member

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
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.

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
Member

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
Contributor Author

Choose a reason for hiding this comment

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

Done

test/Makefile Outdated


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

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
Member

@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.

test/Makefile Outdated

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

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).

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
Member

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
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.

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

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

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
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
Member

@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.

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
Member

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
Contributor Author

Choose a reason for hiding this comment

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

Done

test/Makefile Outdated

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

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
Contributor Author

Choose a reason for hiding this comment

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

Done

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

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
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
Member

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
Member

@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