From 79f68aa9e8b54655aea7e5de12115c113664bb1d Mon Sep 17 00:00:00 2001 From: Patrick Jackson Date: Sat, 27 Apr 2019 10:28:34 -0700 Subject: [PATCH 1/4] Adding comments and a hello world example that compiles. --- exercises/hello-world/example.cpp | 2 +- exercises/hello-world/hello_world.cpp | 16 ++++++++++++++++ exercises/hello-world/hello_world.h | 12 +++++++++++- exercises/hello-world/hello_world_test.cpp | 7 ++++++- 4 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 exercises/hello-world/hello_world.cpp diff --git a/exercises/hello-world/example.cpp b/exercises/hello-world/example.cpp index 0a70d43b..c4ca3e8c 100644 --- a/exercises/hello-world/example.cpp +++ b/exercises/hello-world/example.cpp @@ -4,7 +4,7 @@ using namespace std; namespace hello_world { - + string hello() { return "Hello, World!"; diff --git a/exercises/hello-world/hello_world.cpp b/exercises/hello-world/hello_world.cpp new file mode 100644 index 00000000..91fd3ea9 --- /dev/null +++ b/exercises/hello-world/hello_world.cpp @@ -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 "Hello!"; +} + +} // namespace hello_world diff --git a/exercises/hello-world/hello_world.h b/exercises/hello-world/hello_world.h index 66829218..5f262c1e 100644 --- a/exercises/hello-world/hello_world.h +++ b/exercises/hello-world/hello_world.h @@ -1,12 +1,22 @@ +// This is called an include guard, which ensures that the header is only +// included once. 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 +// 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 diff --git a/exercises/hello-world/hello_world_test.cpp b/exercises/hello-world/hello_world_test.cpp index bee2816c..83ca291e 100644 --- a/exercises/hello-world/hello_world_test.cpp +++ b/exercises/hello-world/hello_world_test.cpp @@ -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!"); } From 33d8c0eda415db3ccc9f834f2e34c77da11852f2 Mon Sep 17 00:00:00 2001 From: Patrick Jackson Date: Sat, 27 Apr 2019 10:41:12 -0700 Subject: [PATCH 2/4] Adding CI test for the compilable, but test failing, hello-world. --- .gitignore | 1 + .travis.yml | 1 + bin/check-hello-world.sh | 28 ++++++++++++++++++++++++++++ 3 files changed, 30 insertions(+) create mode 100755 bin/check-hello-world.sh diff --git a/.gitignore b/.gitignore index 6c166b0d..eaab018f 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ cmake_install.cmake # Ignore alternate directory of exercises that is used for building build_exercises/ +build_hello-world/ diff --git a/.travis.yml b/.travis.yml index fd82b715..f69716b5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -50,4 +50,5 @@ script: - bin/fetch-configlet - bin/configlet lint . - bin/check-configlet-fmt.sh + - bin/check-hello-world.sh - bin/check-exercises.sh diff --git a/bin/check-hello-world.sh b/bin/check-hello-world.sh new file mode 100755 index 00000000..7008cd00 --- /dev/null +++ b/bin/check-hello-world.sh @@ -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" \ No newline at end of file From dd471c45fdf88b043acf589e28777c6fb540067e Mon Sep 17 00:00:00 2001 From: Patrick Jackson Date: Sun, 5 May 2019 09:07:36 -0700 Subject: [PATCH 3/4] Cleaning up hello(). --- exercises/hello-world/hello_world.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/exercises/hello-world/hello_world.cpp b/exercises/hello-world/hello_world.cpp index 91fd3ea9..1b818bb6 100644 --- a/exercises/hello-world/hello_world.cpp +++ b/exercises/hello-world/hello_world.cpp @@ -6,11 +6,11 @@ using namespace std; namespace hello_world { -// Define the function itself. This could have also been written as 'std::string -// hello_world::hello()'. +// Define the function itself. This could have also been written as: +// std::string hello_world::hello() string hello() { // Return the string we need. - return "Hello!"; + return "Fix me!"; } } // namespace hello_world From d4c8d6fe1128bdc6f69a0a8e023757ab358ac06e Mon Sep 17 00:00:00 2001 From: Patrick Jackson Date: Sat, 3 Aug 2019 14:33:57 -0700 Subject: [PATCH 4/4] Improving include guard description. --- exercises/hello-world/hello_world.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/exercises/hello-world/hello_world.h b/exercises/hello-world/hello_world.h index 5f262c1e..191a60fd 100644 --- a/exercises/hello-world/hello_world.h +++ b/exercises/hello-world/hello_world.h @@ -1,6 +1,6 @@ -// This is called an include guard, which ensures that the header is only -// included once. You could alternatively use '#pragma once'. See -// https://en.wikipedia.org/wiki/Include_guard +// 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