File: split_spec.rb

package info (click to toggle)
puppet-agent 8.10.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 27,392 kB
  • sloc: ruby: 286,820; sh: 492; xml: 116; makefile: 88; cs: 68
file content (59 lines) | stat: -rw-r--r-- 2,145 bytes parent folder | download
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
52
53
54
55
56
57
58
59
require 'spec_helper'
require 'puppet/pops'
require 'puppet/loaders'

describe 'the split function' do

  before(:each) do
    loaders = Puppet::Pops::Loaders.new(Puppet::Node::Environment.create(:testing, []))
    Puppet.push_context({:loaders => loaders}, "test-examples")
  end

  after(:each) do
    Puppet.pop_context()
  end

  def split(*args)
    Puppet.lookup(:loaders).puppet_system_loader.load(:function, 'split').call({}, *args)
  end

  let(:type_parser) { Puppet::Pops::Types::TypeParser.singleton }

  it 'should raise an Error if there is less than 2 arguments' do
    expect { split('a,b') }.to raise_error(/'split' expects 2 arguments, got 1/)
  end

  it 'should raise an Error if there is more than 2 arguments' do
    expect { split('a,b','foo', 'bar') }.to raise_error(/'split' expects 2 arguments, got 3/)
  end

  it 'should raise a RegexpError if the regexp is malformed' do
    expect { split('a,b',')') }.to raise_error(/unmatched close parenthesis/)
  end

  it 'should handle pattern in string form' do
    expect(split('a,b',',')).to eql(['a', 'b'])
  end

  it 'should handle pattern in Regexp form' do
    expect(split('a,b',/,/)).to eql(['a', 'b'])
  end

  it 'should handle pattern in Regexp Type form' do
    expect(split('a,b',type_parser.parse('Regexp[/,/]'))).to eql(['a', 'b'])
  end

  it 'should handle pattern in Regexp Type form with empty regular expression' do
    expect(split('ab',type_parser.parse('Regexp[//]'))).to eql(['a', 'b'])
  end

  it 'should handle pattern in Regexp Type form with missing regular expression' do
    expect(split('ab',type_parser.parse('Regexp'))).to eql(['a', 'b'])
  end

  it 'should handle sensitive String' do
    expect(split(Puppet::Pops::Types::PSensitiveType::Sensitive.new('a,b'), ',')).to be_a(Puppet::Pops::Types::PSensitiveType::Sensitive)
    expect(split(Puppet::Pops::Types::PSensitiveType::Sensitive.new('a,b'), /,/)).to be_a(Puppet::Pops::Types::PSensitiveType::Sensitive)
    expect(split(Puppet::Pops::Types::PSensitiveType::Sensitive.new('a,b'), type_parser.parse('Regexp[/,/]'))).to be_a(Puppet::Pops::Types::PSensitiveType::Sensitive)
  end
end