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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
require 'spec_helper'
require 'puppet_spec/compiler'
require 'matchers/resource'
describe 'the sort function' do
include PuppetSpec::Compiler
include Matchers::Resource
{
"'bac'" => "'abc'",
"'BaC'" => "'BCa'",
}.each_pair do |value, expected|
it "sorts characters in a string such that #{value}.sort results in #{expected}" do
expect(compile_to_catalog("notify { String( sort(#{value}) == #{expected}): }")).to have_resource("Notify[true]")
end
end
{
"'bac'" => "'abc'",
"'BaC'" => "'aBC'",
}.each_pair do |value, expected|
it "accepts a lambda when sorting characters in a string such that #{value} results in #{expected}" do
expect(compile_to_catalog("notify { String( sort(#{value}) |$a,$b| { compare($a,$b) } == #{expected}): }")).to have_resource("Notify[true]")
end
end
{
['b', 'a', 'c'] => ['a', 'b', 'c'],
['B', 'a', 'C'] => ['B', 'C', 'a'],
}.each_pair do |value, expected|
it "sorts strings in an array such that #{value}.sort results in #{expected}" do
expect(compile_to_catalog("notify { String( sort(#{value}) == #{expected}): }")).to have_resource("Notify[true]")
end
end
{
['b', 'a', 'c'] => ['a', 'b', 'c'],
['B', 'a', 'C'] => ['a', 'B', 'C'],
}.each_pair do |value, expected|
it "accepts a lambda when sorting an array such that #{value} results in #{expected}" do
expect(compile_to_catalog("notify { String( sort(#{value}) |$a,$b| { compare($a,$b) } == #{expected}): }")).to have_resource("Notify[true]")
end
end
it 'errors if given a mix of data types' do
expect { compile_to_catalog("sort([1, 'a'])")}.to raise_error(/comparison .* failed/)
end
it 'returns empty string for empty string input' do
expect(compile_to_catalog("notify { String(sort('') == ''): }")).to have_resource("Notify[true]")
end
it 'returns empty string for empty string input' do
expect(compile_to_catalog("notify { String(sort([]) == []): }")).to have_resource("Notify[true]")
end
it 'can sort mixed data types when using a lambda' do
# source sorts Numeric before string and uses compare() for same data type
src = <<-SRC
notify{ String(sort(['b', 3, 'a', 2]) |$a, $b| {
case [$a, $b] {
[String, Numeric] : { 1 }
[Numeric, String] : { -1 }
default: { compare($a, $b) }
}
} == [2, 3,'a', 'b']):
}
SRC
expect(compile_to_catalog(src)).to have_resource("Notify[true]")
end
it 'errors if lambda does not accept 2 arguments' do
expect { compile_to_catalog("sort([1, 'a']) || { }")}.to raise_error(/block expects 2 arguments/)
expect { compile_to_catalog("sort([1, 'a']) |$x| { }")}.to raise_error(/block expects 2 arguments/)
expect { compile_to_catalog("sort([1, 'a']) |$x,$y, $z| { }")}.to raise_error(/block expects 2 arguments/)
end
end
|