فهرست منبع

Start making tests for format.cxx

Sam Jaffe 6 سال پیش
والد
کامیت
9f2a4dccda
4فایلهای تغییر یافته به همراه61 افزوده شده و 12 حذف شده
  1. 3 1
      include/logger/format.h
  2. 4 0
      logger.xcodeproj/project.pbxproj
  3. 20 11
      src/format.cxx
  4. 34 0
      test/format_test.cxx

+ 3 - 1
include/logger/format.h

@@ -23,8 +23,10 @@ namespace logging {
     
     using generator = std::function<void(logpacket const &, std::ostream &)>;
     
+    static format parse_format_string(std::string const &);
+    void process(logpacket const & pkt, std::ostream & os) const;
+    std::string process(logpacket const & pkt) const;
   private:
-    static format parse_format_string(std::string const &);    
     std::vector<generator> gen;
   };
   

+ 4 - 0
logger.xcodeproj/project.pbxproj

@@ -15,6 +15,7 @@
 		CD1CDE9222543E7E00E5B6B2 /* test_properties.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD1CDE9122543E7E00E5B6B2 /* test_properties.cxx */; };
 		CD1CDEAF22556B7E00E5B6B2 /* logger_impl.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD1CDEAE22556B7E00E5B6B2 /* logger_impl.cxx */; };
 		CD1CDEB122557FB600E5B6B2 /* default_layout.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD1CDEB022557FB600E5B6B2 /* default_layout.cxx */; };
+		CD1CDEB32256B04600E5B6B2 /* format_test.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD1CDEB22256B04600E5B6B2 /* format_test.cxx */; };
 		CD29739B1D7B401F00E37217 /* logger.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD2973991D7B401F00E37217 /* logger.cxx */; };
 		CD3C80C01D6A2CA300ACC795 /* format.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD3C80BE1D6A2CA300ACC795 /* format.cxx */; };
 		CD6F73EC225187BE0081ED74 /* logger in Headers */ = {isa = PBXBuildFile; fileRef = CD6F73EA225187A10081ED74 /* logger */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -76,6 +77,7 @@
 		CD1CDE9122543E7E00E5B6B2 /* test_properties.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = test_properties.cxx; sourceTree = "<group>"; };
 		CD1CDEAE22556B7E00E5B6B2 /* logger_impl.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = logger_impl.cxx; sourceTree = "<group>"; };
 		CD1CDEB022557FB600E5B6B2 /* default_layout.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = default_layout.cxx; sourceTree = "<group>"; };
+		CD1CDEB22256B04600E5B6B2 /* format_test.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = format_test.cxx; sourceTree = "<group>"; };
 		CD2973991D7B401F00E37217 /* logger.cxx */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = logger.cxx; sourceTree = "<group>"; };
 		CD3C80BE1D6A2CA300ACC795 /* format.cxx */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = format.cxx; sourceTree = "<group>"; };
 		CD6F73EA225187A10081ED74 /* logger */ = {isa = PBXFileReference; lastKnownFileType = folder; name = logger; path = include/logger; sourceTree = "<group>"; };
@@ -163,6 +165,7 @@
 			isa = PBXGroup;
 			children = (
 				CD6F73FC225187E10081ED74 /* logger_test.cxx */,
+				CD1CDEB22256B04600E5B6B2 /* format_test.cxx */,
 				CD1CDE8C22540D9B00E5B6B2 /* c_logger_test.cxx */,
 				CD1CDE8F22542CC500E5B6B2 /* log_manager_test.cxx */,
 				CD1CDE9122543E7E00E5B6B2 /* test_properties.cxx */,
@@ -357,6 +360,7 @@
 				CD1CDE9022542CC500E5B6B2 /* log_manager_test.cxx in Sources */,
 				CD6F740C225187FD0081ED74 /* logger_test.cxx in Sources */,
 				CD1CDE9222543E7E00E5B6B2 /* test_properties.cxx in Sources */,
+				CD1CDEB32256B04600E5B6B2 /* format_test.cxx in Sources */,
 				CD1CDE8D22540D9B00E5B6B2 /* c_logger_test.cxx in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;

+ 20 - 11
src/format.cxx

@@ -158,40 +158,49 @@ namespace logging {
     char const * next = nullptr;
     char const * const end = curr + fmt.size();
     
-    while ( ( next = std::strchr( curr, '%' ) + 1 )
-           != nullptr ) {
-      if ( end == next ) {
+    while ((next = std::strchr(curr, '%')) != nullptr) {
+      ++next;
+      if (end == next) {
         std::string error_msg{"expected format specifier, got end of string"};
         throw format_parsing_exception{error_msg}; // TODO
       }
       
-      if ( curr < next -  1 ) {
+      if (curr < next-1) {
         out.gen.push_back(convert(string_token({curr, next - 1})));
       }
 
-      if ( is('d') ) {
+      if (is('d')) {
         ++next;
-        if ( is('{') ) curr = std::strchr(next, '}');
+        if (is('{')) curr = std::strchr(next, '}');
         out.gen.push_back(convert(date_token(next)));
-      } else if ( is('n') ) {
+      } else if (is('n')) {
         out.gen.push_back(convert(string_token(NEWLINE)));
-      } else if ( is('%') ) {
+      } else if (is('%')) {
         ++next;
         out.gen.push_back(convert(string_token("%")));
-      } else if ( is('.') || is('-') ||
-                 isdigit( *next ) ) {
+      } else if (is('.') || is('-') || isdigit( *next )) {
         parse_with_bounds( next );
       } else {
         handle( next );
       }
       curr = ++next;
     }
-    if ( curr < end ) {
+    if (curr < end) {
       out.gen.push_back(convert(string_token({curr, end})));
     }
     return out;
   }
 #undef is
+  
+  void format::process(logpacket const & pkt, std::ostream & os) const {
+    for (auto func : gen) { func(pkt, os); }
+  }
+  
+  std::string format::process(logpacket const & pkt) const {
+    std::stringstream ss;
+    process(pkt, ss);
+    return ss.str();
+  }
 }
 
 namespace logging {

+ 34 - 0
test/format_test.cxx

@@ -0,0 +1,34 @@
+//
+//  format_test.cxx
+//  logger_test
+//
+//  Created by Sam Jaffe on 4/4/19.
+//
+
+#include <gmock/gmock.h>
+
+#include "logger/exception.h"
+#include "logger/format.h"
+
+using namespace logging;
+
+TEST(FormatTest, EmptyFormatterCanParse) {
+  EXPECT_NO_THROW(format::parse_format_string(""));
+}
+
+TEST(FormatTest, ThrowsForEndOfStringAfterPct) {
+  EXPECT_THROW(format::parse_format_string("%"),
+               format_parsing_exception);
+}
+
+TEST(FormatTest, RawStringFmtReturnsSelf) {
+  using testing::Eq;
+  auto fmt = format::parse_format_string("TEST STRING");
+  EXPECT_THAT(fmt.process({}), Eq("TEST STRING"));
+}
+
+TEST(FormatTest, NCharReturnsNewLine) {
+  using testing::Eq;
+  auto fmt = format::parse_format_string("%n");
+  EXPECT_THAT(fmt.process({}), Eq("\n"));
+}