From 219efec879bcac72d3f422c347d70525980df7ea Mon Sep 17 00:00:00 2001 From: Lee Jarvis Date: Sun, 21 Aug 2016 14:50:59 +0100 Subject: Handle bad constant names in Slop.option_defined? Closes #198 --- lib/slop.rb | 4 ++++ test/slop_test.rb | 17 +++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 test/slop_test.rb diff --git a/lib/slop.rb b/lib/slop.rb index 9526097..45edb7f 100644 --- a/lib/slop.rb +++ b/lib/slop.rb @@ -31,6 +31,10 @@ module Slop # Returns true if an option is defined. def self.option_defined?(name) const_defined?(string_to_option(name.to_s)) + rescue NameError + # If a NameError is raised, it wasn't a valid constant name, + # and thus couldn't have been defined. + false end # Example: diff --git a/test/slop_test.rb b/test/slop_test.rb new file mode 100644 index 0000000..f6fb408 --- /dev/null +++ b/test/slop_test.rb @@ -0,0 +1,17 @@ +require "test_helper" + +describe Slop do + describe ".option_defined?" do + it "handles bad constant names" do + assert_equal false, Slop.option_defined?("Foo?Bar") + end + + it "returns false if the option is not defined" do + assert_equal false, Slop.option_defined?("Foo") + end + + it "returns true if the option is defined" do + assert_equal true, Slop.option_defined?("String") + end + end +end -- cgit v1.2.1