2be23ff2d0039d8cc224580a77f40aaf352d789e
[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, /wrong number of arguments/i) }
6   it { is_expected.to run.with_params('1', 'extras').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
7   it { is_expected.to run.with_params([]).and_raise_error(TypeError, /(can't convert|no implicit conversion of) Array (in)?to String/) }
8   it { is_expected.to run.with_params({}).and_raise_error(TypeError, /(can't convert|no implicit conversion of) Hash (in)?to String/) }
9   it { is_expected.to run.with_params(true).and_raise_error(TypeError, /(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('1k').and_return(1024) }
32       it { is_expected.to run.with_params('1M').and_return(1024*1024) }
33       it { is_expected.to run.with_params('1G').and_return(1024*1024*1024) }
34       it { is_expected.to run.with_params('1T').and_return(1024*1024*1024*1024) }
35       it { is_expected.to run.with_params('1P').and_return(1024*1024*1024*1024*1024) }
36       it { is_expected.to run.with_params('1E').and_return(1024*1024*1024*1024*1024*1024) }
37       it { is_expected.to run.with_params('1.5e3M').and_return(1572864000) }
38
39       it { is_expected.to run.with_params('4k').and_return(4*1024) }
40       it { is_expected.to run.with_params('-4kB').and_return(4*-1024) }
41       it { is_expected.to run.with_params('4k').and_return(4*1024) }
42       it { is_expected.to run.with_params('4M').and_return(4*1024*1024) }
43       it { is_expected.to run.with_params('4G').and_return(4*1024*1024*1024) }
44       it { is_expected.to run.with_params('4T').and_return(4*1024*1024*1024*1024) }
45       it { is_expected.to run.with_params('4P').and_return(4*1024*1024*1024*1024*1024) }
46       it { is_expected.to run.with_params('4E').and_return(4*1024*1024*1024*1024*1024*1024) }
47
48       # these are so wrong
49       it { is_expected.to run.with_params('1.0001 k').and_return(1024) }
50       it { is_expected.to run.with_params('-1.0001 kB').and_return(-1024) }
51     end
52
53     describe 'with a unknown unit' do
54       it { is_expected.to run.with_params('1KB').and_raise_error(Puppet::ParseError, /Unknown prefix/) }
55       it { is_expected.to run.with_params('1K').and_raise_error(Puppet::ParseError, /Unknown prefix/) }
56       it { is_expected.to run.with_params('1mb').and_raise_error(Puppet::ParseError, /Unknown prefix/) }
57       it { is_expected.to run.with_params('1m').and_raise_error(Puppet::ParseError, /Unknown prefix/) }
58       it { is_expected.to run.with_params('1%').and_raise_error(Puppet::ParseError, /Unknown prefix/) }
59       it { is_expected.to run.with_params('1 p').and_raise_error(Puppet::ParseError, /Unknown prefix/) }
60     end
61   end
62
63   # these are so wrong
64   describe 'when passing random stuff' do
65     it { is_expected.to run.with_params('-1....1').and_return(-1) }
66     it { is_expected.to run.with_params('-1.e.e.e.1').and_return(-1) }
67     it { is_expected.to run.with_params('-1+1').and_return(-1) }
68     it { is_expected.to run.with_params('1-1').and_return(1) }
69     it { is_expected.to run.with_params('1 kaboom').and_return(1024) }
70     it { is_expected.to run.with_params('kaboom').and_return(0) }
71   end
72 end