Skip to content

Update bob exercise #290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions exercises/bob/.meta/description.md

This file was deleted.

24 changes: 14 additions & 10 deletions exercises/bob/README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
# Bob

Bob is a lackadaisical teenager. In conversation, his responses are very limited.

Bob answers 'Sure.' if you ask him a question.

He answers 'Whoa, chill out!' if you yell at him.

He says 'Fine. Be that way!' if you address him without actually saying
anything.

He answers 'Whatever.' to anything else.
Bob is a lackadaisical teenager. In conversation, his responses are very limited.

Bob answers 'Sure.' if you ask him a question, such as "How are you?".

He answers 'Whoa, chill out!' if you YELL AT HIM (in all capitals).

He answers 'Calm down, I know what I'm doing!' if you yell a question at him.

He says 'Fine. Be that way!' if you address him without actually saying
anything.

He answers 'Whatever.' to anything else.

Bob's conversational partner is a purist when it comes to written communication and always follows normal rules regarding sentence punctuation in English.

## Getting Started

Expand Down
66 changes: 54 additions & 12 deletions exercises/bob/bob_test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "bob.h"
#include "test/catch.hpp"

// Bob exercise test case data version 1.4.0

TEST_CASE("stating_something")
{
REQUIRE("Whatever." == bob::hey("Tom-ay-to, tom-aaaah-to."));
Expand All @@ -12,11 +14,26 @@ TEST_CASE("shouting")
REQUIRE("Whoa, chill out!" == bob::hey("WATCH OUT!"));
}

TEST_CASE("shouting_gibberish")
{
REQUIRE("Whoa, chill out!" == bob::hey("FCECDFCAAB"));
}

TEST_CASE("asking_a_question")
{
REQUIRE("Sure." == bob::hey("Does this cryogenic chamber make me look fat?"));
}

TEST_CASE("asking_a_numeric_question")
{
REQUIRE("Sure." == bob::hey("You are, what, like 15?"));
}

TEST_CASE("asking_gibberish")
{
REQUIRE("Sure." == bob::hey("fffbbcbeab?"));
}

TEST_CASE("talking_forcefully")
{
REQUIRE("Whatever." == bob::hey("Let's go make out behind the gym!"));
Expand All @@ -29,20 +46,20 @@ TEST_CASE("using_acronyms_in_regular_speech")

TEST_CASE("forceful_questions")
{
REQUIRE("Whoa, chill out!" == bob::hey("WHAT THE HELL WERE YOU THINKING?"));
REQUIRE("Calm down, I know what I'm doing!" == bob::hey("WHAT THE HELL WERE YOU THINKING?"));
}

TEST_CASE("shouting_numbers")
{
REQUIRE("Whoa, chill out!" == bob::hey("1, 2, 3 GO!"));
}

TEST_CASE("only_numbers")
TEST_CASE("no_letters")
{
REQUIRE("Whatever." == bob::hey("1, 2, 3"));
}

TEST_CASE("question_with_only_numbers")
TEST_CASE("question_with_no_letters")
{
REQUIRE("Sure." == bob::hey("4?"));
}
Expand All @@ -54,22 +71,22 @@ TEST_CASE("shouting_with_special_characters")

TEST_CASE("shouting_with_no_exclamation_mark")
{
REQUIRE("Whoa, chill out!" == bob::hey("I HATE YOU"));
REQUIRE("Whoa, chill out!" == bob::hey("I HATE THE DMV"));
}

TEST_CASE("statement_containing_question_mark")
{
REQUIRE("Whatever." == bob::hey("Ending with a ? means a question."));
REQUIRE("Whatever." == bob::hey("Ending with ? means a question."));
}

TEST_CASE("prattling_on")
TEST_CASE("non_letters_with_question")
{
REQUIRE("Sure." == bob::hey("Wait! Hang on. Are you going to be OK?"));
REQUIRE("Sure." == bob::hey(":) ?"));
}

TEST_CASE("question_with_trailing_whitespace")
TEST_CASE("prattling_on")
{
REQUIRE("Sure." == bob::hey("Are you ok? "));
REQUIRE("Sure." == bob::hey("Wait! Hang on. Are you going to be OK?"));
}

TEST_CASE("silence")
Expand All @@ -79,11 +96,36 @@ TEST_CASE("silence")

TEST_CASE("prolonged_silence")
{
REQUIRE("Fine. Be that way!" == bob::hey(" "));
REQUIRE("Fine. Be that way!" == bob::hey(" "));
}

TEST_CASE("alternate_silence")
{
REQUIRE("Fine. Be that way!" == bob::hey("\t\t\t\t\t\t\t\t\t\t"));
}

TEST_CASE("multiple_line_question")
{
REQUIRE("Whatever." == bob::hey("\nDoes this cryogenic chamber make me look fat?\nNo."));
}

TEST_CASE("starting_with_whitespace")
{
REQUIRE("Whatever." == bob::hey(" hmmmmmmm..."));
}

TEST_CASE("ending_with_whitespace")
{
REQUIRE("Sure." == bob::hey("Okay if like my spacebar quite a bit? "));
}

TEST_CASE("other_whitespace")
{
REQUIRE("Fine. Be that way!" == bob::hey("\n\r \t"));
}

TEST_CASE("not_all_silence")
TEST_CASE("non_question_ending_with_whitespace")
{
REQUIRE("Whatever." == bob::hey(" A bit of silence can be nice. "));
REQUIRE("Whatever." == bob::hey("This is a statement ending with whitespace "));
}
#endif
10 changes: 6 additions & 4 deletions exercises/bob/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ string trim_copy(string const& s)
{
string cpy(s);
// Trim front
while (!cpy.empty() && cpy.front() == ' ')
while (!cpy.empty() && std::isspace(static_cast<unsigned char>(cpy.front())))
cpy.erase(cpy.begin());
// Trim back
while (!cpy.empty() && cpy.back() == ' ')
while (!cpy.empty() && std::isspace(static_cast<unsigned char>(cpy.back())))
cpy.pop_back();
return cpy;
}
Expand Down Expand Up @@ -52,14 +52,16 @@ bool is_silence(string const& text)
return trim_copy(text).length() == 0;
}

}
} // anonymous namespace

string hey(string const& text)
{
if (is_silence(text)) {
return "Fine. Be that way!";
}
if (is_shouting(text)) {
if (is_question(text))
return "Calm down, I know what I'm doing!";
return "Whoa, chill out!";
}
if (is_question(text)) {
Expand All @@ -68,4 +70,4 @@ string hey(string const& text)
return "Whatever.";
}

}
} // namespace bob