mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-25 20:14:31 +01:00
A dimension that belongs to one component's own drawing is a documented constant in that component's file; layout sizes stay semantic tokens, and no size-preset token exists for one component (craft-framework.md 5.4). - Spec: 5.4 rule 3 reworded; Appendix A, 4.6 (the component-token rung goes, no such tokens exist), 8.3, porting.md and the theme README agree. - design-lint: literal-size/* treats a module- or impl-level const with a doc comment as a named value: a name that resolves to it is not flagged, and its initializer is its definition. Fn-local and undocumented consts and call-site literals stay flagged. Nine markers that only covered such consts are removed. - Ruler ticks: size.ruler.tick.* leaves the size presets; kit.rs draws TICK_MID = 6 and TICK_MINOR = 3 points in every density, as the old library and the apps' rulers do. Comfortable ticks go back to 6 / 3 (from 9 / 4.5); Compact is unchanged. - size.scroll_fade becomes FADE_LENGTH in scroll_bar.rs, size.notice.width CARD_WIDTH in notice.rs, and size.story.* multiples of size.control.md in testing/stage.rs (15, 10 and 30 control heights: today's values in both densities). pointer_tip, checker, thumb.sample.*, the card gaps and icon.glyph_md stay tokens: each has more than one user.
92 lines
4.7 KiB
Rust
92 lines
4.7 KiB
Rust
//! Integration tests of craft-design-lint: rules.
|
|
|
|
#![allow(clippy::unwrap_used, reason = "tests")]
|
|
|
|
use craft_design_lint::{Finding, analyze_source};
|
|
|
|
fn run(src: &str) -> Vec<Finding> {
|
|
analyze_source(src, false).findings
|
|
}
|
|
|
|
fn rules(src: &str) -> Vec<&'static str> {
|
|
run(src).iter().map(|f| f.rule).collect()
|
|
}
|
|
|
|
fn flagged(src: &str, rule: &str) -> bool {
|
|
rules(src) == [rule]
|
|
}
|
|
|
|
fn clean(src: &str) -> bool {
|
|
let findings = run(src);
|
|
findings.is_empty() || panic!("unexpected findings: {findings:#?}")
|
|
}
|
|
|
|
#[test]
|
|
fn literal_shapes() {
|
|
for arg in ["-4.0", "(4.0)", "4 as f32", "4.0_f32", "2.0 * 3.0", "-(1.0 + 2.0)", "8"] {
|
|
let src = format!("fn f(ui: &mut Ui) {{ ui.add_space({arg}); }}");
|
|
assert!(flagged(&src, "literal-size/add-space"), "{arg}");
|
|
}
|
|
for arg in ["0.0", "-0.0", "0", "0.0 * 5.0", "gap", "self.gap", "tokens.space.sm", "pad()", "w / 2.0"] {
|
|
let src = format!("fn f(ui: &mut Ui) {{ ui.add_space({arg}); }}");
|
|
assert!(clean(&src), "{arg}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn unit_direction_vectors_are_not_sizes() {
|
|
for call in ["vec2(0.0, -1.0)", "Vec2::new(1., 0.)", "DocVec::new(-1.0, 0.0)", "Vec2::new(1, 0)"] {
|
|
assert!(clean(&format!("fn f() {{ let v = {call}; }}")), "{call}");
|
|
}
|
|
for call in ["vec2(0.0, 5.0)", "Vec2::new(1.0, 2.0)", "vec2(0.5, 0.0)"] {
|
|
assert!(flagged(&format!("fn f() {{ let v = {call}; }}"), "literal-size/vec2"), "{call}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn consts_resolve_by_module_and_impl_scope() {
|
|
// A module-level const of the same name in another module is a different const.
|
|
let src = "mod a { const GAP: f32 = 0.0; fn f(ui: &mut Ui) { ui.add_space(GAP); } }\nmod b { const GAP: f32 = 4.0; fn g(ui: &mut Ui) { ui.add_space(GAP); } }\n";
|
|
let found = run(src);
|
|
assert_eq!(found.iter().map(|f| f.line).collect::<Vec<_>>(), [2]);
|
|
|
|
// Shadowing: the innermost scope wins, so a zero outer const does not hide a non-zero inner one.
|
|
let src =
|
|
"const GAP: f32 = 0.0;\nmod m { const GAP: f32 = 4.0; fn f(ui: &mut Ui) { ui.add_space(GAP); } }\nfn g(ui: &mut Ui) { ui.add_space(GAP); }\n";
|
|
assert_eq!(run(src).iter().map(|f| f.line).collect::<Vec<_>>(), [2]);
|
|
|
|
// Self::X and Type::X name the impl's associated const; the message shows the path as written.
|
|
let src = "struct A; struct B;\nimpl A { const PAD: f32 = 6.0; fn f(&self, ui: &mut Ui) { ui.add_space(Self::PAD); } }\nimpl B { const PAD: f32 = 0.0; fn f(&self, ui: &mut Ui) { ui.add_space(Self::PAD); ui.add_space(A::PAD); } }\n";
|
|
let found = run(src);
|
|
assert_eq!(found.iter().map(|f| f.line).collect::<Vec<_>>(), [2, 3]);
|
|
assert!(found[0].message.contains("`Self::PAD` (const, line 2)"), "{}", found[0].message);
|
|
assert!(found[1].message.contains("`A::PAD`"), "{}", found[1].message);
|
|
|
|
// mod::X, self::X, super::X and crate::X.
|
|
let src = "const TOP: f32 = 1.0;\nmod sizes { pub const BIG: f32 = 9.0; }\nmod ui { fn f(ui: &mut Ui) { ui.add_space(super::TOP); ui.add_space(crate::TOP); ui.add_space(crate::sizes::BIG); ui.add_space(self::NONE); } }\nconst NONE: f32 = 0.0;\n";
|
|
assert_eq!(run(src).len(), 3);
|
|
}
|
|
|
|
#[test]
|
|
fn documented_consts_are_component_constants() {
|
|
// A documented module or impl const is a named value, its initializer included; arithmetic on it reads like a token.
|
|
let src = "/// The tick.\nconst TICK: f32 = 3.0;\n/// The room.\nconst ROOM: Vec2 = vec2(30.0, 40.0);\nstruct A;\nimpl A {\n /// The pad.\n const PAD: f32 = 6.0;\n fn f(&self, ui: &mut Ui) { ui.add_space(Self::PAD); ui.add_space(TICK * 2.0); let v = vec2(TICK, ROOM.y); }\n}\n";
|
|
assert!(clean(src));
|
|
|
|
// An undocumented const, a documented const in a fn body and a literal stay flagged.
|
|
let src = "const GAP: f32 = 4.0;\nfn f(ui: &mut Ui) { ui.add_space(GAP); }\nfn g(ui: &mut Ui) {\n /// The gap.\n const LOCAL: f32 = 4.0;\n ui.add_space(LOCAL);\n}\nfn h(ui: &mut Ui) { ui.add_space(4.0); }\n";
|
|
assert_eq!(run(src).iter().map(|f| f.line).collect::<Vec<_>>(), [2, 6, 8]);
|
|
|
|
// The initializer of an undocumented const is a call site.
|
|
assert!(flagged("const ROOM: Vec2 = vec2(30.0, 40.0);\n", "literal-size/vec2"));
|
|
}
|
|
|
|
#[test]
|
|
fn clippy_allow_attributes_are_counted_and_inner_ones_flagged() {
|
|
let src = "#![allow(clippy::disallowed_methods, reason = \"stock widgets\")]\n#[expect(clippy::disallowed_types)]\nstruct S;\n#[allow(clippy::too_many_arguments)]\nfn f() {}\nmod m {\n #![expect(clippy::disallowed_methods)]\n}\n";
|
|
let analysis = analyze_source(src, false);
|
|
assert_eq!(analysis.clippy_allows, 3);
|
|
assert_eq!(analysis.findings.iter().map(|f| (f.rule, f.line)).collect::<Vec<_>>(), [("clippy/inner-allow", 1), ("clippy/inner-allow", 7)]);
|
|
assert!(analysis.findings[0].message.contains("[[exempt]]"), "{}", analysis.findings[0].message);
|
|
}
|