Skip to content

Commit

Permalink
Add login class and login spec
Browse files Browse the repository at this point in the history
  • Loading branch information
luizfonseca committed Jun 20, 2020
1 parent 523c544 commit 27d0aaf
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 4 deletions.
5 changes: 3 additions & 2 deletions README.md
Expand Up @@ -7,6 +7,7 @@ This project aims to help new developers understand the structures that make a p

It's very straightforward and can be modified to use other programming languages as well.


## What you will see here

You will see a series of tasks you have to do in order for you computer to work. Fear not, this computer can also be extended to perform many things for you like finding and saving images, sending emails for you or ordering food. You decide where your computer goes.
Expand All @@ -16,7 +17,7 @@ These tasks will be then tested against the `RSpec` test suite, which you can ca

## Your first steps

Often you would see projects that offer you everything to run them, but this one is different: there are only a set of rules and you have to figure out what's missing to run the project. Check the files and find how `rake` can run.
Often you would see projects that offer you everything to run them, but this one is different: there are only a set of rules and you have to figure out what's missing to run the project. Check the files and find how `rake` can run. The tip here is to make sure you can run `bundle install` without any errors.

Stuck? Your best friend is Google: e.g. `How to install the ruby ...` or `How to split arrays in ruby` are common terms you might be looking for. Keeping in mind that this is a fun pet project to elevate your knowledge and finding the answers by googling them is also part of the learning process.

Expand All @@ -30,7 +31,7 @@ Computers normally have what we can call a `Boot phase`. It's where everything g

Your job here is to create a similar screen and avoid printing everything at once like a good BIOS would do. It should wait a bit before printing every line. Feel free to create any style for it. It's your computer after all.

When you are done, run the `rake` command to test or use `rake run` to actually start your project in your terminal.
When you are done, run the `rake` command to test or use `ruby ./lib/computer.rb` to actually start your project in your terminal.


#### 2. The Operational System (OS) phase
Expand Down
8 changes: 6 additions & 2 deletions lib/computer.rb
@@ -1,5 +1,9 @@
require_relative "./computer/boot"

# http://ruby-for-beginners.rubymonstas.org/advanced/modules.html
# Main computer module. This file just initializes the module naming convention we use in this project.
module Computer; end

require_relative "./computer/boot"
require_relative "./computer/system/system"
require_relative "./computer/app/app"

# Computer::Boot.new.start
1 change: 1 addition & 0 deletions lib/computer/system/desktop.rb
@@ -0,0 +1 @@
# TODO: create your desktop icons.
1 change: 1 addition & 0 deletions lib/computer/system/system.rb
Expand Up @@ -4,3 +4,4 @@ module System; end
end

require_relative "./login.rb"
require_relative "./desktop.rb"
4 changes: 4 additions & 0 deletions spec/computer/boot_spec.rb
@@ -1,6 +1,10 @@
describe Computer::Boot do
subject(:boot) { described_class.new.start }

# This might be the first time you are looking at a test:
# Context -> group of different expectation for a given scenario
# Before -> runs before all the expectations
# http://testing-for-beginners.rubymonstas.org/rspec.html
context "when initializing a computer, it should present information using sleep()" do
before do
allow_any_instance_of(described_class).to receive(:sleep) { puts("Sleeping...") }
Expand Down
50 changes: 50 additions & 0 deletions spec/computer/system/login_spec.rb
@@ -0,0 +1,50 @@
describe Computer::System::Login do
let(:pin) { "1234" }

# The `#` symbol means we are describing an `instance` method in the class.
describe "#prompt_user" do
subject(:login) { described_class.new }

before do
allow_any_instance_of(described_class).to receive(:gets) { instance_double("gets.chomp", chomp: pin) }
allow_any_instance_of(described_class).to receive(:user_input).and_return(false, true)
end

it "keeps prompting the user for a PIN until it gets a valid one" do
expect { login.prompt_user }.to output(/The PIN is incorrect, try again/).to_stdout
end

it "prompts the user for a PIN" do
expect { login.prompt_user }.to output(/In order to access this computer, please insert your PIN/).to_stdout
end
end

# The `#` symbol means we are describing an `instance` method
describe "#authenticate" do
subject(:login) { described_class.new.authenticate(pin: pin_input) }

let(:pin_input) { "" }

before { stub_const("Computer::System::Login::PIN", pin) }

it "has a PIN constant that holds the value to check against" do
expect(described_class::PIN).to be_a(String)
end

context "when the pin is correct" do
let(:pin_input) { pin }

it "authenticates a user when a PIN is correctly filled" do
expect(login).to eq true
end
end

context "when the pin is incorrect" do
let(:input_pin) { "wrong-pin" }

it "doesn't authenticate a user when a PIN is incorrect" do
expect(login).to eq false
end
end
end
end

0 comments on commit 27d0aaf

Please sign in to comment.