Skip to content

Commit

Permalink
Fix parsing of 'false' as Boolean option value (#1382)
Browse files Browse the repository at this point in the history
Rouge allows for options to be passed to a lexer's constructor method.
The options are parsed from strings and the string 'false' was not being
converted to the Boolean value `false`. This commit fixes that and also
adds a rudimentary series of tests to ensure that Boolean options are
being properly parsed.
  • Loading branch information
pyrmont committed Jan 5, 2020
1 parent d342877 commit e22ce33
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/rouge/lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ def initialize(opts={})

def as_bool(val)
case val
when nil, false, 0, '0', 'off'
when nil, false, 0, '0', 'false', 'off'
false
when Array
val.empty? ? true : as_bool(val.last)
Expand Down
19 changes: 19 additions & 0 deletions spec/lexer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,23 @@ def self.detect?
refute { NonDetectableLexer.methods(false).include?(:detect?) }
refute { NonDetectableLexer.detectable? }
end

it 'handles boolean options' do
option_lexer = Class.new(Rouge::RegexLexer) do
option :bool_opt, 'An example boolean option'

def initialize(*)
super
@bool_opt = bool_option(:bool_opt) { nil }
end
end

assert_equal true, option_lexer.new({bool_opt: 'true'}).instance_variable_get(:@bool_opt)
assert_equal false, option_lexer.new({bool_opt: nil}).instance_variable_get(:@bool_opt)
assert_equal false, option_lexer.new({bool_opt: false}).instance_variable_get(:@bool_opt)
assert_equal false, option_lexer.new({bool_opt: 0}).instance_variable_get(:@bool_opt)
assert_equal false, option_lexer.new({bool_opt: '0'}).instance_variable_get(:@bool_opt)
assert_equal false, option_lexer.new({bool_opt: 'false'}).instance_variable_get(:@bool_opt)
assert_equal false, option_lexer.new({bool_opt: 'off'}).instance_variable_get(:@bool_opt)
end
end

0 comments on commit e22ce33

Please sign in to comment.