Add the first unit test#53
Conversation
bmoyles0117
left a comment
There was a problem hiding this comment.
Looks great overall, excited to see tests! One minor comment.
| $(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) && \ |
There was a problem hiding this comment.
Do these commands need to be chained together? If so, can we indent the second line? Same elsewhere.
There was a problem hiding this comment.
I edited the existing Makefile, so have kept the same format.
I have updated the indents in the new Makefile though. PTAL.
|
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.
igorpeshansky
left a comment
There was a problem hiding this comment.
A preliminary review.
| CPPFLAGS+=-isystem $(GTEST_DIR)/include | ||
| CXXFLAGS=-std=c++11 -g -pthread | ||
|
|
||
| # Where to find user code. |
There was a problem hiding this comment.
s/user code/code under test/
| purge: clean | ||
| $(RM) -r init-submodules build-cpp-netlib build-yaml-cpp \ | ||
| $(CPP_NETLIB_DIR) $(YAML_CPP_DIR) | ||
| git submodule deinit --all |
There was a problem hiding this comment.
I'm confused. Is this just do we don't touch the googletest submodule during regular builds?
| 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) |
There was a problem hiding this comment.
Just list both of them on the same line:
git submodule deinit -f $(CPP_NETLIB_DIR) $(YAML_CPP_DIR)| touch init-submodules | ||
| git submodule update --init $(CPP_NETLIB_DIR) && \ | ||
| git submodule update --init $(YAML_CPP_DIR) | ||
| touch ../init-submodules |
There was a problem hiding this comment.
Careful! init-submodules is an actual file target. You can't just move it to the parent directory.
| git submodule init && \ | ||
| git submodule update | ||
| touch init-submodules | ||
| git submodule update --init $(CPP_NETLIB_DIR) && \ |
There was a problem hiding this comment.
Same here, you can just put both of them on the same line:
git submodule update --init $(CPP_NETLIB_DIR) $(YAML_CPP_DIR)| purge: clean | ||
| git submodule deinit -f --all | ||
|
|
||
| init-submodules: |
There was a problem hiding this comment.
Unless you want to re-init the submodules all the time, you shouldn't make init-submodules a phony target.
| $(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c \ | ||
| $(GTEST_SOURCEDIR)/gtest_main.cc | ||
|
|
||
| $(GTEST_LIBS): build-googletest |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
I made the target an actual file - now the only phony targets are clean, purge and all
There was a problem hiding this comment.
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}}"; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Doesn't have to be. But it makes the test more descriptive of the use cases.
Updated.
|
|
||
| namespace { | ||
|
|
||
| TEST(SubstituteTest, ValidFormat) { |
There was a problem hiding this comment.
How about SingleSubstitution?
There was a problem hiding this comment.
Sounds better. Done.
| ); | ||
| } | ||
|
|
||
| TEST(SubstituteTest, ValidFormatMultipleSubstitution) { |
There was a problem hiding this comment.
How about just MultipleSubstitutions?
70449da to
5aa2078
Compare
igorpeshansky
left a comment
There was a problem hiding this comment.
Not done with this round, but some early comments.
| # Where to find user code. | ||
| SRC_DIR=../src | ||
| SRCS=\ | ||
| $(SRC_DIR)/format.cc |
There was a problem hiding this comment.
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| purge: clean | ||
| git submodule deinit -f --all | ||
|
|
||
| init-submodules: |
|
|
||
| $(OBJS): build-source | ||
| $(TEST_OBJS): build-test | ||
| unittest: $(GTEST_LIBS) $(OBJS) $(TEST_OBJS) |
There was a problem hiding this comment.
What I'm saying is: should we name this format-test instead of unittest? Because we'll have tests for other modules.
| $(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c \ | ||
| $(GTEST_SOURCEDIR)/gtest_main.cc | ||
|
|
||
| $(GTEST_LIBS): build-googletest |
There was a problem hiding this comment.
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| $(GTEST_DIR)/include/gtest/internal/*.h | ||
| GTEST_SRCS_=$(GTEST_SOURCEDIR)/*.cc $(GTEST_SOURCEDIR)/*.h $(GTEST_HEADERS) | ||
|
|
||
| CPP_NETLIB_DIR=$(LIBDIR)/cpp-netlib |
There was a problem hiding this comment.
Ok, let's add a TODO, please?
| } | ||
|
|
||
| TEST(SubstituteTest, MultipleSubstitutions) { | ||
| const std::string format_str("prefix|{{subst}}|{{another_subst}}|postfix"); |
There was a problem hiding this comment.
[Optional] "prefix|{{first}}|middle|{{second}}|suffix".
| namespace { | ||
|
|
||
| TEST(SubstituteTest, SingleSubstitution) { | ||
| const std::string format_str("prefix|{{subst}}|postfix"); |
There was a problem hiding this comment.
[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"}}) |
There was a problem hiding this comment.
[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"}}), |
There was a problem hiding this comment.
[Optional] {{"unknown", "value"}}.
igorpeshansky
left a comment
There was a problem hiding this comment.
A couple of stragglers.
|
|
||
| TEST_DIR=. | ||
| TEST_SOURCES=$(wildcard $(TEST_DIR)/*_unittest.cc) | ||
| TEST_OBJS=$(TEST_SOURCES:%.cc=%.o) |
There was a problem hiding this comment.
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)...
There was a problem hiding this comment.
Discussed offline. TEST_DIR is '.', so this works fine.
There was a problem hiding this comment.
[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).
There was a problem hiding this comment.
Given that we already have the $(TEST_DIR) variable, makes sense to use it explicitly. Done.
|
|
||
| gtest-all.o: $(GTEST_SOURCEDIR)/gtest-all.cc | ||
| $(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c -o $@ \ | ||
| $(GTEST_SOURCEDIR)/gtest-all.cc |
There was a problem hiding this comment.
This can now be $^, right? And will fit on the previous line.
Ditto for gtest_main.cc
|
|
||
|
|
||
| $(SRC_DIR)/%.o: $(SRC_DIR)/%.cc | ||
| cd $(SRC_DIR) && $(MAKE) $(subst $@,$(SRC_DIR)/,) |
There was a problem hiding this comment.
I may have gotten this wrong. The following works: $(MAKE) $(@:$(SRC_DIR)/%=%).
igorpeshansky
left a comment
There was a problem hiding this comment.
LGTM ![]()
But see some optional comments.
|
|
||
| TEST_DIR=. | ||
| TEST_SOURCES=$(wildcard $(TEST_DIR)/*_unittest.cc) | ||
| TEST_OBJS=$(TEST_SOURCES:%.cc=%.o) |
There was a problem hiding this comment.
[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).
| $(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 $@ $^ |
There was a problem hiding this comment.
[Optional] We could make it
$(CXX) -c $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -o $@ $^to conform to the implicit rule. WDYT? Also below.
There was a problem hiding this comment.
Done - moved CXXFLAGS up as well.
| $(AR) $(ARFLAGS) $@ $^ | ||
|
|
||
| format_unittest: $(GTEST_LIB) format_unittest.o $(SRC_DIR)/format.o | ||
| $(CXX) $(CPPFLAGS) $(CXXFLAGS) $^ -lpthread -o $@ |
There was a problem hiding this comment.
[Optional] We could make it:
$(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@and set LDLIBS to -lpthread (again, to ~ conform to the implicit rule).
There was a problem hiding this comment.
what about LDFLAGS? set that to CPPFLAGS?
| $(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 $@ $^ |
There was a problem hiding this comment.
This -I$(GTEST_DIR) is part of $(CPPFLAGS), so let's put it right after (before $(CXXFLAGS)).
|
|
||
| CPPFLAGS+=-isystem $(GTEST_DIR)/include | ||
| CXXFLAGS=-std=c++11 -g -pthread | ||
| LDFLAGS=$(CPPFLAGS) |
There was a problem hiding this comment.
No need for this -- the linker ignores the -isystem arguments. Just let $(LDFLAGS) pick up the default (i.e., don't set it).
| CPPFLAGS+=-isystem $(GTEST_DIR)/include | ||
| CXXFLAGS=-std=c++11 -g -pthread | ||
| LDFLAGS=$(CPPFLAGS) | ||
| LDLIBS=-lpthread |
There was a problem hiding this comment.
[Optional] This may actually be implied by -pthread, but it's harmless to keep as-is.
There was a problem hiding this comment.
This is required.
There was a problem hiding this comment.
This may not have been required had we set LDFLAGS=$(CXXFLAGS), but it's not a big deal either way.
This PR sets up the googletest framework, and adds a unit test for format.cc