summaryrefslogtreecommitdiffstats
path: root/spec/unit/parser/functions/split_spec.rb
blob: 18a21a0cfcf6866cdf0fc9656b957c92e72ed1c3 (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
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env rspec
require 'spec_helper'

describe "the split function" do
  before :all do
    Puppet::Parser::Functions.autoloader.loadall
  end

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

  it "should exist" do
    Puppet::Parser::Functions.function("split").should == "function_split"
  end

  it "should raise a ParseError if there is less than 2 arguments" do
    lambda { @scope.function_split(["foo"]) }.should( raise_error(Puppet::ParseError))
  end

  it "should raise a ParseError if there is more than 2 arguments" do
    lambda { @scope.function_split(["foo", "bar", "gazonk"]) }.should( raise_error(Puppet::ParseError))
  end

  it "should raise a RegexpError if the regexp is malformed" do
    lambda { @scope.function_split(["foo", "("]) }.should(
        raise_error(RegexpError))
  end


  it "should handle simple string without metacharacters" do
    result = @scope.function_split([ "130;236;254;10", ";"])
    result.should(eql(["130", "236", "254", "10"]))
  end

  it "should handle simple regexps" do
    result = @scope.function_split([ "130.236;254.;10", "[.;]+"])
    result.should(eql(["130", "236", "254", "10"]))
  end

  it "should handle groups" do
    result = @scope.function_split([ "130.236;254.;10", "([.;]+)"])
    result.should(eql(["130", ".", "236", ";", "254", ".;", "10"]))
  end

  it "should handle simple string without metacharacters" do
    result = @scope.function_split([ "130.236.254.10", ";"])
    result.should(eql(["130.236.254.10"]))
  end

end