-
-
Notifications
You must be signed in to change notification settings - Fork 238
Improves Hello-World #279
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
Improves Hello-World #279
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| #!/usr/bin/env bash | ||
| # | ||
| # Test that the hello-world exercise compiles with no changes and fails. | ||
|
|
||
| # Fail if any command fails | ||
| set -e | ||
|
|
||
| repo=$(cd "$(dirname "$0")/.." && pwd) | ||
|
|
||
| hello_world_tmp_dir="$repo"/build_hello-world | ||
| hello_world_dir="$repo"/exercises/hello-world | ||
|
|
||
| mkdir -p "$hello_world_tmp_dir" | ||
|
|
||
| cd "$hello_world_tmp_dir" | ||
|
|
||
| # Configuring should work. | ||
| cmake -G Ninja "$hello_world_dir" | ||
|
|
||
| echo "Building hello-world, which should fail." | ||
|
|
||
| # The build will fail, since it runs a failing test. | ||
| if cmake --build .; then | ||
| # The build succeeded, which is not supposed to happen. | ||
| exit 1 | ||
| fi | ||
|
|
||
| cd "$repo" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ using namespace std; | |
|
|
||
| namespace hello_world | ||
| { | ||
|
|
||
| string hello() | ||
| { | ||
| return "Hello, World!"; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| #include "hello_world.h" | ||
|
|
||
| // Use everything from the 'std' namespace. | ||
| // This lets us write 'string' instead of 'std::string'. | ||
| using namespace std; | ||
|
|
||
| namespace hello_world { | ||
|
|
||
| // Define the function itself. This could have also been written as: | ||
| // std::string hello_world::hello() | ||
| string hello() { | ||
| // Return the string we need. | ||
| return "Fix me!"; | ||
| } | ||
|
|
||
| } // namespace hello_world | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,22 @@ | ||
| // This is an include guard. | ||
| // You could alternatively use '#pragma once' | ||
| // See https://en.wikipedia.org/wiki/Include_guard | ||
| #if !defined(HELLO_WORLD_H) | ||
| #define HELLO_WORLD_H | ||
|
|
||
| // Include the string header so that we have access to 'std::string' | ||
| #include <string> | ||
|
|
||
| // Declare a namespace for the function(s) we are exporting. | ||
| // https://en.cppreference.com/w/cpp/language/namespace | ||
| namespace hello_world { | ||
|
|
||
| // Declare the 'hello()' function, which takes no arguments and returns a | ||
| // 'std::string'. The function itself is defined in the hello_world.cpp source | ||
| // file. Because it is inside of the 'hello_world' namespace, it's full name is | ||
| // 'hello_world::hello()'. | ||
| std::string hello(); | ||
|
|
||
| } | ||
| } // namespace hello_world | ||
|
|
||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,12 @@ | ||
| // Include the header file with the definitions of the functions you create. | ||
| #include "hello_world.h" | ||
|
|
||
| // Include the test framework. | ||
| #include "test/catch.hpp" | ||
|
|
||
| // Declares a single test. | ||
| TEST_CASE("test_hello") | ||
| { | ||
| REQUIRE("Hello, World!" == hello_world::hello()); | ||
| // Check if your function returns "Hello, World!". | ||
| REQUIRE(hello_world::hello() == "Hello, World!"); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is that really a good idea? This is actually suggesting to use
using namespace std;all the time. Not a good coding styleThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
People tend to disagree on this point - See CppCoreGuidelines and Google.
It's generally safe to use in a small, self-contained source file, though there are potential issues in large codebases. It is one of those conveniences that I see as being very helpful while learning the language and potentially dangerous later. You're going to almost never hit issues from it in a toy problem.
That said, I mostly use this in toy problems because I'm lazy and it's low- to no-risk. Do you think it would be better to encourage new learners to use
using std::string;instead of the whole namespace?