Skip to content

Commit fb97677

Browse files
authored
Improves Hello-World (#279)
* Adding comments and a hello world example that compiles. * Adding CI test for the compilable, but test failing, hello-world. * Cleaning up hello(). * Improving include guard description.
1 parent c249275 commit fb97677

File tree

7 files changed

+64
-3
lines changed

7 files changed

+64
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ cmake_install.cmake
1111

1212
# Ignore alternate directory of exercises that is used for building
1313
build_exercises/
14+
build_hello-world/

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,5 @@ script:
5353
- bin/fetch-configlet
5454
- bin/configlet lint .
5555
- bin/check-configlet-fmt.sh
56+
- bin/check-hello-world.sh
5657
- bin/check-exercises.sh

bin/check-hello-world.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Test that the hello-world exercise compiles with no changes and fails.
4+
5+
# Fail if any command fails
6+
set -e
7+
8+
repo=$(cd "$(dirname "$0")/.." && pwd)
9+
10+
hello_world_tmp_dir="$repo"/build_hello-world
11+
hello_world_dir="$repo"/exercises/hello-world
12+
13+
mkdir -p "$hello_world_tmp_dir"
14+
15+
cd "$hello_world_tmp_dir"
16+
17+
# Configuring should work.
18+
cmake -G Ninja "$hello_world_dir"
19+
20+
echo "Building hello-world, which should fail."
21+
22+
# The build will fail, since it runs a failing test.
23+
if cmake --build .; then
24+
# The build succeeded, which is not supposed to happen.
25+
exit 1
26+
fi
27+
28+
cd "$repo"

exercises/hello-world/example.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ using namespace std;
44

55
namespace hello_world
66
{
7-
7+
88
string hello()
99
{
1010
return "Hello, World!";

exercises/hello-world/hello_world.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "hello_world.h"
2+
3+
// Use everything from the 'std' namespace.
4+
// This lets us write 'string' instead of 'std::string'.
5+
using namespace std;
6+
7+
namespace hello_world {
8+
9+
// Define the function itself. This could have also been written as:
10+
// std::string hello_world::hello()
11+
string hello() {
12+
// Return the string we need.
13+
return "Fix me!";
14+
}
15+
16+
} // namespace hello_world

exercises/hello-world/hello_world.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
1+
// This is an include guard.
2+
// You could alternatively use '#pragma once'
3+
// See https://en.wikipedia.org/wiki/Include_guard
14
#if !defined(HELLO_WORLD_H)
25
#define HELLO_WORLD_H
36

7+
// Include the string header so that we have access to 'std::string'
48
#include <string>
59

10+
// Declare a namespace for the function(s) we are exporting.
11+
// https://en.cppreference.com/w/cpp/language/namespace
612
namespace hello_world {
713

14+
// Declare the 'hello()' function, which takes no arguments and returns a
15+
// 'std::string'. The function itself is defined in the hello_world.cpp source
16+
// file. Because it is inside of the 'hello_world' namespace, it's full name is
17+
// 'hello_world::hello()'.
818
std::string hello();
919

10-
}
20+
} // namespace hello_world
1121

1222
#endif
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
// Include the header file with the definitions of the functions you create.
12
#include "hello_world.h"
3+
4+
// Include the test framework.
25
#include "test/catch.hpp"
36

7+
// Declares a single test.
48
TEST_CASE("test_hello")
59
{
6-
REQUIRE("Hello, World!" == hello_world::hello());
10+
// Check if your function returns "Hello, World!".
11+
REQUIRE(hello_world::hello() == "Hello, World!");
712
}

0 commit comments

Comments
 (0)