Skip to content

Commit

Permalink
assign value to a variable and return at the end.
Browse files Browse the repository at this point in the history
Fixed `exp_find_by_token` method to return values correctly when finding token within an expression with mixed operators

Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1660460
  • Loading branch information
h-kataria committed Jun 14, 2019
1 parent fb17296 commit 070d143
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lib/miq_expression/subst_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,20 @@ def exp_replace_qs_tokens(exp, tokens)
# Find an expression atom based on the token
def exp_find_by_token(exp, token, parent_is_not = false)
if exp.kind_of?(Array) # Is this and AND or OR
exp.find { |e| exp_find_by_token(e, token) } # Look for token
result = nil
exp.find do |e|
result = exp_find_by_token(e, token) # Look for token
end
result
elsif exp[:token] && exp[:token] == token # This is the token exp
@parent_is_not = true if parent_is_not # Remember that token exp's parent is a NOT
exp # return it
exp # return it
elsif exp["not"]
exp_find_by_token(exp["not"], token, true) # Look for token under NOT (indicate we are a NOT)
exp_find_by_token(exp["not"], token, true) # Look for token under NOT (indicate we are a NOT)
elsif exp["and"]
exp_find_by_token(exp["and"], token) # Look for token under AND
exp_find_by_token(exp["and"], token) # Look for token under AND
elsif exp["or"]
exp_find_by_token(exp["or"], token) # Look for token under OR
exp_find_by_token(exp["or"], token) # Look for token under OR
end
end

Expand Down
34 changes: 34 additions & 0 deletions spec/lib/miq_expression/subst_mixin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,38 @@
expect(exp).to eq("and" => [{"=" => {"field" => "Vm-active", "value" => "true"}}, {"=" => {"field" => "Vm-archived", "value" => "true"}}])
end
end

describe "#exp_find_by_token" do
it "returns correct expression when expression has mixed operators" do
exp =
{
"and" =>
[
{"=" => {"field" => "ManageIQ::Providers::InfraManager::Vm-active", "value" => "true"}, :token => 1},
{"or" =>
[
{"=" => {"count" => "ManageIQ::Providers::InfraManager::Vm.advanced_settings", "value" => "1"}, :token => 2},
{"=" => {"count" => "ManageIQ::Providers::InfraManager::Vm.storages", "value" => "1"}, :token => 3}
]
}
]
}
result = test_obj.exp_find_by_token(exp, 2)
expect(result).to eq("=" => {"count" => "ManageIQ::Providers::InfraManager::Vm.advanced_settings", "value" => "1"}, :token => 2)
end

it "returns correct expression when expressions is simple has single operator" do
exp =
{
"and" =>
[
{"=" => {"field" => "ManageIQ::Providers::InfraManager::Vm-active", "value" => "true"}, :token => 1},
{"CONTAINS" => {"tag" => "ManageIQ::Providers::InfraManager::Vm.managed-prov_max_cpu", "value" => "2"}, :token => 2},
{"CONTAINS" => {"tag" => "ManageIQ::Providers::InfraManager::Vm.managed-prov_max_retirement_days", "value" => "60"}, :token => 3}
]
}
result = test_obj.exp_find_by_token(exp, 3)
expect(result).to eq("CONTAINS" => {"tag" => "ManageIQ::Providers::InfraManager::Vm.managed-prov_max_retirement_days", "value" => "60"}, :token => 3)
end
end
end

0 comments on commit 070d143

Please sign in to comment.