|
| 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 | + |
| 14 | +CASE("Can use eckit_format macro") { |
| 15 | + EXPECT_EQUAL(eckit_format("Hello {} {}", 1, 2), std::string("Hello 1 2")); |
| 16 | + EXPECT_EQUAL(eckit_format(std::string_view("Hello {} {}"), 1, 2), std::string("Hello 1 2")); |
| 17 | +} |
| 18 | + |
| 19 | +CASE("Can use eckit::format") { |
| 20 | + EXPECT_EQUAL(eckit::format("Hello {} {}", 1, 2), std::string("Hello 1 2")); |
| 21 | + EXPECT_EQUAL(eckit::format(std::string_view("Hello {} {}"), 1, 2), std::string("Hello 1 2")); |
| 22 | + EXPECT_EQUAL(eckit::format(std::string("Hello {} {}"), 1, 2), std::string("Hello 1 2")); |
| 23 | + std::string str("Hello {} {}"); |
| 24 | + std::string_view str_v(str); |
| 25 | + EXPECT_EQUAL(eckit::format(str_v, 1, 2), std::string("Hello 1 2")); |
| 26 | +} |
| 27 | + |
| 28 | +CASE("eckit::format throws on format erors") { |
| 29 | + EXPECT_THROWS_AS(eckit::format("Hello {} {} {}", 1, 2), fmt::format_error); |
| 30 | + EXPECT_THROWS_AS(eckit::format(std::string("Hello {} {} {}"), 1, 2), fmt::format_error); |
| 31 | + EXPECT_THROWS_AS(eckit::format(std::string_view("Hello {} {} {}"), 1, 2), fmt::format_error); |
| 32 | + EXPECT_THROWS_AS(eckit::format("Hello {} {} {", 1, 2), fmt::format_error); |
| 33 | + EXPECT_THROWS_AS(eckit::format(std::string("Hello {} {} {"), 1, 2), fmt::format_error); |
| 34 | + EXPECT_THROWS_AS(eckit::format(std::string_view("Hello {} {} {"), 1, 2), fmt::format_error); |
| 35 | + EXPECT_THROWS_AS(eckit::format("Hello {:p} {} {}", 1, 2), fmt::format_error); |
| 36 | + EXPECT_THROWS_AS(eckit::format(std::string("Hello {:p} {} {}"), 1, 2), fmt::format_error); |
| 37 | + EXPECT_THROWS_AS(eckit::format(std::string_view("Hello {:p} {} {}"), 1, 2), fmt::format_error); |
| 38 | +} |
| 39 | + |
| 40 | +struct MyType { |
| 41 | + int x{5}; |
| 42 | +}; |
| 43 | +std::ostream& operator<<(std::ostream& out, const MyType& mt) { |
| 44 | + return out << "MyType{" << mt.x << "}"; |
| 45 | +} |
| 46 | +ENABLE_FORMAT(MyType); |
| 47 | + |
| 48 | +CASE("ENABLE_FORMAT works") { |
| 49 | + EXPECT_EQUAL(eckit::format("Hello {}", MyType{}), std::string("Hello MyType{5}")); |
| 50 | +} |
| 51 | + |
| 52 | +ENABLE_FORMAT(eckit::CodeLocation); |
| 53 | +CASE("ENABLE_FORMAT CodeLocation") { |
| 54 | + // This is just an example |
| 55 | + std::cout << eckit::format("{}", Here()); |
| 56 | +} |
| 57 | + |
| 58 | + |
| 59 | +int main(int argc, char** argv) { |
| 60 | + return eckit::testing::run_tests(argc, argv); |
| 61 | +} |
0 commit comments