class Functionator attr_reader :allowed_methods, :callers, :next_caller def initialize(s) @allowed_methods = s.split init_caller end def method_missing(method, *args) method = method.to_s raise NoMethodError, 'Not exists' unless @allowed_methods.include?(method) raise NoMethodError, 'Wrong order' unless @next_caller == method wait_next_caller self end private def wait_next_caller @next_caller = @callers.next rescue StopIteration init_caller end def init_caller @callers = @allowed_methods.each @next_caller = @callers.next end end def functionator(methods) Functionator.new methods end
- class Functionator
- attr_reader :allowed_methods, :callers, :next_caller
- def initialize(s)
s.split(' ').each { | i | define_singleton_method :"#{i}" do Functionator.new(s.split(' ')[1..-1].join(' ')) end }- @allowed_methods = s.split
- init_caller
- end
- def method_missing(method, *args)
- method = method.to_s
- raise NoMethodError, 'Not exists' unless @allowed_methods.include?(method)
- raise NoMethodError, 'Wrong order' unless @next_caller == method
- wait_next_caller
- self
- end
- private
- def wait_next_caller
- @next_caller = @callers.next
- rescue StopIteration
- init_caller
- end
- def init_caller
- @callers = @allowed_methods.each
- @next_caller = @callers.next
- end
- end
def functionator(string)Functionator.new(string)- def functionator(methods)
- Functionator.new methods
- end
describe 'Functionator' do subject(:func) { functionator(input_data) } context 'when first chain is correct' do let(:input_data) { 'there are two kinds of people' } it do expect { func.there().are().two().kinds().of().people() }.not_to raise_error end end context 'when second chain is correct' do let(:input_data) { 'hello codewars world' } it do expect { func.hello().codewars().world() }.not_to raise_error end end context 'when third chain is NOT correct' do let(:input_data) { 'theraretwokindsofpeople' } it do expect { func.is_expected.theraretwokindsofpeople }.to raise_error StandardError end end context 'when fourth chain is NOT correct' do let(:input_data) { 'hello word' } it do expect { func.is_expected.word().hello() }.to raise_error StandardError end end context 'when fifth chain is NOT correct' do let(:input_data) { 'hello word' } it do expect { func.is_expected.hello().word }.to raise_error StandardError end end end
# TODO: Replace examples and use TDD development by writing your own tests# These are some of the methods available:# Test.expect(boolean, [optional] message)# Test.assert_equals(actual, expected, [optional] message)# Test.assert_not_equals(actual, expected, [optional] message)describe "t e s t s" doit "t e s t s" doobj = functionator('there are two kinds of people')Test.expect(obj.there().are().two().kinds().of().people(),'Chain is incorrect');- describe 'Functionator' do
- subject(:func) { functionator(input_data) }
obj = functionator('hello codewars world')Test.expect(obj.hello().codewars().world(),'Chain is incorrect');- context 'when first chain is correct' do
- let(:input_data) { 'there are two kinds of people' }
obj = functionator('theraretwokindsofpeople')Test.expect(obj.theraretwokindsofpeople,'theraretwokindsofpeople is not defined');- it do
- expect { func.there().are().two().kinds().of().people() }.not_to raise_error
- end
- end
- context 'when second chain is correct' do
- let(:input_data) { 'hello codewars world' }
- it do
- expect { func.hello().codewars().world() }.not_to raise_error
- end
- end
- context 'when third chain is NOT correct' do
- let(:input_data) { 'theraretwokindsofpeople' }
- it do
- expect { func.is_expected.theraretwokindsofpeople }.to raise_error StandardError
- end
- end
- context 'when fourth chain is NOT correct' do
- let(:input_data) { 'hello word' }
- it do
- expect { func.is_expected.word().hello() }.to raise_error StandardError
- end
- end
- context 'when fifth chain is NOT correct' do
- let(:input_data) { 'hello word' }
- it do
- expect { func.is_expected.hello().word }.to raise_error StandardError
- end
- end
- end
def array_of_two(n) [n.to_int, n.to_int] end
- def array_of_two(n)
raise 'n is not a number' unless n.is_a?(Numeric) # comment to fail test case[n, n]- [n.to_int, n.to_int]
- end
describe "array_of_two" do context "When raise an exception if N is not a number" do it do expect { array_of_two('hello world') }.to raise_error StandardError end it do expect { array_of_two('2') }.to raise_error StandardError end end context "When array of two integer" do subject { array_of_two(2) } it { is_expected.to eq [2, 2] } end end
describe "Test#expect_error" doTest.expect_error("array_of_two should raise an exception if N is not a number") { array_of_two('hello world') }- describe "array_of_two" do
- context "When raise an exception if N is not a number" do
- it do
- expect { array_of_two('hello world') }.to raise_error StandardError
- end
- it do
- expect { array_of_two('2') }.to raise_error StandardError
- end
- end
- context "When array of two integer" do
- subject { array_of_two(2) }
- it { is_expected.to eq [2, 2] }
- end
- end