1 #! /usr/bin/env ruby -S rspec
5 describe "the to_bytes function" do
6 let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
9 expect(Puppet::Parser::Functions.function("to_bytes")).to eq("function_to_bytes")
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))
16 it "should convert kB to B" do
17 result = scope.function_to_bytes(["4 kB"])
18 expect(result).to(eq(4096))
21 it "should convert MB to B" do
22 result = scope.function_to_bytes(["4 MB"])
23 expect(result).to(eq(4194304))
26 it "should convert GB to B" do
27 result = scope.function_to_bytes(["4 GB"])
28 expect(result).to(eq(4294967296))
31 it "should convert TB to B" do
32 result = scope.function_to_bytes(["4 TB"])
33 expect(result).to(eq(4398046511104))
36 it "should convert PB to B" do
37 result = scope.function_to_bytes(["4 PB"])
38 expect(result).to(eq(4503599627370496))
41 it "should convert PB to B" do
42 result = scope.function_to_bytes(["4 EB"])
43 expect(result).to(eq(4611686018427387904))
46 it "should work without B in unit" do
47 result = scope.function_to_bytes(["4 k"])
48 expect(result).to(eq(4096))
51 it "should work without a space before unit" do
52 result = scope.function_to_bytes(["4k"])
53 expect(result).to(eq(4096))
56 it "should work without a unit" do
57 result = scope.function_to_bytes(["5678"])
58 expect(result).to(eq(5678))
61 it "should convert fractions" do
62 result = scope.function_to_bytes(["1.5 kB"])
63 expect(result).to(eq(1536))
66 it "should convert scientific notation" do
67 result = scope.function_to_bytes(["1.5e2 B"])
68 expect(result).to(eq(150))
71 it "should do nothing with a positive number" do
72 result = scope.function_to_bytes([5678])
73 expect(result).to(eq(5678))
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))
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))