summaryrefslogtreecommitdiff
path: root/spec/unit/puppet/parser/functions/validate_nonemptystring_spec.rb
blob: 41b9dcd32f3de94f1ec3d552e1a05f08e66ed6d5 (plain)
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
#! /usr/bin/env ruby -S rspec

require 'spec_helper'

describe Puppet::Parser::Functions.function(:validate_nonemptystring) do
  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }

  describe 'when calling validate_nonemptystring from puppet' do

    %w{ foo bar baz }.each do |the_string|

      it "should compile when #{the_string} is a string" do
        Puppet[:code] = "validate_nonemptystring('#{the_string}')"
        scope.compiler.compile
      end

      it "should compile when #{the_string} is a bare word" do
        Puppet[:code] = "validate_nonemptystring(#{the_string})"
        scope.compiler.compile
      end

    end

    %w{ true false }.each do |the_string|
      it "should compile when #{the_string} is a string" do
        Puppet[:code] = "validate_nonemptystring('#{the_string}')"
        scope.compiler.compile
      end

      it "should not compile when #{the_string} is a bare word" do
        Puppet[:code] = "validate_nonemptystring(#{the_string})"
        expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not a string/)
      end
    end

    it "should compile when multiple string arguments are passed" do
      Puppet[:code] = <<-'ENDofPUPPETcode'
        $foo = 'one'
        $bar = 'two'
        validate_nonemptystring($foo, $bar)
      ENDofPUPPETcode
      scope.compiler.compile
    end

    it "should not compile when an explicitly undef variable is passed" do
      Puppet[:code] = <<-'ENDofPUPPETcode'
        $foo = undef
        validate_nonemptystring($foo)
      ENDofPUPPETcode
      expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is empty or undef/)
    end

    it "should not compile when an undefined variable is passed" do
      Puppet[:code] = <<-'ENDofPUPPETcode'
        validate_nonemptystring($foobarbazishouldnotexist)
      ENDofPUPPETcode
      expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is empty or undef/)
    end

    it "should not compile when one string is empty" do
      Puppet[:code] = <<-'ENDofPUPPETcode'
        $foo = 'one'
        $bar = ''
        $baz = 'three'
        validate_nonemptystring($foo, $bar)
      ENDofPUPPETcode
      expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is empty or undef/)
    end

  end
end