validate_numeric_spec.rb 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. require 'spec_helper'
  2. describe 'validate_numeric' do
  3. after(:each) do
  4. ENV.delete('STDLIB_LOG_DEPRECATIONS')
  5. end
  6. # Checking for deprecation warning
  7. it 'displays a single deprecation' do
  8. ENV['STDLIB_LOG_DEPRECATIONS'] = 'true'
  9. scope.expects(:warning).with(includes('This method is deprecated'))
  10. is_expected.to run.with_params(3)
  11. end
  12. describe 'signature validation' do
  13. it { is_expected.not_to eq(nil) }
  14. it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
  15. it { is_expected.to run.with_params(1, 2, 3, 4).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
  16. [true, 'true', false, 'false', 'iAmAString', '1test', '1 test', 'test 1', 'test 1 test', {}, { 'key' => 'value' }, { 1 => 2 }, '', :undef, 'x'].each do |invalid|
  17. it { is_expected.to run.with_params(invalid).and_raise_error(Puppet::ParseError, %r{to be a Numeric}) }
  18. it { is_expected.to run.with_params(invalid, 10.0).and_raise_error(Puppet::ParseError, %r{to be a Numeric}) }
  19. it { is_expected.to run.with_params(invalid, 10.0, -10.0).and_raise_error(Puppet::ParseError, %r{to be a Numeric}) }
  20. end
  21. context 'when running on modern rubies', :unless => RUBY_VERSION == '1.8.7' do
  22. it { is_expected.to run.with_params([0, 1, 2, { 1 => 2 }, 3, 4], 10, -10).and_raise_error(Puppet::ParseError, %r{to be a Numeric}) }
  23. end
  24. context 'when running on ruby, which munges hashes weirdly', :if => RUBY_VERSION == '1.8.7' do
  25. it { is_expected.to run.with_params([0, 1, 2, { 1 => 2 }, 3, 4], 10, -10).and_raise_error(Puppet::ParseError) }
  26. it { is_expected.to run.with_params([0, 1, 2, { 0 => 2 }, 3, 4], 10, -10).and_raise_error(Puppet::ParseError) }
  27. end
  28. it { is_expected.to run.with_params(1, '').and_raise_error(Puppet::ParseError, %r{to be unset or a Numeric}) }
  29. it { is_expected.to run.with_params(1, 2, '').and_raise_error(Puppet::ParseError, %r{to be unset or a Numeric}) }
  30. it { is_expected.to run.with_params(1, 2, 3).and_raise_error(Puppet::ParseError, %r{second argument to be larger than third argument}) }
  31. end
  32. context 'with no range constraints' do
  33. it { is_expected.to run.with_params(1) }
  34. it { is_expected.to run.with_params(-1) }
  35. it { is_expected.to run.with_params('1') }
  36. it { is_expected.to run.with_params('-1') }
  37. it { is_expected.to run.with_params([1, 2, 3, 4]) }
  38. it { is_expected.to run.with_params([1, '2', '3', 4]) }
  39. end
  40. context 'with a maximum limit of 10.0' do
  41. describe 'rejects numbers greater than the limit' do
  42. it { is_expected.to run.with_params(11, 10.0).and_raise_error(Puppet::ParseError, %r{to be smaller or equal}) }
  43. it { is_expected.to run.with_params(100, 10.0).and_raise_error(Puppet::ParseError, %r{to be smaller or equal}) }
  44. it { is_expected.to run.with_params(2**65, 10.0).and_raise_error(Puppet::ParseError, %r{to be smaller or equal}) }
  45. it { is_expected.to run.with_params([1, 2, 10.0, 100], 10.0).and_raise_error(Puppet::ParseError, %r{to be smaller or equal}) }
  46. end
  47. describe 'accepts numbers less or equal to the limit' do
  48. it { is_expected.to run.with_params(10.0, 10.0) }
  49. it { is_expected.to run.with_params(1, 10.0) }
  50. it { is_expected.to run.with_params(-1, 10.0) }
  51. it { is_expected.to run.with_params('1', 10.0) }
  52. it { is_expected.to run.with_params('-1', 10.0) }
  53. it { is_expected.to run.with_params([1, 2, 3, 4], 10.0) }
  54. it { is_expected.to run.with_params([1, '2', '3', 4], 10.0) }
  55. end
  56. end
  57. context 'with a minimum limit of -10.0' do
  58. describe 'rejects numbers greater than the upper limit' do
  59. it { is_expected.to run.with_params(11, 10.0, -10.0).and_raise_error(Puppet::ParseError, %r{to be smaller or equal}) }
  60. it { is_expected.to run.with_params(100, 10.0, -10.0).and_raise_error(Puppet::ParseError, %r{to be smaller or equal}) }
  61. it { is_expected.to run.with_params(2**65, 10.0, -10.0).and_raise_error(Puppet::ParseError, %r{to be smaller or equal}) }
  62. it { is_expected.to run.with_params([1, 2, 10.0, 100], 10.0, -10.0).and_raise_error(Puppet::ParseError, %r{to be smaller or equal}) }
  63. end
  64. describe 'rejects numbers smaller than the lower limit' do
  65. it { is_expected.to run.with_params(-11, 10.0, -10.0).and_raise_error(Puppet::ParseError, %r{to be greater or equal}) }
  66. it { is_expected.to run.with_params(-100, 10.0, -10.0).and_raise_error(Puppet::ParseError, %r{to be greater or equal}) }
  67. it { is_expected.to run.with_params(-2**65, 10.0, -10.0).and_raise_error(Puppet::ParseError, %r{to be greater or equal}) }
  68. it { is_expected.to run.with_params([-10.0, 1, 2, 10.0, -100], 10.0, -10.0).and_raise_error(Puppet::ParseError, %r{to be greater or equal}) }
  69. end
  70. describe 'accepts numbers between and including the limits' do
  71. it { is_expected.to run.with_params(10.0, 10.0, -10.0) }
  72. it { is_expected.to run.with_params(-10.0, 10.0, -10.0) }
  73. it { is_expected.to run.with_params(1, 10.0, -10.0) }
  74. it { is_expected.to run.with_params(-1, 10.0, -10.0) }
  75. it { is_expected.to run.with_params('1', 10.0, -10.0) }
  76. it { is_expected.to run.with_params('-1', 10.0, -10.0) }
  77. it { is_expected.to run.with_params([1, 2, 3, 4], 10.0, -10.0) }
  78. it { is_expected.to run.with_params([1, '2', '3', 4], 10.0, -10.0) }
  79. end
  80. end
  81. it { is_expected.to run.with_params(10.0, 10.0, 10.0) }
  82. describe 'empty upper limit is interpreted as infinity' do
  83. it { is_expected.to run.with_params(11, '', 10.0) }
  84. end
  85. end