to_bytes_spec.rb 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. require 'spec_helper'
  2. describe 'to_bytes' do
  3. it { is_expected.not_to eq(nil) }
  4. it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
  5. it { is_expected.to run.with_params('1', 'extras').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
  6. it { is_expected.to run.with_params([]).and_raise_error(TypeError, %r{(can't convert|no implicit conversion of) Array (in)?to String}) }
  7. it { is_expected.to run.with_params({}).and_raise_error(TypeError, %r{(can't convert|no implicit conversion of) Hash (in)?to String}) }
  8. it { is_expected.to run.with_params(true).and_raise_error(TypeError, %r{(can't convert|no implicit conversion of) (TrueClass|true) (in)?to String}) }
  9. describe 'when passing numbers' do
  10. it { is_expected.to run.with_params(0).and_return(0) }
  11. it { is_expected.to run.with_params(1).and_return(1) }
  12. it { is_expected.to run.with_params(-1).and_return(-1) }
  13. it { is_expected.to run.with_params(1.1).and_return(1.1) }
  14. it { is_expected.to run.with_params(-1.1).and_return(-1.1) }
  15. end
  16. describe 'when passing numbers as strings' do
  17. describe 'without a unit' do
  18. it { is_expected.to run.with_params('1').and_return(1) }
  19. it { is_expected.to run.with_params('-1').and_return(-1) }
  20. # these are so wrong
  21. it { is_expected.to run.with_params('1.1').and_return(1) }
  22. it { is_expected.to run.with_params('-1.1').and_return(-1) }
  23. end
  24. describe 'with a unit' do
  25. it { is_expected.to run.with_params('1k').and_return(1024) }
  26. it { is_expected.to run.with_params('-1kB').and_return(-1024) }
  27. it { is_expected.to run.with_params('1M').and_return(1024 * 1024) }
  28. it { is_expected.to run.with_params('1G').and_return(1024 * 1024 * 1024) }
  29. it { is_expected.to run.with_params('1T').and_return(1024 * 1024 * 1024 * 1024) }
  30. it { is_expected.to run.with_params('1P').and_return(1024 * 1024 * 1024 * 1024 * 1024) }
  31. it { is_expected.to run.with_params('1E').and_return(1024 * 1024 * 1024 * 1024 * 1024 * 1024) }
  32. it { is_expected.to run.with_params('1.5e3M').and_return(1_572_864_000) }
  33. it { is_expected.to run.with_params('4k').and_return(4 * 1024) }
  34. it { is_expected.to run.with_params('-4kB').and_return(4 * -1024) }
  35. it { is_expected.to run.with_params('4M').and_return(4 * 1024 * 1024) }
  36. it { is_expected.to run.with_params('4G').and_return(4 * 1024 * 1024 * 1024) }
  37. it { is_expected.to run.with_params('4T').and_return(4 * 1024 * 1024 * 1024 * 1024) }
  38. it { is_expected.to run.with_params('4P').and_return(4 * 1024 * 1024 * 1024 * 1024 * 1024) }
  39. it { is_expected.to run.with_params('4E').and_return(4 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024) }
  40. # these are so wrong
  41. it { is_expected.to run.with_params('1.0001 k').and_return(1024) }
  42. it { is_expected.to run.with_params('-1.0001 kB').and_return(-1024) }
  43. end
  44. describe 'with a unknown unit' do
  45. it { is_expected.to run.with_params('1KB').and_raise_error(Puppet::ParseError, %r{Unknown prefix}) }
  46. it { is_expected.to run.with_params('1K').and_raise_error(Puppet::ParseError, %r{Unknown prefix}) }
  47. it { is_expected.to run.with_params('1mb').and_raise_error(Puppet::ParseError, %r{Unknown prefix}) }
  48. it { is_expected.to run.with_params('1m').and_raise_error(Puppet::ParseError, %r{Unknown prefix}) }
  49. it { is_expected.to run.with_params('1%').and_raise_error(Puppet::ParseError, %r{Unknown prefix}) }
  50. it { is_expected.to run.with_params('1 p').and_raise_error(Puppet::ParseError, %r{Unknown prefix}) }
  51. end
  52. end
  53. # these are so wrong
  54. describe 'when passing random stuff' do
  55. it { is_expected.to run.with_params('-1....1').and_return(-1) }
  56. it { is_expected.to run.with_params('-1.e.e.e.1').and_return(-1) }
  57. it { is_expected.to run.with_params('-1+1').and_return(-1) }
  58. it { is_expected.to run.with_params('1-1').and_return(1) }
  59. it { is_expected.to run.with_params('1 kaboom').and_return(1024) }
  60. it { is_expected.to run.with_params('kaboom').and_return(0) }
  61. end
  62. end