fmt/test/scan-test.cc

133 lines
3.2 KiB
C++
Raw Normal View History

// Formatting library for C++ - scanning API test
2019-05-15 17:02:40 +00:00
//
// Copyright (c) 2019 - present, Victor Zverovich
2019-05-15 17:02:40 +00:00
// All rights reserved.
//
// For the license information refer to format.h.
2020-05-07 22:59:46 +00:00
#include "scan.h"
#include <time.h>
2020-05-07 22:59:46 +00:00
2019-05-15 17:20:51 +00:00
#include <climits>
2019-05-15 17:02:40 +00:00
2023-12-02 17:34:27 +00:00
#include "fmt/os.h"
#include "gmock/gmock.h"
2019-05-15 18:05:19 +00:00
#include "gtest-extra.h"
2019-05-15 17:02:40 +00:00
2021-05-06 00:43:06 +00:00
TEST(scan_test, read_text) {
2023-12-02 17:34:27 +00:00
fmt::string_view s = "foo";
auto end = fmt::scan(s, "foo");
EXPECT_EQ(end, s.end());
EXPECT_THROW_MSG(fmt::scan("fob", "foo"), fmt::format_error, "invalid input");
}
2021-05-06 00:43:06 +00:00
TEST(scan_test, read_int) {
2023-12-02 17:34:27 +00:00
int n = 0;
2019-05-15 17:02:40 +00:00
fmt::scan("42", "{}", n);
EXPECT_EQ(n, 42);
fmt::scan("-42", "{}", n);
EXPECT_EQ(n, -42);
}
2021-05-06 00:43:06 +00:00
TEST(scan_test, read_longlong) {
long long n = 0;
fmt::scan("42", "{}", n);
EXPECT_EQ(n, 42);
fmt::scan("-42", "{}", n);
EXPECT_EQ(n, -42);
}
2021-05-06 00:43:06 +00:00
TEST(scan_test, read_uint) {
2023-12-02 17:34:27 +00:00
unsigned n = 0;
fmt::scan("42", "{}", n);
EXPECT_EQ(n, 42);
EXPECT_THROW_MSG(fmt::scan("-42", "{}", n), fmt::format_error,
"invalid input");
}
2021-05-06 00:43:06 +00:00
TEST(scan_test, read_ulonglong) {
unsigned long long n = 0;
fmt::scan("42", "{}", n);
EXPECT_EQ(n, 42);
EXPECT_THROW_MSG(fmt::scan("-42", "{}", n), fmt::format_error,
"invalid input");
2019-05-15 17:02:40 +00:00
}
2019-06-02 18:11:28 +00:00
2021-05-06 00:43:06 +00:00
TEST(scan_test, read_string) {
2023-12-02 17:34:27 +00:00
std::string s;
2019-06-02 14:30:26 +00:00
fmt::scan("foo", "{}", s);
EXPECT_EQ(s, "foo");
}
2019-05-15 18:05:19 +00:00
2021-05-06 00:43:06 +00:00
TEST(scan_test, read_string_view) {
2023-12-02 17:34:27 +00:00
fmt::string_view s;
2019-06-02 18:11:28 +00:00
fmt::scan("foo", "{}", s);
EXPECT_EQ(s, "foo");
}
#ifdef FMT_HAVE_STRPTIME
namespace fmt {
template <> struct scanner<tm> {
std::string format;
2023-11-25 15:41:04 +00:00
auto parse(scan_parse_context& ctx) -> scan_parse_context::iterator {
auto it = ctx.begin();
if (it != ctx.end() && *it == ':') ++it;
auto end = it;
while (end != ctx.end() && *end != '}') ++end;
2020-05-10 14:25:42 +00:00
format.reserve(detail::to_unsigned(end - it + 1));
format.append(it, end);
format.push_back('\0');
return end;
}
template <class ScanContext>
2023-12-02 17:34:27 +00:00
auto scan(tm&, ScanContext& ctx) const -> typename ScanContext::iterator {
// TODO: replace strptime with get_time
// auto result = strptime(ctx.begin(), format.c_str(), &t);
// if (!result) throw format_error("failed to parse time");
// return result;
return ctx.begin();
}
};
} // namespace fmt
2021-05-06 00:43:06 +00:00
TEST(scan_test, read_custom) {
2023-12-02 17:34:27 +00:00
/*auto input = "Date: 1985-10-25";
auto t = tm();
fmt::scan(input, "Date: {0:%Y-%m-%d}", t);
EXPECT_EQ(t.tm_year, 85);
EXPECT_EQ(t.tm_mon, 9);
2023-12-02 17:34:27 +00:00
EXPECT_EQ(t.tm_mday, 25);*/
}
#endif
2021-05-06 00:43:06 +00:00
TEST(scan_test, invalid_format) {
2019-05-15 18:05:19 +00:00
EXPECT_THROW_MSG(fmt::scan("", "{}"), fmt::format_error,
"argument index out of range");
EXPECT_THROW_MSG(fmt::scan("", "{"), fmt::format_error,
"invalid format string");
}
2019-06-02 14:30:26 +00:00
2021-05-06 00:43:06 +00:00
TEST(scan_test, example) {
auto key = std::string();
auto value = int();
2019-06-02 14:30:26 +00:00
fmt::scan("answer = 42", "{} = {}", key, value);
EXPECT_EQ(key, "answer");
EXPECT_EQ(value, 42);
}
2023-11-26 17:22:31 +00:00
#if FMT_USE_FCNTL
TEST(scan_test, file) {
fmt::file read_end, write_end;
fmt::file::pipe(read_end, write_end);
fmt::string_view input = "4";
write_end.write(input.data(), input.size());
write_end.close();
int value = 0;
fmt::scan(read_end.fdopen("r").get(), "{}", value);
EXPECT_EQ(value, 4);
}
#endif // FMT_USE_FCNTL