Skip to content

Commit

Permalink
Merge pull request #2 from shr3kst3r/master
Browse files Browse the repository at this point in the history
Modify the handling of @ symbol when specifying the check range
  • Loading branch information
dbroeglin committed Jan 26, 2013
2 parents 24d00e3 + 49041e9 commit dc68fc7
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 35 deletions.
14 changes: 11 additions & 3 deletions lib/nagios_check/range.rb
Expand Up @@ -9,7 +9,7 @@ def initialize(string_range)
unless tokens
raise RuntimeError, "Pattern should be of form [@][~][min]:max"
end
@exclusive = true if tokens.include? "@"
@inverse = true if tokens.include? "@"
case tokens[2]
when nil, "" then @min = 0
when '~' then @min = nil
Expand All @@ -19,8 +19,16 @@ def initialize(string_range)
end

def include?(value)
if @exclusive
(@min.nil? || value > @min) && (@max.nil? || value < @max)
if @inverse
if @max.nil?
(@min.nil? || value < @min)
elsif @min.nil?
(@max.nil? || value > @max)
elsif @min == @max
false
else
(@min.nil? || value < @min) || (@max.nil? || value > @max)
end
else
(@min.nil? || value >= @min) && (@max.nil? || value <= @max)
end
Expand Down
90 changes: 58 additions & 32 deletions spec/range_spec.rb
Expand Up @@ -36,12 +36,12 @@
end

context "when pattern is @:10" do
it { should_not contain -1 }
it { should_not contain 0 }
it { should contain 5 }
it { should_not contain 10 }
it { should_not contain 10.0 }
it { should_not contain 11 }
it { should contain -1 }
it { should_not contain 0 }
it { should_not contain 5 }
it { should_not contain 10 }
it { should_not contain 10.0 }
it { should contain 11 }
end

context "when pattern is 10:" do
Expand All @@ -55,13 +55,13 @@
end

context "when pattern is @10:" do
it { should_not contain -1 }
it { should_not contain 1 }
it { should_not contain 9.9 }
it { should_not contain 10 }
it { should_not contain 10.0 }
it { should contain 10.1 }
it { should contain 11 }
it { should contain -1 }
it { should contain 1 }
it { should contain 9.9 }
it { should_not contain 10 }
it { should_not contain 10.0 }
it { should_not contain 10.1 }
it { should_not contain 11 }
end

context "when pattern is 10:11" do
Expand All @@ -77,28 +77,54 @@
it { should_not contain 12 }
end

context "when pattern is 10:10" do
it { should_not contain -1 }
it { should_not contain 1 }
it { should_not contain 9.9 }
it { should contain 10 }
it { should contain 10.0 }
it { should_not contain 10.1 }
it { should_not contain 10.9 }
it { should_not contain 11 }
it { should_not contain 11.1 }
it { should_not contain 12 }
end

context "when pattern is @10:10" do
it { should_not contain -1 }
it { should_not contain 1 }
it { should_not contain 9.9 }
it { should_not contain 10 }
it { should_not contain 10.0 }
it { should_not contain 10.1 }
it { should_not contain 10.9 }
it { should_not contain 11 }
it { should_not contain 11.1 }
it { should_not contain 12 }
end

context "when pattern is @10:11" do
it { should_not contain -1 }
it { should_not contain 1 }
it { should_not contain 9.9 }
it { should contain -1 }
it { should contain 1 }
it { should contain 9.9 }
it { should_not contain 10 }
it { should_not contain 10.0 }
it { should contain 10.1 }
it { should_not contain 10.1 }
it { should_not contain 11 }
it { should_not contain 11.1 }
it { should_not contain 12 }
it { should contain 11.1 }
it { should contain 12 }
end

context "when pattern is @10.05:11.05" do
it { should_not contain -1 }
it { should_not contain 1 }
it { should_not contain 9.9 }
it { should_not contain 10 }
it { should_not contain 10.0 }
it { should contain 10.1 }
it { should contain 11 }
it { should_not contain 11.1 }
it { should_not contain 12 }
it { should contain -1 }
it { should contain 1 }
it { should contain 9.9 }
it { should contain 10 }
it { should contain 10.0 }
it { should_not contain 10.1 }
it { should_not contain 11 }
it { should contain 11.1 }
it { should contain 12 }
end

context "when pattern is -1:1" do
Expand All @@ -120,11 +146,11 @@
end

context "when pattern is @~:1" do
it { should contain -2 }
it { should contain -1 }
it { should contain 0 }
it { should_not contain -2 }
it { should_not contain -1 }
it { should_not contain 0 }
it { should_not contain 1 }
it { should_not contain 2 }
it { should contain 2 }
end

context "when nil pattern" do
Expand Down

0 comments on commit dc68fc7

Please sign in to comment.