summaryrefslogtreecommitdiff
path: root/spec/unit/mixin/params_validate_spec.rb
blob: 489742cb7d0bd03817d41e9a9e6c4966b0b37519 (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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#
# Author:: Adam Jacob (<adam@chef.io>)
# Copyright:: Copyright 2008-2018, Chef Software Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require "spec_helper"

class TinyClass
  include Chef::Mixin::ParamsValidate

  attr_reader :name

  def music(is_good = true)
    is_good
  end
end

describe Chef::Mixin::ParamsValidate do
  before(:each) do
    @vo = TinyClass.new
  end

  it "should allow a hash and a hash as arguments to validate" do
    expect { @vo.validate({ one: "two" }, {}) }.not_to raise_error
  end

  it "should raise an argument error if validate is called incorrectly" do
    expect { @vo.validate("one", "two") }.to raise_error(ArgumentError)
  end

  it "should require validation map keys to be symbols or strings" do
    expect { @vo.validate({ one: "two" }, { one: true }) }.not_to raise_error
    expect { @vo.validate({ one: "two" }, { "one" => true }) }.not_to raise_error
    expect { @vo.validate({ one: "two" }, { {} => true }) }.to raise_error(ArgumentError)
  end

  it "should allow options to be required with true" do
    expect { @vo.validate({ one: "two" }, { one: true }) }.not_to raise_error
  end

  it "should allow options to be optional with false" do
    expect { @vo.validate({}, { one: false }) }.not_to raise_error
  end

  it "should allow you to check what kind_of? thing an argument is with kind_of" do
    expect do
      @vo.validate(
        { one: "string" },
        {
          one: {
            kind_of: String,
          },
        }
      )
    end.not_to raise_error

    expect do
      @vo.validate(
        { one: "string" },
        {
          one: {
            kind_of: Array,
          },
        }
      )
    end.to raise_error(ArgumentError)
  end

  it "should allow you to specify an argument is required with required" do
    expect do
      @vo.validate(
        { one: "string" },
        {
          one: {
            required: true,
          },
        }
      )
    end.not_to raise_error

    expect do
      @vo.validate(
        { two: "string" },
        {
          one: {
            required: true,
          },
        }
      )
    end.to raise_error(ArgumentError)

    expect do
      @vo.validate(
        { two: "string" },
        {
          one: {
            required: false,
          },
        }
      )
    end.not_to raise_error
  end

  it "should allow you to specify whether an object has a method with respond_to" do
    expect do
      @vo.validate(
        { one: @vo },
        {
          one: {
            respond_to: "validate",
          },
        }
      )
    end.not_to raise_error

    expect do
      @vo.validate(
        { one: @vo },
        {
          one: {
            respond_to: "monkey",
          },
        }
      )
    end.to raise_error(ArgumentError)
  end

  it "should allow you to specify whether an object has all the given methods with respond_to and an array" do
    expect do
      @vo.validate(
        { one: @vo },
        {
          one: {
            respond_to: %w{validate music},
          },
        }
      )
    end.not_to raise_error

    expect do
      @vo.validate(
        { one: @vo },
        {
          one: {
            respond_to: %w{monkey validate},
          },
        }
      )
    end.to raise_error(ArgumentError)
  end

  it "should let you set a default value with default => value" do
    arguments = {}
    @vo.validate(arguments, {
      one: {
        default: "is the loneliest number",
      },
    })
    expect(arguments[:one]).to eq("is the loneliest number")
  end

  it "should let you check regular expressions" do
    expect do
      @vo.validate(
        { one: "is good" },
        {
          one: {
            regex: /^is good$/,
          },
        }
      )
    end.not_to raise_error

    expect do
      @vo.validate(
        { one: "is good" },
        {
          one: {
            regex: /^is bad$/,
          },
        }
      )
    end.to raise_error(ArgumentError)
  end

  it "should let you specify your own callbacks" do
    expect do
      @vo.validate(
        { one: "is good" },
        {
          one: {
            callbacks: {
              "should be equal to is good" => lambda do |a|
                a == "is good"
              end,
            },
          },
        }
      )
    end.not_to raise_error

    expect do
      @vo.validate(
        { one: "is bad" },
        {
          one: {
            callbacks: {
              "should be equal to 'is good'" => lambda do |a|
                a == "is good"
              end,
            },
          },
        }
      )
    end.to raise_error(ArgumentError)
  end

  it "should let you combine checks" do
    args = { one: "is good", two: "is bad" }
    expect do
      @vo.validate(
        args,
        {
          one: {
            kind_of: String,
            respond_to: %i{to_s upcase},
            regex: /^is good/,
            callbacks: {
              "should be your friend" => lambda do |a|
                a == "is good"
              end,
            },
            required: true,
          },
          two: {
            kind_of: String,
            required: false,
          },
          three: { default: "neato mosquito" },
        }
      )
    end.not_to raise_error
    expect(args[:three]).to eq("neato mosquito")
    expect do
      @vo.validate(
        args,
        {
          one: {
            kind_of: String,
            respond_to: %i{to_s upcase},
            regex: /^is good/,
            callbacks: {
              "should be your friend" => lambda do |a|
                a == "is good"
              end,
            },
            required: true,
          },
          two: {
            kind_of: Hash,
            required: false,
          },
          three: { default: "neato mosquito" },
        }
      )
    end.to raise_error(ArgumentError)
  end

  it "should raise an ArgumentError if the validation map has an unknown check" do
    expect do
      @vo.validate(
        { one: "two" },
        {
          one: {
            busted: "check",
          },
        }
      )
    end.to raise_error(ArgumentError)
  end

  it "should accept keys that are strings in the options" do
    expect do
      @vo.validate({ "one" => "two" }, { one: { regex: /^two$/ } })
    end.not_to raise_error
  end

  it "should allow an array to kind_of" do
    expect do
      @vo.validate(
        { one: "string" },
        {
          one: {
            kind_of: [ String, Array ],
          },
        }
      )
    end.not_to raise_error
    expect do
      @vo.validate(
        { one: ["string"] },
        {
          one: {
            kind_of: [ String, Array ],
          },
        }
      )
    end.not_to raise_error
    expect do
      @vo.validate(
        { one: {} },
        {
          one: {
            kind_of: [ String, Array ],
          },
        }
      )
    end.to raise_error(ArgumentError)
  end

  it "asserts that a value returns false from a predicate method" do
    expect do
      @vo.validate({ not_blank: "should pass" },
        { not_blank: { cannot_be: %i{nil empty} } })
    end.not_to raise_error
    expect do
      @vo.validate({ not_blank: "" },
        { not_blank: { cannot_be: %i{nil empty} } })
    end.to raise_error(Chef::Exceptions::ValidationFailed)
  end

  it "allows a custom validation message" do
    expect do
      @vo.validate({ not_blank: "should pass" },
        { not_blank: { cannot_be: %i{nil empty}, validation_message: "my validation message" } })
    end.not_to raise_error
    expect do
      @vo.validate({ not_blank: "" },
        { not_blank: { cannot_be: %i{nil empty}, validation_message: "my validation message" } })
    end.to raise_error(Chef::Exceptions::ValidationFailed, "my validation message")
  end

  it "should set and return a value, then return the same value" do
    value = "meow"
    expect(@vo.set_or_return(:test, value, {}).object_id).to eq(value.object_id)
    expect(@vo.set_or_return(:test, nil, {}).object_id).to eq(value.object_id)
  end

  it "should set and return a default value when the argument is nil, then return the same value" do
    value = "meow"
    expect(@vo.set_or_return(:test, nil, { default: value }).object_id).to eq(value.object_id)
    expect(@vo.set_or_return(:test, nil, {}).object_id).to eq(value.object_id)
  end

  it "should raise an ArgumentError when argument is nil and required is true" do
    expect do
      @vo.set_or_return(:test, nil, { required: true })
    end.to raise_error(ArgumentError)
  end

  it "should not raise an error when argument is nil and required is false" do
    expect do
      @vo.set_or_return(:test, nil, { required: false })
    end.not_to raise_error
  end

  it "should set and return @name, then return @name for foo when argument is nil" do
    value = "meow"
    expect(@vo.set_or_return(:name, value, {}).object_id).to eq(value.object_id)
    expect(@vo.set_or_return(:foo, nil, { name_attribute: true }).object_id).to eq(value.object_id)
  end

  it "should allow DelayedEvaluator instance to be set for value regardless of restriction" do
    value = Chef::DelayedEvaluator.new { "test" }
    @vo.set_or_return(:test, value, { kind_of: Numeric })
  end

  it "should raise an error when delayed evaluated attribute is not valid" do
    value = Chef::DelayedEvaluator.new { "test" }
    @vo.set_or_return(:test, value, { kind_of: Numeric })
    expect do
      @vo.set_or_return(:test, nil, { kind_of: Numeric })
    end.to raise_error(Chef::Exceptions::ValidationFailed)
  end

  it "should create DelayedEvaluator instance when #lazy is used" do
    @vo.set_or_return(:delayed, @vo.lazy { "test" }, {})
    expect(@vo.instance_variable_get(:@delayed)).to be_a(Chef::DelayedEvaluator)
  end

  it "should execute block on each call when DelayedEvaluator" do
    value = "fubar"
    @vo.set_or_return(:test, @vo.lazy { value }, {})
    expect(@vo.set_or_return(:test, nil, {})).to eq("fubar")
    value = "foobar"
    expect(@vo.set_or_return(:test, nil, {})).to eq("foobar")
    value = "fauxbar"
    expect(@vo.set_or_return(:test, nil, {})).to eq("fauxbar")
  end

  it "should not evaluate non DelayedEvaluator instances" do
    value = lambda { "test" }
    @vo.set_or_return(:test, value, {})
    expect(@vo.set_or_return(:test, nil, {}).object_id).to eq(value.object_id)
    expect(@vo.set_or_return(:test, nil, {})).to be_a(Proc)
  end

end