forked from rojo-rbx/rojo
Move patch apply test utility into tree_view module
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -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)",
|
||||
|
||||
@@ -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"
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<String, RbxValue>,
|
||||
metadata: InstanceMetadata,
|
||||
children: Vec<InstanceView>,
|
||||
}
|
||||
|
||||
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(),
|
||||
}
|
||||
}
|
||||
|
||||
56
src/tree_view.rs
Normal file
56
src/tree_view.rs
Normal file
@@ -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<String, RbxValue>,
|
||||
metadata: InstanceMetadata,
|
||||
children: Vec<InstanceView>,
|
||||
}
|
||||
|
||||
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(),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user