THRIFT-4060 add better support in the cpp generator for custom ostream operators on structures
Client: C++
This closes #1172
diff --git a/lib/cpp/test/AnnotationTest.cpp b/lib/cpp/test/AnnotationTest.cpp
new file mode 100644
index 0000000..2e18840
--- /dev/null
+++ b/lib/cpp/test/AnnotationTest.cpp
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#define BOOST_TEST_MODULE AnnotationTest
+#include <boost/test/unit_test.hpp>
+#include "gen-cpp/AnnotationTest_types.h"
+#include <ostream>
+#include <sstream>
+
+// Normally thrift generates ostream operators, however
+// with the annotation "cpp.customostream" one can tell the
+// compiler they are going to provide their own, and not
+// emit operator << or printTo().
+
+std::ostream& operator<<(std::ostream& os, const ostr_custom& osc)
+{
+ os << "{ bar = " << osc.bar << "; }";
+ return os;
+}
+
+BOOST_AUTO_TEST_SUITE(BOOST_TEST_MODULE)
+
+BOOST_AUTO_TEST_CASE(test_cpp_compiler_generated_ostream_operator)
+{
+ ostr_default def;
+ def.__set_bar(10);
+
+ std::stringstream ssd;
+ ssd << def;
+ BOOST_CHECK_EQUAL(ssd.str(), "ostr_default(bar=10)");
+}
+
+BOOST_AUTO_TEST_CASE(test_cpp_customostream_uses_consuming_application_definition)
+{
+ ostr_custom cus;
+ cus.__set_bar(10);
+
+ std::stringstream csd;
+ csd << cus;
+ BOOST_CHECK_EQUAL(csd.str(), "{ bar = 10; }");
+}
+
+/**
+ * Disabled; see THRIFT-1567 - not sure what it is supposed to do
+BOOST_AUTO_TEST_CASE(test_cpp_type) {
+ // Check that the "cpp.type" annotation changes "struct foo" to "DenseFoo"
+ // This won't compile if the annotation is mishandled
+ DenseFoo foo;
+ foo.__set_bar(5);
+}
+ */
+
+BOOST_AUTO_TEST_SUITE_END()
diff --git a/lib/cpp/test/CMakeLists.txt b/lib/cpp/test/CMakeLists.txt
index 74b0a09..b7a7798 100644
--- a/lib/cpp/test/CMakeLists.txt
+++ b/lib/cpp/test/CMakeLists.txt
@@ -30,6 +30,8 @@
# Create the thrift C++ test library
set(testgencpp_SOURCES
+ gen-cpp/AnnotationTest_types.cpp
+ gen-cpp/AnnotationTest_types.h
gen-cpp/DebugProtoTest_types.cpp
gen-cpp/DebugProtoTest_types.h
gen-cpp/EnumTest_types.cpp
@@ -146,6 +148,13 @@
add_test(NAME ZlibTest COMMAND ZlibTest)
endif(WITH_ZLIB)
+add_executable(AnnotationTest AnnotationTest.cpp)
+target_link_libraries(AnnotationTest
+ testgencpp
+ ${Boost_LIBRARIES}
+)
+LINK_AGAINST_THRIFT_LIBRARY(AnnotationTest thrift)
+add_test(NAME AnnotationTest COMMAND AnnotationTest)
add_executable(EnumTest EnumTest.cpp)
target_link_libraries(EnumTest
@@ -332,8 +341,16 @@
# Common thrift code generation rules
#
+add_custom_command(OUTPUT gen-cpp/AnnotationTest_constants.cpp
+ gen-cpp/AnnotationTest_constants.h
+ gen-cpp/AnnotationTest_types.cpp
+ gen-cpp/AnnotationTest_types.h
+ gen-cpp/foo_service.cpp
+ gen-cpp/foo_service.h
+ COMMAND ${THRIFT_COMPILER} --gen cpp ${PROJECT_SOURCE_DIR}/test/AnnotationTest.thrift
+)
-add_custom_command(OUTPUT gen-cpp/DebugProtoTest_types.cpp gen-cpp/DebugProtoTest_types.h gen-cpp/EmptyService.cpp gen-cpp/EmptyService.h
+add_custom_command(OUTPUT gen-cpp/DebugProtoTest_types.cpp gen-cpp/DebugProtoTest_types.h gen-cpp/EmptyService.cpp gen-cpp/EmptyService.h
COMMAND ${THRIFT_COMPILER} --gen cpp ${PROJECT_SOURCE_DIR}/test/DebugProtoTest.thrift
)
diff --git a/lib/cpp/test/Makefile.am b/lib/cpp/test/Makefile.am
index 6f46117..d387297 100755
--- a/lib/cpp/test/Makefile.am
+++ b/lib/cpp/test/Makefile.am
@@ -18,7 +18,8 @@
#
AUTOMAKE_OPTIONS = subdir-objects serial-tests
-BUILT_SOURCES = gen-cpp/DebugProtoTest_types.h \
+BUILT_SOURCES = gen-cpp/AnnotationTest_types.h \
+ gen-cpp/DebugProtoTest_types.h \
gen-cpp/EnumTest_types.h \
gen-cpp/OptionalRequiredTest_types.h \
gen-cpp/Recursive_types.h \
@@ -31,6 +32,8 @@
noinst_LTLIBRARIES = libtestgencpp.la libprocessortest.la
nodist_libtestgencpp_la_SOURCES = \
+ gen-cpp/AnnotationTest_types.cpp \
+ gen-cpp/AnnotationTest_types.h \
gen-cpp/DebugProtoTest_types.cpp \
gen-cpp/DebugProtoTest_types.h \
gen-cpp/EnumTest_types.cpp \
@@ -89,7 +92,8 @@
TFileTransportTest \
link_test \
OpenSSLManualInitTest \
- EnumTest
+ EnumTest \
+ AnnotationTest
if AMX_HAVE_LIBEVENT
noinst_PROGRAMS += \
@@ -178,12 +182,19 @@
-lz
EnumTest_SOURCES = \
- EnumTest.cpp
+ EnumTest.cpp
EnumTest_LDADD = \
libtestgencpp.la \
$(BOOST_TEST_LDADD)
+AnnotationTest_SOURCES = \
+ AnnotationTest.cpp
+
+AnnotationTest_LDADD = \
+ libtestgencpp.la \
+ $(BOOST_TEST_LDADD)
+
TFileTransportTest_SOURCES = \
TFileTransportTest.cpp
@@ -334,6 +345,9 @@
#
THRIFT = $(top_builddir)/compiler/cpp/thrift
+gen-cpp/AnnotationTest_constants.cpp gen-cpp/AnnotationTest_constants.h gen-cpp/AnnotationTest_types.cpp gen-cpp/AnnotationTest_types.h: $(top_srcdir)/test/AnnotationTest.thrift
+ $(THRIFT) --gen cpp $<
+
gen-cpp/DebugProtoTest_types.cpp gen-cpp/DebugProtoTest_types.h gen-cpp/EmptyService.cpp gen-cpp/EmptyService.h: $(top_srcdir)/test/DebugProtoTest.thrift
$(THRIFT) --gen cpp $<