#[path = "../../printobserver-types/tests/support/surface.rs"]
pub mod surface;
use surface::{
Field, Method, Param, Variant, crate_dir, crate_source, enum_variants, parse, trait_methods,
workspace_dependency_names,
};
const CRATE: &str = "printobserver-vision-api";
fn method(name: &str, params: &[(&str, &str)], returns: &str) -> Method {
Method {
name: name.to_owned(),
params: params
.iter()
.map(|(name, ty)| Param {
name: (*name).to_owned(),
ty: (*ty).to_owned(),
})
.collect(),
returns: returns.to_owned(),
}
}
fn variant(name: &str, fields: &[(&str, &str)]) -> Variant {
Variant {
name: name.to_owned(),
fields: fields
.iter()
.map(|(name, ty)| Field {
name: (*name).to_owned(),
ty: (*ty).to_owned(),
})
.collect(),
}
}
#[test]
fn vision_port_declares_exactly_the_stated_methods() {
let source = parse(&crate_source(CRATE));
let expected = vec![
method(
"normalize",
&[("body", "RawBytes"), ("content_type", "Option<String>")],
"BoxFuture<'_,Result<NormalizedAlert,VisionError>>",
),
method(
"fetch_image",
&[("source_url", "String")],
"BoxFuture<'_,Result<FetchedImage,VisionError>>",
),
method(
"clear_detection",
&[("detection", "Detection")],
"BoxFuture<'_,Result<(),VisionError>>",
),
method(
"judge_frame",
&[("image", "FetchedImage")],
"BoxFuture<'_,Result<FetchedImage,FrameFault>>",
),
];
assert_eq!(trait_methods(&source, "VisionPort"), expected);
}
#[test]
fn vision_error_carries_exactly_the_stated_variants() {
let source = parse(&crate_source(CRATE));
let expected = vec![
variant("Malformed", &[("detail", "String"), ("raw", "RawBytes")]),
variant("TimedOut", &[]),
variant("TooLarge", &[("limit", "i64")]),
variant("UnacceptableContentType", &[("content_type", "String")]),
variant("Unreachable", &[("detail", "String")]),
variant("NotConfigured", &[("detail", "String")]),
];
assert_eq!(enum_variants(&source, "VisionError"), expected);
}
#[test]
fn every_port_method_answers_a_boxed_future() {
let source = parse(&crate_source(CRATE));
let methods = trait_methods(&source, "VisionPort");
assert!(!methods.is_empty(), "VisionPort declares no method");
for declared in methods {
assert!(
declared.returns.starts_with("BoxFuture<'_,Result<"),
"VisionPort::{} answers {}, which is neither asynchronous nor a Result",
declared.name,
declared.returns
);
}
}
#[test]
fn the_port_error_carries_no_not_yet_implemented_variant() {
let source = parse(&crate_source(CRATE));
for declared in enum_variants(&source, "VisionError") {
let name = declared.name.to_lowercase();
assert!(
!(name.contains("notimplemented")
|| name.contains("unimplemented")
|| name.contains("todo")),
"VisionError carries {}, a variant no real implementation can produce",
declared.name
);
}
}
#[test]
fn the_manifest_names_the_type_crate_and_no_other_workspace_crate() {
let manifest = std::fs::read_to_string(crate_dir(CRATE).join("Cargo.toml"))
.expect("the manifest is readable");
assert_eq!(
workspace_dependency_names(&manifest),
vec!["printobserver-types".to_owned()]
);
}