From 9a8b7ce79ed701f4b213351df7acbb2d2b76ce23 Mon Sep 17 00:00:00 2001 From: Sephyi Date: Thu, 30 Apr 2026 18:52:57 +0200 Subject: [PATCH] test(audit): parametrise make_symbol helper by line range (D-040) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Originally PR #9; rebased onto current development. Adds shared `make_symbol` and `make_symbol_at` helpers in tests/helpers.rs and collapses the per-file copies in tests/splitter.rs and tests/context.rs. Conflict resolution: kept `make_renamed_file_with_diff` (added by D-037 / #7) — PR #9's branch was based on a pre-#7 state that incidentally removed it. Only the make_symbol parametrisation is in scope here. Closes #9. --- tests/context.rs | 53 +++++++++++++++++++++++++++-------------------- tests/helpers.rs | 50 +++++++++++++++++++++++++++++++++++++++++++- tests/splitter.rs | 27 ++---------------------- 3 files changed, 81 insertions(+), 49 deletions(-) diff --git a/tests/context.rs b/tests/context.rs index ec7cd2f..f5b92f3 100644 --- a/tests/context.rs +++ b/tests/context.rs @@ -12,7 +12,9 @@ use commitbee::domain::{ ChangeStatus, CodeSymbol, CommitType, FileCategory, IntentKind, SymbolKind, }; use commitbee::services::context::ContextBuilder; -use helpers::{make_file_change, make_renamed_file, make_staged_changes}; +use helpers::{ + make_file_change, make_renamed_file, make_staged_changes, make_symbol, make_symbol_at, +}; // ─── Helpers ───────────────────────────────────────────────────────────────── @@ -20,28 +22,6 @@ fn default_config() -> Config { Config::default() } -fn make_symbol( - name: &str, - kind: SymbolKind, - file: &str, - is_public: bool, - is_added: bool, -) -> CodeSymbol { - CodeSymbol { - kind, - name: name.to_string(), - file: PathBuf::from(file), - line: 1, - end_line: 10, - is_public, - is_added, - is_whitespace_only: None, - span_change_kind: None, - signature: None, - parent_scope: None, - } -} - // ─── CommitType inference ───────────────────────────────────────────────────── #[test] @@ -2193,3 +2173,30 @@ fn intents_capped_at_three() { ctx.intents.len() ); } + +// ─── make_symbol helper smoke tests ────────────────────────────────────────── + +#[test] +fn make_symbol_defaults_to_lines_1_through_10() { + let sym = make_symbol("foo", SymbolKind::Function, "src/lib.rs", true, true); + assert_eq!(sym.line, 1); + assert_eq!(sym.end_line, 10); +} + +#[test] +fn make_symbol_at_pins_arbitrary_line_range() { + let sym = make_symbol_at( + "bar", + SymbolKind::Function, + "src/lib.rs", + false, + true, + 42, + 57, + ); + assert_eq!(sym.line, 42); + assert_eq!(sym.end_line, 57); + assert_eq!(sym.name, "bar"); + assert!(!sym.is_public); + assert!(sym.is_added); +} diff --git a/tests/helpers.rs b/tests/helpers.rs index 5663b75..fb88be3 100644 --- a/tests/helpers.rs +++ b/tests/helpers.rs @@ -5,7 +5,9 @@ use std::path::PathBuf; use std::sync::Arc; -use commitbee::domain::{ChangeStatus, DiffStats, FileCategory, FileChange, StagedChanges}; +use commitbee::domain::{ + ChangeStatus, CodeSymbol, DiffStats, FileCategory, FileChange, StagedChanges, SymbolKind, +}; /// Create a minimal FileChange for testing #[allow(dead_code)] @@ -60,6 +62,52 @@ pub fn make_renamed_file_with_diff( } } +/// Create a minimal `CodeSymbol` for testing, with `line: 1, end_line: 10`. +/// +/// For tests that need the symbol to sit at a specific line range (e.g. to +/// exercise hunk-to-span mapping), use [`make_symbol_at`] instead. +#[allow(dead_code)] +pub fn make_symbol( + name: &str, + kind: SymbolKind, + file: &str, + is_public: bool, + is_added: bool, +) -> CodeSymbol { + make_symbol_at(name, kind, file, is_public, is_added, 1, 10) +} + +/// Create a minimal `CodeSymbol` at an arbitrary line range. +/// +/// Prefer this variant when a test needs to pin the symbol to specific +/// `line` / `end_line` positions (for example, to line up with a manually +/// crafted diff hunk). For the common case where positions are irrelevant, +/// [`make_symbol`] uses the defaults `line: 1, end_line: 10`. +#[allow(dead_code)] +pub fn make_symbol_at( + name: &str, + kind: SymbolKind, + file: &str, + is_public: bool, + is_added: bool, + line: usize, + end_line: usize, +) -> CodeSymbol { + CodeSymbol { + kind, + name: name.to_string(), + file: PathBuf::from(file), + line, + end_line, + is_public, + is_added, + is_whitespace_only: None, + span_change_kind: None, + signature: None, + parent_scope: None, + } +} + /// Create StagedChanges from a list of FileChanges #[allow(dead_code)] pub fn make_staged_changes(files: Vec) -> StagedChanges { diff --git a/tests/splitter.rs b/tests/splitter.rs index 1cc61ea..ac7e4ac 100644 --- a/tests/splitter.rs +++ b/tests/splitter.rs @@ -6,36 +6,13 @@ mod helpers; use std::path::PathBuf; -use commitbee::domain::{ChangeStatus, CodeSymbol, SymbolKind}; +use commitbee::domain::{ChangeStatus, SymbolKind}; use commitbee::services::splitter::{CommitSplitter, SplitSuggestion}; use helpers::{ make_file_change, make_renamed_file, make_renamed_file_with_diff, make_staged_changes, + make_symbol, }; -// ─── Helpers ───────────────────────────────────────────────────────────────── - -fn make_symbol( - name: &str, - kind: SymbolKind, - file: &str, - is_public: bool, - is_added: bool, -) -> CodeSymbol { - CodeSymbol { - kind, - name: name.to_string(), - file: PathBuf::from(file), - line: 1, - end_line: 10, - is_public, - is_added, - is_whitespace_only: None, - span_change_kind: None, - signature: None, - parent_scope: None, - } -} - // ─── Split detection tests ─────────────────────────────────────────────────── #[test]