summaryrefslogtreecommitdiffstats
path: root/test/language/ast/casestatement.rb
blob: 2711455f2cd8b8d70ceb34c304629c05fa640e02 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env ruby
#
#  Created by Luke A. Kanies on 2006-12-22.
#  Copyright (c) 2006. All rights reserved.

require File.dirname(__FILE__) + '/../../lib/puppettest'

require 'puppettest'
require 'puppettest/parsertesting'

class TestCaseStatement < Test::Unit::TestCase
    include PuppetTest
    include PuppetTest::ParserTesting
    AST = Puppet::Parser::AST

    class ActiveAST < FakeAST
        def self.clear
            $evaluated = []
        end
        def evaluate
            $evaluated ||= []
            $evaluated << @evaluate
        end
    end

    def test_evaluate
        ast = nil
        scope = mkscope
        param = nameobj("MyParam")

        hash = {
            "myparam" => ActiveAST.new("lower"),
            "MyParam" => ActiveAST.new("upper"),
            true => ActiveAST.new(true)
        }
        options = ["myparam", "MyParam"].collect do |p|
            AST::CaseOpt.new(:value => FakeAST.new(p), :statements => hash[p])
        end
        assert_nothing_raised do
            ast = AST::CaseStatement.new(:test => param, :options => options)
        end

        # Start out case-sensitive
        Puppet[:casesensitive] = true

        result = nil
        assert_nothing_raised do
            result = ast.evaluate scope
        end
        assert(result, "did not get valid result")
        assert_equal(["upper"], $evaluated, "Did not match case-sensitively")
        assert(! hash["myparam"].evaluated?, "lower value was evaluated even though it did not match")

        # Now try it case-insensitive
        Puppet[:casesensitive] = false
        $evaluated.clear
        hash["MyParam"].reset
        assert_nothing_raised do
            result = ast.evaluate scope
        end
        assert(result, "did not get valid result")
        assert_equal(["lower"], result, "Did not match case-insensitively")
        assert(! hash["MyParam"].evaluated?, "upper value was evaluated even though it did not match")
    end

    # #522 - test that case statements with multiple values work as
    # expected, where any true value suffices.
    def test_multiple_values
        ast = nil

        tests = {
            "one" => %w{a b c},
            "two" => %w{e f g}
        }
        options = tests.collect do |result, values|
            values = values.collect { |v| AST::Leaf.new :value => v }
            AST::CaseOpt.new(:value => AST::ASTArray.new(:children => values),
                :statements => AST::Leaf.new(:value => result))
        end
        options << AST::CaseOpt.new(:value => AST::Default.new(:value => "default"),
            :statements => AST::Leaf.new(:value => "default"))

        ast = nil
        param = AST::Variable.new(:value => "testparam")
        assert_nothing_raised do
            ast = AST::CaseStatement.new(:test => param, :options => options)
        end
        result = nil
        tests.each do |should, values|
            values.each do |value|
                result = nil
                scope = mkscope
                scope.setvar("testparam", value)
                assert_nothing_raised do
                    result = ast.evaluate(scope)
                end

                assert_equal(should, result, "Got incorrect result for %s" % value)
            end
        end
    end
end