Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing of 'false' as Boolean option value #1382

Merged
merged 1 commit into from Jan 5, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/rouge/lexer.rb
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
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