summaryrefslogtreecommitdiff
path: root/spec/functions/validate_numeric_spec.rb
diff options
context:
space:
mode:
authorDavid Schmitt <david.schmitt@puppetlabs.com>2015-06-01 12:21:59 +0100
committerDavid Schmitt <david.schmitt@puppetlabs.com>2015-06-01 18:02:22 +0100
commitf3e79ddcd56a221c7799b35efde7e9803a5c7923 (patch)
tree730386688574c94169d47d37f79af77c2cda2f08 /spec/functions/validate_numeric_spec.rb
parentb62dff0c6e09faf9bacfb02575e689ed09ee5e56 (diff)
Convert tests to use plain rspec-puppet
Tests in the new style produces the following documentation output: abs should not eq nil should run abs() and raise an Puppet::ParseError should run abs(-34) and return 34 should run abs("-34") and return 34 should run abs(34) and return 34 should run abs("34") and return 34
Diffstat (limited to 'spec/functions/validate_numeric_spec.rb')
-rwxr-xr-xspec/functions/validate_numeric_spec.rb265
1 files changed, 66 insertions, 199 deletions
diff --git a/spec/functions/validate_numeric_spec.rb b/spec/functions/validate_numeric_spec.rb
index c99d879..9b8eb0e 100755
--- a/spec/functions/validate_numeric_spec.rb
+++ b/spec/functions/validate_numeric_spec.rb
@@ -1,222 +1,89 @@
-#! /usr/bin/env ruby -S rspec
-
require 'spec_helper'
-describe Puppet::Parser::Functions.function(:validate_numeric) do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
- describe 'when calling validate_numeric from puppet without any argument or to many' do
- it "should not compile when no argument is passed" do
- Puppet[:code] = "validate_numeric()"
- expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /Wrong number of arguments/)
- end
- it "should not compile when more than three arguments are passed" do
- Puppet[:code] = "validate_numeric(1, 1, 1, 1)"
- expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /Wrong number of arguments/)
- end
- end
-
- describe 'when calling validate_numeric from puppet only with input' do
- %w{ 1 -1 1.0 -1.0 }.each do |the_number|
- it "should compile when #{the_number} is an encapsulated numeric" do
- Puppet[:code] = "validate_numeric('#{the_number}')"
- scope.compiler.compile
- end
- it "should compile when #{the_number} is a bare numeric" do
- Puppet[:code] = "validate_numeric(#{the_number})"
- scope.compiler.compile
- end
- end
-
- %w{ [1,2,3,4,5] ['1','2','3','4','5'] [1.1,2.2,3.3,4.4,5.5] ['1.1','2.2','3.3','4.4','5.5'] }.each do |the_number|
- it "should compile when multiple Numeric arguments are passed in an Array" do
- Puppet[:code] = "validate_numeric(#{the_number})"
- scope.compiler.compile
- end
- end
-
- %w{ true false iAmAString 1test }.each do |the_number|
- it "should not compile when #{the_number} is in a string" do
- Puppet[:code] = "validate_numeric('#{the_number}')"
- expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be a Numeric/)
- end
-
- it "should not compile when #{the_number} is a bare word" do
- Puppet[:code] = "validate_numeric(#{the_number})"
- expect { scope.compiler.compile }.to raise_error
- end
- end
-
- it "should not compile when a Numeric is part of a larger String" do
- Puppet[:code] = "validate_numeric('1.0 test')"
- expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be a Numeric/)
- end
+describe 'validate_numeric' do
+ describe 'signature validation' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params(1, 2, 3, 4).and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
- it "should not compile when an Array with a non-Numeric value is passed" do
- Puppet[:code] = "validate_numeric([1, 'test'])"
- expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /at array position 1 to be a Numeric/)
+ [ true, 'true', false, 'false', 'iAmAString', '1test', '1 test', 'test 1', 'test 1 test', {}, { 'key' => 'value' }, { 1=> 2 }, '', :undef , 'x'].each do |invalid|
+ it { is_expected.to run.with_params(invalid).and_raise_error(Puppet::ParseError, /to be a Numeric/) }
+ it { is_expected.to run.with_params(invalid, 10.0).and_raise_error(Puppet::ParseError, /to be a Numeric/) }
+ it { is_expected.to run.with_params(invalid, 10.0, -10.0).and_raise_error(Puppet::ParseError, /to be a Numeric/) }
end
- it "should not compile when a Hash is passed" do
- Puppet[:code] = "validate_numeric({ 1 => 2 })"
- expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be a Numeric or Array/)
+ context 'when running on modern rubies', :unless => RUBY_VERSION == '1.8.7' do
+ it { is_expected.to run.with_params([0, 1, 2, {1=>2}, 3, 4], 10, -10).and_raise_error(Puppet::ParseError, /to be a Numeric/) }
end
- it "should not compile when a Hash is passed in an Array" do
- Puppet[:code] = "validate_numeric([{ 1 => 2 }])"
- expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be a Numeric/)
+ context 'when running on ruby, which munges hashes weirdly', :if => RUBY_VERSION == '1.8.7' do
+ it { is_expected.to run.with_params([0, 1, 2, {1=>2}, 3, 4], 10, -10).and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params([0, 1, 2, {0=>2}, 3, 4], 10, -10).and_raise_error(Puppet::ParseError) }
end
- it "should not compile when an explicitly undef variable is passed" do
- Puppet[:code] = <<-'ENDofPUPPETcode'
- $foo = undef
- validate_numeric($foo)
- ENDofPUPPETcode
- expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be a Numeric/)
- end
+ it { is_expected.to run.with_params(1, '').and_raise_error(Puppet::ParseError, /to be unset or a Numeric/) }
+ it { is_expected.to run.with_params(1, 2, '').and_raise_error(Puppet::ParseError, /to be unset or a Numeric/) }
+ it { is_expected.to run.with_params(1, 2, 3).and_raise_error(Puppet::ParseError, /second argument to be larger than third argument/) }
+ end
- it "should not compile when an undefined variable is passed" do
- Puppet[:code] = 'validate_numeric($foobarbazishouldnotexist)'
- expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be a Numeric/)
- end
+ context 'with no range constraints' do
+ it { is_expected.to run.with_params(1) }
+ it { is_expected.to run.with_params(-1) }
+ it { is_expected.to run.with_params('1') }
+ it { is_expected.to run.with_params('-1') }
+ it { is_expected.to run.with_params([1, 2, 3, 4]) }
+ it { is_expected.to run.with_params([1, '2', '3', 4]) }
end
- describe 'when calling validate_numeric from puppet with input and a maximum' do
- max = 10
- %w{ 1 -1 1.0 -1.0 }.each do |the_number|
- it "should compile when #{the_number} is lower than a maximum of #{max}" do
- Puppet[:code] = "validate_numeric(#{the_number},#{max})"
- scope.compiler.compile
+ context "with a maximum limit of 10.0" do
+ describe 'rejects numbers greater than the limit' do
+ it { is_expected.to run.with_params(11, 10.0).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
+ it { is_expected.to run.with_params(100, 10.0).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
+ it { is_expected.to run.with_params(2**65, 10.0).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
+ it { is_expected.to run.with_params([1,2,10.0,100], 10.0).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
+ end
+
+ describe 'accepts numbers less or equal to the limit' do
+ it { is_expected.to run.with_params(10.0, 10.0) }
+ it { is_expected.to run.with_params(1, 10.0) }
+ it { is_expected.to run.with_params(-1, 10.0) }
+ it { is_expected.to run.with_params('1', 10.0) }
+ it { is_expected.to run.with_params('-1', 10.0) }
+ it { is_expected.to run.with_params([1, 2, 3, 4], 10.0) }
+ it { is_expected.to run.with_params([1, '2', '3', 4], 10.0) }
+ end
+
+ context "with a minimum limit of -10.0" do
+ describe 'rejects numbers greater than the upper limit' do
+ it { is_expected.to run.with_params(11, 10.0, -10.0).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
+ it { is_expected.to run.with_params(100, 10.0, -10.0).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
+ it { is_expected.to run.with_params(2**65, 10.0, -10.0).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
+ it { is_expected.to run.with_params([1,2,10.0,100], 10.0, -10.0).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
end
- end
- it "should compile when a Numeric is equal the maximum" do
- Puppet[:code] = "validate_numeric(#{max},#{max})"
- scope.compiler.compile
- end
-
- it "should not compile when #{max+1} is greater than a maximum of #{max}" do
- Puppet[:code] = "validate_numeric(#{max+1},#{max})"
- expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be smaller or equal to/)
- end
-
- %w{ [-10,1,2,3,4,5,10] ['-10','1','2','3','4','5','10'] }.each do |the_number|
- it "should compile when each element of #{the_number} is lower than a maximum of #{max}" do
- Puppet[:code] = "validate_numeric(#{the_number},#{max})"
- scope.compiler.compile
+ describe 'rejects numbers smaller than the lower limit' do
+ it { is_expected.to run.with_params(-11, 10.0, -10.0).and_raise_error(Puppet::ParseError, /to be greater or equal/) }
+ it { is_expected.to run.with_params(-100, 10.0, -10.0).and_raise_error(Puppet::ParseError, /to be greater or equal/) }
+ it { is_expected.to run.with_params(-2**65, 10.0, -10.0).and_raise_error(Puppet::ParseError, /to be greater or equal/) }
+ it { is_expected.to run.with_params([-10.0, 1,2,10.0,-100], 10.0, -10.0).and_raise_error(Puppet::ParseError, /to be greater or equal/) }
end
- end
-
- it "should not compile when an element of an Array [-10,1,2,3,4,5,#{max+1}] is greater than a maximum of #{max}" do
- Puppet[:code] = "validate_numeric([-10,1,2,3,4,5,#{max+1}],#{max})"
- expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be smaller or equal to/)
- end
- %w{ true false iAmAString 1test }.each do |the_max|
- it "should not compile when a non-Numeric maximum #{the_max}, encapsulated in a String, is passed" do
- Puppet[:code] = "validate_numeric(1,'#{the_max}')"
- expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be unset or a Numeric/)
+ describe 'accepts numbers between and including the limits' do
+ it { is_expected.to run.with_params(10.0, 10.0, -10.0) }
+ it { is_expected.to run.with_params(-10.0, 10.0, -10.0) }
+ it { is_expected.to run.with_params(1, 10.0, -10.0) }
+ it { is_expected.to run.with_params(-1, 10.0, -10.0) }
+ it { is_expected.to run.with_params('1', 10.0, -10.0) }
+ it { is_expected.to run.with_params('-1', 10.0, -10.0) }
+ it { is_expected.to run.with_params([1, 2, 3, 4], 10.0, -10.0) }
+ it { is_expected.to run.with_params([1, '2', '3', 4], 10.0, -10.0) }
end
-
- it "should not compile when a non-Numeric maximum #{the_max} bare word is passed" do
- Puppet[:code] = "validate_numeric(1,#{the_max})"
- expect { scope.compiler.compile }.to raise_error
- end
- end
-
- it "should not compile when an explicitly undefined variable is passed as maximum and no minimum is passed" do
- Puppet[:code] = <<-'ENDofPUPPETcode'
- $foo = undef
- validate_numeric(10, $foo)
- ENDofPUPPETcode
- expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be unset or a Numeric/)
- end
- it "should not compile when an explicitly undef is passed as maximum and no minimum is passed" do
- Puppet[:code] = "validate_numeric(10, undef)"
- expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be unset or a Numeric/)
- end
- it "should not compile when an empty string is passed as maximum and no minimum is passed" do
- Puppet[:code] = "validate_numeric(10, '')"
- expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be unset or a Numeric/)
- end
- it "should not compile when an undefined variable for a maximum is passed" do
- Puppet[:code] = "validate_numeric(10, $foobarbazishouldnotexist)"
- expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be unset or a Numeric/)
end
end
- describe 'when calling validate_numeric from puppet with input, a maximum and a minimum' do
- it "should not compile when a minimum larger than maximum is passed" do
- Puppet[:code] = "validate_numeric(1,1,2)"
- expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /second argument to be larger than third argument/)
- end
+ it { is_expected.to run.with_params(10.0, 10.0, 10.0) }
- max = 10
- min = -10
- %w{ 1 -1 }.each do |the_number|
- it "should compile when each element of #{the_number} is within a range from #{min} to #{max}" do
- Puppet[:code] = "validate_numeric(#{the_number},#{max},#{min})"
- scope.compiler.compile
- end
- end
-
- it "should compile when a Numeric is equal the minimum" do
- Puppet[:code] = "validate_numeric(#{min},#{max},#{min})"
- scope.compiler.compile
- end
-
- it "should compile when a Numeric is equal the minimum and maximum" do
- Puppet[:code] = "validate_numeric(#{max},#{max},#{max})"
- scope.compiler.compile
- end
-
- it "should compile when an empty maximum is passed and the Numeric is greater than the minimum" do
- Puppet[:code] = "validate_numeric(#{max}.1,'',#{min})"
- scope.compiler.compile
- end
- it "should compile when an explicitly undefined maximum is passed and the Numeric is greater than the minimum" do
- Puppet[:code] = "validate_numeric(#{max}.1,undef,#{min})"
- scope.compiler.compile
- end
- it "should compile when an explicitly undefined variable is passed for maximum and the Numeric is greater than the minimum" do
- Puppet[:code] = <<-"ENDofPUPPETcode"
- $foo = undef
- validate_numeric(#{max}.1, $foo, #{min})
- ENDofPUPPETcode
- scope.compiler.compile
- end
- it "should not compile when no maximum value is given and the Numeric is greater than the minimum" do
- Puppet[:code] = "validate_numeric(#{max}.1,,#{min})"
- expect { scope.compiler.compile }.to raise_error(Puppet::Error, /Syntax error at ','/)
- end
-
- it "should not compile when #{min-1} is lower than a minimum of #{min}" do
- Puppet[:code] = "validate_numeric(#{min-1.0},#{max},#{min})"
- expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be greater or equal to/)
- end
-
- %w{ [-10,1,2,3,4,5,10] ['-10.0','1','2','3','4','5','10.0'] }.each do |the_number|
- it "should compile when each element of #{the_number} is within a range from #{min} to #{max}" do
- Puppet[:code] = "validate_numeric(#{the_number},#{max},#{min})"
- scope.compiler.compile
- end
- end
-
- it "should not compile when an element of an Array [#{min-1.1},1,2,3,4,5,10.0] is lower than a minimum of #{min}" do
- Puppet[:code] = "validate_numeric([#{min-1},1,2,3,4,5,10],#{max},#{min})"
- expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be greater or equal to/)
- end
-
- %w{ true false iAmAString 1test }.each do |the_min|
- it "should not compile when a non-Numeric minimum #{the_min}, encapsulated in a String, is passed" do
- Puppet[:code] = "validate_numeric(1,#{max},'#{the_min}')"
- expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be unset or a Numeric/)
- end
-
- it "should not compile when a non-Numeric minimum #{the_min} bare word is passed" do
- Puppet[:code] = "validate_numeric(1,#{max},#{the_min})"
- expect { scope.compiler.compile }.to raise_error
- end
- end
+ describe 'empty upper limit is interpreted as infinity' do
+ it { is_expected.to run.with_params(11, '', 10.0) }
end
end