Sha256: 5dce0ef16109f9960b0c1d83bb278678990c135fbe76daabe2a5ad61221cdfc1

Contents?: true

Size: 1.78 KB

Versions: 148

Compression:

Stored size: 1.78 KB

Contents

require 'spec_helper'
require 'puppet/pops'
require 'puppet/loaders'

describe 'the match function' do

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

  after(:all) do
    Puppet::Pops::Loaders.clear
    Puppet::pop_context()
  end

  let(:func) do
    Puppet.lookup(:loaders).puppet_system_loader.load(:function, 'match')
  end

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


  it 'matches string and regular expression without captures' do
    expect(func.call({}, 'abc123', /[a-z]+[1-9]+/)).to eql(['abc123'])
  end

  it 'matches string and regular expression with captures' do
    expect(func.call({}, 'abc123', /([a-z]+)([1-9]+)/)).to eql(['abc123', 'abc', '123'])
  end

  it 'produces nil if match is not found' do
    expect(func.call({}, 'abc123', /([x]+)([6]+)/)).to be_nil
  end

  [ 'Pattern[/([a-z]+)([1-9]+)/]',       # regexp
    'Pattern["([a-z]+)([1-9]+)"]',       # string
    'Regexp[/([a-z]+)([1-9]+)/]',        # regexp type
    'Pattern[/x9/, /([a-z]+)([1-9]+)/]', # regexp, first found matches
  ].each do |pattern|
    it "matches string and type #{pattern} with captures" do
      expect(func.call({}, 'abc123', type(pattern))).to eql(['abc123', 'abc', '123'])
    end
  end

  it 'matches an array of strings and yields a map of the result' do
    expect(func.call({}, ['abc123', '2a', 'xyz2'], /([a-z]+)[1-9]+/)).to eql([['abc123', 'abc'], nil, ['xyz2', 'xyz']])
  end

  it 'raises error if Regexp type without regexp is used' do
    expect{func.call({}, 'abc123', type('Regexp'))}.to raise_error(ArgumentError, /Given Regexp Type has no regular expression/)
  end

  def type(s)
    Puppet::Pops::Types::TypeParser.singleton.parse(s)
  end
end

Version data entries

148 entries across 148 versions & 1 rubygems

Version Path
puppet-5.3.7 spec/unit/functions/match_spec.rb
puppet-5.3.7-x86-mingw32 spec/unit/functions/match_spec.rb
puppet-5.3.7-x64-mingw32 spec/unit/functions/match_spec.rb
puppet-5.3.7-universal-darwin spec/unit/functions/match_spec.rb
puppet-4.10.12 spec/unit/functions/match_spec.rb
puppet-4.10.12-x86-mingw32 spec/unit/functions/match_spec.rb
puppet-4.10.12-x64-mingw32 spec/unit/functions/match_spec.rb
puppet-4.10.12-universal-darwin spec/unit/functions/match_spec.rb
puppet-5.5.1 spec/unit/functions/match_spec.rb
puppet-5.5.1-x86-mingw32 spec/unit/functions/match_spec.rb
puppet-5.5.1-x64-mingw32 spec/unit/functions/match_spec.rb
puppet-5.5.1-universal-darwin spec/unit/functions/match_spec.rb
puppet-4.10.11 spec/unit/functions/match_spec.rb
puppet-4.10.11-x86-mingw32 spec/unit/functions/match_spec.rb
puppet-4.10.11-x64-mingw32 spec/unit/functions/match_spec.rb
puppet-4.10.11-universal-darwin spec/unit/functions/match_spec.rb
puppet-5.3.6 spec/unit/functions/match_spec.rb
puppet-5.3.6-x86-mingw32 spec/unit/functions/match_spec.rb
puppet-5.3.6-x64-mingw32 spec/unit/functions/match_spec.rb
puppet-5.3.6-universal-darwin spec/unit/functions/match_spec.rb