diff --git a/src/String/BasicStringView.hpp b/src/String/BasicStringView.hpp index f762ed6..110a421 100644 --- a/src/String/BasicStringView.hpp +++ b/src/String/BasicStringView.hpp @@ -1,5 +1,6 @@ #ifndef ARBUTILS_BASICSTRINGVIEW_HPP #define ARBUTILS_BASICSTRINGVIEW_HPP +#include #include namespace ArbUt { @@ -28,7 +29,11 @@ namespace ArbUt { virtual constexpr bool operator!=(const std::string_view& rhs) const noexcept = 0; virtual constexpr bool operator==(const char* rhs) const noexcept = 0; virtual constexpr bool operator!=(const char* rhs) const noexcept = 0; + + friend std::ostream& operator<<(std::ostream& out, const BasicStringView& c) { + out << c.c_str(); + return out; + } }; } - #endif // ARBUTILS_BASICSTRINGVIEW_HPP diff --git a/tests/ConstStringTests.cpp b/tests/StringViewTests.cpp similarity index 73% rename from tests/ConstStringTests.cpp rename to tests/StringViewTests.cpp index c3a7cc7..d79018d 100644 --- a/tests/ConstStringTests.cpp +++ b/tests/StringViewTests.cpp @@ -1,5 +1,6 @@ #ifdef TESTS_BUILD #include +#include #include #include "../extern/catch.hpp" #include "../src/StringView.hpp" @@ -11,7 +12,7 @@ TEST_CASE("Initialize compile time", "[Utilities]") { TEST_CASE("Compare compile time", "[Utilities]") { static_assert("foo"_cnc != "bar"_cnc); } -TEST_CASE("Compare compile time with CaseInsensitiveConstString", "[Utilities]") { +TEST_CASE("Compare compile time with CaseInsensitiveStringview", "[Utilities]") { static_assert("foo"_cnc == ArbUt::StringViewLiteral("foo")); } @@ -34,18 +35,30 @@ TEST_CASE("Use case insensitive const string in switch case", "[Utilities]") { } } -TEST_CASE("Literal conststring to non literal, then use", "[Utilities]") { +TEST_CASE("Literal stringview to non literal, then use", "[Utilities]") { ArbUt::StringView val; { val = "foobar"_cnc; } INFO(val.c_str()); REQUIRE(strcmp(val.c_str(), "foobar") == 0); } +TEST_CASE("Pipe stringview into strean", "[Utilities]") { + std::stringstream ss; + ArbUt::StringView val = "foo"; + ss << val; + REQUIRE(ss.str() == "foo"); + ss << val; + REQUIRE(ss.str() == "foofoo"); + ArbUt::StringView val2 = "bar"; + ss << val2; + REQUIRE(ss.str() == "foofoobar"); +} + #ifndef WINDOWS __attribute__((optnone)) #endif static ArbUt::StringView -TestCreateConstString() { +TestCreateStringview() { char originalVal[7]; originalVal[0] = 'f'; originalVal[1] = 'o'; @@ -58,7 +71,7 @@ TestCreateConstString() { } TEST_CASE("Out of scope char* doesn't lose reference", "[Utilities]") { - ArbUt::StringView val = TestCreateConstString(); + ArbUt::StringView val = TestCreateStringview(); INFO(val.c_str()); REQUIRE(strcmp(val.c_str(), "foobar") == 0); }