1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
#include <kstd/flat_map>
#include <kstd/format>
#include <kstd/tests/os_panic.hpp>
#include <catch2/catch_test_macros.hpp>
#include <iterator>
#include <sstream>
SCENARIO("Formatting to a new string", "[format]")
{
GIVEN("a format string without any placeholders")
{
auto const & fmt = "This is a test";
WHEN("calling format with without any arguments.")
{
auto result = kstd::format(fmt);
THEN("the result is the unmodified string")
{
REQUIRE(result == "This is a test");
}
}
WHEN("calling format with additional arguments")
{
auto result = kstd::format(fmt, 1, 2, 3);
THEN("the result is the unmodified string")
{
REQUIRE(result == "This is a test");
}
}
}
GIVEN("a format string with placeholders")
{
auto const & fmt = "Here are some placeholders: {} {} {}";
WHEN("calling format with the same number of arguments as there are placeholders")
{
auto result = kstd::format(fmt, 1, true, 'a');
THEN("the result is the formatted string")
{
REQUIRE(result == "Here are some placeholders: 1 true a");
}
}
WHEN("calling format with too many arguments")
{
auto result = kstd::format(fmt, 2, false, 'b', 4, 5, 6);
THEN("the result is the formatted string")
{
REQUIRE(result == "Here are some placeholders: 2 false b");
}
}
}
}
SCENARIO("Formatting to an output iterator", "[format]")
{
auto buffer = std::ostringstream{};
GIVEN("a format string without any placeholders")
{
auto const & fmt = "This is a test";
WHEN("calling format with without any arguments.")
{
kstd::format_to(std::ostream_iterator<char>{buffer}, fmt);
THEN("the unmodified string is written to the iterator")
{
REQUIRE(buffer.str() == "This is a test");
}
}
WHEN("calling format with additional arguments")
{
kstd::format_to(std::ostream_iterator<char>{buffer}, fmt, 1, 2, 3);
THEN("the unmodified string is written to the iterator")
{
REQUIRE(buffer.str() == "This is a test");
}
}
}
GIVEN("a format string with placeholders")
{
auto const & fmt = "Here are some placeholders: {} {} {}";
WHEN("calling format with the same number of arguments as there are placeholders")
{
kstd::format_to(std::ostream_iterator<char>{buffer}, fmt, 1, true, -100);
THEN("the formatted string is written to the iterator")
{
REQUIRE(buffer.str() == "Here are some placeholders: 1 true -100");
}
}
WHEN("calling format with too many arguments")
{
kstd::format_to(std::ostream_iterator<char>{buffer}, fmt, 2, false, -200, 4, 5, 6);
THEN("the formatted string is written to the iterator")
{
REQUIRE(buffer.str() == "Here are some placeholders: 2 false -200");
}
}
}
}
|