Skip to content

Commit

Permalink
chore(compatibility-suite): cleanup a todo
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed Dec 15, 2023
1 parent ec4cf38 commit 8f39922
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 37 deletions.
43 changes: 9 additions & 34 deletions compatibility-suite/tests/v3_steps/generators.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::collections::HashMap;
use std::fmt::Write;
use std::fs::File;
use std::io::BufReader;

Expand All @@ -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;
Expand Down Expand Up @@ -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))
Expand All @@ -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,
Expand All @@ -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))
Expand Down
7 changes: 4 additions & 3 deletions compatibility-suite/tests/v3_steps/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Mutex<HashMap<String, Message>>> = Arc::new(Mutex::new(hashmap![]));
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 8f39922

Please sign in to comment.