diff --git a/compatibility-suite/tests/v3_steps/generators.rs b/compatibility-suite/tests/v3_steps/generators.rs index 8b6ef2d44..7978cd4fd 100644 --- a/compatibility-suite/tests/v3_steps/generators.rs +++ b/compatibility-suite/tests/v3_steps/generators.rs @@ -1,5 +1,4 @@ use std::collections::HashMap; -use std::fmt::Write; use std::fs::File; use std::io::BufReader; @@ -9,7 +8,7 @@ use cucumber::gherkin::Step; use maplit::hashmap; use pact_models::generators::{Generators, GeneratorTestMode}; use pact_models::json_utils::json_to_string; -use pact_models::path_exp::{DocPath, PathToken}; +use pact_models::path_exp::DocPath; use pact_models::request::Request; use pact_models::response::Response; use regex::Regex; @@ -171,9 +170,11 @@ fn the_body_value_for_will_have_been_replaced_with_a_value( ) -> anyhow::Result<()> { let path = DocPath::new(path).unwrap(); let original_json: Value = serde_json::from_str(world.original_body.value_as_string().unwrap().as_str()).unwrap(); - let original_element = original_json.pointer(as_json_pointer(&path).as_str()).unwrap(); + let pointer = path.as_json_pointer().unwrap(); + let pointer = pointer.as_str(); + let original_element = original_json.pointer(pointer).unwrap(); let json: Value = serde_json::from_str(world.generated_body.value_as_string().unwrap().as_str()).unwrap(); - let element = json.pointer(as_json_pointer(&path).as_str()).unwrap(); + let element = json.pointer(pointer).unwrap(); if element == original_element { return Err(anyhow!("Expected original ({:?}) to have been replaced", original_element)) @@ -182,34 +183,6 @@ fn the_body_value_for_will_have_been_replaced_with_a_value( assert_value_type(value_type, element) } -// TODO: Replace this with version from pact_models -pub fn as_json_pointer(path: &DocPath) -> String { - let mut buffer = String::new(); - - for token in path.tokens() { - match token { - PathToken::Root => {}, - PathToken::Field(v) => { - let parsed = v.replace('~', "~0") - .replace('/', "~1"); - let _ = write!(buffer, "/{}", parsed); - } - PathToken::Index(i) => { - buffer.push('/'); - buffer.push_str(i.to_string().as_str()); - } - PathToken::Star => { - panic!("* can not be converted to a JSON pointer"); - } - PathToken::StarIndex => { - panic!("* can not be converted to a JSON pointer"); - } - } - } - - buffer -} - #[then(expr = "the body value for {string} will have been replaced with {string}")] fn the_body_value_for_will_have_been_replaced_with_value( world: &mut V3World, @@ -218,9 +191,11 @@ fn the_body_value_for_will_have_been_replaced_with_value( ) -> anyhow::Result<()> { let path = DocPath::new(path).unwrap(); let original_json: Value = serde_json::from_str(world.original_body.value_as_string().unwrap().as_str()).unwrap(); - let original_element = original_json.pointer(as_json_pointer(&path).as_str()).unwrap(); + let pointer = path.as_json_pointer().unwrap(); + let pointer = pointer.as_str(); + let original_element = original_json.pointer(pointer).unwrap(); let json: Value = serde_json::from_str(world.generated_body.value_as_string().unwrap().as_str()).unwrap(); - let element = json.pointer(as_json_pointer(&path).as_str()).unwrap(); + let element = json.pointer(pointer).unwrap(); if element == original_element { Err(anyhow!("Expected original ({:?}) to have been replaced", original_element)) diff --git a/compatibility-suite/tests/v3_steps/message.rs b/compatibility-suite/tests/v3_steps/message.rs index 26f29f175..b19b01c62 100644 --- a/compatibility-suite/tests/v3_steps/message.rs +++ b/compatibility-suite/tests/v3_steps/message.rs @@ -32,7 +32,6 @@ use pact_verifier::verification_result::{VerificationExecutionResult, Verificati use crate::shared_steps::{assert_value_type, determine_content_type, element_text, IndexType}; use crate::shared_steps::provider::MockProviderStateExecutor; -use crate::v3_steps::generators::as_json_pointer; lazy_static!{ pub static ref MESSAGES: Arc>> = Arc::new(Mutex::new(hashmap![])); @@ -499,9 +498,11 @@ fn the_message_contents_for_will_have_been_replaced_with_an( let message = message_pact.messages.first().unwrap(); let path = DocPath::new(path).unwrap(); let original_json: Value = serde_json::from_str(message.contents.value_as_string().unwrap().as_str()).unwrap(); - let original_element = original_json.pointer(as_json_pointer(&path).as_str()).unwrap(); + let pointer = path.as_json_pointer().unwrap(); + let pointer = pointer.as_str(); + let original_element = original_json.pointer(pointer).unwrap(); let json: Value = serde_json::from_str(world.received_messages.first().unwrap().contents.value_as_string().unwrap().as_str()).unwrap(); - let element = json.pointer(as_json_pointer(&path).as_str()).unwrap(); + let element = json.pointer(pointer).unwrap(); if element == original_element { return Err(anyhow!("Expected original ({:?}) to have been replaced", original_element))