| 
									
										
										
										
											2016-04-20 11:57:49 +03:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2018-03-09 15:26:33 +02:00
										 |  |  |  * This content is released under the MIT License as specified in https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE
 | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2016-04-20 11:57:49 +03:00
										 |  |  | #include "includes.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 12:12:34 +01:00
										 |  |  | using spdlog::details::file_helper; | 
					
						
							| 
									
										
										
										
											2018-03-09 15:26:33 +02:00
										 |  |  | using spdlog::details::log_msg; | 
					
						
							| 
									
										
										
										
											2016-04-20 11:57:49 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | static const std::string target_filename = "logs/file_helper_test.txt"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void write_with_helper(file_helper &helper, size_t howmany) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     log_msg msg; | 
					
						
							| 
									
										
										
										
											2018-06-24 01:32:39 +03:00
										 |  |  |     fmt::memory_buffer formatted; | 
					
						
							|  |  |  |     fmt::format_to(formatted, "{}", std::string(howmany, '1')); | 
					
						
							|  |  |  |     helper.write(formatted); | 
					
						
							| 
									
										
										
										
											2016-10-12 23:08:44 +03:00
										 |  |  |     helper.flush(); | 
					
						
							| 
									
										
										
										
											2016-04-20 11:57:49 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_CASE("file_helper_filename", "[file_helper::filename()]]") | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     prepare_logdir(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-18 00:43:42 +03:00
										 |  |  |     file_helper helper; | 
					
						
							| 
									
										
										
										
											2016-04-20 11:57:49 +03:00
										 |  |  |     helper.open(target_filename); | 
					
						
							|  |  |  |     REQUIRE(helper.filename() == target_filename); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_CASE("file_helper_size", "[file_helper::size()]]") | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     prepare_logdir(); | 
					
						
							| 
									
										
										
										
											2016-07-15 18:41:59 +03:00
										 |  |  |     size_t expected_size = 123; | 
					
						
							| 
									
										
										
										
											2016-04-20 11:57:49 +03:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-09-18 00:43:42 +03:00
										 |  |  |         file_helper helper; | 
					
						
							| 
									
										
										
										
											2016-04-20 11:57:49 +03:00
										 |  |  |         helper.open(target_filename); | 
					
						
							|  |  |  |         write_with_helper(helper, expected_size); | 
					
						
							| 
									
										
										
										
											2016-07-15 18:41:59 +03:00
										 |  |  |         REQUIRE(static_cast<size_t>(helper.size()) == expected_size); | 
					
						
							| 
									
										
										
										
											2016-04-20 11:57:49 +03:00
										 |  |  |     } | 
					
						
							|  |  |  |     REQUIRE(get_filesize(target_filename) == expected_size); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_CASE("file_helper_exists", "[file_helper::file_exists()]]") | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     prepare_logdir(); | 
					
						
							|  |  |  |     REQUIRE(!file_helper::file_exists(target_filename)); | 
					
						
							| 
									
										
										
										
											2016-10-12 23:08:44 +03:00
										 |  |  |     file_helper helper; | 
					
						
							| 
									
										
										
										
											2016-04-20 11:57:49 +03:00
										 |  |  |     helper.open(target_filename); | 
					
						
							|  |  |  |     REQUIRE(file_helper::file_exists(target_filename)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_CASE("file_helper_reopen", "[file_helper::reopen()]]") | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     prepare_logdir(); | 
					
						
							| 
									
										
										
										
											2016-09-18 00:43:42 +03:00
										 |  |  |     file_helper helper; | 
					
						
							| 
									
										
										
										
											2016-04-20 11:57:49 +03:00
										 |  |  |     helper.open(target_filename); | 
					
						
							|  |  |  |     write_with_helper(helper, 12); | 
					
						
							|  |  |  |     REQUIRE(helper.size() == 12); | 
					
						
							|  |  |  |     helper.reopen(true); | 
					
						
							|  |  |  |     REQUIRE(helper.size() == 0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_CASE("file_helper_reopen2", "[file_helper::reopen(false)]]") | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     prepare_logdir(); | 
					
						
							| 
									
										
										
										
											2016-07-30 19:32:51 +03:00
										 |  |  |     size_t expected_size = 14; | 
					
						
							| 
									
										
										
										
											2016-10-12 23:08:44 +03:00
										 |  |  |     file_helper helper; | 
					
						
							| 
									
										
										
										
											2016-04-20 11:57:49 +03:00
										 |  |  |     helper.open(target_filename); | 
					
						
							|  |  |  |     write_with_helper(helper, expected_size); | 
					
						
							|  |  |  |     REQUIRE(helper.size() == expected_size); | 
					
						
							|  |  |  |     helper.reopen(false); | 
					
						
							|  |  |  |     REQUIRE(helper.size() == expected_size); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-09 15:26:33 +02:00
										 |  |  | static void test_split_ext(const char *fname, const char *expect_base, const char *expect_ext) | 
					
						
							| 
									
										
										
										
											2017-12-22 18:55:19 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     spdlog::filename_t filename(fname); | 
					
						
							|  |  |  |     spdlog::filename_t expected_base(expect_base); | 
					
						
							|  |  |  |     spdlog::filename_t expected_ext(expect_ext); | 
					
						
							| 
									
										
										
										
											2017-12-01 03:40:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-22 18:37:51 +02:00
										 |  |  | #ifdef _WIN32 // replace folder sep
 | 
					
						
							| 
									
										
										
										
											2017-12-22 18:55:19 +02:00
										 |  |  |     std::replace(filename.begin(), filename.end(), '/', '\\'); | 
					
						
							|  |  |  |     std::replace(expected_base.begin(), expected_base.end(), '/', '\\'); | 
					
						
							| 
									
										
										
										
											2017-12-01 03:40:49 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2017-12-22 18:55:19 +02:00
										 |  |  |     spdlog::filename_t basename, ext; | 
					
						
							|  |  |  |     std::tie(basename, ext) = file_helper::split_by_extenstion(filename); | 
					
						
							|  |  |  |     REQUIRE(basename == expected_base); | 
					
						
							|  |  |  |     REQUIRE(ext == expected_ext); | 
					
						
							| 
									
										
										
										
											2017-12-01 03:40:49 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-22 18:37:51 +02:00
										 |  |  | TEST_CASE("file_helper_split_by_extenstion", "[file_helper::split_by_extenstion()]]") | 
					
						
							| 
									
										
										
										
											2017-12-22 18:55:19 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     test_split_ext("mylog.txt", "mylog", ".txt"); | 
					
						
							|  |  |  |     test_split_ext(".mylog.txt", ".mylog", ".txt"); | 
					
						
							|  |  |  |     test_split_ext(".mylog", ".mylog", ""); | 
					
						
							|  |  |  |     test_split_ext("/aaa/bb.d/mylog", "/aaa/bb.d/mylog", ""); | 
					
						
							|  |  |  |     test_split_ext("/aaa/bb.d/mylog.txt", "/aaa/bb.d/mylog", ".txt"); | 
					
						
							|  |  |  |     test_split_ext("aaa/bbb/ccc/mylog.txt", "aaa/bbb/ccc/mylog", ".txt"); | 
					
						
							|  |  |  |     test_split_ext("aaa/bbb/ccc/mylog.", "aaa/bbb/ccc/mylog.", ""); | 
					
						
							|  |  |  |     test_split_ext("aaa/bbb/ccc/.mylog.txt", "aaa/bbb/ccc/.mylog", ".txt"); | 
					
						
							|  |  |  |     test_split_ext("/aaa/bbb/ccc/mylog.txt", "/aaa/bbb/ccc/mylog", ".txt"); | 
					
						
							|  |  |  |     test_split_ext("/aaa/bbb/ccc/.mylog", "/aaa/bbb/ccc/.mylog", ""); | 
					
						
							|  |  |  |     test_split_ext("../mylog.txt", "../mylog", ".txt"); | 
					
						
							|  |  |  |     test_split_ext(".././mylog.txt", ".././mylog", ".txt"); | 
					
						
							|  |  |  |     test_split_ext(".././mylog.txt/xxx", ".././mylog.txt/xxx", ""); | 
					
						
							|  |  |  |     test_split_ext("/mylog.txt", "/mylog", ".txt"); | 
					
						
							|  |  |  |     test_split_ext("//mylog.txt", "//mylog", ".txt"); | 
					
						
							|  |  |  |     test_split_ext("", "", ""); | 
					
						
							|  |  |  |     test_split_ext(".", ".", ""); | 
					
						
							|  |  |  |     test_split_ext("..txt", ".", ".txt"); | 
					
						
							| 
									
										
										
										
											2017-12-22 17:52:50 +08:00
										 |  |  | } |