Skip to content

Commit

Permalink
🐹Adding test for patch_unsafe
Browse files Browse the repository at this point in the history
  • Loading branch information
idubrov committed Aug 25, 2019
1 parent 0263154 commit 9959a96
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
4 changes: 3 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ jobs:
steps:
- checkout
- run: rustup component add clippy
- run: rustup component add rustfmt
- run: cargo build
- run: cargo test --all-targets
- run: cargo clippy
- run: cargo clippy --all --all-targets
- run: cargo fmt -- --check
# - run: cargo tarpaulin --out Xml
# - run:
# name: Uploading code coverage
Expand Down
47 changes: 37 additions & 10 deletions src/tests/util.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::{merge, patch, Patch};
use serde::Deserialize;
use serde_json::Value;
use std::fmt::Write;
Expand All @@ -17,18 +16,19 @@ struct TestCase {
merge: bool,
}

fn run_case(doc: Value, patches: Value, merge_patch: bool) -> Result<Value, String> {
fn run_case(doc: &Value, patches: &Value, merge_patch: bool) -> Result<Value, String> {
let mut actual = doc.clone();
if merge_patch {
merge(&mut actual, &patches);
crate::merge(&mut actual, &patches);
} else {
let patches: Patch = serde_json::from_value(patches).map_err(|e| e.to_string())?;
let patches: crate::Patch =
serde_json::from_value(patches.clone()).map_err(|e| e.to_string())?;

// Patch and verify that in case of error document wasn't changed
patch(&mut actual, &patches)
crate::patch(&mut actual, &patches)
.map_err(|e| {
assert_eq!(
doc, actual,
*doc, actual,
"no changes should be made to the original document"
);
e
Expand All @@ -38,6 +38,14 @@ fn run_case(doc: Value, patches: Value, merge_patch: bool) -> Result<Value, Stri
Ok(actual)
}

fn run_case_patch_unsafe(doc: &Value, patches: &Value) -> Result<Value, String> {
let mut actual = doc.clone();
let patches: crate::Patch =
serde_json::from_value(patches.clone()).map_err(|e| e.to_string())?;
crate::patch_unsafe(&mut actual, &patches).map_err(|e| e.to_string())?;
Ok(actual)
}

pub fn run_specs(path: &str) {
let file = fs::File::open(path).unwrap();
let buf = io::BufReader::new(file);
Expand All @@ -56,20 +64,39 @@ pub fn run_specs(path: &str) {
continue;
}

match run_case(tc.doc, tc.patch, tc.merge) {
match run_case(&tc.doc, &tc.patch, tc.merge) {
Ok(actual) => {
if let Some(ref error) = tc.error {
println!("expected to fail with '{}'", error);
panic!("expected to fail, got document {:?}", actual);
}
println!();
if let Some(expected) = tc.expected {
assert_eq!(expected, actual);
if let Some(ref expected) = tc.expected {
assert_eq!(*expected, actual);
}
}
Err(err) => {
println!("failed with '{}'", err);
tc.error.expect("patch expected to succeed");
tc.error.as_ref().expect("patch expected to succeed");
}
}

if !tc.merge {
match run_case_patch_unsafe(&tc.doc, &tc.patch) {
Ok(actual) => {
if let Some(ref error) = tc.error {
println!("expected to fail with '{}'", error);
panic!("expected to fail, got document {:?}", actual);
}
println!();
if let Some(ref expected) = tc.expected {
assert_eq!(*expected, actual);
}
}
Err(err) => {
println!("failed with '{}'", err);
tc.error.as_ref().expect("patch expected to succeed");
}
}
}
}
Expand Down

0 comments on commit 9959a96

Please sign in to comment.