From 2ebb5d337b085a4c7f21ffcd0e63f969272285ce Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Sat, 28 Dec 2019 19:35:55 +0100 Subject: new_type: implement stream io operators --- test/include/io_operators_suite.hpp | 11 ++++ test/src/driver.cpp | 2 + test/src/io_operators_suite.cpp | 121 ++++++++++++++++++++++++++++++++++++ 3 files changed, 134 insertions(+) create mode 100644 test/include/io_operators_suite.hpp create mode 100644 test/src/io_operators_suite.cpp (limited to 'test') diff --git a/test/include/io_operators_suite.hpp b/test/include/io_operators_suite.hpp new file mode 100644 index 0000000..c7af2ee --- /dev/null +++ b/test/include/io_operators_suite.hpp @@ -0,0 +1,11 @@ +#ifndef NEWTYPE_TEST_IO_OPERATORS_SUITE_HPP +#define NEWTYPE_TEST_IO_OPERATORS_SUITE_HPP + +#include + +#include +#include + +auto io_operators_suite() -> std::pair; + +#endif \ No newline at end of file diff --git a/test/src/driver.cpp b/test/src/driver.cpp index 1ba3d68..c650dda 100644 --- a/test/src/driver.cpp +++ b/test/src/driver.cpp @@ -1,6 +1,7 @@ #include "conversion_suite.hpp" #include "derivation_clause_suite.hpp" #include "equality_comparison_suite.hpp" +#include "io_operators_suite.hpp" #include "new_type_constructor_suite.hpp" #include "relational_operators_suite.hpp" @@ -55,6 +56,7 @@ int main(int argc, char ** argv) conversion_suite(), equality_comparison_suite(), relational_operators_suite(), + io_operators_suite(), }; auto selectors = get_test_selectors(suites); diff --git a/test/src/io_operators_suite.cpp b/test/src/io_operators_suite.cpp new file mode 100644 index 0000000..7f19963 --- /dev/null +++ b/test/src/io_operators_suite.cpp @@ -0,0 +1,121 @@ +#include "io_operators_suite.hpp" + +#include "kawaii.hpp" +#include "newtype/derivable.hpp" +#include "newtype/deriving.hpp" +#include "newtype/new_type.hpp" + +#include + +#include +#include +#include +#include +#include + +inline namespace traits_extensions +{ + + template + struct has_stream_input : std::false_type + { + }; + + template + struct has_stream_input() >> std::declval())>> : std::true_type + { + }; + + template + auto constexpr has_stream_input_v = has_stream_input::value; + + template + struct has_stream_output : std::false_type + { + }; + + template + struct has_stream_output() << std::declval())>> : std::true_type + { + }; + + template + auto constexpr has_stream_output_v = has_stream_output::value; + +} // namespace traits_extensions + +inline namespace stream_input_operator_tests +{ + + auto a_new__type_has_the_stream_input_operator_if_the_derivation_clause_contains_read() -> void + { + using type_alias = nt::new_type; + ASSERT(has_stream_input_v); + } + + auto a_new__type_does_not_have_the_stream_input_operator_if_the_derivation_clause_does_not_contain_read() -> void + { + using type_alias = nt::new_type; + ASSERT(!has_stream_input_v); + } + + auto a_instance_of_a_new__type_can_be_read_from_an_istream_if_the_base_type_can_be_read_from_an_istream() -> void + { + static_assert(has_stream_input_v, "Sanity Check"); + using type_alias = nt::new_type; + + auto obj = type_alias{}; + auto input = std::istringstream{"42"}; + + input >> obj; + + ASSERT_EQUAL(42, obj.decay()); + } + +} // namespace stream_input_operator_tests + +inline namespace stream_output_operator_tests +{ + + auto a_new__type_has_the_stream_output_operator_if_the_derivation_clause_contains_show() -> void + { + using type_alias = nt::new_type; + ASSERT(has_stream_output_v); + } + + auto a_new__type_does_not_have_the_stream_output_operator_if_the_derivation_clause_does_not_contain_show() -> void + { + using type_alias = nt::new_type; + ASSERT(!has_stream_output_v); + } + + auto a_instance_of_a_new__type_can_be_written_to_an_ostream_if_the_base_type_can_be_written_to_an_ostream() -> void + { + static_assert(has_stream_output_v, "Sanity Check"); + using type_alias = nt::new_type; + + auto obj = type_alias{42}; + auto output = std::ostringstream{}; + + output << obj; + + ASSERT_EQUAL("42", output.str()); + } + +} // namespace stream_output_operator_tests + +auto io_operators_suite() -> std::pair +{ + return {{ + // Stream Input Operator Tests + KAWAII(a_new__type_has_the_stream_input_operator_if_the_derivation_clause_contains_read), + KAWAII(a_new__type_does_not_have_the_stream_input_operator_if_the_derivation_clause_does_not_contain_read), + KAWAII(a_instance_of_a_new__type_can_be_read_from_an_istream_if_the_base_type_can_be_read_from_an_istream), + + // Stream Ouput Operator Tests + KAWAII(a_new__type_has_the_stream_output_operator_if_the_derivation_clause_contains_show), + KAWAII(a_new__type_does_not_have_the_stream_output_operator_if_the_derivation_clause_does_not_contain_show), + KAWAII(a_instance_of_a_new__type_can_be_written_to_an_ostream_if_the_base_type_can_be_written_to_an_ostream), + }, + "I/O Operators Tests"}; +} \ No newline at end of file -- cgit v1.2.3