Skip to content

Commit

Permalink
final master
Browse files Browse the repository at this point in the history
  • Loading branch information
aviflombaum committed Sep 10, 2015
1 parent 896689c commit c78b0f9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
28 changes: 13 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,11 @@
2. Become familiar with the strcutre of a CLI application.
3. Get comfortable reading test output for a CLI application.


## Overview

<table style="margin-left: 30%"t>
<caption align="bottom" style="font-size: 12px">Theatrical release poster by Robert McCall</caption>
<tr><td><img src="http://readme-pics.s3.amazonaws.com/2001_A_Space_Odyssey_Style_B.jpg"/></td></tr>
</table>

![2001 Space Odyssey](http://readme-pics.s3.amazonaws.com/2001_A_Space_Odyssey_Style_B.jpg)

The year is 2001, humanity has discovered a mysterious object buried beneath the Lunar surface. Before Earth's most capable (and heroic) scientists can board the *Discovery One* and investigate, we must program the super-intelligent computer, HAL 9000. Luckily for us, however, most of HAL's functionality is already up and running. Our job is to program HAL's greeting to the user.
The year is 2001, humanity has discovered a mysterious object buried beneath the Lunar surface. Before Earth's most capable (and heroic) scientists can board the *Discovery One* and investigate, we must program the super-intelligent computer, HAL 9000. Luckily for us, however, most of HAL's functionality is already up and running. Our job is to program HAL's greeting for the user.

Our program will:

Expand All @@ -32,8 +27,6 @@ Notice that the the program is run with `ruby bin/greet`––we are using Ruby

## Project Structure

**If you haven't done so already, fork and clone this lab down onto your computer**.

Check out the file structure below.

```
Expand All @@ -52,25 +45,25 @@ Let's take a moment to review:

### The `bin` Directory

The `bin` directory holds our **exectuable** file. This file is responsible for running the program. It contains code that actually enacts the command line interaction––i.e. greeting the user, asking the user for input, storing that input and then acting on it.
The `bin` directory holds our **exectuable** file. This file is responsible for running the program. It contains code that actually enacts the command line interaction - i.e. greeting the user, asking the user for input, storing that input and then acting on it.

### The `lib` Directory

The `lib` directory is where we place the code that our program relies on to run. It is the meat of our CLI application. Our executable file *requires* the files in the `lib` directory and uses the code (for example, calling on any methods we might define) to to enact the CLI.

### The `spec` Directory

This directory contains the tests for our program. We have tests for the code in the `lib` directory in `spec/01_greeting.rb` and tests for the CLI, i.e. the user interaction portion of our application, in `spec/02_cli_spec.rb`. We'll use the test output from running `learn` along with the guidelines below to get our program working.
This directory contains the tests for our program. We have tests for the code in the `lib` directory in `spec/01_greeting_spec.rb` and tests for the CLI, i.e. the user interaction portion of our application, in `spec/02_cli_spec.rb`. We'll use the test output from running `learn` along with the guidelines below to get our program working.

## Instructions

### Part I: The `greeting` Method

Before we worry about building the CLI, we need to build the core functionality of our program––that is, the actions that will be carried out once we obtain the user's input. That code belongs in the `lib` directory.

Open up `lib/greeting.rb`. This is where we'll be writing our code. This part of the lab is test-driven. So, run the test for *just this section* by typing `spec/01_greeting.rb` in your terminal.
Open up `lib/greeting.rb`. This is where we'll be writing our code. This part of the lab is test-driven. So, run the test for *just this section* by typing `learn spec/01_greeting_spec.rb` in your terminal.

In order to get our tests passing, we'll need to define a method, `#greeting` that takes in an argument of a person's name and uses string interpolation to return `"Hello #{name}. It's nice to meet you."`.
In order to get our tests passing, we'll need to define a method, `#greeting` that takes in an argument of a person's name and uses string interpolation to return `"Hello #{name}. It's nice to meet you."` (*return and not print*).

Once you get these tests passing, you're ready to move on to the next section.

Expand All @@ -82,12 +75,13 @@ Our CLI code belongs in the `bin` directory. Open up `bin/greet`. Notice that, a
#!/usr/bin/env ruby
require_relative '../lib/greeting.rb'
```

First, we have our shebang line that tells the terminal which interpreter to use to execute the remainder of the file. Second, we are requiring the `greeting.rb` file, from within the `lib` directory. This gives our executable file access to whatever code we write in that `lib` file.

If we run `learn` we can see from the test output that the following is required:
If we run `learn spec/02_cli_spec.rb` we can see from the test output that the following is required:

1. Use `#puts` to output the string "Hi! I'm HAL, what's your name?"
2. Use the `#gets.strip` method to store the user's input and set it equal to a variable called `name`
2. Use the `#gets.strip` method to store the user's input and set it equal to a variable called `name`.
3. Call the `#greeting` method with an argument of the user's name (captured using `#gets` in step 2) to output the interpolated welcome string.

### Reading CLI Error Messages
Expand Down Expand Up @@ -153,3 +147,7 @@ local variable `name' not defined in ./bin/greet.
This is telling us that the `bin/greet` file fails to set a variable called `name` equal to the user's input that is captured via the `#gets.strip` methods.
Now that you have a basic sense of how to read these error messages, as well as some guidelines for getting those tests passing, go ahead and get HAL working properly!
When you're done, run `learn` to confirm all tests are passing.
<iframe width="560" height="315" src="https://www.youtube.com/embed/7R0mD3uWk5c" frameborder="0" allowfullscreen></iframe>
4 changes: 3 additions & 1 deletion spec/01_greeting_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@

context '#greeting' do
it 'takes in an argument of a persons name' do
expect {greeting("Bobby")}.to_not raise_error
expect{greeting("Bobby")}.to_not raise_error
end

it 'greets the person whose name was passed in as an argument' do
allow($stdout).to receive(:puts)

output = capture_puts{ greeting("Sally") }

expect(output).to include("Hello Sally. It's nice to meet you.")
end
end
Expand Down
12 changes: 9 additions & 3 deletions spec/02_cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,36 @@
it 'outputs a welcome message and asks the user for input' do
allow($stdout).to receive(:puts)
allow(self).to receive(:gets).and_return("Don")

expect($stdout).to receive(:puts).with("Hi! I'm HAL, what's your name?"), "Make sure bin/greet has code that can output 'Hi! I'm HAL, what's your name?' exactly."

run_file("./bin/greet")
end

it 'uses #gets.strip to capture the user input and set it equal to a variable called name' do
allow($stdout).to receive(:puts)
allow(self).to receive(:gets).and_return("Don")

expect(self).to receive(:gets).and_return("Don")
name = get_variable_from_file("./bin/greet", "name")

expect(name).to eq("Don")
run_file("./bin/greet")
end

it "calls on the #greeting method with an argument of the user's name" do
allow($stdout).to receive(:puts)
allow(self).to receive(:gets).and_return("Don")

expect(self).to receive(:greeting).with("Don"), "Make sure the bin/greet file has code that calls the #greeting method with an argument of the user's name."

run_file("./bin/greet")
end

it "calls on the #greeting method with an argument of the user's name and returns the new greeting, interpolating the user's name" do
allow($stdout).to receive(:puts)
allow(self).to receive(:gets).and_return("Don")

output = capture_puts{ run_file("./bin/greet") }

expect(output).to include("Hello Don. It's nice to meet you.")
run_file("./bin/greet")
end
end

0 comments on commit c78b0f9

Please sign in to comment.