File: boolean_operator_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 (54 lines) | stat: -rwxr-xr-x 1,989 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
#! /usr/bin/env ruby
require 'spec_helper'

describe Puppet::Parser::AST::BooleanOperator do

  ast = Puppet::Parser::AST

  before :each do
    node     = Puppet::Node.new('localhost')
    compiler = Puppet::Parser::Compiler.new(node)
    @scope   = Puppet::Parser::Scope.new(compiler)
    @true_ast = ast::Boolean.new( :value => true)
    @false_ast = ast::Boolean.new( :value => false)
  end

  it "should evaluate left operand inconditionally" do
    lval = stub "lval"
    lval.expects(:safeevaluate).with(@scope).returns("true")
    rval = stub "rval", :safeevaluate => false
    rval.expects(:safeevaluate).never

    operator = ast::BooleanOperator.new :rval => rval, :operator => "or", :lval => lval
    operator.evaluate(@scope)
  end

  it "should evaluate right 'and' operand only if left operand is true" do
    lval = stub "lval", :safeevaluate => true
    rval = stub "rval", :safeevaluate => false
    rval.expects(:safeevaluate).with(@scope).returns(false)
    operator = ast::BooleanOperator.new :rval => rval, :operator => "and", :lval => lval
    operator.evaluate(@scope)
  end

  it "should evaluate right 'or' operand only if left operand is false" do
    lval = stub "lval", :safeevaluate => false
    rval = stub "rval", :safeevaluate => false
    rval.expects(:safeevaluate).with(@scope).returns(false)
    operator = ast::BooleanOperator.new :rval => rval, :operator => "or", :lval => lval
    operator.evaluate(@scope)
  end

  it "should return true for false OR true" do
    ast::BooleanOperator.new(:rval => @true_ast, :operator => "or", :lval => @false_ast).evaluate(@scope).should be_true
  end

  it "should return false for true AND false" do
    ast::BooleanOperator.new(:rval => @true_ast, :operator => "and", :lval => @false_ast ).evaluate(@scope).should be_false
  end

  it "should return true for true AND true" do
    ast::BooleanOperator.new(:rval => @true_ast, :operator => "and", :lval => @true_ast ).evaluate(@scope).should be_true
  end

end