Skip to content

Commit

Permalink
Merge pull request #15 from rewindio/feature/==
Browse files Browse the repository at this point in the history
Add == method to Features and Conditions
  • Loading branch information
MarcMorinville-Rewind authored Jun 24, 2020
2 parents b8da313 + a56a558 commit c7f21ca
Show file tree
Hide file tree
Showing 11 changed files with 190 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## [2.2.0]
- Add `==` to `Feature` and `Conditions`

## [2.1.0]
- Add `features` parameter to `EightBall.marshall` to allow marshalling any Features, not just the ones
from the configured Provider.
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
eight_ball (2.1.0)
eight_ball (2.2.0)
awrence (~> 1.1)
plissken (~> 1.2)

Expand Down
13 changes: 13 additions & 0 deletions lib/eight_ball/conditions/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,21 @@ def satisfied?
raise 'You can never satisfy the Base condition'
end

def ==(other)
other.class == self.class && other.state == state
end
alias eql? ==

def hash
state.hash
end

protected

def state
[@parameter]
end

def parameter=(parameter)
return if parameter.nil?

Expand Down
6 changes: 6 additions & 0 deletions lib/eight_ball/conditions/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,11 @@ def initialize(options = {})
def satisfied?(value)
values.include? value
end

protected

def state
super + [@values.sort]
end
end
end
6 changes: 6 additions & 0 deletions lib/eight_ball/conditions/range.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,11 @@ def initialize(options = {})
def satisfied?(value)
value >= min && value <= max
end

protected

def state
super + [@min, @max]
end
end
end
9 changes: 9 additions & 0 deletions lib/eight_ball/feature.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ def enabled?(parameters = {})
any_satisfied?(@enabled_for, parameters) && !any_satisfied?(@disabled_for, parameters)
end

def ==(other)
name == other.name &&
enabled_for.size == other.enabled_for.size &&
enabled_for.all? { |condition| other.enabled_for.any? { |other_condition| condition == other_condition } } &&
disabled_for.size == other.disabled_for.size &&
disabled_for.all? { |condition| other.disabled_for.any? { |other_condition| condition == other_condition } }
end
alias eql? ==

private

def any_satisfied?(conditions, parameters)
Expand Down
2 changes: 1 addition & 1 deletion lib/eight_ball/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module EightBall
VERSION = '2.1.0'
VERSION = '2.2.0'
end
23 changes: 23 additions & 0 deletions spec/conditions/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,29 @@ def parameter=(parameter)
end
end

describe '==' do
it 'should return true for identical objects' do
c1 = EightBall::Conditions::Base.new
c2 = EightBall::Conditions::Base.new

expect(c1 == c2).to be true
end

it 'should return false for objects of different types' do
c1 = EightBall::Conditions::Always.new
c2 = EightBall::Conditions::Never.new

expect(c1 == c2).to be false
end

it 'should return false for objects that have different parameter names' do
c1 = EightBall::Conditions::List.new parameter: 'param1'
c2 = EightBall::Conditions::List.new parameter: 'param2'

expect(c1 == c2).to be false
end
end

describe 'satisfied?' do
it 'should raise' do
expect { EightBall::Conditions::Base.new.satisfied? }.to raise_error RuntimeError, 'You can never satisfy the Base condition'
Expand Down
30 changes: 30 additions & 0 deletions spec/conditions/list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,34 @@
expect(list2.satisfied?('Jeremy')).to eq false
end
end

describe '==' do
it 'should return true for identical Lists' do
c1 = EightBall::Conditions::List.new parameter: 'id', values: [1, 2, 3]
c2 = EightBall::Conditions::List.new parameter: 'id', values: [1, 2, 3]

expect(c1 == c2).to be true
end

it 'should return true for Lists that have the same values in different order' do
c1 = EightBall::Conditions::List.new parameter: 'id', values: [1, 2, 3]
c2 = EightBall::Conditions::List.new parameter: 'id', values: [3, 2, 1]

expect(c1 == c2).to be true
end

it 'should return false for Lists with different values' do
c1 = EightBall::Conditions::List.new parameter: 'id', values: [1, 2, 3]
c2 = EightBall::Conditions::Base.new parameter: 'id', values: [4, 5, 6]

expect(c1 == c2).to be false
end

it 'should return false for Lists with different paramter names' do
c1 = EightBall::Conditions::List.new parameter: 'id', values: [1, 2, 3]
c2 = EightBall::Conditions::Base.new parameter: 'id2', values: [1, 2, 3]

expect(c1 == c2).to be false
end
end
end
23 changes: 23 additions & 0 deletions spec/conditions/range_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,27 @@
expect(range2.satisfied?('d')).to be false
end
end

describe '==' do
it 'should return true for identical Ranges' do
c1 = EightBall::Conditions::Range.new parameter: 'id', min: 1, max: 2
c2 = EightBall::Conditions::Range.new parameter: 'id', min: 1, max: 2

expect(c1 == c2).to be true
end

it 'should return false for Ranges with different values' do
c1 = EightBall::Conditions::Range.new parameter: 'id', min: 1, max: 2
c2 = EightBall::Conditions::Range.new parameter: 'id', min: 3, max: 4

expect(c1 == c2).to be false
end

it 'should return false for Ranges with different paramter names' do
c1 = EightBall::Conditions::Range.new parameter: 'id', min: 1, max: 2
c2 = EightBall::Conditions::Range.new parameter: 'id2', min: 1, max: 2

expect(c1 == c2).to be false
end
end
end
75 changes: 75 additions & 0 deletions spec/feature_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,79 @@
feature.enabled? param1: 1
end
end

describe '==' do
it 'should return false if the names do not match' do
f1 = EightBall::Feature.new 'name1'
f2 = EightBall::Feature.new 'name2'

expect(f1 == f2).to be false
end

it 'should return false if the number of "enabled for" conditions differ' do
f1 = EightBall::Feature.new 'name1', [EightBall::Conditions::Always.new, EightBall::Conditions::Always.new]
f2 = EightBall::Feature.new 'name1', [EightBall::Conditions::Always.new]

expect(f1 == f2).to be false
end

it 'should return false if the number of "disabled for" conditions differ' do
f1 = EightBall::Feature.new 'name1', [], [EightBall::Conditions::Always.new, EightBall::Conditions::Always.new]
f2 = EightBall::Feature.new 'name1', [], [EightBall::Conditions::Always.new]

expect(f1 == f2).to be false
end

it 'should return true if every "enabled for" condition is matched, regardless of order' do
f1 = EightBall::Feature.new('name1', [
EightBall::Conditions::Range.new(min: 1, max: 2),
EightBall::Conditions::List.new(values: [1, 2, 3])
])
f2 = EightBall::Feature.new('name1', [
EightBall::Conditions::List.new(values: [1, 2, 3]),
EightBall::Conditions::Range.new(min: 1, max: 2)
])

expect(f1 == f2).to be true
end

it 'should return false if a single "enabled for" condition is not matched' do
f1 = EightBall::Feature.new('name1', [
EightBall::Conditions::Range.new(min: 1, max: 2),
EightBall::Conditions::List.new(values: [1, 2, 3])
])
f2 = EightBall::Feature.new('name1', [
EightBall::Conditions::Range.new(min: 3, max: 4),
EightBall::Conditions::List.new(values: [1, 2, 3])
])

expect(f1 == f2).to be false
end

it 'should return true if every "disabled for" condition is matched, regardless of order' do
f1 = EightBall::Feature.new('name1', [], [
EightBall::Conditions::Range.new(min: 1, max: 2),
EightBall::Conditions::List.new(values: [1, 2, 3])
])
f2 = EightBall::Feature.new('name1', [], [
EightBall::Conditions::List.new(values: [1, 2, 3]),
EightBall::Conditions::Range.new(min: 1, max: 2)
])

expect(f1 == f2).to be true
end

it 'should return false if a single "disabled for" condition is not matched' do
f1 = EightBall::Feature.new('name1', [], [
EightBall::Conditions::Range.new(min: 1, max: 2),
EightBall::Conditions::List.new(values: [1, 2, 3])
])
f2 = EightBall::Feature.new('name1', [], [
EightBall::Conditions::Range.new(min: 3, max: 4),
EightBall::Conditions::List.new(values: [1, 2, 3])
])

expect(f1 == f2).to be false
end
end
end

0 comments on commit c7f21ca

Please sign in to comment.