X-Git-Url: https://git.adam-barratt.org.uk/?a=blobdiff_plain;f=3rdparty%2Fmodules%2Fstdlib%2Fspec%2Ffunctions%2Frange_spec.rb;h=7b9e6d43cc2918a207c87fc32aa0aae335532e50;hb=d53c789b368b6d3dbc41671fd7a1f16050172627;hp=ef86f97199e728443d89dd36d38b7ab1ea192786;hpb=ad88f67c13ae0f1a08936dad643f1e3509ab5f40;p=mirror%2Fdsa-puppet.git diff --git a/3rdparty/modules/stdlib/spec/functions/range_spec.rb b/3rdparty/modules/stdlib/spec/functions/range_spec.rb old mode 100755 new mode 100644 index ef86f9719..7b9e6d43c --- a/3rdparty/modules/stdlib/spec/functions/range_spec.rb +++ b/3rdparty/modules/stdlib/spec/functions/range_spec.rb @@ -1,86 +1,157 @@ -#! /usr/bin/env ruby -S rspec require 'spec_helper' -describe "the range function" do - let(:scope) { PuppetlabsSpec::PuppetInternals.scope } +describe 'range' do + it { is_expected.not_to eq(nil) } - it "exists" do - expect(Puppet::Parser::Functions.function("range")).to eq("function_range") + describe 'signature validation in puppet3', :unless => RSpec.configuration.puppet_future do + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { + pending('Current implementation ignores parameters after the third.') + is_expected.to run.with_params(1, 2, 3, 4).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) + } + it { is_expected.to run.with_params('1..2..3').and_raise_error(Puppet::ParseError, %r{Unable to compute range}i) } + it { is_expected.to run.with_params('').and_raise_error(Puppet::ParseError, %r{Unknown range format}i) } end - it "raises a ParseError if there is less than 1 arguments" do - expect { scope.function_range([]) }.to raise_error Puppet::ParseError, /Wrong number of arguments.*0 for 1/ + describe 'signature validation in puppet4', :if => RSpec.configuration.puppet_future do + it { + pending 'the puppet 4 implementation' + is_expected.to run.with_params.and_raise_error(ArgumentError) + } + it { + pending 'the puppet 4 implementation' + is_expected.to run.with_params('').and_raise_error(ArgumentError) + } + it { + pending 'the puppet 4 implementation' + is_expected.to run.with_params({}).and_raise_error(ArgumentError) + } + it { + pending 'the puppet 4 implementation' + is_expected.to run.with_params([]).and_raise_error(ArgumentError) + } + it { + pending 'the puppet 4 implementation' + is_expected.to run.with_params(true).and_raise_error(ArgumentError) + } + it { + is_expected.to run.with_params(1, 2, 'foo').and_raise_error(ArgumentError) + } + it { + pending 'the puppet 4 implementation' + is_expected.to run.with_params(1, 2, []).and_raise_error(ArgumentError) + } + it { + pending 'the puppet 4 implementation' + is_expected.to run.with_params(1, 2, {}).and_raise_error(ArgumentError) + } + it { + pending 'the puppet 4 implementation' + is_expected.to run.with_params(1, 2, true).and_raise_error(ArgumentError) + } + it { + pending 'the puppet 4 implementation' + is_expected.to run.with_params(1, 2, 3, 4).and_raise_error(ArgumentError) + } + it { + pending 'the puppet 4 implementation' + is_expected.to run.with_params('1..2..3').and_raise_error(ArgumentError) + } end - describe 'with a letter range' do - it "returns a letter range" do - result = scope.function_range(["a","d"]) - expect(result).to eq ['a','b','c','d'] - end + context 'with characters as bounds' do + it { is_expected.to run.with_params('d', 'a').and_return([]) } + it { is_expected.to run.with_params('a', 'a').and_return(['a']) } + it { is_expected.to run.with_params('a', 'b').and_return(['a', 'b']) } + it { is_expected.to run.with_params('a', 'd').and_return(['a', 'b', 'c', 'd']) } + it { is_expected.to run.with_params('a', 'd', 1).and_return(['a', 'b', 'c', 'd']) } + it { is_expected.to run.with_params('a', 'd', '1').and_return(['a', 'b', 'c', 'd']) } + it { is_expected.to run.with_params('a', 'd', 2).and_return(['a', 'c']) } + it { is_expected.to run.with_params('a', 'd', -2).and_return(['a', 'c']) } + it { is_expected.to run.with_params('a', 'd', 3).and_return(['a', 'd']) } + it { is_expected.to run.with_params('a', 'd', 4).and_return(['a']) } + end - it "returns a letter range given a step of 1" do - result = scope.function_range(["a","d","1"]) - expect(result).to eq ['a','b','c','d'] - end + context 'with strings as bounds' do + it { is_expected.to run.with_params('onea', 'oned').and_return(['onea', 'oneb', 'onec', 'oned']) } + it { is_expected.to run.with_params('two', 'one').and_return([]) } + it { is_expected.to run.with_params('true', 'false').and_return([]) } + it { is_expected.to run.with_params('false', 'true').and_return(['false']) } + end - it "returns a stepped letter range" do - result = scope.function_range(["a","d","2"]) - expect(result).to eq ['a','c'] - end + context 'with integers as bounds' do + it { is_expected.to run.with_params(4, 1).and_return([]) } + it { is_expected.to run.with_params(1, 1).and_return([1]) } + it { is_expected.to run.with_params(1, 2).and_return([1, 2]) } + it { is_expected.to run.with_params(1, 4).and_return([1, 2, 3, 4]) } + it { is_expected.to run.with_params(1, 4, 1).and_return([1, 2, 3, 4]) } + it { is_expected.to run.with_params(1, 4, '1').and_return([1, 2, 3, 4]) } + it { is_expected.to run.with_params(1, 4, 2).and_return([1, 3]) } + it { is_expected.to run.with_params(1, 4, -2).and_return([1, 3]) } + it { is_expected.to run.with_params(1, 4, 3).and_return([1, 4]) } + it { is_expected.to run.with_params(1, 4, 4).and_return([1]) } + end - it "returns a stepped letter range given a negative step" do - result = scope.function_range(["a","d","-2"]) - expect(result).to eq ['a','c'] - end + context 'with integers as strings as bounds' do + it { is_expected.to run.with_params('4', '1').and_return([]) } + it { is_expected.to run.with_params('1', '1').and_return([1]) } + it { is_expected.to run.with_params('1', '2').and_return([1, 2]) } + it { is_expected.to run.with_params('1', '4').and_return([1, 2, 3, 4]) } + it { is_expected.to run.with_params('1', '4', 1).and_return([1, 2, 3, 4]) } + it { is_expected.to run.with_params('1', '4', '1').and_return([1, 2, 3, 4]) } + it { is_expected.to run.with_params('1', '4', 2).and_return([1, 3]) } + it { is_expected.to run.with_params('1', '4', -2).and_return([1, 3]) } + it { is_expected.to run.with_params('1', '4', 3).and_return([1, 4]) } + it { is_expected.to run.with_params('1', '4', 4).and_return([1]) } end - describe 'with a number range' do - it "returns a number range" do - result = scope.function_range(["1","4"]) - expect(result).to eq [1,2,3,4] - end + context 'with prefixed numbers as strings as bounds' do + it { is_expected.to run.with_params('host01', 'host04').and_return(['host01', 'host02', 'host03', 'host04']) } + it { is_expected.to run.with_params('01', '04').and_return([1, 2, 3, 4]) } + end - it "returns a number range given a step of 1" do - result = scope.function_range(["1","4","1"]) - expect(result).to eq [1,2,3,4] - end + context 'with prefixed numbers as utf8 strings as bounds' do + it { is_expected.to run.with_params('ħөŝŧ01', 'ħөŝŧ04').and_return(['ħөŝŧ01', 'ħөŝŧ02', 'ħөŝŧ03', 'ħөŝŧ04']) } + end - it "returns a stepped number range" do - result = scope.function_range(["1","4","2"]) - expect(result).to eq [1,3] - end + context 'with prefixed numbers as double byte character strings as bounds' do + it { is_expected.to run.with_params('ホスト01', 'ホスト04').and_return(['ホスト01', 'ホスト02', 'ホスト03', 'ホスト04']) } + end - it "returns a stepped number range given a negative step" do - result = scope.function_range(["1","4","-2"]) - expect(result).to eq [1,3] - end + context 'with dash-range syntax' do + it { is_expected.to run.with_params('4-1').and_return([]) } + it { is_expected.to run.with_params('1-1').and_return([1]) } + it { is_expected.to run.with_params('1-2').and_return([1, 2]) } + it { is_expected.to run.with_params('1-4').and_return([1, 2, 3, 4]) } end - describe 'with a numeric-like string range' do - it "works with padded hostname like strings" do - expected = ("host01".."host10").to_a - expect(scope.function_range(["host01","host10"])).to eq expected - end + context 'with two-dot-range syntax' do + it { is_expected.to run.with_params('4..1').and_return([]) } + it { is_expected.to run.with_params('1..1').and_return([1]) } + it { is_expected.to run.with_params('1..2').and_return([1, 2]) } + it { is_expected.to run.with_params('1..4').and_return([1, 2, 3, 4]) } + end - it "coerces zero padded digits to integers" do - expected = (0..10).to_a - expect(scope.function_range(["00", "10"])).to eq expected - end + context 'with three-dot-range syntax' do + it { is_expected.to run.with_params('4...1').and_return([]) } + it { is_expected.to run.with_params('1...1').and_return([]) } + it { is_expected.to run.with_params('1...2').and_return([1]) } + it { is_expected.to run.with_params('1...3').and_return([1, 2]) } + it { is_expected.to run.with_params('1...5').and_return([1, 2, 3, 4]) } end - describe 'with a numeric range' do - it "returns a range of numbers" do - expected = (1..10).to_a - expect(scope.function_range([1,10])).to eq expected - end - it "returns a range of numbers with step parameter" do - expected = (1..10).step(2).to_a - expect(scope.function_range([1,10,2])).to eq expected - end - it "works with mixed numeric like strings and numeric arguments" do - expected = (1..10).to_a - expect(scope.function_range(['1',10])).to eq expected - expect(scope.function_range([1,'10'])).to eq expected - end + describe 'when passing mixed arguments as bounds' do + it { + pending('these bounds should not be allowed as ruby will OOM hard. e.g. `(\'host0\'..\'hosta\').to_a` has 3239930 elements on ruby 1.9, adding more \'0\'s and \'a\'s increases that exponentially') # rubocop:disable Metrics/LineLength : unable to cut line to required length + is_expected.to run.with_params('0', 'a').and_raise_error(Puppet::ParseError, %r{cannot interpolate between numeric and non-numeric bounds}) + } + it { + pending('these bounds should not be allowed as ruby will OOM hard. e.g. `(\'host0\'..\'hosta\').to_a` has 3239930 elements on ruby 1.9, adding more \'0\'s and \'a\'s increases that exponentially') # rubocop:disable Metrics/LineLength : unable to cut line to required length + is_expected.to run.with_params(0, 'a').and_raise_error(Puppet::ParseError, %r{cannot interpolate between numeric and non-numeric bounds}) + } + it { + pending('these bounds should not be allowed as ruby will OOM hard. e.g. `(\'host0\'..\'hosta\').to_a` has 3239930 elements on ruby 1.9, adding more \'0\'s and \'a\'s increases that exponentially') # rubocop:disable Metrics/LineLength : unable to cut line to required length + is_expected.to run.with_params('h0', 'ha').and_raise_error(Puppet::ParseError, %r{cannot interpolate between numeric and non-numeric bounds}) + } end end