166 lines
5.1 KiB
Rust
166 lines
5.1 KiB
Rust
mod common;
|
|
|
|
use marqdo_lint::{
|
|
input::SourceDocument,
|
|
model::{ExecutionProjection, MarkdownTree},
|
|
rules::check_ast_document,
|
|
};
|
|
use serde_json::{json, Value};
|
|
|
|
fn document_with(raw: Value) -> SourceDocument {
|
|
let projection = ExecutionProjection::from_json(raw).unwrap();
|
|
let module = projection.entry_module().unwrap().clone();
|
|
let source = module
|
|
.source_snapshot(std::path::PathBuf::from("index.mq.md"))
|
|
.unwrap();
|
|
let markdown = MarkdownTree::from_analysis(
|
|
module.document.clone(),
|
|
&module.units,
|
|
&module.functions,
|
|
&source,
|
|
)
|
|
.unwrap();
|
|
SourceDocument {
|
|
source,
|
|
markdown,
|
|
module: Some(module),
|
|
}
|
|
}
|
|
|
|
fn reference(source_unit: &str, id: &str, state: &str) -> Value {
|
|
let mut value = json!({
|
|
"source": id,
|
|
"source_unit": source_unit,
|
|
"url": format!("./{state}.mq.md"),
|
|
"span": {"start": 0, "end": 0},
|
|
"state": state,
|
|
"target_kind": null,
|
|
"target_module": null,
|
|
"target_unit": null,
|
|
"candidate_package_instance_id": null,
|
|
"candidate_path": null,
|
|
"fragment": null,
|
|
"candidates": []
|
|
});
|
|
match state {
|
|
"resolved" => {
|
|
value["target_kind"] = json!("module");
|
|
value["target_module"] = json!(id);
|
|
}
|
|
"ambiguous" => {
|
|
value["candidates"] = json!(["a#one", "a#two"]);
|
|
}
|
|
_ => {}
|
|
}
|
|
value
|
|
}
|
|
|
|
#[test]
|
|
fn reference_states_map_to_distinct_rule_ids_and_messages() {
|
|
let mut raw = common::analysis_value("branches");
|
|
let module_id = raw["modules"][0]["id"].as_str().unwrap().to_string();
|
|
let unit_id = raw["modules"][0]["units"][1]["id"]
|
|
.as_str()
|
|
.unwrap()
|
|
.to_string();
|
|
let mut refs = Vec::new();
|
|
for state in [
|
|
"resolved",
|
|
"missing",
|
|
"not_loaded",
|
|
"ambiguous",
|
|
"external",
|
|
"outside_scope",
|
|
"invalid",
|
|
] {
|
|
let mut value = reference(&unit_id, &module_id, state);
|
|
if state != "missing" {
|
|
value["url"] = json!(format!("./{state}"));
|
|
}
|
|
refs.push(value);
|
|
}
|
|
raw["modules"][0]["references"] = Value::Array(refs);
|
|
let document = document_with(raw);
|
|
let diagnostics = check_ast_document(&document);
|
|
let ids = diagnostics
|
|
.iter()
|
|
.map(|diagnostic| diagnostic.id.as_str())
|
|
.collect::<Vec<_>>();
|
|
assert_eq!(
|
|
ids.iter().filter(|id| **id == "MQD-LINK-001").count(),
|
|
4,
|
|
"{diagnostics:?}"
|
|
);
|
|
assert_eq!(ids.iter().filter(|id| **id == "MQD-LINK-002").count(), 1);
|
|
assert_eq!(ids.iter().filter(|id| **id == "MQD-LINK-003").count(), 1);
|
|
let ambiguity = diagnostics
|
|
.iter()
|
|
.find(|diagnostic| {
|
|
diagnostic.id == "MQD-LINK-001" && diagnostic.message.contains("多个候选")
|
|
})
|
|
.unwrap();
|
|
assert!(ambiguity.message.contains("a#one"));
|
|
assert!(ambiguity.message.contains("a#two"));
|
|
let unverified = diagnostics
|
|
.iter()
|
|
.find(|diagnostic| diagnostic.id == "MQD-LINK-002")
|
|
.unwrap();
|
|
assert!(unverified.message.contains("不等于断链"));
|
|
}
|
|
|
|
#[test]
|
|
fn unresolved_calls_are_informational_not_errors() {
|
|
let mut raw = common::analysis_value("branches");
|
|
let module_id = raw["modules"][0]["id"].as_str().unwrap().to_string();
|
|
let function_id = raw["modules"][0]["functions"][0]["id"]
|
|
.as_str()
|
|
.unwrap()
|
|
.to_string();
|
|
raw["modules"][0]["calls"] = json!([{
|
|
"function": function_id,
|
|
"span": {"start": 0, "end": 0},
|
|
"callee": {"kind": "ident", "name": "动态目标"},
|
|
"state": "unresolved",
|
|
"target": null,
|
|
"target_kind": null,
|
|
"target_owner": null,
|
|
"reason": "dynamic"
|
|
}]);
|
|
let document = document_with(raw);
|
|
let diagnostics = check_ast_document(&document);
|
|
let call = diagnostics
|
|
.iter()
|
|
.find(|diagnostic| diagnostic.id == "MQD-CALL-001")
|
|
.unwrap();
|
|
assert_eq!(call.severity, marqdo_lint::rules::Severity::Info);
|
|
assert!(call.message.contains("不是错误"));
|
|
assert_eq!(module_id, document.module.as_ref().unwrap().id);
|
|
}
|
|
|
|
#[test]
|
|
fn resource_digest_is_checked_against_the_ast_literal() {
|
|
let mut raw = common::analysis_value("branches");
|
|
let source_len = raw["modules"][0]["source"].as_str().unwrap().len();
|
|
let module_id = raw["modules"][0]["id"].as_str().unwrap().to_string();
|
|
let unit_id = raw["modules"][0]["units"][1]["id"]
|
|
.as_str()
|
|
.unwrap()
|
|
.to_string();
|
|
raw["modules"][0]["resources"] = json!([{
|
|
"id": format!("{module_id}#resource:code-1"),
|
|
"module_id": module_id,
|
|
"unit_id": unit_id,
|
|
"kind": "code",
|
|
"span": {"start": 0, "end": 0},
|
|
"content_digest": "0".repeat(64),
|
|
"language": null,
|
|
"display": null
|
|
}]);
|
|
let error = ExecutionProjection::from_json(raw).unwrap_err();
|
|
assert!(
|
|
error.contains("content_digest") || error.contains("AST literal"),
|
|
"{error}"
|
|
);
|
|
let _ = source_len;
|
|
}
|