Commits

Anonymous committed 81b0316

Added test cases for string formatting.

Comments (0)

Files changed (3)

neglect/cpplib/src/format.cpp

         switch (c = fmt[i++]) {
         case '\\':
             if (fmt[i] == '{' || fmt[i] == '}')
-                i++;
+                ss << fmt[i++];
             else
                 ss << '\\';
             break;

neglect/tests/CMakeLists.txt

 set(SOURCES
     src/main.cpp
     src/test_vectors.cpp
+    src/test_format.cpp
 )
 set(HEADERS
 )
 	${CMAKE_CURRENT_SOURCE_DIR}/../clib/include
 	${Boost_INCLUDE_DIRS}
 )
+link_directories(${CMAKE_CURRENT_SOURCE_DIR}/../neglect)
 
 add_executable(neglect_tests ${SOURCES} ${HEADERS})
+target_link_libraries(neglect_tests neglect)
 
 enable_testing()
 add_test(neglect_tests ${CMAKE_CURRENT_BINARY_DIR}/neglect_tests)

neglect/tests/src/test_format.cpp

+#include <neglect/unittest.hpp>
+#include <neglect/format.hpp>
+
+
+using namespace neglect;
+
+NUT_TESTGROUP(string_format) {
+
+    NUT_TESTCASE(primitive_formatting)
+    {
+        NUT_CHECK_EQUAL(format("Hello {}!", "World"), "Hello World!");
+        NUT_CHECK_EQUAL(format("foo={}", 42), "foo=42");
+        NUT_CHECK_EQUAL(format("foo={}", 42.0f), "foo=42");
+        NUT_CHECK_EQUAL(format("foo={}", true), "foo=true");
+    }
+
+    NUT_TESTCASE(multiple_arguments)
+    {
+        NUT_CHECK_EQUAL(format("Hello {}! The weather is {}.", "World", "good"),
+                        "Hello World! The weather is good.");
+        NUT_CHECK_EQUAL(format("Hello {0}! The weather is {1}.", "World", "good"),
+                        "Hello World! The weather is good.");
+        NUT_CHECK_EQUAL(format("Hello {1}! The weather is {0}.", "good", "World"),
+                        "Hello World! The weather is good.");
+    }
+
+    NUT_TESTCASE(escaping)
+    {
+        NUT_CHECK_EQUAL(format("\\begin\\{verbatim}{}", 0), "\\begin{verbatim}0");
+    }
+
+    NUT_TESTCASE(number_formatting)
+    {
+        NUT_CHECK_EQUAL(format("Value: {:05}", 23), "Value: 00023");
+        NUT_CHECK_EQUAL(format("{: 5}", 42), "   42");
+        NUT_CHECK_EQUAL(format("{:.2}", 42.1337), "42.13");
+    }
+}