Facebook
From aaaaaaaaaaaa, 2 Weeks ago, written in C++.
This paste is a reply to template specialization from aaaaaaaaaaaa - view diff
Embed
Download Paste or View Raw
Hits: 47
  1. //CastString.h
  2.  
  3. #ifndef __CAST_STRING_H
  4. #define __CAST_STRING_H
  5.  
  6. #include <string>
  7.  
  8. namespace Strings
  9. {
  10.  
  11. template<typename T>
  12. class CastString
  13. {
  14. public:
  15.     CastString();
  16.  
  17.     T cast(const std::string& value);
  18. };
  19.  
  20. void test(int i);
  21.  
  22. }
  23.  
  24. #endif
  25.  
  26. // CastString.cpp
  27.  
  28. #include "CastString.h"
  29.  
  30. namespace Strings
  31. {
  32.  
  33. template<>
  34. class CastString <int>
  35. {
  36. public:
  37.     CastString() {}
  38.  
  39.     int cast(const std::string& value){return std::stoi(value);}
  40. };
  41.  
  42. template<>
  43. class CastString <std::string>
  44. {
  45. public:
  46.     CastString() {}
  47.  
  48.     std::string cast(const std::string& value){return value;}
  49. };
  50.  
  51. void test(int i)
  52. {
  53.     // dziaĹ‚a dopiero po odkomentowaniu tego
  54.     // CastString<int>().cast("1234");
  55.     // CastString<std::string>().cast("1234");
  56. }
  57.  
  58. } // namespace Strings
  59.  
  60.  
  61. // CastStringTests.cpp
  62.  
  63. #include <gmock/gmock.h>
  64.  
  65. #include "CastString.h"
  66.  
  67. using namespace testing;
  68.  
  69. class CastStringsTests : public testing::Test
  70. {
  71. };
  72.  
  73. TEST_F(CastStringsTests, returnsTheSameStringWhenCastingToString)
  74. {
  75.     auto text = "some text";
  76.     auto result = Strings::CastString<std::string>().cast(text);
  77.  
  78.     ASSERT_STREQ(text, result.c_str());
  79. }
  80.  
  81. TEST_F(CastStringsTests, castsStringToInt)
  82. {
  83.     auto text = "12345";
  84.     auto result = Strings::CastString<int>().cast(text);
  85.  
  86.     ASSERT_EQ(result, 12345);
  87. }
  88.