Skip to content

Commit 90066b3

Browse files
Land rapid7#19660, Make enum options case normalizing
2 parents 852bb8b + 96a7a32 commit 90066b3

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

lib/msf/core/opt_enum.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,22 @@ def initialize(in_name, attrs = [],
2020
def valid?(value = self.value, check_empty: true)
2121
return false if check_empty && empty_required_value?(value)
2222
return true if value.nil? && !required?
23+
return false if value.nil?
2324

24-
!value.nil? && enums.include?(value.to_s)
25+
if case_sensitive?
26+
enums.include?(value.to_s)
27+
else
28+
enums.map(&:downcase).include?(value.to_s.downcase)
29+
end
2530
end
2631

2732
def normalize(value = self.value)
2833
if valid?(value) && !value.nil?
29-
value.to_s
34+
if case_sensitive?
35+
value.to_s
36+
else
37+
enums.find { |e| e.casecmp? value }
38+
end
3039
else
3140
nil
3241
end
@@ -44,6 +53,10 @@ def desc
4453

4554
protected
4655

56+
def case_sensitive?
57+
enums.map(&:downcase).uniq.length != enums.uniq.length
58+
end
59+
4760
attr_accessor :desc_string # :nodoc:
4861
end
4962
end

spec/lib/msf/core/opt_enum_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
expect(required_optenum.valid?('Bar')).to eq true
1818
end
1919

20+
it 'should return true for a value in the list with alternative casing' do
21+
expect(required_optenum.valid?('bar')).to eq true
22+
end
23+
2024
it 'should return false for a nil value' do
2125
expect(required_optenum.valid?(nil)).to eq false
2226
end
@@ -31,6 +35,10 @@
3135
expect(not_required_optenum.valid?('Bar')).to eq true
3236
end
3337

38+
it 'should return true for a value in the list with alternative casing' do
39+
expect(not_required_optenum.valid?('bar')).to eq true
40+
end
41+
3442
it 'should return false for a value not in the list' do
3543
expect(not_required_optenum.valid?('Snap')).to eq false
3644
end
@@ -45,6 +53,10 @@
4553
expect(required_optenum.normalize('Bar')).to eq 'Bar'
4654
end
4755

56+
it 'should return the value string for a value with alternative casing' do
57+
expect(required_optenum.normalize('bar')).to eq 'Bar'
58+
end
59+
4860
it 'should return nil for a value not in the list' do
4961
expect(required_optenum.normalize('Snap')).to eq nil
5062
end
@@ -59,6 +71,10 @@
5971
expect(not_required_optenum.normalize('Bar')).to eq 'Bar'
6072
end
6173

74+
it 'should return the value string for a value with alternative casing' do
75+
expect(not_required_optenum.normalize('bar')).to eq 'Bar'
76+
end
77+
6278
it 'should return nil for a value not in the list' do
6379
expect(not_required_optenum.normalize('Snap')).to eq nil
6480
end

0 commit comments

Comments
 (0)