Sha256: 8d4a3860117fe6571b1f52b509888eef11fd370b370c616a0214c7a435cf74c8

Contents?: true

Size: 1.89 KB

Versions: 24

Compression:

Stored size: 1.89 KB

Contents

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

describe 'the assert_type function' do
  after(:all) { Puppet::Pops::Loaders.clear }

  let(:loaders) { Puppet::Pops::Loaders.new(Puppet::Node::Environment.create(:testing, [])) }
  let(:func) { loaders.puppet_system_loader.load(:function, 'assert_type') }

  it 'asserts compliant type by returning the value' do
    expect(func.call({}, type(String), 'hello world')).to eql('hello world')
  end

  it 'accepts type given as a String' do
    expect(func.call({}, 'String', 'hello world')).to eql('hello world')
  end

  it 'asserts non compliant type by raising an error' do
    expect do
      func.call({}, type(Integer), 'hello world')
    end.to raise_error(Puppet::ParseError, /does not match actual/)
  end

  it 'checks that first argument is a type' do
    expect do
      func.call({}, 10, 10)
    end.to raise_error(ArgumentError, Regexp.new(Regexp.escape(
"function 'assert_type' called with mis-matched arguments
expected one of:
  assert_type(Type type, Any value, Callable[Type, Type] block {0,1}) - arg count {2,3}
  assert_type(String type_string, Any value, Callable[Type, Type] block {0,1}) - arg count {2,3}
actual:
  assert_type(Integer, Integer) - arg count {2}")))
  end

  it 'allows the second arg to be undef/nil)' do
    expect do
      func.call({}, optional(String), nil)
    end.to_not raise_error
  end

  it 'can be called with a callable that receives a specific type' do
    expected, actual, actual2 = func.call({}, 'Optional[String]', 1) { |expected, actual| [expected, actual, actual] }
    expect(expected.to_s).to eql('Optional[String]')
    expect(actual.to_s).to eql('Integer[1, 1]')
    expect(actual2.to_s).to eql('Integer[1, 1]')
  end

  def optional(type_ref)
    Puppet::Pops::Types::TypeFactory.optional(type(type_ref))
  end

  def type(type_ref)
    Puppet::Pops::Types::TypeFactory.type_of(type_ref)
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
puppet-3.8.7 spec/unit/functions/assert_type_spec.rb
puppet-3.8.7-x86-mingw32 spec/unit/functions/assert_type_spec.rb
puppet-3.8.7-x64-mingw32 spec/unit/functions/assert_type_spec.rb
puppet-3.8.6 spec/unit/functions/assert_type_spec.rb
puppet-3.8.6-x86-mingw32 spec/unit/functions/assert_type_spec.rb
puppet-3.8.6-x64-mingw32 spec/unit/functions/assert_type_spec.rb
puppet-3.8.5 spec/unit/functions/assert_type_spec.rb
puppet-3.8.5-x86-mingw32 spec/unit/functions/assert_type_spec.rb
puppet-3.8.5-x64-mingw32 spec/unit/functions/assert_type_spec.rb
puppet-3.8.4 spec/unit/functions/assert_type_spec.rb
puppet-3.8.4-x86-mingw32 spec/unit/functions/assert_type_spec.rb
puppet-3.8.4-x64-mingw32 spec/unit/functions/assert_type_spec.rb
puppet-3.8.3 spec/unit/functions/assert_type_spec.rb
puppet-3.8.3-x86-mingw32 spec/unit/functions/assert_type_spec.rb
puppet-3.8.3-x64-mingw32 spec/unit/functions/assert_type_spec.rb
puppet-3.8.2 spec/unit/functions/assert_type_spec.rb
puppet-3.8.2-x86-mingw32 spec/unit/functions/assert_type_spec.rb
puppet-3.8.2-x64-mingw32 spec/unit/functions/assert_type_spec.rb
puppet-3.8.1 spec/unit/functions/assert_type_spec.rb
puppet-3.8.1-x86-mingw32 spec/unit/functions/assert_type_spec.rb