added pattern formatter tests
This commit is contained in:
		@@ -39,7 +39,6 @@ TEST_CASE("basic_logging ", "[basic_logging]")
 | 
				
			|||||||
    //REQUIRE(log_info(some_logged_class("some_val")) == "some_val");
 | 
					    //REQUIRE(log_info(some_logged_class("some_val")) == "some_val");
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
TEST_CASE("log_levels", "[log_levels]")
 | 
					TEST_CASE("log_levels", "[log_levels]")
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    REQUIRE(log_info("Hello", spdlog::level::err) == "");
 | 
					    REQUIRE(log_info("Hello", spdlog::level::err) == "");
 | 
				
			||||||
@@ -54,3 +53,6 @@ TEST_CASE("log_levels", "[log_levels]")
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
							
								
								
									
										68
									
								
								tests/test_pattern_formatter.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										68
									
								
								tests/test_pattern_formatter.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,68 @@
 | 
				
			|||||||
 | 
					#include "includes.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// log to str and return it
 | 
				
			||||||
 | 
					static std::string log_to_str(const std::string& msg, std::shared_ptr<spdlog::formatter> formatter = nullptr)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						std::ostringstream oss;
 | 
				
			||||||
 | 
						auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
 | 
				
			||||||
 | 
						spdlog::logger oss_logger("pattern_tester", oss_sink);
 | 
				
			||||||
 | 
						oss_logger.set_level(spdlog::level::info);
 | 
				
			||||||
 | 
						if (formatter) oss_logger.set_formatter(formatter);
 | 
				
			||||||
 | 
						oss_logger.info(msg);
 | 
				
			||||||
 | 
						return oss.str();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					TEST_CASE("custom eol", "[pattern_formatter]")
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						std::string msg = "Hello custom eol test";
 | 
				
			||||||
 | 
						std::string eol = ";)";
 | 
				
			||||||
 | 
						auto formatter = std::make_shared<spdlog::pattern_formatter>("%v", spdlog::pattern_time_type::local, ";)");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						REQUIRE(log_to_str(msg, formatter) == msg + eol);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					TEST_CASE("empty format", "[pattern_formatter]")
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						auto formatter = std::make_shared<spdlog::pattern_formatter>("", spdlog::pattern_time_type::local, "");
 | 
				
			||||||
 | 
						REQUIRE(log_to_str("Some message", formatter) == "");
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					TEST_CASE("empty format2", "[pattern_formatter]")
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						auto formatter = std::make_shared<spdlog::pattern_formatter>("", spdlog::pattern_time_type::local, "\n");
 | 
				
			||||||
 | 
						REQUIRE(log_to_str("Some message", formatter) == "\n");
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					TEST_CASE("level", "[pattern_formatter]")
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						auto formatter = std::make_shared<spdlog::pattern_formatter>("[%l] %v", spdlog::pattern_time_type::local, "\n");
 | 
				
			||||||
 | 
						REQUIRE(log_to_str("Some message", formatter) == "[info] Some message\n");
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					TEST_CASE("short level", "[pattern_formatter]")
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						auto formatter = std::make_shared<spdlog::pattern_formatter>("[%L] %v", spdlog::pattern_time_type::local, "\n");
 | 
				
			||||||
 | 
						REQUIRE(log_to_str("Some message", formatter) == "[I] Some message\n");
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					TEST_CASE("name", "[pattern_formatter]")
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						auto formatter = std::make_shared<spdlog::pattern_formatter>("[%n] %v", spdlog::pattern_time_type::local, "\n");
 | 
				
			||||||
 | 
						REQUIRE(log_to_str("Some message", formatter) == "[pattern_tester] Some message\n");
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					TEST_CASE("date MM/DD/YY ", "[pattern_formatter]")
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						using namespace::std::chrono;
 | 
				
			||||||
 | 
						auto formatter = std::make_shared<spdlog::pattern_formatter>("%D %v", spdlog::pattern_time_type::local, "\n");	
 | 
				
			||||||
 | 
						auto now_tm = spdlog::details::os::localtime();			
 | 
				
			||||||
 | 
						std::stringstream oss;	
 | 
				
			||||||
 | 
						oss << std::put_time(&now_tm, "%D") << " Some message\n";		
 | 
				
			||||||
 | 
						REQUIRE(log_to_str("Some message", formatter) == oss.str());
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -21,6 +21,7 @@
 | 
				
			|||||||
  <PropertyGroup Label="Globals">
 | 
					  <PropertyGroup Label="Globals">
 | 
				
			||||||
    <ProjectGuid>{59A07559-5F38-4DD6-A7FA-DB4153690B42}</ProjectGuid>
 | 
					    <ProjectGuid>{59A07559-5F38-4DD6-A7FA-DB4153690B42}</ProjectGuid>
 | 
				
			||||||
    <RootNamespace>tests</RootNamespace>
 | 
					    <RootNamespace>tests</RootNamespace>
 | 
				
			||||||
 | 
					    <WindowsTargetPlatformVersion>10.0.16299.0</WindowsTargetPlatformVersion>
 | 
				
			||||||
  </PropertyGroup>
 | 
					  </PropertyGroup>
 | 
				
			||||||
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
 | 
					  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
 | 
				
			||||||
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
 | 
					  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
 | 
				
			||||||
@@ -128,10 +129,11 @@
 | 
				
			|||||||
    <ClCompile Include="errors.cpp" />
 | 
					    <ClCompile Include="errors.cpp" />
 | 
				
			||||||
    <ClCompile Include="file_helper.cpp" />
 | 
					    <ClCompile Include="file_helper.cpp" />
 | 
				
			||||||
    <ClCompile Include="file_log.cpp" />
 | 
					    <ClCompile Include="file_log.cpp" />
 | 
				
			||||||
    <ClCompile Include="format.cpp" />
 | 
					    <ClCompile Include="test_misc.cpp" />
 | 
				
			||||||
    <ClCompile Include="main.cpp" />
 | 
					    <ClCompile Include="main.cpp" />
 | 
				
			||||||
    <ClCompile Include="registry.cpp" />
 | 
					    <ClCompile Include="registry.cpp" />
 | 
				
			||||||
    <ClCompile Include="test_macros.cpp" />
 | 
					    <ClCompile Include="test_macros.cpp" />
 | 
				
			||||||
 | 
					    <ClCompile Include="test_pattern_formatter.cpp" />
 | 
				
			||||||
    <ClCompile Include="utils.cpp" />
 | 
					    <ClCompile Include="utils.cpp" />
 | 
				
			||||||
  </ItemGroup>
 | 
					  </ItemGroup>
 | 
				
			||||||
  <ItemGroup>
 | 
					  <ItemGroup>
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -18,9 +18,6 @@
 | 
				
			|||||||
    <ClCompile Include="file_log.cpp">
 | 
					    <ClCompile Include="file_log.cpp">
 | 
				
			||||||
      <Filter>Source Files</Filter>
 | 
					      <Filter>Source Files</Filter>
 | 
				
			||||||
    </ClCompile>
 | 
					    </ClCompile>
 | 
				
			||||||
    <ClCompile Include="format.cpp">
 | 
					 | 
				
			||||||
      <Filter>Source Files</Filter>
 | 
					 | 
				
			||||||
    </ClCompile>
 | 
					 | 
				
			||||||
    <ClCompile Include="main.cpp">
 | 
					    <ClCompile Include="main.cpp">
 | 
				
			||||||
      <Filter>Source Files</Filter>
 | 
					      <Filter>Source Files</Filter>
 | 
				
			||||||
    </ClCompile>
 | 
					    </ClCompile>
 | 
				
			||||||
@@ -39,6 +36,12 @@
 | 
				
			|||||||
    <ClCompile Include="test_macros.cpp">
 | 
					    <ClCompile Include="test_macros.cpp">
 | 
				
			||||||
      <Filter>Source Files</Filter>
 | 
					      <Filter>Source Files</Filter>
 | 
				
			||||||
    </ClCompile>
 | 
					    </ClCompile>
 | 
				
			||||||
 | 
					    <ClCompile Include="test_pattern_formatter.cpp">
 | 
				
			||||||
 | 
					      <Filter>Source Files</Filter>
 | 
				
			||||||
 | 
					    </ClCompile>
 | 
				
			||||||
 | 
					    <ClCompile Include="test_misc.cpp">
 | 
				
			||||||
 | 
					      <Filter>Source Files</Filter>
 | 
				
			||||||
 | 
					    </ClCompile>
 | 
				
			||||||
  </ItemGroup>
 | 
					  </ItemGroup>
 | 
				
			||||||
  <ItemGroup>
 | 
					  <ItemGroup>
 | 
				
			||||||
    <ClInclude Include="includes.h">
 | 
					    <ClInclude Include="includes.h">
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user