-
Notifications
You must be signed in to change notification settings - Fork 216
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(generators): Pass context and add ProviderState generator
- Loading branch information
Showing
5 changed files
with
108 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
|
||
require 'pact/provider/generators/provider_state'; | ||
|
||
module Pact | ||
module Provider | ||
class Generators | ||
def self.add_generator generator | ||
generators.unshift(generator) | ||
end | ||
|
||
def self.generators | ||
@generators ||= [] | ||
end | ||
|
||
def self.execute_generators object, interaction_context = nil | ||
generators.each do | parser | | ||
return parser.call(object, interaction_context) if parser.can_generate?(object) | ||
end | ||
|
||
raise Pact::UnrecognizePactFormatError.new("This document does not use a recognised Pact generator: #{object}") | ||
end | ||
|
||
add_generator(ProviderStateGenerator.new) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
require 'pact/provider/generators' | ||
|
||
module Pact | ||
module Provider | ||
class ProviderStateGenerator | ||
|
||
|
||
# rewrite of https://github.com/DiUS/pact-jvm/blob/master/core/support/src/main/kotlin/au/com/dius/pact/core/support/expressions/ExpressionParser.kt#L27 | ||
VALUES_SEPARATOR = "," | ||
START_EXPRESSION = "\${" | ||
END_EXPRESSION = '}' | ||
def parse_expression expression, params | ||
|
||
return_string = [] | ||
|
||
buffer = expression; | ||
# initial value | ||
position = buffer.index(START_EXPRESSION) | ||
|
||
while (position && position >= 0) | ||
if (position > 0) | ||
# add string | ||
return_string.push(buffer[0...position]) | ||
end | ||
end_position = buffer.index(END_EXPRESSION, position) | ||
if (end_position < 0) | ||
raise "Missing closing brace in expression string \"#{$value}\"" | ||
end | ||
|
||
variable = "" | ||
|
||
if (end_position - position > 2) | ||
expression = params[buffer[position+2...end_position]] || "" | ||
end | ||
return_string.push(expression) | ||
|
||
buffer = buffer[end_position + 1...-1] | ||
position = buffer.index(START_EXPRESSION) | ||
end | ||
|
||
return_string.join("") | ||
end | ||
|
||
def call hash, interaction_context = nil | ||
params = interaction_context.state_params || {} | ||
|
||
parse_expression hash["expression"], params | ||
end | ||
|
||
def can_generate?(hash) | ||
hash.key?('type') && hash['type'] === 'ProviderState' | ||
end | ||
end | ||
end | ||
end | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters