|
| 1 | +/* |
| 2 | + * (C) Copyright 2025- ECMWF. |
| 3 | + * |
| 4 | + * This software is licensed under the terms of the Apache Licence Version 2.0 |
| 5 | + * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
| 6 | + * In applying this licence, ECMWF does not waive the privileges and immunities |
| 7 | + * granted to it by virtue of its status as an intergovernmental organisation nor |
| 8 | + * does it submit to any jurisdiction. |
| 9 | + */ |
| 10 | +#include <eckit/testing/Test.h> |
| 11 | + |
| 12 | +#include "eckit/format/Format.h" |
| 13 | +#include "eckit/log/Log.h" |
| 14 | +#include "eckit/log/OStreamTarget.h" |
| 15 | +#include "eckit/log/Channel.h" |
| 16 | + |
| 17 | +#include <sstream> |
| 18 | + |
| 19 | +CASE("Format to output iterator") { |
| 20 | + { |
| 21 | + auto output = fmt::memory_buffer(); |
| 22 | + eckit::format_to(std::back_inserter(output), "Hello {} {} {}", "through", "a", "stream"); |
| 23 | + EXPECT_EQUAL(std::string(output.data(), output.size()), std::string("Hello through a stream")); |
| 24 | + } |
| 25 | + { |
| 26 | + std::vector<char> output; |
| 27 | + eckit::format_to(std::back_inserter(output), "Hello {} {} {}", "through", "a", "stream"); |
| 28 | + EXPECT_EQUAL(std::string(output.data(), output.size()), std::string("Hello through a stream")); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +CASE("Format to streams") { |
| 33 | + std::ostringstream oss; |
| 34 | + eckit::format_to(oss, "Hello {} {} {}", "through", "a", "stream"); |
| 35 | + EXPECT_EQUAL(oss.str(), std::string("Hello through a stream")); |
| 36 | +} |
| 37 | + |
| 38 | +CASE("Format to eckit channel") { |
| 39 | + std::ostringstream oss; |
| 40 | + eckit::Channel ch{new eckit::OStreamTarget{oss}}; |
| 41 | + eckit::format_to(ch, "Hello {} {} {}", "through", "a", "stream"); |
| 42 | + ch.flush(); |
| 43 | + EXPECT_EQUAL(oss.str(), std::string("Hello through a stream")); |
| 44 | +} |
| 45 | + |
| 46 | + |
| 47 | +int main(int argc, char** argv) { |
| 48 | + return eckit::testing::run_tests(argc, argv); |
| 49 | +} |
0 commit comments