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