From b020cff2f743b91dfda7b6c520163f24c12bef63 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Fri, 9 Nov 2018 09:37:57 +0200 Subject: [PATCH 01/13] First red test --- tdd_intro/demo/02_word_count/test.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tdd_intro/demo/02_word_count/test.cpp b/tdd_intro/demo/02_word_count/test.cpp index 6bdc8367..9a19ea34 100644 --- a/tdd_intro/demo/02_word_count/test.cpp +++ b/tdd_intro/demo/02_word_count/test.cpp @@ -17,3 +17,9 @@ such: 1 #include #include + +TEST(WordsCount, TestSeparateFirstWord) +{ + std::map words = SeparateWords("hello bro"); + EXPECT_EQ(1, words.size()); +} From 2e932c29feadfbba594011fa35897f2dc77f1761 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Fri, 9 Nov 2018 10:19:06 +0200 Subject: [PATCH 02/13] Green --- tdd_intro/demo/02_word_count/test.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/tdd_intro/demo/02_word_count/test.cpp b/tdd_intro/demo/02_word_count/test.cpp index 9a19ea34..29300489 100644 --- a/tdd_intro/demo/02_word_count/test.cpp +++ b/tdd_intro/demo/02_word_count/test.cpp @@ -17,9 +17,19 @@ such: 1 #include #include +using words_mt = std::map; +const static char wordSeparator = ' '; + +words_mt SeparateWords(const std::string& phrase) +{ + const size_t index = phrase.find_first_of(wordSeparator); + return {{phrase.substr(0, index), 1}}; +} TEST(WordsCount, TestSeparateFirstWord) { - std::map words = SeparateWords("hello bro"); - EXPECT_EQ(1, words.size()); + words_mt words = SeparateWords("hello"); + ASSERT_EQ(1, words.size()); + EXPECT_TRUE(words.find("hello") != words.end()); + EXPECT_EQ(1, words["hello"]); } From a22a075302f8a51348a3c3114248bc4c613d1807 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Fri, 9 Nov 2018 10:24:37 +0200 Subject: [PATCH 03/13] Refactoring --- tdd_intro/demo/02_word_count/test.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tdd_intro/demo/02_word_count/test.cpp b/tdd_intro/demo/02_word_count/test.cpp index 29300489..72675392 100644 --- a/tdd_intro/demo/02_word_count/test.cpp +++ b/tdd_intro/demo/02_word_count/test.cpp @@ -23,7 +23,12 @@ const static char wordSeparator = ' '; words_mt SeparateWords(const std::string& phrase) { const size_t index = phrase.find_first_of(wordSeparator); - return {{phrase.substr(0, index), 1}}; + + if (index != std::string::npos) + { + return {{phrase.substr(0, index), 1}}; + } + return {{phrase, 1}}; } TEST(WordsCount, TestSeparateFirstWord) From aa48d05aea905270a48b7c3186010ad576fb5c8d Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Fri, 9 Nov 2018 10:26:08 +0200 Subject: [PATCH 04/13] Red --- tdd_intro/demo/02_word_count/test.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/tdd_intro/demo/02_word_count/test.cpp b/tdd_intro/demo/02_word_count/test.cpp index 72675392..c0925593 100644 --- a/tdd_intro/demo/02_word_count/test.cpp +++ b/tdd_intro/demo/02_word_count/test.cpp @@ -34,7 +34,20 @@ words_mt SeparateWords(const std::string& phrase) TEST(WordsCount, TestSeparateFirstWord) { words_mt words = SeparateWords("hello"); - ASSERT_EQ(1, words.size()); + EXPECT_EQ(1, words.size()); EXPECT_TRUE(words.find("hello") != words.end()); EXPECT_EQ(1, words["hello"]); } + +TEST(WordsCount, TestSeparateSeveralWords) +{ + words_mt words = SeparateWords("hello bro"); + EXPECT_EQ(2, words.size()); + + EXPECT_TRUE(words.find("hello") != words.end()); + EXPECT_EQ(1, words["hello"]); + + EXPECT_TRUE(words.find("bro") != words.end()); + EXPECT_EQ(1, words["bro"]); + +} From 47c09e6fe27c9edd8492441dfff2d238130f2931 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Fri, 9 Nov 2018 10:48:47 +0200 Subject: [PATCH 05/13] Green --- tdd_intro/demo/02_word_count/test.cpp | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/tdd_intro/demo/02_word_count/test.cpp b/tdd_intro/demo/02_word_count/test.cpp index c0925593..24681f25 100644 --- a/tdd_intro/demo/02_word_count/test.cpp +++ b/tdd_intro/demo/02_word_count/test.cpp @@ -22,13 +22,26 @@ const static char wordSeparator = ' '; words_mt SeparateWords(const std::string& phrase) { - const size_t index = phrase.find_first_of(wordSeparator); + std::string separatedPhrase = phrase; + words_mt words; + size_t wordEndIndex = 0; - if (index != std::string::npos) + do { - return {{phrase.substr(0, index), 1}}; + separatedPhrase = phrase.substr(wordEndIndex, phrase.size() - wordEndIndex); + wordEndIndex = separatedPhrase.find_first_of(wordSeparator); + if (wordEndIndex == std::string::npos) + { + wordEndIndex = phrase.size(); + } + + std::string word = separatedPhrase.substr(0, wordEndIndex); + ++words[word]; + ++wordEndIndex; } - return {{phrase, 1}}; + while(wordEndIndex < phrase.size()); + + return words; } TEST(WordsCount, TestSeparateFirstWord) @@ -49,5 +62,4 @@ TEST(WordsCount, TestSeparateSeveralWords) EXPECT_TRUE(words.find("bro") != words.end()); EXPECT_EQ(1, words["bro"]); - } From cfcbb1d9e4ba8193ce997bfb601e1ed4956a8e04 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Fri, 9 Nov 2018 10:53:05 +0200 Subject: [PATCH 06/13] Red --- tdd_intro/demo/02_word_count/test.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tdd_intro/demo/02_word_count/test.cpp b/tdd_intro/demo/02_word_count/test.cpp index 24681f25..c8ea4ed6 100644 --- a/tdd_intro/demo/02_word_count/test.cpp +++ b/tdd_intro/demo/02_word_count/test.cpp @@ -52,6 +52,17 @@ TEST(WordsCount, TestSeparateFirstWord) EXPECT_EQ(1, words["hello"]); } +TEST(WordsCount, TestTrimWord) +{ + std::string phrase = "tdd course"; + + EXPECT_EQ("tdd", TrimWord(phrase, ' ')); + EXPECT_EQ("course", phrase); + + EXPECT_EQ("course", TrimWord(phrase, ' ')); + EXPECT_EQ("course", phrase); +} + TEST(WordsCount, TestSeparateSeveralWords) { words_mt words = SeparateWords("hello bro"); From c6d369a9add8ec7db6a46fabc9dc8735531eb365 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Fri, 9 Nov 2018 11:00:34 +0200 Subject: [PATCH 07/13] Green --- tdd_intro/demo/02_word_count/test.cpp | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/tdd_intro/demo/02_word_count/test.cpp b/tdd_intro/demo/02_word_count/test.cpp index c8ea4ed6..9a034053 100644 --- a/tdd_intro/demo/02_word_count/test.cpp +++ b/tdd_intro/demo/02_word_count/test.cpp @@ -20,6 +20,25 @@ such: 1 using words_mt = std::map; const static char wordSeparator = ' '; +std::string TrimWord(std::string& phrase, const char seperator) +{ + size_t index = phrase.find_first_of(seperator); + std::string word; + + if (index == std::string::npos || index == phrase.size()) + { + word = phrase; + phrase.clear(); + } + else + { + word = phrase.substr(0, index); + phrase = phrase.substr(index + 1, phrase.size() - index); + } + + return word; +} + words_mt SeparateWords(const std::string& phrase) { std::string separatedPhrase = phrase; @@ -60,7 +79,7 @@ TEST(WordsCount, TestTrimWord) EXPECT_EQ("course", phrase); EXPECT_EQ("course", TrimWord(phrase, ' ')); - EXPECT_EQ("course", phrase); + EXPECT_EQ("", phrase); } TEST(WordsCount, TestSeparateSeveralWords) From c4d0e8da32c7436f168ce9361b28251a732daec7 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Fri, 9 Nov 2018 11:09:01 +0200 Subject: [PATCH 08/13] Refactoring --- tdd_intro/demo/02_word_count/test.cpp | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/tdd_intro/demo/02_word_count/test.cpp b/tdd_intro/demo/02_word_count/test.cpp index 9a034053..e525adcc 100644 --- a/tdd_intro/demo/02_word_count/test.cpp +++ b/tdd_intro/demo/02_word_count/test.cpp @@ -41,24 +41,14 @@ std::string TrimWord(std::string& phrase, const char seperator) words_mt SeparateWords(const std::string& phrase) { - std::string separatedPhrase = phrase; words_mt words; - size_t wordEndIndex = 0; + std::string currentPhrase = phrase; + std::string curentWord; - do + while (!(curentWord = TrimWord(currentPhrase, wordSeparator)).empty()) { - separatedPhrase = phrase.substr(wordEndIndex, phrase.size() - wordEndIndex); - wordEndIndex = separatedPhrase.find_first_of(wordSeparator); - if (wordEndIndex == std::string::npos) - { - wordEndIndex = phrase.size(); - } - - std::string word = separatedPhrase.substr(0, wordEndIndex); - ++words[word]; - ++wordEndIndex; + ++words[curentWord]; } - while(wordEndIndex < phrase.size()); return words; } @@ -71,7 +61,7 @@ TEST(WordsCount, TestSeparateFirstWord) EXPECT_EQ(1, words["hello"]); } -TEST(WordsCount, TestTrimWord) +TEST(WordsCount, TestTrimOneWord) { std::string phrase = "tdd course"; From 1b77c23578f948c3a1f974579e42b8e569d699d0 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Fri, 9 Nov 2018 11:10:32 +0200 Subject: [PATCH 09/13] Red --- tdd_intro/demo/02_word_count/test.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tdd_intro/demo/02_word_count/test.cpp b/tdd_intro/demo/02_word_count/test.cpp index e525adcc..760f7e80 100644 --- a/tdd_intro/demo/02_word_count/test.cpp +++ b/tdd_intro/demo/02_word_count/test.cpp @@ -64,12 +64,14 @@ TEST(WordsCount, TestSeparateFirstWord) TEST(WordsCount, TestTrimOneWord) { std::string phrase = "tdd course"; - - EXPECT_EQ("tdd", TrimWord(phrase, ' ')); + std::string word; + EXPECT_TRUE(TrimWord(phrase, word, ' ')); EXPECT_EQ("course", phrase); - EXPECT_EQ("course", TrimWord(phrase, ' ')); + EXPECT_TRUE(TrimWord(phrase, word, ' ')); EXPECT_EQ("", phrase); + + EXPECT_FALSE(TrimWord(phrase, word, ' ')); } TEST(WordsCount, TestSeparateSeveralWords) From 66d9de7e6c5d543f12afd53c417399da749d578c Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Fri, 9 Nov 2018 11:14:08 +0200 Subject: [PATCH 10/13] Green --- tdd_intro/demo/02_word_count/test.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/tdd_intro/demo/02_word_count/test.cpp b/tdd_intro/demo/02_word_count/test.cpp index 760f7e80..21f635be 100644 --- a/tdd_intro/demo/02_word_count/test.cpp +++ b/tdd_intro/demo/02_word_count/test.cpp @@ -20,11 +20,14 @@ such: 1 using words_mt = std::map; const static char wordSeparator = ' '; -std::string TrimWord(std::string& phrase, const char seperator) +bool TrimWord(std::string& phrase, std::string& word, const char seperator) { - size_t index = phrase.find_first_of(seperator); - std::string word; + if (phrase.empty()) + { + return false; + } + size_t index = phrase.find_first_of(seperator); if (index == std::string::npos || index == phrase.size()) { word = phrase; @@ -36,7 +39,7 @@ std::string TrimWord(std::string& phrase, const char seperator) phrase = phrase.substr(index + 1, phrase.size() - index); } - return word; + return true; } words_mt SeparateWords(const std::string& phrase) @@ -45,7 +48,7 @@ words_mt SeparateWords(const std::string& phrase) std::string currentPhrase = phrase; std::string curentWord; - while (!(curentWord = TrimWord(currentPhrase, wordSeparator)).empty()) + while (TrimWord(currentPhrase, curentWord, wordSeparator)) { ++words[curentWord]; } From e90471676fb4d97d8506856ebeeaa50b5e0587f5 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Fri, 9 Nov 2018 11:18:12 +0200 Subject: [PATCH 11/13] Added several green tests --- tdd_intro/demo/02_word_count/test.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tdd_intro/demo/02_word_count/test.cpp b/tdd_intro/demo/02_word_count/test.cpp index 21f635be..59f6bf08 100644 --- a/tdd_intro/demo/02_word_count/test.cpp +++ b/tdd_intro/demo/02_word_count/test.cpp @@ -88,3 +88,27 @@ TEST(WordsCount, TestSeparateSeveralWords) EXPECT_TRUE(words.find("bro") != words.end()); EXPECT_EQ(1, words["bro"]); } + +TEST(WordsCount, TestSeparateSeveralSameWords) +{ + words_mt words = SeparateWords("hello bro hello"); + + EXPECT_EQ(2, words.size()); + EXPECT_TRUE(words.find("hello") != words.end()); + EXPECT_EQ(2, words["hello"]); +} + +TEST(WordsCount, TestEmptyString) +{ + words_mt words = SeparateWords(""); + EXPECT_TRUE(words.empty()); +} + +TEST(WordsCount, TestSeparatorInEnd) +{ + words_mt words = SeparateWords("hello "); + + EXPECT_EQ(1, words.size()); + EXPECT_TRUE(words.find("hello") != words.end()); + EXPECT_EQ(1, words["hello"]); +} From f1b20f51d04f1958bb83afd982b4e99606a33ba5 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Fri, 9 Nov 2018 11:22:22 +0200 Subject: [PATCH 12/13] AcceptanceTest --- tdd_intro/demo/02_word_count/test.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tdd_intro/demo/02_word_count/test.cpp b/tdd_intro/demo/02_word_count/test.cpp index 59f6bf08..311def6f 100644 --- a/tdd_intro/demo/02_word_count/test.cpp +++ b/tdd_intro/demo/02_word_count/test.cpp @@ -112,3 +112,21 @@ TEST(WordsCount, TestSeparatorInEnd) EXPECT_TRUE(words.find("hello") != words.end()); EXPECT_EQ(1, words["hello"]); } + +TEST(WordsCount, AcceptanceTest) +{ + words_mt words = SeparateWords("olly olly in come free please please let it be in such manner olly"); + + ASSERT_EQ(10, words.size()); + + ASSERT_EQ(3, words["olly"]); + ASSERT_EQ(2, words["in"]); + ASSERT_EQ(1, words["come"]); + ASSERT_EQ(1, words["free"]); + ASSERT_EQ(2, words["please"]); + ASSERT_EQ(1, words["let"]); + ASSERT_EQ(1, words["it"]); + ASSERT_EQ(1, words["be"]); + ASSERT_EQ(1, words["manner"]); + ASSERT_EQ(1, words["such"]); +} From 4080ca57e3ace9fa3e5b522631e75c97342c0dc0 Mon Sep 17 00:00:00 2001 From: Alexandr Chernysh Date: Fri, 9 Nov 2018 11:32:23 +0200 Subject: [PATCH 13/13] Refactoring tests --- tdd_intro/demo/02_word_count/test.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tdd_intro/demo/02_word_count/test.cpp b/tdd_intro/demo/02_word_count/test.cpp index 311def6f..e8ee188b 100644 --- a/tdd_intro/demo/02_word_count/test.cpp +++ b/tdd_intro/demo/02_word_count/test.cpp @@ -77,7 +77,7 @@ TEST(WordsCount, TestTrimOneWord) EXPECT_FALSE(TrimWord(phrase, word, ' ')); } -TEST(WordsCount, TestSeparateSeveralWords) +TEST(WordsCount, TestCountSeveralDifferentWords) { words_mt words = SeparateWords("hello bro"); EXPECT_EQ(2, words.size()); @@ -89,7 +89,7 @@ TEST(WordsCount, TestSeparateSeveralWords) EXPECT_EQ(1, words["bro"]); } -TEST(WordsCount, TestSeparateSeveralSameWords) +TEST(WordsCount, TestCountSeveralSameWords) { words_mt words = SeparateWords("hello bro hello"); @@ -98,13 +98,13 @@ TEST(WordsCount, TestSeparateSeveralSameWords) EXPECT_EQ(2, words["hello"]); } -TEST(WordsCount, TestEmptyString) +TEST(WordsCount, TestNoWordsWhenEmptyInput) { words_mt words = SeparateWords(""); EXPECT_TRUE(words.empty()); } -TEST(WordsCount, TestSeparatorInEnd) +TEST(WordsCount, TestOneWordWhenSeparatorInEnd) { words_mt words = SeparateWords("hello ");