2012-08-07 23:36:20 +02:00
|
|
|
#! /usr/bin/env ruby -S rspec
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe "the to_bytes function" do
|
|
|
|
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
|
|
|
|
|
|
|
|
it "should exist" do
|
2014-06-04 20:38:37 +02:00
|
|
|
expect(Puppet::Parser::Functions.function("to_bytes")).to eq("function_to_bytes")
|
2012-08-07 23:36:20 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should raise a ParseError if there is less than 1 arguments" do
|
2014-06-04 20:38:37 +02:00
|
|
|
expect { scope.function_to_bytes([]) }.to( raise_error(Puppet::ParseError))
|
2012-08-07 23:36:20 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should convert kB to B" do
|
|
|
|
result = scope.function_to_bytes(["4 kB"])
|
2014-06-04 20:38:37 +02:00
|
|
|
expect(result).to(eq(4096))
|
2012-08-07 23:36:20 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should work without B in unit" do
|
|
|
|
result = scope.function_to_bytes(["4 k"])
|
2014-06-04 20:38:37 +02:00
|
|
|
expect(result).to(eq(4096))
|
2012-08-07 23:36:20 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should work without a space before unit" do
|
|
|
|
result = scope.function_to_bytes(["4k"])
|
2014-06-04 20:38:37 +02:00
|
|
|
expect(result).to(eq(4096))
|
2012-08-07 23:36:20 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should work without a unit" do
|
|
|
|
result = scope.function_to_bytes(["5678"])
|
2014-06-04 20:38:37 +02:00
|
|
|
expect(result).to(eq(5678))
|
2012-08-07 23:36:20 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should convert fractions" do
|
|
|
|
result = scope.function_to_bytes(["1.5 kB"])
|
2014-06-04 20:38:37 +02:00
|
|
|
expect(result).to(eq(1536))
|
2012-08-07 23:36:20 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should convert scientific notation" do
|
|
|
|
result = scope.function_to_bytes(["1.5e2 B"])
|
2014-06-04 20:38:37 +02:00
|
|
|
expect(result).to(eq(150))
|
2012-08-07 23:36:20 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should do nothing with a positive number" do
|
|
|
|
result = scope.function_to_bytes([5678])
|
2014-06-04 20:38:37 +02:00
|
|
|
expect(result).to(eq(5678))
|
2012-08-07 23:36:20 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should should raise a ParseError if input isn't a number" do
|
2014-06-04 20:38:37 +02:00
|
|
|
expect { scope.function_to_bytes(["foo"]) }.to( raise_error(Puppet::ParseError))
|
2012-08-07 23:36:20 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should should raise a ParseError if prefix is unknown" do
|
2014-06-04 20:38:37 +02:00
|
|
|
expect { scope.function_to_bytes(["5 uB"]) }.to( raise_error(Puppet::ParseError))
|
2012-08-07 23:36:20 +02:00
|
|
|
end
|
|
|
|
end
|