Skip to content

Instantly share code, notes, and snippets.

@sumanmukherjee03
Created May 21, 2013 19:51
Show Gist options
  • Save sumanmukherjee03/5622688 to your computer and use it in GitHub Desktop.
Save sumanmukherjee03/5622688 to your computer and use it in GitHub Desktop.
Implementation of Greeting Message application and barewords
require 'rspec'
require 'forwardable'
RSpec.configure do |config|
config.mock_with :rspec
config.color_enabled = true
end
def program_name
"Greeting Messages"
end
module GreetingMessages
VERSION = "Version 1"
def version
VERSION
end
class Error < StandardError
def initialize(attr)
super("#{attr.to_s} is not present")
end
end
end
class Specialization
def initialize(subject)
@subject = subject
end
def specialization
"#{@subject} magic"
end
end
class PairingSession
include GreetingMessages
extend ::Forwardable
SALUTATION = "Hi"
attr_reader :pair_name
def_delegators :@specialization, :specialization
def initialize(p_name, sp)
@pair_name = p_name
@specialization = Specialization.new(sp)
end
def salutation
SALUTATION
end
def greet(person_info = {})
[:title, :first_name, :last_name].each {|sym| raise Error.new(sym) unless person_info[sym]}
title = "#{person_info[:title]}"
name = "#{person_info[:first_name]} #{person_info[:last_name]}"
"#{salutation} #{title} #{name}, welcome to #{program_name} #{version}. My name is #{pair_name}. Let me show you some #{specialization}."
end
end
describe "barewords" do
it "prints a greeting message for Suman Mukherjee" do
PairingSession.new("James", "Ruby").greet(:title => "Mr", :first_name => "Suman", :last_name => "Mukherjee").should eq("Hi Mr Suman Mukherjee, welcome to Greeting Messages Version 1. My name is James. Let me show you some Ruby magic.")
end
it "prints a greeting message for Vicky Farrand" do
PairingSession.new("Mike", "Javascript").greet(:title => "Miss", :first_name => "Vicky", :last_name => "Farrand").should eq("Hi Miss Vicky Farrand, welcome to Greeting Messages Version 1. My name is Mike. Let me show you some Javascript magic.")
end
context "when parameters are not passed" do
it "raises an error" do
expect { PairingSession.new("Mike", "Javascript").greet(:first_name => "Vicky", :last_name => "Farrand") }.to raise_exception(GreetingMessages::Error, "title is not present")
expect { PairingSession.new("Mike", "Javascript").greet(:title => "Miss", :last_name => "Farrand")}.to raise_exception(GreetingMessages::Error, "first_name is not present")
expect { PairingSession.new("Mike", "Javascript").greet(:title => "Miss", :first_name => "Vicky")}.to raise_exception(GreetingMessages::Error, "last_name is not present")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment