From b512e707a527552f2754fbd5fc2563077125b0d9 Mon Sep 17 00:00:00 2001 From: Lucien Greathouse Date: Mon, 30 Sep 2019 17:00:12 -0700 Subject: [PATCH] First round of snapshot tests for patch_compute --- Cargo.lock | 1 + Cargo.toml | 5 ++ src/snapshot/instance_snapshot.rs | 3 +- src/snapshot/metadata.rs | 4 +- src/snapshot/patch.rs | 7 +- src/snapshot/tests/compute.rs | 80 +++++++++++++++++-- .../snapshots/compute__remove_property.snap | 13 +++ .../compute__set_name_and_class_name.snap | 12 +++ .../snapshots/compute__set_property.snap | 15 ++++ 9 files changed, 128 insertions(+), 12 deletions(-) create mode 100644 src/snapshot/tests/snapshots/compute__remove_property.snap create mode 100644 src/snapshot/tests/snapshots/compute__set_name_and_class_name.snap create mode 100644 src/snapshot/tests/snapshots/compute__set_property.snap diff --git a/Cargo.lock b/Cargo.lock index 9a8686aa..04940478 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1460,6 +1460,7 @@ dependencies = [ "futures 0.1.28 (registry+https://github.com/rust-lang/crates.io-index)", "humantime 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "hyper 0.12.33 (registry+https://github.com/rust-lang/crates.io-index)", + "insta 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "jod-thread 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index b1826785..81bdd960 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -68,8 +68,13 @@ winreg = "0.6.2" [dev-dependencies] rojo-insta-ext = { path = "rojo-insta-ext" } + lazy_static = "1.2" paste = "0.1" pretty_assertions = "0.6.1" tempfile = "3.0" walkdir = "2.1" + +[dev-dependencies.insta] +version = "0.11.0" +features = ["redactions"] diff --git a/src/snapshot/instance_snapshot.rs b/src/snapshot/instance_snapshot.rs index 83ad5055..e609c66c 100644 --- a/src/snapshot/instance_snapshot.rs +++ b/src/snapshot/instance_snapshot.rs @@ -3,6 +3,7 @@ use std::{borrow::Cow, collections::HashMap}; use rbx_dom_weak::{RbxId, RbxTree, RbxValue}; +use serde::{Deserialize, Serialize}; use super::InstanceMetadata; @@ -13,7 +14,7 @@ use super::InstanceMetadata; // Possible future improvements: // - Use refcounted/interned strings // - Replace use of RbxValue with a sum of RbxValue + borrowed value -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct InstanceSnapshot<'source> { /// A temporary ID applied to the snapshot that's used for Ref properties. pub snapshot_id: Option, diff --git a/src/snapshot/metadata.rs b/src/snapshot/metadata.rs index 4dface7b..eadbd1ca 100644 --- a/src/snapshot/metadata.rs +++ b/src/snapshot/metadata.rs @@ -1,10 +1,12 @@ use std::path::PathBuf; +use serde::{Deserialize, Serialize}; + use crate::project::ProjectNode; /// Rojo-specific metadata that can be associated with an instance or a snapshot /// of an instance. -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct InstanceMetadata { /// Whether instances not present in the source should be ignored when /// live-syncing. This is useful when there are instances that Rojo does not diff --git a/src/snapshot/patch.rs b/src/snapshot/patch.rs index 3908f78b..f7ce22b0 100644 --- a/src/snapshot/patch.rs +++ b/src/snapshot/patch.rs @@ -3,6 +3,7 @@ use std::collections::HashMap; use rbx_dom_weak::{RbxId, RbxValue}; +use serde::{Deserialize, Serialize}; use super::{InstanceMetadata, InstanceSnapshot}; @@ -11,7 +12,7 @@ use super::{InstanceMetadata, InstanceSnapshot}; /// These patches shouldn't be persisted: there's no mechanism in place to make /// sure that another patch wasn't applied before this one that could cause a /// conflict! -#[derive(Debug, Default, Clone, PartialEq)] +#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)] pub struct PatchSet<'a> { pub removed_instances: Vec, pub added_instances: Vec>, @@ -29,14 +30,14 @@ impl<'a> PatchSet<'a> { } /// A patch containing an instance that was added to the tree. -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct PatchAdd<'a> { pub parent_id: RbxId, pub instance: InstanceSnapshot<'a>, } /// A patch indicating that properties of an instance changed. -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct PatchUpdate { pub id: RbxId, pub changed_name: Option, diff --git a/src/snapshot/tests/compute.rs b/src/snapshot/tests/compute.rs index c523c3d1..3478d484 100644 --- a/src/snapshot/tests/compute.rs +++ b/src/snapshot/tests/compute.rs @@ -1,14 +1,21 @@ use std::borrow::Cow; -use rbx_dom_weak::RbxInstanceProperties; +use insta::assert_yaml_snapshot; +use maplit::hashmap; +use rbx_dom_weak::{RbxInstanceProperties, RbxValue}; + +use rojo_insta_ext::RedactionMap; use crate::snapshot::{compute_patch_set, InstancePropertiesWithMeta, InstanceSnapshot, RojoTree}; #[test] -fn reify_folder() { - let tree = empty_tree(); +fn set_name_and_class_name() { + let mut redactions = RedactionMap::new(); - let folder = InstanceSnapshot { + let tree = empty_tree(); + redactions.intern(tree.get_root_id()); + + let snapshot = InstanceSnapshot { snapshot_id: None, metadata: Default::default(), name: Cow::Borrowed("Some Folder"), @@ -17,10 +24,69 @@ fn reify_folder() { children: Vec::new(), }; - let _patch_set = compute_patch_set(&folder, &tree, tree.get_root_id()); + let patch_set = compute_patch_set(&snapshot, &tree, tree.get_root_id()); + let patch_value = redactions.redacted_yaml(patch_set); - // TODO: Make assertions about patch set using snapshots. This needs patches - // to be serializable and also to have ID redactions more readily available. + assert_yaml_snapshot!(patch_value); +} + +#[test] +fn set_property() { + let mut redactions = RedactionMap::new(); + + let tree = empty_tree(); + redactions.intern(tree.get_root_id()); + + let snapshot = InstanceSnapshot { + snapshot_id: None, + metadata: Default::default(), + name: Cow::Borrowed("ROOT"), + class_name: Cow::Borrowed("ROOT"), + properties: hashmap! { + "PropertyName".to_owned() => RbxValue::String { + value: "Hello, world!".to_owned(), + }, + }, + children: Vec::new(), + }; + + let patch_set = compute_patch_set(&snapshot, &tree, tree.get_root_id()); + let patch_value = redactions.redacted_yaml(patch_set); + + assert_yaml_snapshot!(patch_value); +} + +#[test] +fn remove_property() { + let mut redactions = RedactionMap::new(); + + let mut tree = empty_tree(); + redactions.intern(tree.get_root_id()); + + { + let root_id = tree.get_root_id(); + let mut root_instance = tree.get_instance_mut(root_id).unwrap(); + root_instance.properties_mut().insert( + "Foo".to_owned(), + RbxValue::String { + value: "This should be removed by the patch.".to_owned(), + }, + ); + } + + let snapshot = InstanceSnapshot { + snapshot_id: None, + metadata: Default::default(), + name: Cow::Borrowed("ROOT"), + class_name: Cow::Borrowed("ROOT"), + properties: Default::default(), + children: Vec::new(), + }; + + let patch_set = compute_patch_set(&snapshot, &tree, tree.get_root_id()); + let patch_value = redactions.redacted_yaml(patch_set); + + assert_yaml_snapshot!(patch_value); } fn empty_tree() -> RojoTree { diff --git a/src/snapshot/tests/snapshots/compute__remove_property.snap b/src/snapshot/tests/snapshots/compute__remove_property.snap new file mode 100644 index 00000000..44e81098 --- /dev/null +++ b/src/snapshot/tests/snapshots/compute__remove_property.snap @@ -0,0 +1,13 @@ +--- +source: src/snapshot/tests/compute.rs +expression: patch_value +--- +removed_instances: [] +added_instances: [] +updated_instances: + - id: id-1 + changed_name: ~ + changed_class_name: ~ + changed_properties: + Foo: ~ + changed_metadata: ~ diff --git a/src/snapshot/tests/snapshots/compute__set_name_and_class_name.snap b/src/snapshot/tests/snapshots/compute__set_name_and_class_name.snap new file mode 100644 index 00000000..f949a901 --- /dev/null +++ b/src/snapshot/tests/snapshots/compute__set_name_and_class_name.snap @@ -0,0 +1,12 @@ +--- +source: src/snapshot/tests/compute.rs +expression: patch_value +--- +removed_instances: [] +added_instances: [] +updated_instances: + - id: id-1 + changed_name: Some Folder + changed_class_name: Folder + changed_properties: {} + changed_metadata: ~ diff --git a/src/snapshot/tests/snapshots/compute__set_property.snap b/src/snapshot/tests/snapshots/compute__set_property.snap new file mode 100644 index 00000000..8c39d3fd --- /dev/null +++ b/src/snapshot/tests/snapshots/compute__set_property.snap @@ -0,0 +1,15 @@ +--- +source: src/snapshot/tests/compute.rs +expression: patch_value +--- +removed_instances: [] +added_instances: [] +updated_instances: + - id: id-1 + changed_name: ~ + changed_class_name: ~ + changed_properties: + PropertyName: + Type: String + Value: "Hello, world!" + changed_metadata: ~