File: ifstatement_spec.rb

package info (click to toggle)
puppet 3.7.2-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 18,896 kB
  • sloc: ruby: 210,387; sh: 2,050; xml: 1,554; lisp: 300; makefile: 142; python: 108; sql: 103; yacc: 72
file content (77 lines) | stat: -rwxr-xr-x 2,102 bytes parent folder | download | duplicates (3)
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
#! /usr/bin/env ruby
require 'spec_helper'

describe Puppet::Parser::AST::IfStatement do
  before :each do
    node     = Puppet::Node.new('localhost')
    compiler = Puppet::Parser::Compiler.new(node)
    @scope   = Puppet::Parser::Scope.new(compiler)
  end

  describe "when evaluating" do

    before :each do
      @test = stub 'test'
      @test.stubs(:safeevaluate).with(@scope)

      @stmt = stub 'stmt'
      @stmt.stubs(:safeevaluate).with(@scope)

      @else = stub 'else'
      @else.stubs(:safeevaluate).with(@scope)

      @ifstmt = Puppet::Parser::AST::IfStatement.new :test => @test, :statements => @stmt
      @ifelsestmt = Puppet::Parser::AST::IfStatement.new :test => @test, :statements => @stmt, :else => @else
    end

    it "should evaluate test" do
      Puppet::Parser::Scope.stubs(:true?).returns(false)

      @test.expects(:safeevaluate).with(@scope)

      @ifstmt.evaluate(@scope)
    end

    it "should evaluate if statements if test is true" do
      Puppet::Parser::Scope.stubs(:true?).returns(true)

      @stmt.expects(:safeevaluate).with(@scope)

      @ifstmt.evaluate(@scope)
    end

    it "should not evaluate if statements if test is false" do
      Puppet::Parser::Scope.stubs(:true?).returns(false)

      @stmt.expects(:safeevaluate).with(@scope).never

      @ifstmt.evaluate(@scope)
    end

    it "should evaluate the else branch if test is false" do
      Puppet::Parser::Scope.stubs(:true?).returns(false)

      @else.expects(:safeevaluate).with(@scope)

      @ifelsestmt.evaluate(@scope)
    end

    it "should not evaluate the else branch if test is true" do
      Puppet::Parser::Scope.stubs(:true?).returns(true)

      @else.expects(:safeevaluate).with(@scope).never

      @ifelsestmt.evaluate(@scope)
    end

    it "should reset ephemeral statements after evaluation" do
      @scope.expects(:ephemeral_level).returns(:level)
      Puppet::Parser::Scope.stubs(:true?).returns(true)

      @stmt.expects(:safeevaluate).with(@scope)
      @scope.expects(:unset_ephemeral_var).with(:level)

      @ifstmt.evaluate(@scope)
    end
  end
end