|
1 |
| - |
2 |
| -require 'pact/provider/generators/provider_state'; |
| 1 | +require 'pact/provider/generator/boolean' |
| 2 | +require 'pact/provider/generator/date' |
| 3 | +require 'pact/provider/generator/datetime' |
| 4 | +require 'pact/provider/generator/provider_state' |
| 5 | +require 'pact/provider/generator/random_decimal' |
| 6 | +require 'pact/provider/generator/random_hexadecimal' |
| 7 | +require 'pact/provider/generator/random_int' |
| 8 | +require 'pact/provider/generator/random_string' |
| 9 | +require 'pact/provider/generator/regex' |
| 10 | +require 'pact/provider/generator/time' |
| 11 | +require 'pact/provider/generator/uuid' |
| 12 | +require 'pact/matching_rules/jsonpath' |
| 13 | +require 'pact/matching_rules/v3/extract' |
| 14 | +require 'jsonpath' |
3 | 15 |
|
4 | 16 | module Pact
|
5 |
| - module Provider |
| 17 | + module Provider |
6 | 18 | class Generators
|
7 |
| - def self.add_generator generator |
| 19 | + def self.add_generator(generator) |
8 | 20 | generators.unshift(generator)
|
9 | 21 | end
|
10 | 22 |
|
11 |
| - def self.generators |
| 23 | + def self.generators |
12 | 24 | @generators ||= []
|
13 | 25 | end
|
14 | 26 |
|
15 |
| - def self.execute_generators object, interaction_context = nil |
16 |
| - generators.each do | parser | |
17 |
| - return parser.call(object, interaction_context) if parser.can_generate?(object) |
| 27 | + def self.execute_generators(object, state_params = nil, example_value = nil) |
| 28 | + generators.each do |parser| |
| 29 | + return parser.call(object, state_params, example_value) if parser.can_generate?(object) |
| 30 | + end |
| 31 | + |
| 32 | + raise Pact::UnrecognizePactFormatError, "This document does not use a recognised Pact generator: #{object}" |
| 33 | + end |
| 34 | + |
| 35 | + def self.apply_generators(expected_request, component, example_value, state_params) |
| 36 | + # Latest pact-support is required to have generators exposed |
| 37 | + if expected_request.methods.include?(:generators) && expected_request.generators[component] |
| 38 | + # Some component will have single generator without selectors, i.e. path |
| 39 | + generators = expected_request.generators[component] |
| 40 | + if generators.is_a?(Hash) && generators.key?('type') |
| 41 | + return execute_generators(generators, state_params, example_value) |
| 42 | + end |
| 43 | + |
| 44 | + generators.each do |selector, generator| |
| 45 | + val = JsonPath.new(selector).on(example_value) |
| 46 | + replace = execute_generators(generator, state_params, val) |
| 47 | + example_value = JsonPath.for(example_value).gsub(selector) { |_v| replace }.to_hash |
| 48 | + end |
18 | 49 | end
|
19 |
| - |
20 |
| - raise Pact::UnrecognizePactFormatError.new("This document does not use a recognised Pact generator: #{object}") |
| 50 | + example_value |
21 | 51 | end
|
22 | 52 |
|
23 |
| - add_generator(ProviderStateGenerator.new) |
| 53 | + add_generator(Generator::Boolean.new) |
| 54 | + add_generator(Generator::Date.new) |
| 55 | + add_generator(Generator::DateTime.new) |
| 56 | + add_generator(Generator::ProviderState.new) |
| 57 | + add_generator(Generator::RandomDecimal.new) |
| 58 | + add_generator(Generator::RandomHexadecimal.new) |
| 59 | + add_generator(Generator::RandomInt.new) |
| 60 | + add_generator(Generator::RandomString.new) |
| 61 | + add_generator(Generator::Regex.new) |
| 62 | + add_generator(Generator::Time.new) |
| 63 | + add_generator(Generator::Uuid.new) |
24 | 64 | end
|
25 | 65 | end
|
26 | 66 | end
|
0 commit comments