summaryrefslogtreecommitdiff
path: root/spec/unit/pops/parser/parse_conditionals_spec.rb
blob: 591b20e97aa58d5ea985db7e4d82161d0c7d2442 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#! /usr/bin/env ruby
require 'spec_helper'
require 'puppet/pops'

# relative to this spec file (./) does not work as this file is loaded by rspec
require File.join(File.dirname(__FILE__), '/parser_rspec_helper')

describe "egrammar parsing conditionals" do
  include ParserRspecHelper

  context "When parsing if statements" do
    it "if true { $a = 10 }" do
      dump(parse("if true { $a = 10 }")).should == "(if true\n  (then (= $a 10)))"
    end

    it "if true { $a = 10 } else {$a = 20}" do
      dump(parse("if true { $a = 10 } else {$a = 20}")).should ==
      ["(if true",
        "  (then (= $a 10))",
        "  (else (= $a 20)))"].join("\n")
    end

    it "if true { $a = 10 } elsif false { $a = 15} else {$a = 20}" do
      dump(parse("if true { $a = 10 } elsif false { $a = 15} else {$a = 20}")).should ==
      ["(if true",
        "  (then (= $a 10))",
        "  (else (if false",
        "      (then (= $a 15))",
        "      (else (= $a 20)))))"].join("\n")
    end

    it "if true { $a = 10 $b = 10 } else {$a = 20}" do
      dump(parse("if true { $a = 10 $b = 20} else {$a = 20}")).should == [
       "(if true",
       "  (then (block",
       "      (= $a 10)",
       "      (= $b 20)",
       "    ))",
       "  (else (= $a 20)))"
       ].join("\n")
    end

    it "allows a parenthesized conditional expression" do
      dump(parse("if (true) { 10 }")).should == "(if true\n  (then 10))"
    end

    it "allows a parenthesized elsif conditional expression" do
      dump(parse("if true { 10 } elsif (false) { 20 }")).should ==
        ["(if true",
         "  (then 10)",
         "  (else (if false",
         "      (then 20))))"].join("\n")
    end
  end

  context "When parsing unless statements" do
    it "unless true { $a = 10 }" do
      dump(parse("unless true { $a = 10 }")).should == "(unless true\n  (then (= $a 10)))"
    end

    it "unless true { $a = 10 } else {$a = 20}" do
      dump(parse("unless true { $a = 10 } else {$a = 20}")).should ==
      ["(unless true",
        "  (then (= $a 10))",
        "  (else (= $a 20)))"].join("\n")
    end

    it "allows a parenthesized conditional expression" do
      dump(parse("unless (true) { 10 }")).should == "(unless true\n  (then 10))"
    end

    it "unless true { $a = 10 } elsif false { $a = 15} else {$a = 20} # is illegal" do
      expect { parse("unless true { $a = 10 } elsif false { $a = 15} else {$a = 20}")}.to raise_error(Puppet::ParseError)
    end
  end

  context "When parsing selector expressions" do
    it "$a = $b ? banana => fruit " do
      dump(parse("$a = $b ? banana => fruit")).should ==
      "(= $a (? $b (banana => fruit)))"
    end

    it "$a = $b ? { banana => fruit}" do
      dump(parse("$a = $b ? { banana => fruit }")).should ==
      "(= $a (? $b (banana => fruit)))"
    end

    it "does not fail on a trailing blank line" do
      dump(parse("$a = $b ? { banana => fruit }\n\n")).should ==
      "(= $a (? $b (banana => fruit)))"
    end

    it "$a = $b ? { banana => fruit, grape => berry }" do
      dump(parse("$a = $b ? {banana => fruit, grape => berry}")).should ==
      "(= $a (? $b (banana => fruit) (grape => berry)))"
    end

    it "$a = $b ? { banana => fruit, grape => berry, default => wat }" do
      dump(parse("$a = $b ? {banana => fruit, grape => berry, default => wat}")).should ==
      "(= $a (? $b (banana => fruit) (grape => berry) (:default => wat)))"
    end

    it "$a = $b ? { default => wat, banana => fruit, grape => berry,  }" do
      dump(parse("$a = $b ? {default => wat, banana => fruit, grape => berry}")).should ==
      "(= $a (? $b (:default => wat) (banana => fruit) (grape => berry)))"
    end
  end

  context "When parsing case statements" do
    it "case $a { a : {}}" do
      dump(parse("case $a { a : {}}")).should ==
      ["(case $a",
        "  (when (a) (then ())))"
      ].join("\n")
    end

    it "allows a parenthesized value expression" do
      dump(parse("case ($a) { a : {}}")).should ==
      ["(case $a",
        "  (when (a) (then ())))"
      ].join("\n")
    end

    it "case $a { /.*/ : {}}" do
      dump(parse("case $a { /.*/ : {}}")).should ==
      ["(case $a",
        "  (when (/.*/) (then ())))"
      ].join("\n")
    end

    it "case $a { a, b : {}}" do
      dump(parse("case $a { a, b : {}}")).should ==
      ["(case $a",
        "  (when (a b) (then ())))"
      ].join("\n")
    end

    it "case $a { a, b : {} default : {}}" do
      dump(parse("case $a { a, b : {} default : {}}")).should ==
      ["(case $a",
        "  (when (a b) (then ()))",
        "  (when (:default) (then ())))"
      ].join("\n")
    end

    it "case $a { a : {$b = 10 $c = 20}}" do
      dump(parse("case $a { a : {$b = 10 $c = 20}}")).should ==
      ["(case $a",
       "  (when (a) (then (block",
       "    (= $b 10)",
       "    (= $c 20)",
       "  ))))"
      ].join("\n")
    end
  end

end