Files
2026-11-27 06:03:56 +00:00

190 lines
6.4 KiB
Rust

use super::*;
#[test]
fn settings_load_once_validate_nested_shapes_and_keep_configuration_out_of_builds() {
let root = directory("settings");
let domain = root.join("module/demo/site");
fs::create_dir_all(&domain).unwrap();
fs::create_dir_all(root.join("config")).unwrap();
write_source(
domain.join("app/main.dever"),
r#"
type Mode { Active, Paused }
type Banner { title: Text, note: Text?, limit: Int >= 1 and <= 20 }
setting banner: Banner
setting modes: List<Mode>
setting size: Int >= 1 and <= 20
setting note: Text?
read() (out: Text) { out = banner.title }
measure() (out: Int >= 1 and <= 20) { out = size }
flow start() (out: Text)
| step | kind | do |
| out | calc | read() |
"#,
)
.unwrap();
fs::write(
domain.join("api.dever"),
"cmd read = app.start\ncmd size = app.measure",
)
.unwrap();
let path = root.to_str().unwrap();
success(cli(&["fmt", path]));
success(cli(&["check", path])); // 检查和构建不要求配置存在。
let index: serde_json::Value =
serde_json::from_str(&success(cli(&["inspect", path, "--json"]))).unwrap();
assert_eq!(
index["symbols"]
.as_array()
.unwrap()
.iter()
.filter(|symbol| symbol["kind"] == "setting")
.count(),
4
);
assert_eq!(
index["calls"]
.as_array()
.unwrap()
.iter()
.filter(|edge| edge["kind"] == "reference")
.count(),
2
);
let (project, program) = analyze(&root);
let artifact = build::build(&project, &program, "main", Profile::Dev, None).unwrap();
let binary = root.join("application");
artifact.publish(&binary).unwrap();
drop(artifact);
let config = root.join("config/setting.json");
let input = r#"{"module":{"demo":{"site":{"banner":{"title":"雪😀","note":null,"limit":8},"modes":["Active"],"size":12,"note":null}}}}"#;
fs::write(&config, input).unwrap();
let run = || {
Command::new(&binary)
.args(["demo.site.read", "{}"])
.output()
.unwrap()
};
assert_eq!(
serde_json::from_str::<serde_json::Value>(&success(run())).unwrap()["data"],
"雪😀"
);
fs::write(&config, input.replace("雪😀", "更新")).unwrap();
assert_eq!(
serde_json::from_str::<serde_json::Value>(&success(run())).unwrap()["data"],
"更新"
);
assert_eq!(
serde_json::from_str::<serde_json::Value>(&success(cli(&[
"run",
path,
"--",
"demo.site.size",
"{}"
])))
.unwrap()["data"],
12
);
let invalid = [
input.replace("\"size\":12", "\"size\":0"),
input.replace("\"limit\":8", "\"limit\":21"),
input.replace("\"size\":12,", ""),
input.replace("\"note\":null,", ""),
input.replace(",\"note\":null", ""),
input.replace("\"size\":12", "\"size\":12,\"size\":13"),
input.replace("\"limit\":8", "\"limit\":8,\"extra\":1"),
input.replace("\"Active\"", "\"Unknown\""),
input.replace(
"\"banner\":{\"title\":\"雪😀\",\"note\":null,\"limit\":8}",
"\"banner\":[\"雪😀\",null,8]",
),
input.replace("\"size\":12", "\"size\":true"),
format!("{input} null"),
"{}".into(),
];
for invalid in invalid {
fs::write(&config, &invalid).unwrap();
let result = run();
assert!(!result.status.success(), "accepted {invalid}");
assert!(result.stdout.is_empty());
assert!(String::from_utf8_lossy(&result.stderr).contains("setting.json"));
}
fs::remove_file(&config).unwrap();
assert!(!run().status.success());
let custom = root.join("custom.json");
fs::write(&custom, input).unwrap();
success(
Command::new(&binary)
.args(["--config", custom.to_str().unwrap(), "demo.site.read", "{}"])
.output()
.unwrap(),
);
success(cli(&[
"run",
path,
"--",
"--config",
custom.to_str().unwrap(),
"demo.site.read",
"{}",
]));
}
#[test]
fn setting_references_obey_roles_and_participate_in_domain_cycles() {
let root = directory("setting-access");
let first = root.join("module/demo/first");
let second = root.join("module/demo/second");
for directory in [&first, &second] {
fs::create_dir_all(directory).unwrap();
}
fs::write(first.join("domain.dever"), "setting capacity: Int >= 1").unwrap();
write_source(
first.join("app/main.dever"),
"setting label: Text\nget() (out: Int >= 1) { out = domain.capacity }",
)
.unwrap();
write_source(
second.join("app/main.dever"),
"setting limit: Int\nname() (out: Text) { out = first.label }",
)
.unwrap();
analyze(&root);
for (first_source, second_source, code) in [
(
"setting label: Text\nf() (out: Int) { out = second.limit }",
"setting limit: Int\nf() (out: Text) { out = first.label }",
"C010",
),
(
"setting label: Text",
"f() (out: Int) { out = demo.first.domain.capacity }",
"C003",
),
("setting label: Text\nconst text = label", "", "C005"),
("setting capacity: Int", "", "C001"),
("setting label: Text\nconst label = 1", "", "C001"),
("setting get: Text\nget() () {}", "", "C001"),
] {
write_source(first.join("app/main.dever"), first_source).unwrap();
write_source(second.join("app/main.dever"), second_source).unwrap();
let project = Project::load(&root).unwrap();
let error = program::check(&project.declarations, &project.modules)
.err()
.unwrap();
assert_eq!(error.code, code, "{error}");
}
}
#[test]
fn invalid_settings_do_not_survive_in_navigation() {
let root = directory("settings-navigation");
let path = root.join("app.dever");
fs::write(&path, "setting value: Int\nsetting value: Int\nf() (out: Int) { out = value }\nindependent() (out: Int) { out = 1 }").unwrap();
let project = Project::load(&path).unwrap();
let index = dever::compiler::inspect::analyze(&project, None, 1, false).unwrap();
assert_eq!(index.symbols.len(), 1);
assert_eq!(index.symbols[0].name, "main.main.app.independent");
assert!(index.diagnostics.iter().any(|error| error.code == "C001"));
}