summaryrefslogtreecommitdiffstats
path: root/spec/unit/parser/functions/sprintf_spec.rb
blob: 4f29012b3fc0debf259c4eb86828b9bc54847a01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#! /usr/bin/env ruby

require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper')

describe "the sprintf function" do

  before :each do
    @scope = Puppet::Parser::Scope.new
  end

  it "should exist" do
    Puppet::Parser::Functions.function("sprintf").should == "function_sprintf"
  end

  it "should raise a ParseError if there is less than 1 argument" do
    lambda { @scope.function_sprintf([]) }.should( raise_error(Puppet::ParseError))
  end

  it "should format integers" do
    result = @scope.function_sprintf(["%+05d", "23"])
    result.should(eql("+0023"))
  end

  it "should format floats" do
    result = @scope.function_sprintf(["%+.2f", "2.7182818284590451"])
    result.should(eql("+2.72"))
  end

  it "should format large floats" do
    result = @scope.function_sprintf(["%+.2e", "27182818284590451"])
    result.should(eql("+2.72e+16"))
  end

  it "should perform more complex formatting" do
    result = @scope.function_sprintf(
      [ "<%.8s:%#5o %#8X (%-8s)>",
        "overlongstring", "23", "48879", "foo" ])
    result.should(eql("<overlong:  027   0XBEEF (foo     )>"))
  end

end