First round of snapshot tests for patch_compute

This commit is contained in:
Lucien Greathouse
2019-09-30 17:00:12 -07:00
parent 3678ddfa36
commit b512e707a5
9 changed files with 128 additions and 12 deletions

1
Cargo.lock generated
View File

@@ -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)",

View File

@@ -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"]

View File

@@ -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<RbxId>,

View File

@@ -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

View File

@@ -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<RbxId>,
pub added_instances: Vec<PatchAdd<'a>>,
@@ -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<String>,

View File

@@ -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 {

View File

@@ -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: ~

View File

@@ -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: ~

View File

@@ -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: ~