summaryrefslogtreecommitdiffstats
path: root/spec/unit/parser/ast/astarray_spec.rb
blob: 1791c711c74fd5919e6394e3abfc60cb796dcfa1 (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
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../../../spec_helper'

describe Puppet::Parser::AST::ASTArray do
    before :each do
        @scope = Puppet::Parser::Scope.new()
    end

    it "should have a [] accessor" do
        array = Puppet::Parser::AST::ASTArray.new :children => []
        array.should respond_to(:[])
    end

    it "should evaluate all its children" do
        item1 = stub "item1", :is_a? => true
        item2 = stub "item2", :is_a? => true

        item1.expects(:safeevaluate).with(@scope).returns(123)
        item2.expects(:safeevaluate).with(@scope).returns(246)

        operator = Puppet::Parser::AST::ASTArray.new :children => [item1,item2]
        operator.evaluate(@scope)
    end

    it "should evaluate childrens of type ASTArray" do
        item1 = stub "item1", :is_a? => true
        item2 = stub "item2"
        item2.stubs(:is_a?).with(Puppet::Parser::AST).returns(true)
        item2.stubs(:instance_of?).with(Puppet::Parser::AST::ASTArray).returns(true)
        item2.stubs(:each).yields(item1)

        item1.expects(:safeevaluate).with(@scope).returns(123)

        operator = Puppet::Parser::AST::ASTArray.new :children => [item2]
        operator.evaluate(@scope).should == [123]
    end

    it "should flatten children coming from children ASTArray" do
        item1 = stub "item1", :is_a? => true
        item2 = stub "item2"
        item2.stubs(:is_a?).with(Puppet::Parser::AST).returns(true)
        item2.stubs(:instance_of?).with(Puppet::Parser::AST::ASTArray).returns(true)
        item2.stubs(:each).yields([item1])

        item1.expects(:safeevaluate).with(@scope).returns(123)

        operator = Puppet::Parser::AST::ASTArray.new :children => [item2]
        operator.evaluate(@scope).should == [123]
    end

    it "should not flatten the results of children evaluation" do
        item1 = stub "item1", :is_a? => true
        item2 = stub "item2"
        item2.stubs(:is_a?).with(Puppet::Parser::AST).returns(true)
        item2.stubs(:instance_of?).with(Puppet::Parser::AST::ASTArray).returns(true)
        item2.stubs(:each).yields([item1])

        item1.expects(:safeevaluate).with(@scope).returns([123])

        operator = Puppet::Parser::AST::ASTArray.new :children => [item2]
        operator.evaluate(@scope).should == [[123]]
    end

    it "should return a valid string with to_s" do
        a = stub 'a', :is_a? => true, :to_s => "a"
        b = stub 'b', :is_a? => true, :to_s => "b"
        array = Puppet::Parser::AST::ASTArray.new :children => [a,b]

        array.to_s.should == "[a, b]"
    end
end