Skip to content

Commit

Permalink
Implemented Lotus::Utils::Kernel.Symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
jodosha committed Jun 19, 2014
1 parent 3455473 commit a969fda
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## v0.2.0
### Jun 23, 2014

1894be5 2014-06-12 **Luca Guidi** Implemented Lotus::Utils::Kernel.Symbol

13532f5 2014-06-13 **Luca Guidi** [breaking] Implemented Lotus::Utils::Callbacks::Chain#freeze in order to prevent modification after the object has been frozen

e438da8 2014-06-13 **Luca Guidi** [breaking] All the Utils::Kernel methods will raise TypeError in case of failed coercion.
Expand Down
52 changes: 52 additions & 0 deletions lib/lotus/utils/kernel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,58 @@ def self.Pathname(arg)
rescue NoMethodError
raise TypeError.new "can't convert into Pathname"
end

# Coerces the argument to be a String.
#
# @param arg [#to_sym] the argument
#
# @return [Symbol] the result of the coercion
#
# @raise [TypeError] if the argument can't be coerced
#
# @since 0.2.0
#
# @example Basic Usage
# require 'lotus/utils/kernel'
#
# Lotus::Utils::Kernel.Symbol(:hello) # => :hello
# Lotus::Utils::Kernel.Symbol('hello') # => :hello
#
# @example Symbol Interface
# require 'lotus/utils/kernel'
#
# class StatusSymbol
# def to_sym
# :success
# end
# end
#
# Lotus::Utils::Kernel.Symbol(StatusSymbol.new) # => :success
#
# @example Unchecked Exceptions
# require 'lotus/utils/kernel'
#
# # When nil
# input = nil
# Lotus::Utils::Kernel.Symbol(input) # => TypeError
#
# # When empty string
# input = ''
# Lotus::Utils::Kernel.Symbol(input) # => TypeError
#
# # Missing #respond_to?
# input = BasicObject.new
# Lotus::Utils::Kernel.Symbol(input) # => TypeError
def self.Symbol(arg)
case arg
when '' then raise TypeError.new "can't convert into Symbol"
when ->(a) { a.respond_to?(:to_sym) } then arg.to_sym
else
raise TypeError.new "can't convert into Symbol"
end
rescue NoMethodError
raise TypeError.new "can't convert into Symbol"
end
end
end
end
Expand Down
124 changes: 124 additions & 0 deletions test/kernel_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2096,4 +2096,128 @@ def to_pathname
end
end
end

describe '.Symbol' do
describe 'successful operations' do
before do
class StatusSymbol
def to_sym
:success
end
end

@result = Lotus::Utils::Kernel.Symbol(input)
end

after do
Object.send(:remove_const, :StatusSymbol)
end

describe 'when a symbol is given' do
let(:input) { :hello }

it 'returns a symbol' do
@result.must_equal :hello
end
end

describe 'when a string is given' do
let(:input) { 'hello' }

it 'returns a symbol' do
@result.must_equal :hello
end
end

describe 'when an object that implements #to_sym' do
let(:input) { StatusSymbol.new }

it 'returns a symbol' do
@result.must_equal :success
end
end
end

describe 'failure operations' do
describe "when nil is given" do
let(:input) { nil }

it 'raises error' do
-> { Lotus::Utils::Kernel.Symbol(input) }.must_raise TypeError
end
end

describe "when empty string is given" do
let(:input) { '' }

it 'raises error' do
-> { Lotus::Utils::Kernel.Symbol(input) }.must_raise TypeError
end
end

describe "when true is given" do
let(:input) { true }

it 'raises error' do
-> { Lotus::Utils::Kernel.Symbol(input) }.must_raise TypeError
end
end

describe "when false is given" do
let(:input) { false }

it 'raises error' do
-> { Lotus::Utils::Kernel.Symbol(input) }.must_raise TypeError
end
end

describe "when a number is given" do
let(:input) { 12 }

it 'raises error' do
-> { Lotus::Utils::Kernel.Symbol(input) }.must_raise TypeError
end
end

describe "when a date is given" do
let(:input) { Date.today }

it 'raises error' do
-> { Lotus::Utils::Kernel.Symbol(input) }.must_raise TypeError
end
end

describe "when a datetime is given" do
let(:input) { DateTime.now }

it 'raises error' do
-> { Lotus::Utils::Kernel.Symbol(input) }.must_raise TypeError
end
end

describe "when a time is given" do
let(:input) { Time.now }

it 'raises error' do
-> { Lotus::Utils::Kernel.Symbol(input) }.must_raise TypeError
end
end

describe "when an object that doesn't implement #respond_to?" do
let(:input) { BasicObject.new }

it 'raises error' do
-> { Lotus::Utils::Kernel.Symbol(input) }.must_raise TypeError
end

it 'returns useful informations about the failure' do
begin
Lotus::Utils::Kernel.Symbol(input)
rescue => e
e.message.must_equal "can't convert into Symbol"
end
end
end
end
end
end

0 comments on commit a969fda

Please sign in to comment.