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
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
[submodule "lib/yaml-cpp"]
path = lib/yaml-cpp
url = https://github.com/jbeder/yaml-cpp
[submodule "lib/googletest"]
path = lib/googletest
url = https://github.com/google/googletest
1 change: 1 addition & 0 deletions lib/googletest
Submodule googletest added at 421405
67 changes: 67 additions & 0 deletions test/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
LIBDIR=../lib
GTEST_MODULE=$(LIBDIR)/googletest
GTEST_DIR=$(LIBDIR)/googletest/googletest
GTEST_SOURCEDIR=$(GTEST_DIR)/src
GTEST_HEADERS=$(GTEST_DIR)/include/gtest/*.h \
$(GTEST_DIR)/include/gtest/internal/*.h
GTEST_SRCS_=$(GTEST_SOURCEDIR)/*.cc $(GTEST_SOURCEDIR)/*.h $(GTEST_HEADERS)

# TODO: Factor out the common variables.
CPP_NETLIB_DIR=$(LIBDIR)/cpp-netlib
YAML_CPP_DIR=$(LIBDIR)/yaml-cpp

CPPFLAGS+=-isystem $(GTEST_DIR)/include
CXXFLAGS=-std=c++11 -g -pthread
LDLIBS=-lpthread

# Where to find code under test.
SRC_DIR=../src

TEST_DIR=.
TEST_SOURCES=$(wildcard $(TEST_DIR)/*_unittest.cc)
TEST_OBJS=$(TEST_SOURCES:$(TEST_DIR)/%.cc=%.o)
TESTS=\
format_unittest

GTEST_LIB=gtest_lib.a

all: $(TESTS)

# TODO: Running the test prints "Running main() from gtest_main.cc".
# Figure out how to fix this.
test: $(TESTS)
@for t in $(TESTS); do \
echo "Running $$t"; \
./$$t; \
done

clean:
$(RM) $(TESTS) $(GTEST_LIB) $(TEST_OBJS)

purge: clean
$(RM) init-submodules
git submodule deinit -f $(GTEST_MODULE)

init-submodules:
git submodule update --init $(GTEST_MODULE)
touch init-submodules


$(SRC_DIR)/%.o: $(SRC_DIR)/%.cc
cd $(SRC_DIR) && $(MAKE) $(@:$(SRC_DIR)/%=%)

$(GTEST_SOURCEDIR)/gtest-all.cc $(GTEST_SOURCEDIR)/gtest_main.cc: init-submodules

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

gtest_main.o: $(GTEST_SOURCEDIR)/gtest_main.cc
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $^

$(GTEST_LIB): gtest-all.o gtest_main.o
$(AR) $(ARFLAGS) $@ $^

format_unittest: $(GTEST_LIB) format_unittest.o $(SRC_DIR)/format.o
$(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@

.PHONY: all test clean purge
38 changes: 38 additions & 0 deletions test/format_unittest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "../src/format.h"
#include "gtest/gtest.h"

namespace {

TEST(SubstituteTest, SingleSubstitution) {
const std::string format_str("prefix|{{subst}}|suffix");
EXPECT_EQ(
"prefix|value|suffix",
format::Substitute(format_str, {{"subst", "value"}})
);
}

TEST(SubstituteTest, MultipleSubstitutions) {
const std::string format_str("prefix|{{first}}|middle|{{second}}|suffix");
EXPECT_EQ(
"prefix|one|middle|two|suffix",
format::Substitute(
format_str, {{"first", "one"}, {"second", "two"}})
);
}

TEST(SubstituteTest, UnterminatedPlaceholder) {
const std::string format_str("prefix|{{subst|suffix");
EXPECT_THROW(
format::Substitute(format_str, {{"subst", "value"}}),
format::Exception
);
}

TEST(SubstituteTest, UnknownParameter) {
const std::string format_str("prefix|{{subst}}|suffix");
EXPECT_THROW(
format::Substitute(format_str, {{"unknown", "value"}}),
format::Exception
);
}
} // namespace