| 
									
										
										
										
											2019-06-08 06:50:03 -07:00
										 |  |  | // Formatting library for C++ - scanning API test
 | 
					
						
							| 
									
										
										
										
											2019-05-15 10:02:40 -07:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2019-06-08 06:50:03 -07:00
										 |  |  | // Copyright (c) 2019 - present, Victor Zverovich
 | 
					
						
							| 
									
										
										
										
											2019-05-15 10:02:40 -07:00
										 |  |  | // All rights reserved.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // For the license information refer to format.h.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-07 15:59:46 -07:00
										 |  |  | #include "scan.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-08 06:50:03 -07:00
										 |  |  | #include <time.h>
 | 
					
						
							| 
									
										
										
										
											2020-05-07 15:59:46 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-15 10:20:51 -07:00
										 |  |  | #include <climits>
 | 
					
						
							| 
									
										
										
										
											2019-05-15 10:02:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-29 01:59:43 +03:00
										 |  |  | #include "gmock/gmock.h"
 | 
					
						
							| 
									
										
										
										
											2019-05-15 11:05:19 -07:00
										 |  |  | #include "gtest-extra.h"
 | 
					
						
							| 
									
										
										
										
											2019-05-15 10:02:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 17:43:06 -07:00
										 |  |  | TEST(scan_test, read_text) { | 
					
						
							|  |  |  |   auto s = fmt::string_view("foo"); | 
					
						
							| 
									
										
										
										
											2019-05-19 07:02:41 -07:00
										 |  |  |   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-05 17:43:06 -07:00
										 |  |  | TEST(scan_test, read_int) { | 
					
						
							|  |  |  |   auto n = int(); | 
					
						
							| 
									
										
										
										
											2019-05-15 10:02:40 -07:00
										 |  |  |   fmt::scan("42", "{}", n); | 
					
						
							|  |  |  |   EXPECT_EQ(n, 42); | 
					
						
							| 
									
										
										
										
											2019-05-19 07:02:41 -07:00
										 |  |  |   fmt::scan("-42", "{}", n); | 
					
						
							|  |  |  |   EXPECT_EQ(n, -42); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 17:43:06 -07:00
										 |  |  | TEST(scan_test, read_longlong) { | 
					
						
							| 
									
										
										
										
											2019-05-19 07:02:41 -07:00
										 |  |  |   long long n = 0; | 
					
						
							|  |  |  |   fmt::scan("42", "{}", n); | 
					
						
							|  |  |  |   EXPECT_EQ(n, 42); | 
					
						
							|  |  |  |   fmt::scan("-42", "{}", n); | 
					
						
							|  |  |  |   EXPECT_EQ(n, -42); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 17:43:06 -07:00
										 |  |  | TEST(scan_test, read_uint) { | 
					
						
							|  |  |  |   auto n = unsigned(); | 
					
						
							| 
									
										
										
										
											2019-05-19 07:02:41 -07:00
										 |  |  |   fmt::scan("42", "{}", n); | 
					
						
							|  |  |  |   EXPECT_EQ(n, 42); | 
					
						
							|  |  |  |   EXPECT_THROW_MSG(fmt::scan("-42", "{}", n), fmt::format_error, | 
					
						
							|  |  |  |                    "invalid input"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 17:43:06 -07:00
										 |  |  | TEST(scan_test, read_ulonglong) { | 
					
						
							| 
									
										
										
										
											2019-05-19 07:02:41 -07:00
										 |  |  |   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 10:02:40 -07:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-06-02 11:11:28 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 17:43:06 -07:00
										 |  |  | TEST(scan_test, read_string) { | 
					
						
							|  |  |  |   auto s = std::string(); | 
					
						
							| 
									
										
										
										
											2019-06-02 07:30:26 -07:00
										 |  |  |   fmt::scan("foo", "{}", s); | 
					
						
							|  |  |  |   EXPECT_EQ(s, "foo"); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-05-15 11:05:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 17:43:06 -07:00
										 |  |  | TEST(scan_test, read_string_view) { | 
					
						
							|  |  |  |   auto s = fmt::string_view(); | 
					
						
							| 
									
										
										
										
											2019-06-02 11:11:28 -07:00
										 |  |  |   fmt::scan("foo", "{}", s); | 
					
						
							|  |  |  |   EXPECT_EQ(s, "foo"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-26 16:42:02 +05:00
										 |  |  | #ifdef FMT_HAVE_STRPTIME
 | 
					
						
							| 
									
										
										
										
											2019-06-08 06:50:03 -07:00
										 |  |  | namespace fmt { | 
					
						
							|  |  |  | template <> struct scanner<tm> { | 
					
						
							|  |  |  |   std::string format; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   scan_parse_context::iterator parse(scan_parse_context& ctx) { | 
					
						
							|  |  |  |     auto it = ctx.begin(); | 
					
						
							|  |  |  |     if (it != ctx.end() && *it == ':') ++it; | 
					
						
							|  |  |  |     auto end = it; | 
					
						
							|  |  |  |     while (end != ctx.end() && *end != '}') ++end; | 
					
						
							| 
									
										
										
										
											2020-05-10 07:25:42 -07:00
										 |  |  |     format.reserve(detail::to_unsigned(end - it + 1)); | 
					
						
							| 
									
										
										
										
											2019-06-08 06:50:03 -07:00
										 |  |  |     format.append(it, end); | 
					
						
							|  |  |  |     format.push_back('\0'); | 
					
						
							|  |  |  |     return end; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   template <class ScanContext> | 
					
						
							|  |  |  |   typename ScanContext::iterator scan(tm& t, ScanContext& ctx) { | 
					
						
							|  |  |  |     auto result = strptime(ctx.begin(), format.c_str(), &t); | 
					
						
							|  |  |  |     if (!result) throw format_error("failed to parse time"); | 
					
						
							|  |  |  |     return result; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | }  // namespace fmt
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 17:43:06 -07:00
										 |  |  | TEST(scan_test, read_custom) { | 
					
						
							|  |  |  |   auto input = "Date: 1985-10-25"; | 
					
						
							| 
									
										
										
										
											2019-06-08 06:50:03 -07:00
										 |  |  |   auto t = tm(); | 
					
						
							|  |  |  |   fmt::scan(input, "Date: {0:%Y-%m-%d}", t); | 
					
						
							|  |  |  |   EXPECT_EQ(t.tm_year, 85); | 
					
						
							|  |  |  |   EXPECT_EQ(t.tm_mon, 9); | 
					
						
							|  |  |  |   EXPECT_EQ(t.tm_mday, 25); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 17:43:06 -07:00
										 |  |  | TEST(scan_test, invalid_format) { | 
					
						
							| 
									
										
										
										
											2019-05-15 11:05:19 -07: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 07:30:26 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 17:43:06 -07:00
										 |  |  | TEST(scan_test, example) { | 
					
						
							|  |  |  |   auto key = std::string(); | 
					
						
							|  |  |  |   auto value = int(); | 
					
						
							| 
									
										
										
										
											2019-06-02 07:30:26 -07:00
										 |  |  |   fmt::scan("answer = 42", "{} = {}", key, value); | 
					
						
							|  |  |  |   EXPECT_EQ(key, "answer"); | 
					
						
							|  |  |  |   EXPECT_EQ(value, 42); | 
					
						
							|  |  |  | } |