Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions lib/ldclient-rb/impl/model/feature_flag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def initialize(data, logger = nil)
@off_variation = data[:offVariation]
check_variation_range(self, errors, @off_variation, "off variation")
@prerequisites = (data[:prerequisites] || []).map do |prereq_data|
Prerequisite.new(prereq_data, self, errors)
Prerequisite.new(prereq_data, self)
end
@targets = (data[:targets] || []).map do |target_data|
Target.new(target_data, self, errors)
Expand Down Expand Up @@ -108,13 +108,12 @@ def to_json(*a)
end

class Prerequisite
def initialize(data, flag, errors_out = nil)
def initialize(data, flag)
@data = data
@key = data[:key]
@variation = data[:variation]
@failure_result = EvaluatorHelpers.evaluation_detail_for_off_variation(flag,
EvaluationReason::prerequisite_failed(@key))
check_variation_range(flag, errors_out, @variation, "prerequisite")
end

# @return [Hash]
Expand Down
32 changes: 32 additions & 0 deletions spec/impl/evaluator_prereq_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,38 @@ module Impl
expect(result2.detail).to be result1.detail
end

it "returns off variation and event if prereq condition is invalid" do
flag = Flags.from_hash(
{
key: 'feature0',
on: true,
prerequisites: [{ key: 'feature1', variation: 2 }], # there are only 2 variations, so variation index 2 is invalid
fallthrough: { variation: 0 },
offVariation: 1,
variations: %w[a b c],
version: 1,
}
)
flag1 = Flags.from_hash(
{
key: 'feature1',
on: true,
fallthrough: { variation: 0 },
variations: %w[d e],
version: 2,
}
)
context = LDContext.create({ key: 'x' })
detail = EvaluationDetail.new('b', 1, EvaluationReason::prerequisite_failed('feature1'))
expected_prereqs = [
PrerequisiteEvalRecord.new(flag1, flag, EvaluationDetail.new('d', 0, EvaluationReason::fallthrough())),
]
e = EvaluatorBuilder.new(logger).with_flag(flag1).with_unknown_flag('feature2').build
result = e.evaluate(flag, context)
expect(result.detail).to eq(detail)
expect(result.prereq_evals).to eq(expected_prereqs)
end

it "returns off variation and event if prerequisite of a prerequisite is not found" do
flag = Flags.from_hash(
{
Expand Down