Skip to content

Commit

Permalink
Tests for block with single and double arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
jnunemaker committed Sep 24, 2016
1 parent 5b06bdf commit ac2cb1f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
17 changes: 14 additions & 3 deletions lib/flipper/types/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,22 @@ def self.wrap(group_or_name)
def initialize(name, &block)
@name = name.to_sym
@value = @name
@block = block

if block_given?
@block = block
@single_argument = @block.arity == 1
else
@block = lambda { |thing, context| false }
@single_argument = false
end
end

def match?(*args)
@block.call(*args)
def match?(thing, context)
if @single_argument
@block.call(thing)
else
@block.call(thing, context)
end
end
end
end
Expand Down
33 changes: 29 additions & 4 deletions spec/flipper/types/group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
require 'flipper/types/group'

RSpec.describe Flipper::Types::Group do
let(:fake_context) { double("FeatureCheckContext") }

subject do
Flipper.register(:admins) { |actor| actor.admin? }
end
Expand Down Expand Up @@ -42,25 +44,48 @@
let(:non_admin_actor) { double('Actor', :admin? => false) }

it "returns true if block matches" do
expect(subject.match?(admin_actor)).to eq(true)
expect(subject.match?(admin_actor, fake_context)).to eq(true)
end

it "returns false if block does not match" do
expect(subject.match?(non_admin_actor)).to eq(false)
expect(subject.match?(non_admin_actor, fake_context)).to eq(false)
end

it "returns true for truthy block results" do
group = Flipper::Types::Group.new(:examples) do |actor|
actor.email =~ /@example\.com/
end
expect(group.match?(double('Actor', :email => "foo@example.com"))).to be_truthy
expect(group.match?(double('Actor', :email => "foo@example.com"), fake_context)).to be_truthy
end

it "returns false for falsey block results" do
group = Flipper::Types::Group.new(:examples) do |actor|
nil
end
expect(group.match?(double('Actor'))).to be_falsey
expect(group.match?(double('Actor'), fake_context)).to be_falsey
end

it "can yield without context as block argument" do
context = Flipper::FeatureCheckContext.new(
feature_name: :my_feature,
values: Flipper::GateValues.new({}),
thing: Flipper::Types::Actor.new(Struct.new(:flipper_id).new(1)),
)
group = Flipper.register(:group_with_context) { |actor| actor }
yielded_actor = group.match?(admin_actor, context)
expect(yielded_actor).to be(admin_actor)
end

it "can yield with context as block argument" do
context = Flipper::FeatureCheckContext.new(
feature_name: :my_feature,
values: Flipper::GateValues.new({}),
thing: Flipper::Types::Actor.new(Struct.new(:flipper_id).new(1)),
)
group = Flipper.register(:group_with_context) { |actor, context| [actor, context] }
yielded_actor, yielded_context = group.match?(admin_actor, context)
expect(yielded_actor).to be(admin_actor)
expect(yielded_context).to be(context)
end
end
end

0 comments on commit ac2cb1f

Please sign in to comment.