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 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..1b818bb6 --- /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 "Fix me!"; +} + +} // namespace hello_world diff --git a/exercises/hello-world/hello_world.h b/exercises/hello-world/hello_world.h index 66829218..191a60fd 100644 --- a/exercises/hello-world/hello_world.h +++ b/exercises/hello-world/hello_world.h @@ -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 +// 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!"); }