Skip to content

Commit

Permalink
Land #19660, Make enum options case normalizing
Browse files Browse the repository at this point in the history
  • Loading branch information
cgranleese-r7 authored Dec 13, 2024
2 parents 852bb8b + 96a7a32 commit 90066b3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
17 changes: 15 additions & 2 deletions lib/msf/core/opt_enum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,22 @@ def initialize(in_name, attrs = [],
def valid?(value = self.value, check_empty: true)
return false if check_empty && empty_required_value?(value)
return true if value.nil? && !required?
return false if value.nil?

!value.nil? && enums.include?(value.to_s)
if case_sensitive?
enums.include?(value.to_s)
else
enums.map(&:downcase).include?(value.to_s.downcase)
end
end

def normalize(value = self.value)
if valid?(value) && !value.nil?
value.to_s
if case_sensitive?
value.to_s
else
enums.find { |e| e.casecmp? value }
end
else
nil
end
Expand All @@ -44,6 +53,10 @@ def desc

protected

def case_sensitive?
enums.map(&:downcase).uniq.length != enums.uniq.length
end

attr_accessor :desc_string # :nodoc:
end
end
16 changes: 16 additions & 0 deletions spec/lib/msf/core/opt_enum_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
expect(required_optenum.valid?('Bar')).to eq true
end

it 'should return true for a value in the list with alternative casing' do
expect(required_optenum.valid?('bar')).to eq true
end

it 'should return false for a nil value' do
expect(required_optenum.valid?(nil)).to eq false
end
Expand All @@ -31,6 +35,10 @@
expect(not_required_optenum.valid?('Bar')).to eq true
end

it 'should return true for a value in the list with alternative casing' do
expect(not_required_optenum.valid?('bar')).to eq true
end

it 'should return false for a value not in the list' do
expect(not_required_optenum.valid?('Snap')).to eq false
end
Expand All @@ -45,6 +53,10 @@
expect(required_optenum.normalize('Bar')).to eq 'Bar'
end

it 'should return the value string for a value with alternative casing' do
expect(required_optenum.normalize('bar')).to eq 'Bar'
end

it 'should return nil for a value not in the list' do
expect(required_optenum.normalize('Snap')).to eq nil
end
Expand All @@ -59,6 +71,10 @@
expect(not_required_optenum.normalize('Bar')).to eq 'Bar'
end

it 'should return the value string for a value with alternative casing' do
expect(not_required_optenum.normalize('bar')).to eq 'Bar'
end

it 'should return nil for a value not in the list' do
expect(not_required_optenum.normalize('Snap')).to eq nil
end
Expand Down

0 comments on commit 90066b3

Please sign in to comment.