diff --git a/Cargo.lock b/Cargo.lock index 02fb2029..3175ce60 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1617,6 +1617,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)", "walkdir 2.2.9 (registry+https://github.com/rust-lang/crates.io-index)", ] diff --git a/rojo-test/Cargo.toml b/rojo-test/Cargo.toml index 173cbb16..366dfe8c 100644 --- a/rojo-test/Cargo.toml +++ b/rojo-test/Cargo.toml @@ -13,8 +13,10 @@ rbx_dom_weak = "1.9.0" reqwest = "0.9.20" serde = "1.0.99" serde_json = "1.0.40" +serde_yaml = "0.8.9" tempfile = "3.1.0" walkdir = "2.2.9" + rojo-insta-ext = { path = "../rojo-insta-ext" } # We execute Rojo via std::process::Command, so depend on it so it's built! diff --git a/rojo-test/src/lib.rs b/rojo-test/src/lib.rs index 218b3483..29258e3b 100644 --- a/rojo-test/src/lib.rs +++ b/rojo-test/src/lib.rs @@ -1,6 +1,8 @@ #![cfg(test)] +#[macro_use] +mod serve_util; + mod build_test; mod serve_test; -mod serve_util; mod util; diff --git a/rojo-test/src/serve_test.rs b/rojo-test/src/serve_test.rs index bdcc65ba..82a8a539 100644 --- a/rojo-test/src/serve_test.rs +++ b/rojo-test/src/serve_test.rs @@ -2,22 +2,18 @@ use std::fs; use insta::assert_yaml_snapshot; -use crate::serve_util::{intern_read_response, run_serve_test}; +use crate::serve_util::{run_serve_test, InternAndRedact}; #[test] fn empty() { run_serve_test("empty", |session, mut redactions| { let info = session.get_api_rojo().unwrap(); - let root_id = info.root_instance_id; - let info = redactions.redacted_yaml(info); - assert_yaml_snapshot!(info); + assert_yaml_snapshot!(redactions.redacted_yaml(info)); - let read_result = session.get_api_read(root_id).unwrap(); - intern_read_response(&mut redactions, &read_result, root_id); - let read_result = redactions.redacted_yaml(read_result); - assert_yaml_snapshot!(read_result); + let read_response = session.get_api_read(root_id).unwrap(); + assert_yaml_snapshot!(read_response.intern_and_redact(&mut redactions, root_id)); }); } @@ -27,23 +23,18 @@ fn scripts() { let info = session.get_api_rojo().unwrap(); let root_id = info.root_instance_id; - let info = redactions.redacted_yaml(info); - assert_yaml_snapshot!(info); + assert_yaml_snapshot!(redactions.redacted_yaml(info)); - let read_result = session.get_api_read(root_id).unwrap(); - intern_read_response(&mut redactions, &read_result, root_id); - let read_result = redactions.redacted_yaml(read_result); - assert_yaml_snapshot!(read_result); + let read_response = session.get_api_read(root_id).unwrap(); + assert_yaml_snapshot!(read_response.intern_and_redact(&mut redactions, root_id)); fs::write(session.path().join("foo.lua"), "Updated foo!").unwrap(); - let subscribe_result = session.get_api_subscribe(0).unwrap(); - let subscribe_result = redactions.redacted_yaml(subscribe_result); - assert_yaml_snapshot!(subscribe_result); + let subscribe_response = session.get_api_subscribe(0).unwrap(); + assert_yaml_snapshot!(redactions.redacted_yaml(subscribe_response)); - let read_result = session.get_api_read(root_id).unwrap(); - let read_result = redactions.redacted_yaml(read_result); - assert_yaml_snapshot!(read_result); + let read_response = session.get_api_read(root_id).unwrap(); + assert_yaml_snapshot!(read_response.intern_and_redact(&mut redactions, root_id)); }); } @@ -51,25 +42,15 @@ fn scripts() { fn just_txt() { run_serve_test("just-txt.txt", |session, mut redactions| { let info = session.get_api_rojo().unwrap(); - let root_id = info.root_instance_id; - let info = redactions.redacted_yaml(info); - assert_yaml_snapshot!(info); + assert_yaml_snapshot!(redactions.redacted_yaml(info)); - let read_result = session.get_api_read(root_id).unwrap(); - redactions.intern_iter(read_result.instances.keys().copied()); - let read_result = redactions.redacted_yaml(read_result); - - assert_yaml_snapshot!(read_result); + let read_response = session.get_api_read(root_id).unwrap(); + assert_yaml_snapshot!(read_response.intern_and_redact(&mut redactions, root_id)); fs::write(session.path(), "Changed content!").unwrap(); // TODO: Directly served files currently don't trigger changed events! - - // let subscribe_result = session.get_api_subscribe(0).unwrap(); - // let subscribe_result = redactions.redacted_yaml(subscribe_result); - - // assert_yaml_snapshot!(subscribe_result); }); } diff --git a/rojo-test/src/serve_util.rs b/rojo-test/src/serve_util.rs index e62e63ba..95695053 100644 --- a/rojo-test/src/serve_util.rs +++ b/rojo-test/src/serve_util.rs @@ -6,6 +6,7 @@ use std::{ }; use rbx_dom_weak::RbxId; +use serde::Serialize; use tempfile::{tempdir, TempDir}; use librojo::web_interface::{ReadResponse, ServerInfoResponse, SubscribeResponse}; @@ -35,19 +36,33 @@ pub fn run_serve_test(test_name: &str, callback: impl FnOnce(TestServeSession, R settings.bind(move || callback(session, redactions)); } -/// Intern the response to Rojo's read API, doing traversal in a deterministic -/// order. -pub fn intern_read_response( - redactions: &mut RedactionMap, - response: &ReadResponse, - root_id: RbxId, -) { - redactions.intern(root_id); +pub trait Internable { + fn intern(&self, redactions: &mut RedactionMap, extra: T); +} - let root_instance = response.instances.get(&root_id).unwrap(); +impl Internable for ReadResponse<'_> { + fn intern(&self, redactions: &mut RedactionMap, root_id: RbxId) { + redactions.intern(root_id); - for &child_id in root_instance.children.iter() { - intern_read_response(redactions, response, child_id); + let root_instance = self.instances.get(&root_id).unwrap(); + + for &child_id in root_instance.children.iter() { + self.intern(redactions, child_id); + } + } +} + +pub trait InternAndRedact { + fn intern_and_redact(&self, redactions: &mut RedactionMap, extra: T) -> serde_yaml::Value; +} + +impl InternAndRedact for I +where + I: Serialize + Internable, +{ + fn intern_and_redact(&self, redactions: &mut RedactionMap, extra: T) -> serde_yaml::Value { + self.intern(redactions, extra); + redactions.redacted_yaml(self) } }