summaryrefslogtreecommitdiff
path: root/spec/unit/pops/factory_spec.rb
blob: 779a0630d7b2a4c765bf3b739a4f82371dc4a1cd (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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#! /usr/bin/env ruby
require 'spec_helper'
require 'puppet/pops'
require File.join(File.dirname(__FILE__), '/factory_rspec_helper')

# This file contains testing of the pops model factory
#

describe Puppet::Pops::Model::Factory do
  include FactoryRspecHelper

  context "When factory methods are invoked they should produce expected results" do
    it "tests #var should create a VariableExpression" do
      var('a').current.class.should == Puppet::Pops::Model::VariableExpression
    end

    it "tests #fqn should create a QualifiedName" do
      fqn('a').current.class.should == Puppet::Pops::Model::QualifiedName
    end

    it "tests #QNAME should create a QualifiedName" do
      QNAME('a').current.class.should == Puppet::Pops::Model::QualifiedName
    end

    it "tests #QREF should create a QualifiedReference" do
      QREF('a').current.class.should == Puppet::Pops::Model::QualifiedReference
    end

    it "tests #block should create a BlockExpression" do
      block().current.is_a?(Puppet::Pops::Model::BlockExpression).should == true
    end

    it "should create a literal undef on :undef" do
      literal(:undef).current.class.should == Puppet::Pops::Model::LiteralUndef
    end

    it "should create a literal default on :default" do
      literal(:default).current.class.should == Puppet::Pops::Model::LiteralDefault
    end
  end

  context "When calling block_or_expression" do
    it "A single expression should produce identical output" do
      block_or_expression(literal(1) + literal(2)).current.is_a?(Puppet::Pops::Model::ArithmeticExpression).should == true
    end

    it "Multiple expressions should produce a block expression" do
      built = block_or_expression(literal(1) + literal(2), literal(2) + literal(3)).current
      built.is_a?(Puppet::Pops::Model::BlockExpression).should == true
      built.statements.size.should == 2
    end
  end

  context "When processing calls with CALL_NAMED" do
    it "Should be possible to state that r-value is required" do
      built = CALL_NAMED("foo", true, []).current
      built.is_a?(Puppet::Pops::Model::CallNamedFunctionExpression).should == true
      built.rval_required.should == true
    end

    it "Should produce a call expression without arguments" do
      built = CALL_NAMED("foo", false, []).current
      built.is_a?(Puppet::Pops::Model::CallNamedFunctionExpression).should == true
      built.functor_expr.is_a?(Puppet::Pops::Model::QualifiedName).should == true
      built.functor_expr.value.should == "foo"
      built.rval_required.should == false
      built.arguments.size.should == 0
    end

    it "Should produce a call expression with one argument" do
      built = CALL_NAMED("foo", false, [literal(1) + literal(2)]).current
      built.is_a?(Puppet::Pops::Model::CallNamedFunctionExpression).should == true
      built.functor_expr.is_a?(Puppet::Pops::Model::QualifiedName).should == true
      built.functor_expr.value.should == "foo"
      built.rval_required.should == false
      built.arguments.size.should == 1
      built.arguments[0].is_a?(Puppet::Pops::Model::ArithmeticExpression).should == true
    end

    it "Should produce a call expression with two arguments" do
      built = CALL_NAMED("foo", false, [literal(1) + literal(2), literal(1) + literal(2)]).current
      built.is_a?(Puppet::Pops::Model::CallNamedFunctionExpression).should == true
      built.functor_expr.is_a?(Puppet::Pops::Model::QualifiedName).should == true
      built.functor_expr.value.should == "foo"
      built.rval_required.should == false
      built.arguments.size.should == 2
      built.arguments[0].is_a?(Puppet::Pops::Model::ArithmeticExpression).should == true
      built.arguments[1].is_a?(Puppet::Pops::Model::ArithmeticExpression).should == true
    end
  end

  context "When creating attribute operations" do
    it "Should produce an attribute operation for =>" do
      built = ATTRIBUTE_OP("aname", :'=>', 'x').current
      built.is_a?(Puppet::Pops::Model::AttributeOperation)
      built.operator.should == :'=>'
      built.attribute_name.should == "aname"
      built.value_expr.is_a?(Puppet::Pops::Model::LiteralString).should == true
    end

    it "Should produce an attribute operation for +>" do
      built = ATTRIBUTE_OP("aname", :'+>', 'x').current
      built.is_a?(Puppet::Pops::Model::AttributeOperation)
      built.operator.should == :'+>'
      built.attribute_name.should == "aname"
      built.value_expr.is_a?(Puppet::Pops::Model::LiteralString).should == true
    end
  end

  context "When processing RESOURCE" do
    it "Should create a Resource body" do
      built = RESOURCE_BODY("title", [ATTRIBUTE_OP('aname', :'=>', 'x')]).current
      built.is_a?(Puppet::Pops::Model::ResourceBody).should == true
      built.title.is_a?(Puppet::Pops::Model::LiteralString).should == true
      built.operations.size.should == 1
      built.operations[0].class.should == Puppet::Pops::Model::AttributeOperation
      built.operations[0].attribute_name.should == 'aname'
    end

    it "Should create a RESOURCE without a resource body" do
      bodies = []
      built = RESOURCE("rtype", bodies).current
      built.class.should == Puppet::Pops::Model::ResourceExpression
      built.bodies.size.should == 0
    end

    it "Should create a RESOURCE with 1 resource body" do
      bodies = [] << RESOURCE_BODY('title', [])
      built = RESOURCE("rtype", bodies).current
      built.class.should == Puppet::Pops::Model::ResourceExpression
      built.bodies.size.should == 1
      built.bodies[0].title.value.should == 'title'
    end

    it "Should create a RESOURCE with 2 resource bodies" do
      bodies = [] << RESOURCE_BODY('title', []) << RESOURCE_BODY('title2', [])
      built = RESOURCE("rtype", bodies).current
      built.class.should == Puppet::Pops::Model::ResourceExpression
      built.bodies.size.should == 2
      built.bodies[0].title.value.should == 'title'
      built.bodies[1].title.value.should == 'title2'
    end
  end

  context "When processing simple literals" do
    it "Should produce a literal boolean from a boolean" do
      built = literal(true).current
      built.class.should == Puppet::Pops::Model::LiteralBoolean
      built.value.should == true
      built = literal(false).current
      built.class.should == Puppet::Pops::Model::LiteralBoolean
      built.value.should == false
    end
  end

  context "When processing COLLECT" do
    it "should produce a virtual query" do
      built = VIRTUAL_QUERY(fqn('a') == literal(1)).current
      built.class.should == Puppet::Pops::Model::VirtualQuery
      built.expr.class.should == Puppet::Pops::Model::ComparisonExpression
      built.expr.operator.should ==  :'=='
    end

    it "should produce an export query" do
      built = EXPORTED_QUERY(fqn('a') == literal(1)).current
      built.class.should == Puppet::Pops::Model::ExportedQuery
      built.expr.class.should == Puppet::Pops::Model::ComparisonExpression
      built.expr.operator.should ==  :'=='
    end

    it "should produce a collect expression" do
      q = VIRTUAL_QUERY(fqn('a') == literal(1))
      built = COLLECT(literal('t'), q, [ATTRIBUTE_OP('name', :'=>', 3)]).current
      built.class.should == Puppet::Pops::Model::CollectExpression
      built.operations.size.should == 1
    end

    it "should produce a collect expression without attribute operations" do
      q = VIRTUAL_QUERY(fqn('a') == literal(1))
      built = COLLECT(literal('t'), q, []).current
      built.class.should == Puppet::Pops::Model::CollectExpression
      built.operations.size.should == 0
    end
  end

  context "When processing concatenated string(iterpolation)" do
    it "should handle 'just a string'" do
      built = string('blah blah').current
      built.class.should == Puppet::Pops::Model::ConcatenatedString
      built.segments.size == 1
      built.segments[0].class.should == Puppet::Pops::Model::LiteralString
      built.segments[0].value.should == "blah blah"
    end

    it "should handle one expression in the middle" do
      built = string('blah blah', TEXT(literal(1)+literal(2)), 'blah blah').current
      built.class.should == Puppet::Pops::Model::ConcatenatedString
      built.segments.size == 3
      built.segments[0].class.should == Puppet::Pops::Model::LiteralString
      built.segments[0].value.should == "blah blah"
      built.segments[1].class.should == Puppet::Pops::Model::TextExpression
      built.segments[1].expr.class.should == Puppet::Pops::Model::ArithmeticExpression
      built.segments[2].class.should == Puppet::Pops::Model::LiteralString
      built.segments[2].value.should == "blah blah"
    end

    it "should handle one expression at the end" do
      built = string('blah blah', TEXT(literal(1)+literal(2))).current
      built.class.should == Puppet::Pops::Model::ConcatenatedString
      built.segments.size == 2
      built.segments[0].class.should == Puppet::Pops::Model::LiteralString
      built.segments[0].value.should == "blah blah"
      built.segments[1].class.should == Puppet::Pops::Model::TextExpression
      built.segments[1].expr.class.should == Puppet::Pops::Model::ArithmeticExpression
    end

    it "should handle only one expression" do
      built = string(TEXT(literal(1)+literal(2))).current
      built.class.should == Puppet::Pops::Model::ConcatenatedString
      built.segments.size == 1
      built.segments[0].class.should == Puppet::Pops::Model::TextExpression
      built.segments[0].expr.class.should == Puppet::Pops::Model::ArithmeticExpression
    end

    it "should handle several expressions" do
      built = string(TEXT(literal(1)+literal(2)), TEXT(literal(1)+literal(2))).current
      built.class.should == Puppet::Pops::Model::ConcatenatedString
      built.segments.size == 2
      built.segments[0].class.should == Puppet::Pops::Model::TextExpression
      built.segments[0].expr.class.should == Puppet::Pops::Model::ArithmeticExpression
      built.segments[1].class.should == Puppet::Pops::Model::TextExpression
      built.segments[1].expr.class.should == Puppet::Pops::Model::ArithmeticExpression
    end

    it "should handle no expression" do
      built = string().current
      built.class.should == Puppet::Pops::Model::ConcatenatedString
      built.segments.size == 0
    end
  end

  context "When processing UNLESS" do
    it "should create an UNLESS expression with then part" do
      built = UNLESS(true, literal(1), nil).current
      built.class.should == Puppet::Pops::Model::UnlessExpression
      built.test.class.should == Puppet::Pops::Model::LiteralBoolean
      built.then_expr.class.should == Puppet::Pops::Model::LiteralInteger
      built.else_expr.class.should == Puppet::Pops::Model::Nop
    end

    it "should create an UNLESS expression with then and else parts" do
      built = UNLESS(true, literal(1), literal(2)).current
      built.class.should == Puppet::Pops::Model::UnlessExpression
      built.test.class.should == Puppet::Pops::Model::LiteralBoolean
      built.then_expr.class.should == Puppet::Pops::Model::LiteralInteger
      built.else_expr.class.should == Puppet::Pops::Model::LiteralInteger
    end
  end

  context "When processing IF" do
    it "should create an IF expression with then part" do
      built = IF(true, literal(1), nil).current
      built.class.should == Puppet::Pops::Model::IfExpression
      built.test.class.should == Puppet::Pops::Model::LiteralBoolean
      built.then_expr.class.should == Puppet::Pops::Model::LiteralInteger
      built.else_expr.class.should == Puppet::Pops::Model::Nop
    end

    it "should create an IF expression with then and else parts" do
      built = IF(true, literal(1), literal(2)).current
      built.class.should == Puppet::Pops::Model::IfExpression
      built.test.class.should == Puppet::Pops::Model::LiteralBoolean
      built.then_expr.class.should == Puppet::Pops::Model::LiteralInteger
      built.else_expr.class.should == Puppet::Pops::Model::LiteralInteger
    end
  end

  context "When processing a Parameter" do
    it "should create a Parameter" do
      # PARAM(name, expr)
      # PARAM(name)
      #
    end
  end

  # LIST, HASH, KEY_ENTRY
  context "When processing Definition" do
    # DEFINITION(classname, arguments, statements)
    # should accept empty arguments, and no statements
  end

  context "When processing Hostclass" do
    # HOSTCLASS(classname, arguments, parent, statements)
    # parent may be passed as a nop /nil - check this works, should accept empty statements (nil)
    # should accept empty arguments

  end

  context "When processing Node" do
  end

  # Tested in the evaluator test already, but should be here to test factory assumptions
  #
  # TODO: CASE / WHEN
  # TODO: MAP
end