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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
#! /usr/bin/env ruby
require 'spec_helper'
require 'puppet/pops'
require 'puppet/pops/evaluator/evaluator_impl'
# relative to this spec file (./) does not work as this file is loaded by rspec
require File.join(File.dirname(__FILE__), '/evaluator_rspec_helper')
# This file contains testing of Conditionals, if, case, unless, selector
#
describe 'Puppet::Pops::Evaluator::EvaluatorImpl' do
include EvaluatorRspecHelper
context "When the evaluator evaluates" do
context "an if expression" do
it 'should output the expected result when dumped' do
dump(IF(literal(true), literal(2), literal(5))).should == unindent(<<-TEXT
(if true
(then 2)
(else 5))
TEXT
)
end
it 'if true {5} == 5' do
evaluate(IF(literal(true), literal(5))).should == 5
end
it 'if false {5} == nil' do
evaluate(IF(literal(false), literal(5))).should == nil
end
it 'if false {2} else {5} == 5' do
evaluate(IF(literal(false), literal(2), literal(5))).should == 5
end
it 'if false {2} elsif true {5} == 5' do
evaluate(IF(literal(false), literal(2), IF(literal(true), literal(5)))).should == 5
end
it 'if false {2} elsif false {5} == nil' do
evaluate(IF(literal(false), literal(2), IF(literal(false), literal(5)))).should == nil
end
end
context "an unless expression" do
it 'should output the expected result when dumped' do
dump(UNLESS(literal(true), literal(2), literal(5))).should == unindent(<<-TEXT
(unless true
(then 2)
(else 5))
TEXT
)
end
it 'unless false {5} == 5' do
evaluate(UNLESS(literal(false), literal(5))).should == 5
end
it 'unless true {5} == nil' do
evaluate(UNLESS(literal(true), literal(5))).should == nil
end
it 'unless true {2} else {5} == 5' do
evaluate(UNLESS(literal(true), literal(2), literal(5))).should == 5
end
it 'unless true {2} elsif true {5} == 5' do
# not supported by concrete syntax
evaluate(UNLESS(literal(true), literal(2), IF(literal(true), literal(5)))).should == 5
end
it 'unless true {2} elsif false {5} == nil' do
# not supported by concrete syntax
evaluate(UNLESS(literal(true), literal(2), IF(literal(false), literal(5)))).should == nil
end
end
context "a case expression" do
it 'should output the expected result when dumped' do
dump(CASE(literal(2),
WHEN(literal(1), literal('wat')),
WHEN([literal(2), literal(3)], literal('w00t'))
)).should == unindent(<<-TEXT
(case 2
(when (1) (then 'wat'))
(when (2 3) (then 'w00t')))
TEXT
)
dump(CASE(literal(2),
WHEN(literal(1), literal('wat')),
WHEN([literal(2), literal(3)], literal('w00t'))
).default(literal(4))).should == unindent(<<-TEXT
(case 2
(when (1) (then 'wat'))
(when (2 3) (then 'w00t'))
(when (:default) (then 4)))
TEXT
)
end
it "case 1 { 1 : { 'w00t'} } == 'w00t'" do
evaluate(CASE(literal(1), WHEN(literal(1), literal('w00t')))).should == 'w00t'
end
it "case 2 { 1,2,3 : { 'w00t'} } == 'w00t'" do
evaluate(CASE(literal(2), WHEN([literal(1), literal(2), literal(3)], literal('w00t')))).should == 'w00t'
end
it "case 2 { 1,3 : {'wat'} 2: { 'w00t'} } == 'w00t'" do
evaluate(CASE(literal(2),
WHEN([literal(1), literal(3)], literal('wat')),
WHEN(literal(2), literal('w00t')))).should == 'w00t'
end
it "case 2 { 1,3 : {'wat'} 5: { 'wat'} default: {'w00t'}} == 'w00t'" do
evaluate(CASE(literal(2),
WHEN([literal(1), literal(3)], literal('wat')),
WHEN(literal(5), literal('wat'))).default(literal('w00t'))
).should == 'w00t'
end
it "case 2 { 1,3 : {'wat'} 5: { 'wat'} } == nil" do
evaluate(CASE(literal(2),
WHEN([literal(1), literal(3)], literal('wat')),
WHEN(literal(5), literal('wat')))
).should == nil
end
it "case 'banana' { 1,3 : {'wat'} /.*ana.*/: { 'w00t'} } == w00t" do
evaluate(CASE(literal('banana'),
WHEN([literal(1), literal(3)], literal('wat')),
WHEN(literal(/.*ana.*/), literal('w00t')))
).should == 'w00t'
end
context "with regular expressions" do
it "should set numeric variables from the match" do
evaluate(CASE(literal('banana'),
WHEN([literal(1), literal(3)], literal('wat')),
WHEN(literal(/.*(ana).*/), var(1)))
).should == 'ana'
end
end
end
context "select expressions" do
it 'should output the expected result when dumped' do
dump(literal(2).select(
MAP(literal(1), literal('wat')),
MAP(literal(2), literal('w00t'))
)).should == "(? 2 (1 => 'wat') (2 => 'w00t'))"
end
it "1 ? {1 => 'w00t'} == 'w00t'" do
evaluate(literal(1).select(MAP(literal(1), literal('w00t')))).should == 'w00t'
end
it "2 ? {1 => 'wat', 2 => 'w00t'} == 'w00t'" do
evaluate(literal(2).select(
MAP(literal(1), literal('wat')),
MAP(literal(2), literal('w00t'))
)).should == 'w00t'
end
it "3 ? {1 => 'wat', 2 => 'wat', default => 'w00t'} == 'w00t'" do
evaluate(literal(3).select(
MAP(literal(1), literal('wat')),
MAP(literal(2), literal('wat')),
MAP(literal(:default), literal('w00t'))
)).should == 'w00t'
end
it "3 ? {1 => 'wat', default => 'w00t', 3 => 'wat'} == 'w00t'" do
evaluate(literal(3).select(
MAP(literal(1), literal('wat')),
MAP(literal(:default), literal('w00t')),
MAP(literal(2), literal('wat'))
)).should == 'w00t'
end
it "should set numerical variables from match" do
evaluate(literal('banana').select(
MAP(literal(1), literal('wat')),
MAP(literal(/.*(ana).*/), var(1))
)).should == 'ana'
end
end
end
end
|