Class: Sbmt::Pact::Matchers::V3::Each

Inherits:
Base
  • Object
show all
Defined in:
lib/sbmt/pact/matchers/v3/each.rb

Instance Attribute Summary

Attributes inherited from Base

#kind, #opts, #spec_version, #template

Instance Method Summary collapse

Methods inherited from Base

#as_basic

Constructor Details

#initialize(template, min) ⇒ Each

Returns a new instance of Each.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/sbmt/pact/matchers/v3/each.rb', line 8

def initialize(template, min)
  raise MatcherInitializationError, "#{self.class}: #{min} should be greater than 0" if min.present? && min < 1

  min_array_size = min.presence || 1
  val = template.is_a?(Array) ? template : [template] * min_array_size

  raise MatcherInitializationError, "#{self.class}: #{min} is invalid: template size is #{val.size}" if min_array_size != val.size

  super(
    spec_version: Sbmt::Pact::Matchers::PACT_SPEC_V3,
    kind: "type",
    template: val,
    opts: {min: min_array_size})
end

Instance Method Details

#as_pluginObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/sbmt/pact/matchers/v3/each.rb', line 23

def as_plugin
  if @template.first.is_a?(Hash)
    return {
      "pact:match" => "eachValue(matching($'SAMPLE'))",
      "SAMPLE" => serialize!(@template.first.deep_dup, :plugin)
    }
  end

  params = @opts.except(:min).values.map { |v| format_primitive(v) }.join(",")
  value = format_primitive(@template.first)

  return "eachValue(matching(#{@kind}, #{params}, #{value}))" if params.present?

  "eachValue(matching(#{@kind}, #{value}))"
end