Advertisement
ulfben

Greeter

Nov 23rd, 2022
609
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.39 KB | None | 0 0
  1. //
  2. // pch.h
  3. //
  4. #pragma once
  5. #include <codeanalysis\warnings.h>
  6. #pragma warning(push)
  7. #pragma warning(disable : ALL_CODE_ANALYSIS_WARNINGS)
  8. #include "gtest/gtest.h"
  9. #pragma warning(pop)
  10. #include <algorithm>
  11. #include <chrono>
  12. #include <iostream>
  13. #include <string>
  14. #include <string_view>
  15. //
  16. //
  17. //
  18.  
  19. //test.cpp
  20. #include "pch.h"
  21. using namespace std::chrono;
  22. using namespace std::string_view_literals;
  23. using namespace std::string_literals;
  24. using std::string_view;
  25. using TimeOfDay = int;
  26. constexpr string_view WHITE_SPACE = " \f\n\r\t\v"sv;
  27.  
  28. constexpr string_view trimLeft(string_view in,
  29.                                string_view delims = WHITE_SPACE) noexcept {
  30.   const auto countFromTheLeft = in.find_first_not_of(delims);
  31.   if (countFromTheLeft != in.npos) {
  32.     in.remove_prefix(countFromTheLeft);
  33.   }
  34.   return in;
  35. };
  36. constexpr string_view trimRight(string_view in,
  37.                                 string_view delims = WHITE_SPACE) noexcept {
  38.   const auto lastValidPos = in.find_last_not_of(delims);
  39.   const auto countFromTheRight = in.size() - lastValidPos - 1;
  40.   in.remove_suffix(countFromTheRight);
  41.   return in;
  42. };
  43. constexpr string_view trim(string_view in,
  44.                            string_view delims = WHITE_SPACE) noexcept {
  45.   return trimRight(trimLeft(in, delims), delims);
  46. }
  47.  
  48. constexpr std::string to_string(string_view s) { return std::string(s); }
  49.  
  50. constexpr std::string capitalize(std::string s) {
  51.   if (s.empty()) {
  52.     return s;
  53.   }
  54.   s[0] = static_cast<char>(std::toupper(static_cast<unsigned char>(s[0])));
  55.   return s;
  56. }
  57.  
  58. constexpr bool isBetween(TimeOfDay in, TimeOfDay min, TimeOfDay max) noexcept {
  59.   return in >= min && in <= max;
  60. }
  61. constexpr bool isMorning(TimeOfDay in) noexcept { return isBetween(in, 6, 12); }
  62. constexpr bool isEvening(TimeOfDay in) noexcept {
  63.   return isBetween(in, 18, 22);
  64. }
  65. constexpr bool isNight(TimeOfDay in) noexcept {
  66.   return isBetween(in, 22, 24) || isBetween(in, 0, 6);
  67. }
  68.  
  69. constexpr std::string_view getGreetingFor(TimeOfDay time) noexcept {
  70.   if (isMorning(time)) {
  71.     return "Good morning "sv;
  72.   }
  73.   if (isEvening(time)) {
  74.     return "Good evening "sv;
  75.   }
  76.   if (isNight(time)) {
  77.     return "Good night "sv;
  78.   }
  79.   return "Hello "sv;
  80. }
  81.  
  82. struct Greeter {
  83.   constexpr void time(TimeOfDay time) noexcept { _time = time; }
  84.   constexpr TimeOfDay time() const noexcept { return _time; }
  85.  
  86.   constexpr std::string greet(string_view name) {
  87.     const auto clean_name = to_string(trim(name));
  88.     const auto greeting = to_string(getGreetingFor(time()));
  89.     return greeting + capitalize(clean_name);
  90.   }
  91.  
  92. private:
  93.   TimeOfDay _time = TimeOfDay(14);
  94. };
  95.  
  96. TEST(Greeter, canReturnInput) {
  97.   Greeter g;
  98.   EXPECT_EQ(g.greet("Ulf"), "Hello Ulf");
  99. }
  100.  
  101. TEST(Greeter, canTrimInput) {
  102.   Greeter g;
  103.   EXPECT_EQ(g.greet("   Ulf    "), "Hello Ulf");
  104. }
  105.  
  106. TEST(Greeter, canCapitalizeFirst) {
  107.   Greeter g;
  108.   EXPECT_EQ(g.greet("   ulf    "), "Hello Ulf");
  109. }
  110.  
  111. TEST(Greeter, saysGoodMorningInTheMorning) {
  112.   Greeter g;
  113.   g.time(11);
  114.   EXPECT_EQ(g.greet("   ulf    "), "Good morning Ulf");
  115. }
  116.  
  117. TEST(Greeter, saysGoodEveningInTheEvening) {
  118.   Greeter g;
  119.   g.time(20);
  120.   EXPECT_EQ(g.greet("   ulf    "), "Good evening Ulf");
  121. }
  122.  
  123. TEST(Greeter, saysGoodNightAtNight) {
  124.   Greeter g;
  125.   g.time(2);
  126.   EXPECT_EQ(g.greet("   ulf    "), "Good night Ulf");
  127. }
  128.  
  129. constexpr auto clean = "foo"sv;
  130. constexpr auto whitespace_left = "     foo"sv;
  131. constexpr auto whitespace_right = "foo     "sv;
  132.  
  133. TEST(trim, canTrimLeft) {
  134.   EXPECT_EQ(trim(whitespace_left), clean);
  135.   EXPECT_EQ(trimLeft(whitespace_right),
  136.             whitespace_right); // don't touch the right side
  137. }
  138. TEST(trim, canTrimEmpty) {
  139.   EXPECT_EQ(trimLeft(""), ""sv);
  140.   EXPECT_EQ(trimRight(""), ""sv);
  141.   EXPECT_EQ(trim(""), ""sv);
  142. }
  143.  
  144. TEST(trim, canTrimPreTrimmed) {
  145.   EXPECT_EQ(trim(clean), clean);
  146.   EXPECT_EQ(trimRight(clean), clean);
  147.   EXPECT_EQ(trimLeft(clean), clean);
  148. }
  149. TEST(trim, canTrimRight) {
  150.   EXPECT_EQ(trim(whitespace_right), clean);
  151.   EXPECT_EQ(trimRight(whitespace_left),
  152.             whitespace_left); // don't touch the left side
  153. }
  154.  
  155. TEST(trim, canTrimBoth) {
  156.   string_view input = "      foo     "sv;
  157.   EXPECT_EQ(trim(input), clean);
  158. }
  159.  
  160. TEST(trim, canTrimWithSpaceInMiddle) {
  161.   string_view input = "      f o o b a r     "sv;
  162.   EXPECT_EQ(trim(input), "f o o b a r"sv);
  163.  
  164.   string_view notrim = "f o o b a r"sv;
  165.   EXPECT_EQ(trim(notrim), "f o o b a r"sv);
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement