Created
October 25, 2012 21:54
-
-
Save flakyfilibuster/3955684 to your computer and use it in GitHub Desktop.
Rspec Hospital
This file contains 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
class Hospital | |
attr_reader :name, :location, :employees, :patients, :admins | |
def initialize(name, location) | |
@name, @location, @employees, @patients, @admins = name, location, [], [], [] | |
end | |
end | |
class Person | |
attr_reader :name, :username, :id | |
def initialize(name, username, password, id) | |
@name, @username, @password, @id = name, username, password, id | |
end | |
end | |
class Patient < Person | |
attr_reader :name, :username, :password, :condition | |
def initialize(name, username, password, condition, id) | |
super(name, username, password, id) | |
@condition = condition | |
end | |
end | |
class Employees < Person | |
attr_reader :name, :username, :password, :expertise | |
def initialize(name, username, password, expertise, id) | |
super(name, username, password, id) | |
@expertise = expertise | |
end | |
end | |
class Doctor < Employees | |
end | |
class Admin < Employees | |
end | |
# => User Interface Logic | |
######################################################## | |
class UserInterface | |
def initialize(hospital) | |
@hospital = hospital | |
@username = "" | |
@password = "" | |
welcome | |
end | |
def welcome | |
puts "Welcome to #{@hospital.name}s database interface".ljust(40) | |
puts "".ljust(40,"-") | |
select_screen if authorized? | |
end | |
def authorized? | |
ask_for_username and ask_for_password | |
end | |
# returns true if username is correct | |
def ask_for_username | |
valid_user_name_or_exit = false | |
until valid_user_name_or_exit | |
puts "Please enter your username or enter 'exit'" | |
print ">" | |
@username = gets.chomp | |
#puts @username | |
valid_user_name_or_exit = true if username_check | |
end | |
valid_user_name_or_exit | |
end | |
# returns true if password is correct | |
def ask_for_password | |
valid_user_name_or_exit = false | |
until valid_user_name_or_exit | |
puts "Please enter your password or enter 'exit'" | |
print ">" | |
@password = gets.chomp | |
valid_user_name_or_exit = true if password_check | |
end | |
valid_user_name_or_exit | |
end | |
def username_check | |
array = (@hospital.patients + @hospital.employees + @hospital.admins).map{|user| user.username} | |
array.include?(@username) ? true : false | |
end | |
def password_check | |
array = (@hospital.patients + @hospital.employees + @hospital.admins) | |
array.map!{|user| user.username == @username && user.password == @password} | |
array.include?(true) | |
end | |
def clearance_check | |
user_clearance = (@hospital.patients + @hospital.employees + @hospital.admins) | |
user_clearance.map{|user| user_clearance = user if user.username == @username && user.password == @password} | |
user_clearance | |
end | |
def select_screen | |
user_clearance = clearance_check | |
prompt_admin = | |
""" | |
------------------------------------------ | |
Welcome #{user_clearance.name} | |
your clearance level is: #{user_clearance.class} | |
Please select one of the following options: | |
------------------------------------------ | |
> 'ls p' to list all patients | |
> 'ls d' to list all doctors | |
> 'ap' add patient | |
> 'rp' remove patient | |
> 'ad' add doctor | |
> 'rd' remove doctor | |
> 'log' to sign in as another user | |
------------------------------------------ | |
""" | |
prompt_doctor = | |
""" | |
------------------------------------------ | |
Welcome #{user_clearance.name} | |
your clearance level is: #{user_clearance.class} | |
Please select one of the following options: | |
------------------------------------------ | |
> 'ls p' to list all patients | |
> 'ls d' to list all doctors | |
> 'ap' add patient | |
> 'rp' remove patient | |
> 'log' to sign in as another user | |
------------------------------------------ | |
""" | |
prompt_patient = | |
""" | |
------------------------------------------ | |
Welcome #{user_clearance.name} | |
your clearance level is: #{user_clearance.class} | |
Please select one of the following options: | |
------------------------------------------ | |
> 'ls p' to list all patients | |
> 'log' to sign in as another user | |
------------------------------------------ | |
""" | |
puts prompt_admin if user_clearance.class == Admin | |
puts prompt_doctor if user_clearance.class == Doctor | |
print ">" | |
while true | |
selection = gets.chomp | |
if selection == "ls p" | |
list_patients | |
elsif selection == "ap" | |
add_patient | |
elsif selection == "rp" | |
remove_patient | |
elsif selection == "ls d" | |
list_doctors | |
elsif selection == "ad" | |
add_doctor | |
elsif selection == "rd" | |
remove_doctor | |
elsif selection == "log" | |
puts | |
puts "...jumping back to login screen..." | |
puts "\e[H\e[2J" | |
login | |
else | |
puts | |
puts "Please provide a proper option" | |
next | |
end | |
end | |
end | |
# => Patient logic | |
####################################### | |
def list_patients | |
@hospital.patients.each{|patient| puts "ID: #{patient.id} || name: #{patient.name} || username: #{patient.username} || password: #{patient.password} || condition: #{patient.condition}"} | |
select_screen | |
end | |
def add_patient | |
puts "Patient add screen:" | |
puts "please specify patients name:" | |
print ">" | |
patient_name = gets.chomp | |
puts "please specify patients username:" | |
print ">" | |
patient_username = gets.chomp | |
puts "please specify patients password:" | |
print ">" | |
patient_password = gets.chomp | |
puts "please specify patients condition:" | |
print ">" | |
patient_condition = gets.chomp | |
id = @hospital.patients.size | |
@hospital.patients << id = Patient.new(patient_name, patient_username, patient_password, patient_condition, id) | |
puts "name: #{patient_name} || username: #{patient_username} || password: #{patient_password} || patients condition: #{patient_condition}" | |
select_screen | |
end | |
def remove_patient | |
puts "Patient remove screen:" | |
puts "please specify patients ID:" | |
print ">" | |
patient_id = gets.chomp.to_i | |
@hospital.patients.each do |patient| | |
if patient.id == patient_id | |
@hospital.patients.delete_at(patient_id) | |
end | |
end | |
puts "patient removed" | |
select_screen | |
end | |
def list_patients | |
@hospital.patients.each{|patient| puts "ID: #{patient.id} || name: #{patient.name} || username: #{patient.username} || password: #{patient.password} || condition: #{patient.condition}"} | |
select_screen | |
end | |
# => Doctors logic | |
####################################### | |
def add_doctor | |
puts "Doctor add screen:" | |
puts "please specify doctors name:" | |
print ">" | |
doctor_name = gets.chomp | |
puts "please specify doctors username:" | |
print ">" | |
doctor_username = gets.chomp | |
puts "please specify doctors password:" | |
print ">" | |
doctor_password = gets.chomp | |
puts "please specify doctors expertise:" | |
print ">" | |
doctor_condition = gets.chomp | |
id = @hospital.employees.size | |
@hospital.employees << id = Employees.new(doctor_name, doctor_username, doctor_password, doctor_condition, id) | |
puts "name: #{doctor_name} || username: #{doctor_username} || password: #{doctor_password} || doctors expertise: #{doctor_expertise}" | |
select_screen | |
end | |
def remove_doctor | |
puts "Doctor remove screen:" | |
puts "please specify doctors ID:" | |
print ">" | |
doctor_id = gets.chomp.to_i | |
@hospital.employees.each do |employee| | |
if employee.id == doctor_id | |
@hospital.employees.delete_at(doctor_id) | |
end | |
end | |
puts "doctor removed" | |
select_screen | |
end | |
def list_doctors | |
@hospital.employees.each{|doctor| puts "ID: #{doctor.id} || name: #{doctor.name} || username: #{doctor.username} || password: #{doctor.password} || expertise: #{doctor.expertise}"} | |
select_screen | |
end | |
end | |
# sf_central = Hospital.new("SF Central", "San Francisco") | |
# sf_central.admins << admin = Admin.new("Ferdi Cam", "admin", "12345", "admin", 0) | |
# sf_central.employees << doctor = Doctor.new("John doe", "doctor", "111111", "surgeon", 0) | |
# sf_central.patients << patient = Patient.new("Jane Doe", "patient", "222222", "head trauma", 0) | |
# sf_central_mainframe = UserInterface.new(sf_central) | |
This file contains 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
require 'simplecov' | |
SimpleCov.start | |
require './hospital_interface' | |
#Opens up our first big test | |
describe Hospital do | |
let(:hospital) { Hospital.new("SF Central", "San Francisco") } | |
# before(:each) do | |
# @hospital = Hospital.new | |
# end | |
# context to organize tests within tests - think of it as methods within classes | |
context "#initialize" do # method name we want to test and all the tests that we want to run for it - '#' denots that we are testing a method | |
# let(:name) {hospital.name} \ so instead of calling hospital.name everytime we can assign hospital.name -> :name | |
it "should have a name" do # our expectation - usually starts with it "should...." | |
hospital.name.should eq "SF Central" | |
end | |
it "should have a location / locationname" do | |
hospital.location.should eq "San Francisco" | |
end | |
it "should not have patients" do | |
hospital.patients.should be_empty | |
end | |
it "should not have employees" do | |
hospital.employees.should be_empty | |
end | |
it "should not have admins" do | |
hospital.admins.should be_empty | |
end | |
end | |
end | |
describe UserInterface do | |
let(:hospital) { Hospital.new("SF Central", "San Francisco") } | |
context "initialize" do | |
it "should call the welcome method" do | |
UserInterface.any_instance.should_receive(:welcome) #expectation | |
UserInterface.new(hospital) # case that is 'hopefully' satisfying our expectation | WE NEED THIS! | |
end | |
end | |
context "#welcome" do | |
it "should call the login method" do | |
# welcome gets called from initialize | |
UserInterface.any_instance.should_receive(:select_screen) | |
UserInterface.any_instance.stub(:authorized?).and_return(true) | |
UserInterface.new(hospital) | |
end | |
end | |
end | |
# mocking takes over the method and has an expectation | |
# stubbing takes over the method and has NO expectation |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment