validate_slength_spec.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. require 'spec_helper'
  2. describe 'validate_slength' 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('1234567890', 10)
  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('').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
  16. it { is_expected.to run.with_params('', 2, 3, 'extra').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
  17. it { is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, %r{second argument to be a positive Numeric}) }
  18. it { is_expected.to run.with_params('', -1).and_raise_error(Puppet::ParseError, %r{second argument to be a positive Numeric}) }
  19. it { is_expected.to run.with_params('', 1, '').and_raise_error(Puppet::ParseError, %r{third argument to be unset or a positive Numeric}) }
  20. it { is_expected.to run.with_params('', 1, -1).and_raise_error(Puppet::ParseError, %r{third argument to be unset or a positive Numeric}) }
  21. it { is_expected.to run.with_params('', 1, 2).and_raise_error(Puppet::ParseError, %r{argument to be equal to or larger than third argument}) }
  22. end
  23. context 'with a maximum length of 10' do
  24. describe 'rejects strings longer than the limit' do
  25. it { is_expected.to run.with_params('1234567890a', 10).and_raise_error(Puppet::ParseError, %r{Expected length}) }
  26. it { is_expected.to run.with_params('1234567890abcdef', 10).and_raise_error(Puppet::ParseError, %r{Expected length}) }
  27. it { is_expected.to run.with_params(%w[one 1234567890abcdef], 10).and_raise_error(Puppet::ParseError, %r{Expected length}) }
  28. end
  29. describe 'accepts strings shorter or equal to the limit' do
  30. it { is_expected.to run.with_params('1234567890', 10) }
  31. it { is_expected.to run.with_params('12345', 10) }
  32. it { is_expected.to run.with_params(%w[one two], 10) }
  33. end
  34. end
  35. context 'with a minimum length of 5' do
  36. describe 'rejects strings longer than the upper limit' do
  37. it { is_expected.to run.with_params('1234567890a', 10, 5).and_raise_error(Puppet::ParseError, %r{Expected length}) }
  38. it { is_expected.to run.with_params('1234567890abcdef', 10, 5).and_raise_error(Puppet::ParseError, %r{Expected length}) }
  39. end
  40. describe 'rejects numbers shorter than the lower limit' do
  41. it { is_expected.to run.with_params('one', 10, 5).and_raise_error(Puppet::ParseError, %r{Expected length}) }
  42. it { is_expected.to run.with_params(%w[12345678 two], 10, 5).and_raise_error(Puppet::ParseError, %r{Expected length}) }
  43. end
  44. describe 'accepts strings of length between and including the limits' do
  45. it { is_expected.to run.with_params('12345', 10, 5) }
  46. it { is_expected.to run.with_params('123456', 10, 5) }
  47. it { is_expected.to run.with_params('1234567', 10, 5) }
  48. it { is_expected.to run.with_params('12345678', 10, 5) }
  49. it { is_expected.to run.with_params('123456789', 10, 5) }
  50. it { is_expected.to run.with_params('1234567890', 10, 5) }
  51. it { is_expected.to run.with_params(%w[1233456 12345678], 10, 5) }
  52. end
  53. end
  54. describe 'corner cases' do
  55. it {
  56. pending('this should work')
  57. is_expected.to run.with_params('', 0, 0)
  58. }
  59. it { is_expected.to run.with_params('1234567890', 10, 10) }
  60. end
  61. describe 'empty upper limit is interpreted as infinity' do
  62. it {
  63. pending('not implemented')
  64. is_expected.to run.with_params('1234567890ab', '', 10)
  65. }
  66. it {
  67. pending('not implemented')
  68. is_expected.to run.with_params('12345678', '', 10).and_raise_error(Puppet::ParseError, %r{Expected length})
  69. }
  70. end
  71. end