Skip to content

Commit

Permalink
Better specs for option testing
Browse files Browse the repository at this point in the history
Added ability to transform an option (to_i, to_f, etc.)
  • Loading branch information
dbroeglin committed Nov 24, 2011
1 parent 45650b2 commit 20d32da
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 3 deletions.
14 changes: 11 additions & 3 deletions lib/nagios_check.rb
Expand Up @@ -62,12 +62,14 @@ def store_value(name, value, opts = {})

module ClassMethods
def on(*args, &block)
@ons << [args, args.delete(:required), block]
@ons << [args, args.delete(:mandatory), block]
end

def store(name, opts = {})
@defaults[name] = opts[:default] if opts.has_key?(:default)
transform = opts[:transform]
Proc::new do |value|
value = value.send transform if transform
self.options.send "#{name}=", value
end
end
Expand Down Expand Up @@ -108,8 +110,10 @@ def self.included(base)
def opt_parse
unless @opt_parse
opt_parse = OptionParser::new
self.class.instance_variable_get("@ons").each do |args, req, block|
opt_parse.on(*args) {|value| instance_exec(value, &block) }
self.class.instance_variable_get("@ons").each do |args, mand, block|
opt_parse.on(*args) do |value|
instance_exec(value, &block)
end
end
@opt_parse = opt_parse
end
Expand All @@ -121,7 +125,11 @@ def parse_options(argv = ARGV)
opt_parse.parse!(argv)
end


def check_with_timeout
Timeout.timeout(@timeout) { check }
end

class MissingOption < StandardError
end
end
92 changes: 92 additions & 0 deletions spec/options_spec.rb
@@ -0,0 +1,92 @@
describe NagiosCheck do
context "when no options specified" do
subject do
Class::new do
include NagiosCheck
end.new
end

specify { subject.send :parse_options, [] }
end

context "when warning is specified" do
subject do
Class::new do
include NagiosCheck
enable_warning
end.new
end

it("works with no arguments"){ subject.send :parse_options, %w{} }
specify { subject.send :parse_options, %w{-w 10} }

it "fails if -w has no argument" do
lambda {
subject.send :parse_options, %w{-w}
}.should raise_error(OptionParser::MissingArgument)
end
it "fails if -c is given" do
lambda {
subject.send :parse_options, %w{-c}
}.should raise_error(OptionParser::InvalidOption)
end
end

context "when critical is specified" do
subject do
Class::new do
include NagiosCheck
enable_critical
end.new
end

it("works with no arguments"){ subject.send :parse_options, %w{} }
specify { subject.send :parse_options, %w{-c 10} }

it "fails if -c has no argument" do
lambda {
subject.send :parse_options, %w{-c}
}.should raise_error(OptionParser::MissingArgument)
end

it "fails if -w is given" do
lambda {
subject.send :parse_options, %w{-w}
}.should raise_error(OptionParser::InvalidOption)
end
end

context "when a non mandatory option is specified" do
subject do
Class::new do
include NagiosCheck
on "-a VALUE", &store(:a)
on "-b VALUE", &store(:b, transform: :to_i)
end.new
end

it("works with no arguments"){ subject.send :parse_options, %w{} }

it "works with '-a 10'" do
subject.send :parse_options, %w{-a 10}
subject.options.a.should == "10"
end

it "works with '-b 20' and converts to int" do
subject.send :parse_options, %w{-b 20}
subject.options.b.should == 20
end

it "fails if -a has no argument" do
lambda {
subject.send :parse_options, %w{-a}
}.should raise_error(OptionParser::MissingArgument)
end

it "fails if -w is given" do
lambda {
subject.send :parse_options, %w{-w}
}.should raise_error(OptionParser::InvalidOption)
end
end
end

0 comments on commit 20d32da

Please sign in to comment.