Created
August 26, 2020 12:20
-
-
Save dearshrewdwit/d82e37937eba39c7d1aba907b5185061 to your computer and use it in GitHub Desktop.
Secret Diary demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ------------------------------------------------------- | |
# | |
# spec/diary_spec.rb | |
describe 'Diary' do | |
it 'can add entry' do | |
diary = Diary.new | |
expect(diary.add('hello')).to eq :ok | |
end | |
it 'can read entries' do | |
diary = Diary.new | |
diary.add('hello') | |
diary.add('world') | |
expect(diary.read).to eq "hello\nworld" | |
end | |
end | |
# ------------------------------------------------------- | |
# | |
# lib/diary.rb | |
class Diary | |
def initialize | |
@entries = [] | |
end | |
def add(str) | |
@entries << str | |
:ok | |
end | |
def read | |
@entries.join("\n") | |
end | |
end | |
# ------------------------------------------------------- | |
# | |
# spec/secret_diary_spec.rb | |
describe 'A secret diary' do | |
describe 'can be locked' do | |
it 'starts locked' do | |
diary = test_double | |
secret_diary = SecretDiary.new(diary) | |
expect(secret_diary.locked?).to eq true | |
end | |
it 'can be unlocked' do | |
diary = test_double | |
secret_diary = SecretDiary.new(diary) | |
secret_diary.unlock | |
expect(secret_diary.locked?).to eq false | |
end | |
it 'can be locked' do | |
diary = test_double | |
secret_diary = SecretDiary.new(diary) | |
secret_diary.unlock | |
secret_diary.lock | |
expect(secret_diary.locked?).to eq true | |
end | |
end | |
describe 'when locked' do | |
it 'can not add entry' do | |
diary = test_double | |
secret_diary = SecretDiary.new(diary) | |
expect(secret_diary.add('hello')).to eq 'Sorry, diary is locked!' | |
end | |
it 'can not read entries' do | |
diary = test_double | |
secret_diary = SecretDiary.new(diary) | |
expect(secret_diary.read).to eq 'Sorry, diary is locked!' | |
end | |
end | |
describe 'when unlocked' do | |
it 'can add entry' do | |
diary = test_double | |
allow(diary).to receive(:add) { :ok } | |
secret_diary = SecretDiary.new(diary) | |
secret_diary.unlock | |
expect(secret_diary.add('hello')).to eq :ok | |
end | |
it 'can read entries' do | |
diary = test_double | |
allow(diary).to receive(:add) { :ok } | |
allow(diary).to receive(:read) { "hello\nworld" } | |
secret_diary = SecretDiary.new(diary) | |
secret_diary.unlock | |
secret_diary.add('hello') | |
secret_diary.add('world') | |
expect(secret_diary.read).to eq "hello\nworld" | |
end | |
end | |
end | |
# ------------------------------------------------------- | |
# | |
# lib/secret_diary.rb | |
require_relative 'diary.rb' | |
class SecretDiary | |
def initialize(diary) | |
@status = :locked | |
@diary = diary | |
end | |
def add(str) | |
return "Sorry, diary is locked!" if locked? | |
@diary.add(str) | |
end | |
def read | |
return "Sorry, diary is locked!" if locked? | |
@diary.read | |
end | |
def unlock | |
@status = :unlocked | |
end | |
def lock | |
@status = :locked | |
end | |
def locked? | |
@status == :locked | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment