Sha256: 4fadcb2f1c6fe27d4bf8d4c825991d5439c5b3a98eac175cb97e74cc3e17226f

Contents?: true

Size: 1.98 KB

Versions: 348

Compression:

Stored size: 1.98 KB

Contents

require 'spec_helper'

require 'puppet_spec/compiler'
require 'matchers/resource'

describe 'the floor function' do
  include PuppetSpec::Compiler
  include Matchers::Resource

  context 'for an integer' do
    [ 0, 1, -1].each do |x|
      it "called as floor(#{x}) results in the same value" do
        expect(compile_to_catalog("notify { String( floor(#{x}) == #{x}): }")).to have_resource("Notify[true]")
      end
    end
  end

  context 'for a float' do
    {
      0.0 => 0,
      1.1 => 1,
      -1.1 => -2,
    }.each_pair do |x, expected|
      it "called as floor(#{x}) results in #{expected}" do
        expect(compile_to_catalog("notify { String( floor(#{x}) == #{expected}): }")).to have_resource("Notify[true]")
      end
    end
  end

  context 'for a string' do
    let(:logs) { [] }
    let(:warnings) { logs.select { |log| log.level == :warning }.map { |log| log.message } }

    { "0" => 0,
      "1" => 1,
      "-1" => -1,
      "0.0" => 0,
      "1.1" => 1,
      "-1.1" => -2,
      "0777" => 777,
      "-0777" => -777,
      "0xFF" => 0xFF,
    }.each_pair do |x, expected|
      it "called as floor('#{x}') results in #{expected} and a deprecation warning" do
        Puppet::Util::Log.with_destination(Puppet::Test::LogCollector.new(logs)) do
          expect(compile_to_catalog("notify { String( floor('#{x}') == #{expected}): }")).to have_resource("Notify[true]")
        end
        expect(warnings).to include(/auto conversion of .* is deprecated/)
      end
    end

    ['blue', '0.2.3'].each do |x|
      it "errors as the string '#{x}' cannot be converted to a float (indirectly deprecated)" do
        expect{ compile_to_catalog("floor('#{x}')") }.to raise_error(/cannot convert given value to a floating point value/)
      end
    end
  end

  [[1,2,3], {'a' => 10}].each do |x|
    it "errors for a value of class #{x.class} (indirectly deprecated)" do
      expect{ compile_to_catalog("floor(#{x})") }.to raise_error(/expects a value of type Numeric or String/)
    end
  end

end

Version data entries

348 entries across 348 versions & 1 rubygems

Version Path
puppet-8.3.0 spec/unit/functions/floor_spec.rb
puppet-8.3.0-x86-mingw32 spec/unit/functions/floor_spec.rb
puppet-8.3.0-x64-mingw32 spec/unit/functions/floor_spec.rb
puppet-8.3.0-universal-darwin spec/unit/functions/floor_spec.rb
puppet-8.4.0 spec/unit/functions/floor_spec.rb
puppet-8.4.0-x86-mingw32 spec/unit/functions/floor_spec.rb
puppet-8.4.0-x64-mingw32 spec/unit/functions/floor_spec.rb
puppet-8.4.0-universal-darwin spec/unit/functions/floor_spec.rb
puppet-7.28.0 spec/unit/functions/floor_spec.rb
puppet-7.28.0-x86-mingw32 spec/unit/functions/floor_spec.rb
puppet-7.28.0-x64-mingw32 spec/unit/functions/floor_spec.rb
puppet-7.28.0-universal-darwin spec/unit/functions/floor_spec.rb
puppet-8.3.1 spec/unit/functions/floor_spec.rb
puppet-8.3.1-x86-mingw32 spec/unit/functions/floor_spec.rb
puppet-8.3.1-x64-mingw32 spec/unit/functions/floor_spec.rb
puppet-8.3.1-universal-darwin spec/unit/functions/floor_spec.rb
puppet-7.27.0 spec/unit/functions/floor_spec.rb
puppet-7.27.0-x86-mingw32 spec/unit/functions/floor_spec.rb
puppet-7.27.0-x64-mingw32 spec/unit/functions/floor_spec.rb
puppet-7.27.0-universal-darwin spec/unit/functions/floor_spec.rb