]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/stdlib/spec/functions/to_bytes_spec.rb
upgrade to stdlib 4.6.1
[dsa-puppet.git] / 3rdparty / modules / stdlib / spec / functions / to_bytes_spec.rb
1 #! /usr/bin/env ruby -S rspec
2
3 require 'spec_helper'
4
5 describe "the to_bytes function" do
6   let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
7
8   it "should exist" do
9     expect(Puppet::Parser::Functions.function("to_bytes")).to eq("function_to_bytes")
10   end
11
12   it "should raise a ParseError if there is less than 1 arguments" do
13     expect { scope.function_to_bytes([]) }.to( raise_error(Puppet::ParseError))
14   end
15
16   it "should convert kB to B" do
17     result = scope.function_to_bytes(["4 kB"])
18     expect(result).to(eq(4096))
19   end
20
21   it "should convert MB to B" do
22     result = scope.function_to_bytes(["4 MB"])
23     expect(result).to(eq(4194304))
24   end
25
26   it "should convert GB to B" do
27     result = scope.function_to_bytes(["4 GB"])
28     expect(result).to(eq(4294967296))
29   end
30
31   it "should convert TB to B" do
32     result = scope.function_to_bytes(["4 TB"])
33     expect(result).to(eq(4398046511104))
34   end
35
36   it "should convert PB to B" do
37     result = scope.function_to_bytes(["4 PB"])
38     expect(result).to(eq(4503599627370496))
39   end
40
41   it "should convert PB to B" do
42     result = scope.function_to_bytes(["4 EB"])
43     expect(result).to(eq(4611686018427387904))
44   end
45
46   it "should work without B in unit" do
47     result = scope.function_to_bytes(["4 k"])
48     expect(result).to(eq(4096))
49   end
50
51   it "should work without a space before unit" do
52     result = scope.function_to_bytes(["4k"])
53     expect(result).to(eq(4096))
54   end
55
56   it "should work without a unit" do
57     result = scope.function_to_bytes(["5678"])
58     expect(result).to(eq(5678))
59   end
60
61   it "should convert fractions" do
62     result = scope.function_to_bytes(["1.5 kB"])
63     expect(result).to(eq(1536))
64   end
65
66   it "should convert scientific notation" do
67     result = scope.function_to_bytes(["1.5e2 B"])
68     expect(result).to(eq(150))
69   end
70
71   it "should do nothing with a positive number" do
72     result = scope.function_to_bytes([5678])
73     expect(result).to(eq(5678))
74   end
75
76   it "should should raise a ParseError if input isn't a number" do
77     expect { scope.function_to_bytes(["foo"]) }.to( raise_error(Puppet::ParseError))
78   end
79
80   it "should should raise a ParseError if prefix is unknown" do
81     expect { scope.function_to_bytes(["5 uB"]) }.to( raise_error(Puppet::ParseError))
82   end
83 end