From fd19b5a980757dafc42002b364dbb9503ad2504e Mon Sep 17 00:00:00 2001 From: Kevin Perez Date: Sat, 11 May 2019 00:11:22 -0500 Subject: [PATCH] add some unit tests for new rake tasks --- lib/tasks/support/methods.rb | 18 ++++---- test/unit/tasks/support/methods_test.rb | 57 +++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 8 deletions(-) create mode 100644 test/unit/tasks/support/methods_test.rb diff --git a/lib/tasks/support/methods.rb b/lib/tasks/support/methods.rb index 03ecb1d..c2e7776 100644 --- a/lib/tasks/support/methods.rb +++ b/lib/tasks/support/methods.rb @@ -7,14 +7,6 @@ def status_label(enabled) enabled.nil? ? '' : (enabled ? 'ON' : 'OFF') end - def find_feature_by_name(name) - features.find { |f| f.name == name } || raise("Feature :#{name} is not defined.") - end - - def find_strategy_by_name(name) - strategies.find { |s| s.name == name } || raise("Strategy :#{name} is not available.") - end - def table_header %w[feature description] + strategies.map(&:name) end @@ -48,6 +40,16 @@ def clear_feature!(feature_name, strategy_name) strategy.clear!(feature.key) end + + protected + + def find_feature_by_name(name) + features.find { |f| f.name == name } || raise("Feature :#{name} is not defined.") + end + + def find_strategy_by_name(name) + strategies.find { |s| s.name == name } || raise("Strategy :#{name} is not available.") + end end end end diff --git a/test/unit/tasks/support/methods_test.rb b/test/unit/tasks/support/methods_test.rb new file mode 100644 index 0000000..e50a2ac --- /dev/null +++ b/test/unit/tasks/support/methods_test.rb @@ -0,0 +1,57 @@ +require File.expand_path('../../../../test_helper', __FILE__) +require File.expand_path('../../../../../lib/tasks/support/methods', __FILE__) + +describe Flipflop::Rake::SupportMethods do + subject do + object = Object.new + object.extend Flipflop::Rake::SupportMethods + end + + describe '#status_label' do + it 'returns the "enabled" label when its argument is "true"' do + assert_equal 'ON', subject.status_label(true) + end + + it 'returns the "disabled" label when its argument is "false"' do + assert_equal 'OFF', subject.status_label(false) + end + + it 'returns the "unset" label when its argument is "nil"' do + assert_equal '', subject.status_label(nil) + end + end + + describe '#switch_feature!' do + def with_feature_and_strategy + # Stubs finder methods to avoid going into FeatureSet code. + feature = Flipflop::FeatureDefinition.new(:world_domination) + subject.stub :find_feature_by_name, feature do + strategy = Flipflop::Strategies::TestStrategy.new(name: 'test') + subject.stub :find_strategy_by_name, strategy do + yield(strategy, feature) if block_given? + end + end + end + + it 'enables a feature using a strategy' do + with_feature_and_strategy do |strategy, feature| + subject.switch_feature! 'world_domination', 'test', true + assert_equal true, strategy.enabled?(feature.key) + end + end + + it 'disables a feature using a strategy' do + with_feature_and_strategy do |strategy, feature| + subject.switch_feature! 'world_domination', 'test', false + assert_equal false, strategy.enabled?(feature.key) + end + end + + it 'clears a feature using a strategy' do + with_feature_and_strategy do |strategy, feature| + subject.clear_feature! 'world_domination', 'test' + assert_nil strategy.enabled?(feature.key) + end + end + end +end