d223a1d215
Make #[slhx::component] compare generated handle symbols with inline #[slhx::handler] functions and fail when a required handler implementation is missing. req: component/005 req: check/002 req: test/003
515 lines
12 KiB
Rust
515 lines
12 KiB
Rust
use std::path::{Path, PathBuf};
|
|
use std::process::Command;
|
|
|
|
#[test]
|
|
fn component_macro_reports_missing_handler_implementation() {
|
|
// req: component/005 req: check/002 req: test/003
|
|
let fixture = Fixture::new("slhx-derive-component-missing-handler-fail");
|
|
fixture.write(
|
|
"Cargo.toml",
|
|
&format!(
|
|
r#"[package]
|
|
name = "slhx-derive-component-missing-handler-fail"
|
|
version = "0.0.0"
|
|
edition = "2021"
|
|
|
|
[lib]
|
|
path = "src/lib.rs"
|
|
|
|
[dependencies]
|
|
slhx = {{ path = {:?} }}
|
|
"#,
|
|
repo_path("slhx")
|
|
),
|
|
);
|
|
fixture.write(
|
|
"build.rs",
|
|
r#"fn main() {
|
|
let out = std::path::PathBuf::from(std::env::var_os("OUT_DIR").unwrap());
|
|
std::fs::write(
|
|
out.join("slhx.syms"),
|
|
"slhx-syms-v1\nhandle\ttemplates/app.heml::create\tcreate\t1\nhandle\ttemplates/app.heml::delete\tdelete\t2\n",
|
|
)
|
|
.unwrap();
|
|
}
|
|
"#,
|
|
);
|
|
fixture.write(
|
|
"src/lib.rs",
|
|
r#"#[slhx::component]
|
|
mod todos {
|
|
#[slhx::handler]
|
|
fn create() -> impl slhx::IntoEffect {
|
|
slhx::EffectBatch::default()
|
|
}
|
|
}
|
|
"#,
|
|
);
|
|
|
|
let output = check_fixture(&fixture);
|
|
|
|
assert!(!output.status.success(), "fixture unexpectedly compiled");
|
|
let stderr = String::from_utf8_lossy(&output.stderr);
|
|
assert!(
|
|
stderr.contains("#[slhx::component] missing handler implementation(s): delete"),
|
|
"missing component diagnostic in stderr:\n{stderr}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn handler_macro_reports_unknown_handle_and_bad_shape() {
|
|
// req: derive_handler/001 req: test/003
|
|
let fixture = Fixture::new("slhx-derive-handler-fail");
|
|
fixture.write(
|
|
"Cargo.toml",
|
|
&format!(
|
|
r#"[package]
|
|
name = "slhx-derive-handler-fail"
|
|
version = "0.0.0"
|
|
edition = "2021"
|
|
|
|
[lib]
|
|
path = "src/lib.rs"
|
|
|
|
[dependencies]
|
|
slhx = {{ path = {:?} }}
|
|
"#,
|
|
repo_path("slhx")
|
|
),
|
|
);
|
|
fixture.write(
|
|
"build.rs",
|
|
r#"fn main() {
|
|
let out = std::path::PathBuf::from(std::env::var_os("OUT_DIR").unwrap());
|
|
std::fs::write(
|
|
out.join("slhx.syms"),
|
|
"slhx-syms-v1\nhandle\ttemplates/app.heml::known\tknown\t1\n",
|
|
)
|
|
.unwrap();
|
|
}
|
|
"#,
|
|
);
|
|
fixture.write(
|
|
"src/lib.rs",
|
|
r#"#[slhx::handler]
|
|
fn missing() -> impl slhx::IntoEffect {
|
|
slhx::EffectBatch::default()
|
|
}
|
|
|
|
#[slhx::handler]
|
|
fn known() {}
|
|
"#,
|
|
);
|
|
|
|
let output = check_fixture(&fixture);
|
|
|
|
assert!(!output.status.success(), "fixture unexpectedly compiled");
|
|
let stderr = String::from_utf8_lossy(&output.stderr);
|
|
assert!(
|
|
stderr.contains("unknown slhx handle `missing`; add `data-slhx-handle=\"missing\"`"),
|
|
"missing unknown-handle diagnostic in stderr:\n{stderr}"
|
|
);
|
|
assert!(
|
|
stderr.contains("slhx handler `known` must accept a form/context parameter or return a value implementing IntoEffect"),
|
|
"missing handler-shape diagnostic in stderr:\n{stderr}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn handler_macro_reports_missing_syms() {
|
|
// req: build/003 req: test/003
|
|
let fixture = Fixture::new("slhx-derive-handler-syms-fail");
|
|
fixture.write(
|
|
"Cargo.toml",
|
|
&format!(
|
|
r#"[package]
|
|
name = "slhx-derive-handler-syms-fail"
|
|
version = "0.0.0"
|
|
edition = "2021"
|
|
|
|
[lib]
|
|
path = "src/lib.rs"
|
|
|
|
[dependencies]
|
|
slhx = {{ path = {:?} }}
|
|
"#,
|
|
repo_path("slhx")
|
|
),
|
|
);
|
|
fixture.write(
|
|
"build.rs",
|
|
r#"fn main() {
|
|
println!("cargo:rerun-if-changed=build.rs");
|
|
}
|
|
"#,
|
|
);
|
|
fixture.write(
|
|
"src/lib.rs",
|
|
r#"#[slhx::handler]
|
|
fn create() -> impl slhx::IntoEffect {
|
|
slhx::EffectBatch::default()
|
|
}
|
|
"#,
|
|
);
|
|
|
|
let output = check_fixture(&fixture);
|
|
|
|
assert!(!output.status.success(), "fixture unexpectedly compiled");
|
|
let stderr = String::from_utf8_lossy(&output.stderr);
|
|
assert!(
|
|
stderr.contains("#[slhx::handler] could not find"),
|
|
"missing handler syms diagnostic in stderr:\n{stderr}"
|
|
);
|
|
assert!(
|
|
stderr.contains("add slhx_build::app().run()? to build.rs"),
|
|
"missing build.rs hint in stderr:\n{stderr}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn handler_requires_generated_param_arguments() {
|
|
// req: derive_handler/001 req: test/003
|
|
let fixture = Fixture::new("slhx-derive-param-handler-fail");
|
|
fixture.write(
|
|
"Cargo.toml",
|
|
&format!(
|
|
r#"[package]
|
|
name = "slhx-derive-param-handler-fail"
|
|
version = "0.0.0"
|
|
edition = "2021"
|
|
|
|
[lib]
|
|
path = "src/lib.rs"
|
|
|
|
[dependencies]
|
|
slhx = {{ path = {:?} }}
|
|
"#,
|
|
repo_path("slhx")
|
|
),
|
|
);
|
|
fixture.write(
|
|
"build.rs",
|
|
r#"fn main() {
|
|
let out = std::path::PathBuf::from(std::env::var_os("OUT_DIR").unwrap());
|
|
std::fs::write(
|
|
out.join("slhx.syms"),
|
|
"slhx-syms-v1\nhandle\ttemplates/app.heml::show\tshow\t1\nhandle_param\tshow\ttodo_id\nhandle_param\tshow\tmode\n",
|
|
)
|
|
.unwrap();
|
|
}
|
|
"#,
|
|
);
|
|
fixture.write(
|
|
"src/lib.rs",
|
|
r#"#[slhx::handler]
|
|
fn show(todo_id: String) -> impl slhx::IntoEffect {
|
|
slhx::EffectBatch::default()
|
|
}
|
|
"#,
|
|
);
|
|
|
|
let output = check_fixture(&fixture);
|
|
|
|
assert!(!output.status.success(), "fixture unexpectedly compiled");
|
|
let stderr = String::from_utf8_lossy(&output.stderr);
|
|
assert!(
|
|
stderr.contains("slhx handler `show` is missing generated param argument(s): mode"),
|
|
"missing param diagnostic in stderr:\n{stderr}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn form_handle_requires_form_parameter() {
|
|
// req: form/004 req: form/006 req: test/003
|
|
let fixture = Fixture::new("slhx-derive-form-handler-fail");
|
|
fixture.write(
|
|
"Cargo.toml",
|
|
&format!(
|
|
r#"[package]
|
|
name = "slhx-derive-form-handler-fail"
|
|
version = "0.0.0"
|
|
edition = "2021"
|
|
|
|
[lib]
|
|
path = "src/lib.rs"
|
|
|
|
[dependencies]
|
|
slhx = {{ path = {:?} }}
|
|
"#,
|
|
repo_path("slhx")
|
|
),
|
|
);
|
|
fixture.write(
|
|
"build.rs",
|
|
r#"fn main() {
|
|
let out = std::path::PathBuf::from(std::env::var_os("OUT_DIR").unwrap());
|
|
std::fs::write(
|
|
out.join("slhx.syms"),
|
|
"slhx-syms-v1\nhandle\ttemplates/app.heml::create\tcreate\t1\nhandle_form\tcreate\tnew_todo\n",
|
|
)
|
|
.unwrap();
|
|
}
|
|
"#,
|
|
);
|
|
fixture.write(
|
|
"src/lib.rs",
|
|
r#"#[slhx::handler]
|
|
fn create() -> impl slhx::IntoEffect {
|
|
slhx::EffectBatch::default()
|
|
}
|
|
"#,
|
|
);
|
|
|
|
let output = check_fixture(&fixture);
|
|
|
|
assert!(!output.status.success(), "fixture unexpectedly compiled");
|
|
let stderr = String::from_utf8_lossy(&output.stderr);
|
|
assert!(
|
|
stderr.contains("slhx handler `create` handles a generated form and must accept slhx::Form<_>"),
|
|
"missing form-handler diagnostic in stderr:\n{stderr}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn form_handle_rejects_form_name_suffix_impostor() {
|
|
// req: form/004 req: form/006 req: test/003
|
|
let fixture = Fixture::new("slhx-derive-form-impostor-fail");
|
|
fixture.write(
|
|
"Cargo.toml",
|
|
&format!(
|
|
r#"[package]
|
|
name = "slhx-derive-form-impostor-fail"
|
|
version = "0.0.0"
|
|
edition = "2021"
|
|
|
|
[lib]
|
|
path = "src/lib.rs"
|
|
|
|
[dependencies]
|
|
slhx = {{ path = {:?} }}
|
|
"#,
|
|
repo_path("slhx")
|
|
),
|
|
);
|
|
fixture.write(
|
|
"build.rs",
|
|
r#"fn main() {
|
|
let out = std::path::PathBuf::from(std::env::var_os("OUT_DIR").unwrap());
|
|
std::fs::write(
|
|
out.join("slhx.syms"),
|
|
"slhx-syms-v1\nhandle\ttemplates/app.heml::create\tcreate\t1\nhandle_form\tcreate\tnew_todo\n",
|
|
)
|
|
.unwrap();
|
|
}
|
|
"#,
|
|
);
|
|
fixture.write(
|
|
"src/lib.rs",
|
|
r#"struct CreateForm;
|
|
|
|
#[slhx::handler]
|
|
fn create(_form: CreateForm) -> impl slhx::IntoEffect {
|
|
slhx::EffectBatch::default()
|
|
}
|
|
"#,
|
|
);
|
|
|
|
let output = check_fixture(&fixture);
|
|
|
|
assert!(!output.status.success(), "fixture unexpectedly compiled");
|
|
let stderr = String::from_utf8_lossy(&output.stderr);
|
|
assert!(
|
|
stderr.contains("slhx handler `create` handles a generated form and must accept slhx::Form<_>"),
|
|
"missing form-impostor diagnostic in stderr:\n{stderr}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn surface_macro_reports_missing_generated_include() {
|
|
// req: build/004 req: test/003
|
|
let fixture = Fixture::new("slhx-derive-surface-include-fail");
|
|
fixture.write(
|
|
"Cargo.toml",
|
|
&format!(
|
|
r#"[package]
|
|
name = "slhx-derive-surface-include-fail"
|
|
version = "0.0.0"
|
|
edition = "2021"
|
|
|
|
[lib]
|
|
path = "src/lib.rs"
|
|
|
|
[dependencies]
|
|
slhx = {{ path = {:?} }}
|
|
"#,
|
|
repo_path("slhx")
|
|
),
|
|
);
|
|
fixture.write(
|
|
"build.rs",
|
|
r#"fn main() {
|
|
println!("cargo:rerun-if-changed=build.rs");
|
|
}
|
|
"#,
|
|
);
|
|
fixture.write(
|
|
"src/lib.rs",
|
|
r#"#[slhx::surface]
|
|
pub mod ui {}
|
|
"#,
|
|
);
|
|
|
|
let output = check_fixture(&fixture);
|
|
|
|
assert!(!output.status.success(), "fixture unexpectedly compiled");
|
|
let stderr = String::from_utf8_lossy(&output.stderr);
|
|
assert!(
|
|
stderr.contains("#[slhx::surface] could not find"),
|
|
"missing surface include diagnostic in stderr:\n{stderr}"
|
|
);
|
|
assert!(
|
|
stderr.contains("add slhx_build::app().run()? to build.rs"),
|
|
"missing build.rs hint in stderr:\n{stderr}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn surface_macro_requires_inline_module() {
|
|
// req: codegen/001 req: test/003
|
|
let fixture = Fixture::new("slhx-derive-surface-inline-fail");
|
|
fixture.write(
|
|
"Cargo.toml",
|
|
&format!(
|
|
r#"[package]
|
|
name = "slhx-derive-surface-inline-fail"
|
|
version = "0.0.0"
|
|
edition = "2021"
|
|
|
|
[lib]
|
|
path = "src/lib.rs"
|
|
|
|
[dependencies]
|
|
slhx = {{ path = {:?} }}
|
|
"#,
|
|
repo_path("slhx")
|
|
),
|
|
);
|
|
fixture.write(
|
|
"src/lib.rs",
|
|
r#"#[slhx::surface]
|
|
pub mod ui;
|
|
"#,
|
|
);
|
|
|
|
let output = check_fixture(&fixture);
|
|
|
|
assert!(!output.status.success(), "fixture unexpectedly compiled");
|
|
let stderr = String::from_utf8_lossy(&output.stderr);
|
|
assert!(
|
|
stderr.contains("#[slhx::surface] must be used on an inline module"),
|
|
"missing inline-module diagnostic in stderr:\n{stderr}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn generated_resource_references_fail_when_name_is_absent() {
|
|
// req: style/002 req: codegen/001 req: test/003
|
|
let fixture = Fixture::new("slhx-derive-generated-resource-fail");
|
|
fixture.write(
|
|
"Cargo.toml",
|
|
&format!(
|
|
r#"[package]
|
|
name = "slhx-derive-generated-resource-fail"
|
|
version = "0.0.0"
|
|
edition = "2021"
|
|
|
|
[lib]
|
|
path = "src/lib.rs"
|
|
|
|
[dependencies]
|
|
slhx = {{ path = {:?} }}
|
|
"#,
|
|
repo_path("slhx")
|
|
),
|
|
);
|
|
fixture.write(
|
|
"build.rs",
|
|
r#"fn main() {
|
|
let out = std::path::PathBuf::from(std::env::var_os("OUT_DIR").unwrap());
|
|
std::fs::write(
|
|
out.join("slhx.generated.rs"),
|
|
"pub mod classes { pub const card: ::slhx::CssClass = ::slhx::CssClass::new(\"card\"); }\n",
|
|
)
|
|
.unwrap();
|
|
}
|
|
"#,
|
|
);
|
|
fixture.write(
|
|
"src/lib.rs",
|
|
r#"#[slhx::surface]
|
|
pub mod ui {}
|
|
|
|
pub const KNOWN: slhx::CssClass = ui::classes::card;
|
|
pub const MISSING: slhx::CssClass = ui::classes::missing;
|
|
"#,
|
|
);
|
|
|
|
let output = check_fixture(&fixture);
|
|
|
|
assert!(!output.status.success(), "fixture unexpectedly compiled");
|
|
let stderr = String::from_utf8_lossy(&output.stderr);
|
|
assert!(
|
|
stderr.contains("cannot find value `missing` in module `ui::classes`"),
|
|
"missing generated-resource diagnostic in stderr:\n{stderr}"
|
|
);
|
|
}
|
|
|
|
fn check_fixture(fixture: &Fixture) -> std::process::Output {
|
|
Command::new("cargo")
|
|
.arg("check")
|
|
.arg("--quiet")
|
|
.arg("--manifest-path")
|
|
.arg(fixture.path.join("Cargo.toml"))
|
|
.env("CARGO_TARGET_DIR", fixture.path.join("target"))
|
|
.output()
|
|
.expect("cargo check fixture runs")
|
|
}
|
|
|
|
fn repo_path(crate_name: &str) -> PathBuf {
|
|
Path::new(env!("CARGO_MANIFEST_DIR"))
|
|
.parent()
|
|
.expect("crate has workspace parent")
|
|
.join(crate_name)
|
|
}
|
|
|
|
struct Fixture {
|
|
path: PathBuf,
|
|
}
|
|
|
|
impl Fixture {
|
|
fn new(name: &str) -> Self {
|
|
let path = std::env::temp_dir().join(format!(
|
|
"{name}-{}-{}",
|
|
std::process::id(),
|
|
std::thread::current().name().unwrap_or("test")
|
|
));
|
|
let _ = std::fs::remove_dir_all(&path);
|
|
std::fs::create_dir_all(path.join("src")).unwrap();
|
|
Self { path }
|
|
}
|
|
|
|
fn write(&self, relative: &str, contents: &str) {
|
|
let path = self.path.join(relative);
|
|
if let Some(parent) = path.parent() {
|
|
std::fs::create_dir_all(parent).unwrap();
|
|
}
|
|
std::fs::write(path, contents).unwrap();
|
|
}
|
|
}
|
|
|
|
impl Drop for Fixture {
|
|
fn drop(&mut self) {
|
|
let _ = std::fs::remove_dir_all(&self.path);
|
|
}
|
|
}
|