Permalink
Browse files

Add specs for loading coverage thresholds from a file

  • Loading branch information...
1 parent 6cb9519 commit 38dc52a16934214af05862ae4473dd23ceea30d7 @DanielHeath DanielHeath committed with xaviershay Sep 17, 2012
Showing with 61 additions and 17 deletions.
  1. +18 −11 lib/cane/threshold_check.rb
  2. +43 −6 spec/threshold_check_spec.rb
View
29 lib/cane/threshold_check.rb
@@ -16,18 +16,18 @@ def self.options
end
def violations
- thresholds.map do |operator, file, limit|
- value = value_from_file(file)
-
- if limit.to_f != limit
- if Cane::File.exists?(limit)
- limit = value_from_file(limit)
- else
- limit = UnavailableValue.new
- end
- end
+ thresholds.map do |operator, file, threshold|
+ value = normalized_limit(file)
+ limit = normalized_limit(threshold)
- unless value.send(operator, limit.to_f)
+ if limit.is_a? UnavailableValue
+ {
+ description: 'Quality threshold could not be read',
+ label: "%s is not a number or a file" % [
+ threshold
+ ]
+ }
+ elsif !value.send(operator, limit.to_f)
{
description: 'Quality threshold crossed',
label: "%s is %s, should be %s %s" % [
@@ -38,6 +38,13 @@ def violations
end.compact
end
+ def normalized_limit(limit)
+ if limit.to_f != limit
+ limit = value_from_file(limit)
+ end
+ limit
+ end
+
def value_from_file(file)
begin
contents = Cane::File.contents(file).chomp.to_f
View
49 spec/threshold_check_spec.rb
@@ -3,11 +3,48 @@
require 'cane/threshold_check'
describe Cane::ThresholdCheck do
- it 'returns a value of unavailable when file cannot be read' do
- check = Cane::ThresholdCheck.new(gte: [['bogus_file', 20]])
- violations = check.violations
- violations.length.should == 1
- violations[0][:label].should ==
- 'bogus_file is unavailable, should be >= 20'
+
+ context "checking violations" do
+
+ context "when the current coverage cannot be read" do
+ it 'reports a violation' do
+ check = Cane::ThresholdCheck.new(gte: [['bogus_file', 20]])
+ violations = check.violations
+ violations.length.should == 1
+ violations[0][:label].should ==
+ 'bogus_file is unavailable, should be >= 20'
+ end
+ end
+
+ context "when the coverage threshold is incorrectly specified" do
+ it 'reports a violation' do
+ check = Cane::ThresholdCheck.new(gte: [[20, 'bogus_file']])
+ violations = check.violations
+ violations.length.should == 1
+ violations[0][:label].should ==
+ 'bogus_file is not a number or a file'
+ end
+ end
+
+ end
+
+ context "normalizing a user supplied value to a threshold" do
+ it "normalizes an integer to itself" do
+ subject.normalized_limit(99).should == 99
+ end
+
+ it "normalizes a float to itself" do
+ subject.normalized_limit(99.6).should == 99.6
+ end
+
+ it "normalizes a valid file to its contents" do
+ subject.normalized_limit(make_file('99.5')).should == 99.5
+ end
+
+ it "normalizes an invalid file to an unavailable value" do
+ limit = subject.normalized_limit("/File.does.not.exist")
+ limit.should be_a Cane::ThresholdCheck::UnavailableValue
+ end
end
+
end

0 comments on commit 38dc52a

Please sign in to comment.