From dc49afb20c0635937f2e56c84856faab91819108 Mon Sep 17 00:00:00 2001 From: rashidsp Date: Tue, 13 Mar 2018 17:00:52 +0500 Subject: [PATCH] coverall test --- lib/optimizely/notification_center.rb | 5 +- spec/notification_center_spec.rb | 99 ++++++++++++++++++++------- 2 files changed, 79 insertions(+), 25 deletions(-) diff --git a/lib/optimizely/notification_center.rb b/lib/optimizely/notification_center.rb index 984799b9..0a592f75 100644 --- a/lib/optimizely/notification_center.rb +++ b/lib/optimizely/notification_center.rb @@ -33,7 +33,7 @@ def initialize(logger, error_handler) @error_handler = error_handler end - def add_notification_listener(notification_type, notification_callback) + def add_notification_listener(notification_type, callback = nil, &block) # Adds notification callback to the notification center # Args: @@ -45,12 +45,13 @@ def add_notification_listener(notification_type, notification_callback) return nil unless notification_type_valid?(notification_type) + notification_callback = callback.present? ? callback : block unless notification_callback @logger.log Logger::ERROR, 'Callback can not be empty.' return nil end - unless notification_callback.is_a? Method + unless notification_callback.respond_to? :call @logger.log Logger::ERROR, 'Invalid notification callback given.' return nil end diff --git a/spec/notification_center_spec.rb b/spec/notification_center_spec.rb index b3df98c2..b687b722 100644 --- a/spec/notification_center_spec.rb +++ b/spec/notification_center_spec.rb @@ -30,9 +30,7 @@ before(:context) do class CallBack - def call(args) - args - end + def call; end end @callback = CallBack.new @@ -88,6 +86,47 @@ def call(args) expect(notification_center.notifications[Optimizely::NotificationCenter::NOTIFICATION_TYPES[:ACTIVATE]].length) .to eq(1) end + + it 'should add, and return notification ID when valid callbacks as closures are added' do + def sum(args) + spy_logger.log Logger::INFO, "Sum: #{args.inject(:+)}" + end + + callback_proc = proc { |args| sum(args) } + callback_lambda = ->(args) { sum(args) } + + expect(notification_center.add_notification_listener( + Optimizely::NotificationCenter::NOTIFICATION_TYPES[:ACTIVATE], + callback_proc + )).to eq(1) + + expect(notification_center.add_notification_listener( + Optimizely::NotificationCenter::NOTIFICATION_TYPES[:ACTIVATE], + callback_lambda + )).to eq(2) + + # verifies that two notifications are added + expect(notification_center.notifications[Optimizely::NotificationCenter::NOTIFICATION_TYPES[:ACTIVATE]].length) + .to eq(2) + end + + it 'should add, and return notification ID when callback as a block is added' do + expect(notification_center.add_notification_listener( + Optimizely::NotificationCenter::NOTIFICATION_TYPES[:ACTIVATE] + ) { |args| spy_logger.log Logger::INFO, "Sum: #{args.inject(:+)}" }).to eq(1) + end + + it 'should add only closure callback when both closure and a block are sent' do + closure_callback = proc { |args| spy_logger.log Logger::INFO, "closure sum: #{args.inject(:+)}" } + expect(notification_center.add_notification_listener( + Optimizely::NotificationCenter::NOTIFICATION_TYPES[:ACTIVATE], + closure_callback + ) { |args| spy_logger.log Logger::INFO, "block sum: #{args.inject(:+)}" }).to eq(1) + + # verifies that only closure callback is added + expect(notification_center.notifications[Optimizely::NotificationCenter::NOTIFICATION_TYPES[:ACTIVATE]][0][:callback]) + .to eq(closure_callback) + end end describe 'test add notification for multiple notification types' do @@ -108,9 +147,7 @@ def call(args) it 'should add and return notification ID when multiple valid callbacks are added for a single notification type' do class CallBackSecond - def call(_args) - 'Test multi listner.' - end + def call; end end @callback_second = CallBackSecond.new @@ -166,11 +203,6 @@ def call(_args) let(:notification_center) { Optimizely::NotificationCenter.new(spy_logger, raise_error_handler) } before(:example) do @inner_notification_center = notification_center - class CallBackSecond - def call(_args) - 'Test remove notification.' - end - end @callback_second = CallBackSecond.new @callback_reference_second = @callback_second.method(:call) @@ -321,19 +353,12 @@ def call(_args) let(:notification_center) { Optimizely::NotificationCenter.new(spy_logger, raise_error_handler) } before(:example) do @inner_notification_center = notification_center - class CallBackSecond - def call(_args) - 'Test remove notification.' - end - end @callback_second = CallBackSecond.new @callback_reference_second = @callback_second.method(:call) class CallBackThird - def call(_args) - 'Test remove notification.' - end + def call; end end @callback_third = CallBackThird.new @@ -406,9 +431,7 @@ def deliver_two(_args) @logger.log Logger::INFO, 'delivered two.' end - def deliver_three(_args) - @logger.log Logger::INFO, 'delivered three.' - end + def deliver_three; end end let(:raise_error_handler) { Optimizely::RaiseErrorHandler.new } let(:invitation) { Invitation.new(spy_logger) } @@ -470,7 +493,7 @@ def deliver_three(_args) .with(Logger::INFO, 'delivered two.') end - it 'should send notifications and verify that all callbacks are called' do + it 'should send notifications and verify that all callbacks as method references are called' do notification_type_decision = Optimizely::NotificationCenter::NOTIFICATION_TYPES[:ACTIVATE] notification_type_track = Optimizely::NotificationCenter::NOTIFICATION_TYPES[:TRACK] @@ -488,6 +511,36 @@ def deliver_three(_args) expect(spy_logger).to_not have_received(:log) .with(Logger::INFO, 'delivered three.') end + + it 'should send notifications and verify that all callbacks as closures are called' do + notification_type_decision = Optimizely::NotificationCenter::NOTIFICATION_TYPES[:ACTIVATE] + + def sum(args) + spy_logger.log Logger::INFO, "Sum: #{args.inject(:+)}" + end + + callback_proc = proc { |args| sum(args) } + callback_lambda = ->(args) { sum(args) } + + notification_center.add_notification_listener(notification_type_decision, callback_lambda) + notification_center.add_notification_listener(notification_type_decision, callback_proc) + args = [8, 8] + notification_center.send_notifications(notification_type_decision, args) + expect(spy_logger).to have_received(:log).twice + .with(Logger::INFO, 'Sum: 16') + end + + it 'should send notifications and verify that callback as a block is called' do + notification_type_decision = Optimizely::NotificationCenter::NOTIFICATION_TYPES[:ACTIVATE] + + notification_center.add_notification_listener( + notification_type_decision + ) { |args| spy_logger.log Logger::INFO, "Sum: #{args.inject(:+)}" } + args = [8, 8] + notification_center.send_notifications(notification_type_decision, args) + expect(spy_logger).to have_received(:log).once + .with(Logger::INFO, 'Sum: 16') + end end describe '@error_handler' do