Skip to content

Commit afb78e2

Browse files
Ashley Penneyhunner
authored andcommitted
Add some acceptance tests for functions.
1 parent 746a4cc commit afb78e2

14 files changed

+335
-0
lines changed

spec/acceptance/abs_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require 'spec_helper_acceptance'
2+
3+
describe 'abs function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do
4+
it 'should accept a string' do
5+
pp = <<-EOS
6+
$input = '-34.56'
7+
$output = abs($input)
8+
notify { $output: }
9+
EOS
10+
11+
apply_manifest(pp, :catch_failures => true) do |r|
12+
expect(r.stdout).to match(/Notice: 34.56/)
13+
end
14+
end
15+
16+
it 'should accept a float' do
17+
pp = <<-EOS
18+
$input = -34.56
19+
$output = abs($input)
20+
notify { $output: }
21+
EOS
22+
23+
apply_manifest(pp, :catch_failures => true) do |r|
24+
expect(r.stdout).to match(/Notice: 34.56/)
25+
end
26+
end
27+
end

spec/acceptance/any2array_spec.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
require 'spec_helper_acceptance'
2+
3+
describe 'any2array function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do
4+
it 'should create an empty array' do
5+
pp = <<-EOS
6+
$input = ''
7+
$output = any2array($input)
8+
validate_array($output)
9+
notify { "Output: ${output}": }
10+
EOS
11+
12+
apply_manifest(pp, :catch_failures => true) do |r|
13+
expect(r.stdout).to match(/Notice: Output: /)
14+
end
15+
end
16+
17+
it 'should leave arrays modified' do
18+
pp = <<-EOS
19+
$input = ['test', 'array']
20+
$output = any2array($input)
21+
validate_array($output)
22+
notify { "Output: ${output}": }
23+
EOS
24+
25+
apply_manifest(pp, :catch_failures => true) do |r|
26+
expect(r.stdout).to match(/Notice: Output: testarray/)
27+
end
28+
end
29+
30+
it 'should turn a hash into an array' do
31+
pp = <<-EOS
32+
$input = {'test' => 'array'}
33+
$output = any2array($input)
34+
35+
validate_array($output)
36+
# Check each element of the array is a plain string.
37+
validate_string($output[0])
38+
validate_string($output[1])
39+
notify { "Output: ${output}": }
40+
EOS
41+
42+
apply_manifest(pp, :catch_failures => true) do |r|
43+
expect(r.stdout).to match(/Notice: Output: testarray/)
44+
end
45+
end
46+
end

spec/acceptance/base64_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require 'spec_helper_acceptance'
2+
3+
describe 'base64 function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do
4+
it 'should encode then decode a string' do
5+
pp = <<-EOS
6+
$encodestring = base64('encode', 'thestring')
7+
$decodestring = base64('decode', $encodestring)
8+
notify { $decodestring: }
9+
EOS
10+
11+
apply_manifest(pp, :catch_failures => true) do |r|
12+
expect(r.stdout).to match(/thestring/)
13+
end
14+
end
15+
end

spec/acceptance/bool2num_spec.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
require 'spec_helper_acceptance'
2+
3+
describe 'bool2num function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do
4+
['false', 'f', '0', 'n', 'no'].each do |bool|
5+
it 'should convert a given boolean, #{bool}, to 0' do
6+
pp = <<-EOS
7+
$input = #{bool}
8+
$output = bool2num($input)
9+
notify { $output: }
10+
EOS
11+
12+
apply_manifest(pp, :catch_failures => true) do |r|
13+
expect(r.stdout).to match(/Notice: 0/)
14+
end
15+
end
16+
end
17+
18+
['true', 't', '1', 'y', 'yes'].each do |bool|
19+
it 'should convert a given boolean, #{bool}, to 1' do
20+
pp = <<-EOS
21+
$input = #{bool}
22+
$output = bool2num($input)
23+
notify { $output: }
24+
EOS
25+
26+
apply_manifest(pp, :catch_failures => true) do |r|
27+
expect(r.stdout).to match(/Notice: 1/)
28+
end
29+
end
30+
end
31+
end

spec/acceptance/capitalize_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
require 'spec_helper_acceptance'
2+
3+
describe 'capitalize function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do
4+
it 'should capitalize the first letter of a string' do
5+
pp = <<-EOS
6+
$input = 'this is a string'
7+
$output = capitalize($input)
8+
notify { $output: }
9+
EOS
10+
11+
apply_manifest(pp, :catch_failures => true) do |r|
12+
expect(r.stdout).to match(/Notice: This is a string/)
13+
end
14+
end
15+
16+
it 'should capitalize the first letter of an array of strings' do
17+
pp = <<-EOS
18+
$input = ['this', 'is', 'a', 'string']
19+
$output = capitalize($input)
20+
notify { $output: }
21+
EOS
22+
23+
apply_manifest(pp, :catch_failures => true) do |r|
24+
expect(r.stdout).to match(/Notice: This/)
25+
expect(r.stdout).to match(/Notice: Is/)
26+
expect(r.stdout).to match(/Notice: A/)
27+
expect(r.stdout).to match(/Notice: String/)
28+
end
29+
end
30+
end

spec/acceptance/chomp_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
require 'spec_helper_acceptance'
2+
3+
describe 'chomp function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do
4+
it 'should eat the newline' do
5+
pp = <<-EOS
6+
$input = "test\n"
7+
if size($input) != 5 {
8+
fail("Size of ${input} is not 5.")
9+
}
10+
$output = chomp($input)
11+
if size($output) != 4 {
12+
fail("Size of ${input} is not 4.")
13+
}
14+
EOS
15+
16+
apply_manifest(pp, :catch_failures => true)
17+
end
18+
end

spec/acceptance/chop_spec.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
require 'spec_helper_acceptance'
2+
3+
describe 'chop function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do
4+
it 'should eat the last character' do
5+
pp = <<-EOS
6+
$input = "test"
7+
if size($input) != 4 {
8+
fail("Size of ${input} is not 4.")
9+
}
10+
$output = chop($input)
11+
if size($output) != 3 {
12+
fail("Size of ${input} is not 3.")
13+
}
14+
EOS
15+
16+
apply_manifest(pp, :catch_failures => true)
17+
end
18+
19+
it 'should eat the last two characters of \r\n' do
20+
pp = <<-EOS
21+
$input = "test\r\n"
22+
if size($input) != 6 {
23+
fail("Size of ${input} is not 6.")
24+
}
25+
$output = chop($input)
26+
if size($output) != 4 {
27+
fail("Size of ${input} is not 4.")
28+
}
29+
EOS
30+
31+
apply_manifest(pp, :catch_failures => true)
32+
end
33+
34+
it 'should not fail on empty strings' do
35+
pp = <<-EOS
36+
$input = ""
37+
$output = chop($input)
38+
EOS
39+
40+
apply_manifest(pp, :catch_failures => true)
41+
end
42+
end

spec/acceptance/concat_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require 'spec_helper_acceptance'
2+
3+
describe 'concat function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do
4+
it 'should concat one array to another' do
5+
pp = <<-EOS
6+
$output = concat(['1','2','3'],['4','5','6'])
7+
validate_array($output)
8+
if size($output) != 6 {
9+
fail("${output} should have 6 elements.")
10+
}
11+
EOS
12+
13+
apply_manifest(pp, :catch_failures => true)
14+
end
15+
end

spec/acceptance/count_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require 'spec_helper_acceptance'
2+
3+
describe 'count function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do
4+
it 'should count elements in an array' do
5+
pp = <<-EOS
6+
$input = [1,2,3,4]
7+
$output = count($input)
8+
notify { $output: }
9+
EOS
10+
11+
apply_manifest(pp, :catch_failures => true) do |r|
12+
expect(r.stdout).to match(/Notice: 4/)
13+
end
14+
end
15+
16+
it 'should count elements in an array that match a second argument' do
17+
pp = <<-EOS
18+
$input = [1,1,1,2]
19+
$output = count($input, 1)
20+
notify { $output: }
21+
EOS
22+
23+
apply_manifest(pp, :catch_failures => true) do |r|
24+
expect(r.stdout).to match(/Notice: 3/)
25+
end
26+
end
27+
end

spec/acceptance/deep_merge_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require 'spec_helper_acceptance'
2+
3+
describe 'deep_merge function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do
4+
it 'should deep merge two hashes' do
5+
pp = <<-EOS
6+
$hash1 = {'one' => 1, 'two' => 2, 'three' => { 'four' => 4 } }
7+
$hash2 = {'two' => 'dos', 'three' => { 'five' => 5 } }
8+
$merged_hash = deep_merge($hash1, $hash2)
9+
10+
if $merged_hash != { 'one' => 1, 'two' => 'dos', 'three' => { 'four' => 4, 'five' => 5 } } {
11+
fail("Hash was incorrectly merged.")
12+
}
13+
EOS
14+
15+
apply_manifest(pp, :catch_failures => true)
16+
end
17+
end

0 commit comments

Comments
 (0)