From e60be94be04646fa64c3b3ea5576f15272a4bb24 Mon Sep 17 00:00:00 2001 From: Lucien Greathouse Date: Mon, 7 Oct 2019 16:21:25 -0700 Subject: [PATCH] Move patch apply test utility into tree_view module --- Cargo.lock | 1 + Cargo.toml | 1 + src/lib.rs | 3 ++ src/snapshot/tests/apply.rs | 69 ++++++++----------------------------- src/tree_view.rs | 56 ++++++++++++++++++++++++++++++ 5 files changed, 76 insertions(+), 54 deletions(-) create mode 100644 src/tree_view.rs diff --git a/Cargo.lock b/Cargo.lock index 56c7d7f6..2a03d2dc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1470,6 +1470,7 @@ dependencies = [ "rojo-insta-ext 0.1.0", "serde 1.0.101 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.41 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_yaml 0.8.9 (registry+https://github.com/rust-lang/crates.io-index)", "tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "termcolor 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", "uuid 0.7.4 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index 81bdd960..5f4a77be 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -72,6 +72,7 @@ rojo-insta-ext = { path = "rojo-insta-ext" } lazy_static = "1.2" paste = "0.1" pretty_assertions = "0.6.1" +serde_yaml = "0.8.9" tempfile = "3.0" walkdir = "2.1" diff --git a/src/lib.rs b/src/lib.rs index a8999ccc..0ae30678 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,6 +12,9 @@ pub mod commands; #[doc(hidden)] pub mod project; +#[cfg(test)] +mod tree_view; + mod auth_cookie; mod change_processor; mod imfs; diff --git a/src/snapshot/tests/apply.rs b/src/snapshot/tests/apply.rs index d36d6538..d9bb0753 100644 --- a/src/snapshot/tests/apply.rs +++ b/src/snapshot/tests/apply.rs @@ -1,14 +1,12 @@ -use std::collections::HashMap; - use insta::assert_yaml_snapshot; use maplit::hashmap; -use rbx_dom_weak::{RbxId, RbxInstanceProperties, RbxValue}; -use serde::Serialize; +use rbx_dom_weak::{RbxInstanceProperties, RbxValue}; use rojo_insta_ext::RedactionMap; -use crate::snapshot::{ - apply_patch_set, InstanceMetadata, InstancePropertiesWithMeta, PatchSet, PatchUpdate, RojoTree, +use crate::{ + snapshot::{apply_patch_set, InstancePropertiesWithMeta, PatchSet, PatchUpdate, RojoTree}, + tree_view::{intern_tree, view_tree}, }; #[test] @@ -16,7 +14,7 @@ fn set_name_and_class_name() { let mut redactions = RedactionMap::new(); let mut tree = empty_tree(); - redactions.intern(tree.get_root_id()); + intern_tree(&tree, &mut redactions); let patch_set = PatchSet { updated_instances: vec![PatchUpdate { @@ -31,9 +29,8 @@ fn set_name_and_class_name() { let applied_patch_set = apply_patch_set(&mut tree, patch_set); - let tree_view = view_tree(&tree); - let tree_value = redactions.redacted_yaml(tree_view); - assert_yaml_snapshot!(tree_value); + let tree_view = view_tree(&tree, &mut redactions); + assert_yaml_snapshot!(tree_view); let applied_patch_value = redactions.redacted_yaml(applied_patch_set); assert_yaml_snapshot!(applied_patch_value); @@ -44,7 +41,7 @@ fn add_property() { let mut redactions = RedactionMap::new(); let mut tree = empty_tree(); - redactions.intern(tree.get_root_id()); + intern_tree(&tree, &mut redactions); let patch_set = PatchSet { updated_instances: vec![PatchUpdate { @@ -63,9 +60,8 @@ fn add_property() { let applied_patch_set = apply_patch_set(&mut tree, patch_set); - let tree_view = view_tree(&tree); - let tree_value = redactions.redacted_yaml(tree_view); - assert_yaml_snapshot!(tree_value); + let tree_view = view_tree(&tree, &mut redactions); + assert_yaml_snapshot!(tree_view); let applied_patch_value = redactions.redacted_yaml(applied_patch_set); assert_yaml_snapshot!(applied_patch_value); @@ -76,7 +72,7 @@ fn remove_property() { let mut redactions = RedactionMap::new(); let mut tree = empty_tree(); - redactions.intern(tree.get_root_id()); + intern_tree(&tree, &mut redactions); { let root_id = tree.get_root_id(); @@ -90,9 +86,8 @@ fn remove_property() { ); } - let tree_view = view_tree(&tree); - let tree_value = redactions.redacted_yaml(tree_view); - assert_yaml_snapshot!("remove_property_initial", tree_value); + let tree_view = view_tree(&tree, &mut redactions); + assert_yaml_snapshot!("remove_property_initial", tree_view); let patch_set = PatchSet { updated_instances: vec![PatchUpdate { @@ -109,9 +104,8 @@ fn remove_property() { let applied_patch_set = apply_patch_set(&mut tree, patch_set); - let tree_view = view_tree(&tree); - let tree_value = redactions.redacted_yaml(tree_view); - assert_yaml_snapshot!("remove_property_after_patch", tree_value); + let tree_view = view_tree(&tree, &mut redactions); + assert_yaml_snapshot!("remove_property_after_patch", tree_view); let applied_patch_value = redactions.redacted_yaml(applied_patch_set); assert_yaml_snapshot!("remove_property_appied_patch", applied_patch_value); @@ -127,36 +121,3 @@ fn empty_tree() -> RojoTree { metadata: Default::default(), }) } - -/// Copy of data from RojoTree in the right shape to have useful snapshots. -#[derive(Debug, Serialize)] -struct InstanceView { - id: RbxId, - name: String, - class_name: String, - properties: HashMap, - metadata: InstanceMetadata, - children: Vec, -} - -fn view_tree(tree: &RojoTree) -> InstanceView { - view_instance(tree, tree.get_root_id()) -} - -fn view_instance(tree: &RojoTree, id: RbxId) -> InstanceView { - let instance = tree.get_instance(id).unwrap(); - - InstanceView { - id: instance.id(), - name: instance.name().to_owned(), - class_name: instance.class_name().to_owned(), - properties: instance.properties().clone(), - metadata: instance.metadata().clone(), - children: instance - .children() - .iter() - .copied() - .map(|id| view_instance(tree, id)) - .collect(), - } -} diff --git a/src/tree_view.rs b/src/tree_view.rs new file mode 100644 index 00000000..a55e7e92 --- /dev/null +++ b/src/tree_view.rs @@ -0,0 +1,56 @@ +use std::collections::HashMap; + +use rbx_dom_weak::{RbxId, RbxValue}; +use rojo_insta_ext::RedactionMap; +use serde::Serialize; + +use crate::snapshot::{InstanceMetadata, RojoTree}; + +/// Adds the given Rojo tree into the redaction map and produces a redacted +/// copy that can be immediately fed to one of Insta's snapshot macros like +/// `assert_snapshot_yaml`. +pub fn view_tree(tree: &RojoTree, redactions: &mut RedactionMap) -> serde_yaml::Value { + intern_tree(tree, redactions); + + let view = extract_instance_view(tree, tree.get_root_id()); + redactions.redacted_yaml(view) +} + +/// Adds the given Rojo tree into the redaction map. +pub fn intern_tree(tree: &RojoTree, redactions: &mut RedactionMap) { + let root_id = tree.get_root_id(); + redactions.intern(root_id); + + for descendant in tree.descendants(root_id) { + redactions.intern(descendant.id()); + } +} + +/// Copy of data from RojoTree in the right shape to have useful snapshots. +#[derive(Debug, Serialize)] +struct InstanceView { + id: RbxId, + name: String, + class_name: String, + properties: HashMap, + metadata: InstanceMetadata, + children: Vec, +} + +fn extract_instance_view(tree: &RojoTree, id: RbxId) -> InstanceView { + let instance = tree.get_instance(id).unwrap(); + + InstanceView { + id: instance.id(), + name: instance.name().to_owned(), + class_name: instance.class_name().to_owned(), + properties: instance.properties().clone(), + metadata: instance.metadata().clone(), + children: instance + .children() + .iter() + .copied() + .map(|id| extract_instance_view(tree, id)) + .collect(), + } +}