#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod visitor_tests {
use super::*;
use crate::models::unified_ast::NodeFlags;
fn create_if_statement() -> UnifiedAstNode {
UnifiedAstNode {
kind: AstKind::Statement(StmtKind::If),
lang: crate::models::unified_ast::Language::Rust,
flags: NodeFlags::default(),
parent: 0,
first_child: 0,
next_sibling: 0,
source_range: 0..50,
semantic_hash: 0,
structural_hash: 0,
name_vector: 0,
metadata: crate::models::unified_ast::NodeMetadata::default(),
proof_annotations: None,
}
}
fn create_while_loop() -> UnifiedAstNode {
UnifiedAstNode {
kind: AstKind::Statement(StmtKind::While),
lang: crate::models::unified_ast::Language::Rust,
flags: NodeFlags::default(),
parent: 0,
first_child: 0,
next_sibling: 0,
source_range: 0..50,
semantic_hash: 0,
structural_hash: 0,
name_vector: 0,
metadata: crate::models::unified_ast::NodeMetadata::default(),
proof_annotations: None,
}
}
#[test]
fn test_visit_cyclomatic_if() {
let analyzer = VerifiedComplexityAnalyzer::new();
let if_stmt = create_if_statement();
let mut complexity = 0;
analyzer.visit_cyclomatic(&if_stmt, &mut complexity);
assert_eq!(complexity, 1);
}
#[test]
fn test_visit_cyclomatic_while() {
let analyzer = VerifiedComplexityAnalyzer::new();
let while_loop = create_while_loop();
let mut complexity = 0;
analyzer.visit_cyclomatic(&while_loop, &mut complexity);
assert_eq!(complexity, 1);
}
#[test]
fn test_visit_cyclomatic_for() {
let analyzer = VerifiedComplexityAnalyzer::new();
let for_loop = UnifiedAstNode {
kind: AstKind::Statement(StmtKind::For),
lang: crate::models::unified_ast::Language::Rust,
flags: NodeFlags::default(),
parent: 0,
first_child: 0,
next_sibling: 0,
source_range: 0..50,
semantic_hash: 0,
structural_hash: 0,
name_vector: 0,
metadata: crate::models::unified_ast::NodeMetadata::default(),
proof_annotations: None,
};
let mut complexity = 0;
analyzer.visit_cyclomatic(&for_loop, &mut complexity);
assert_eq!(complexity, 1);
}
#[test]
fn test_visit_cyclomatic_switch() {
let analyzer = VerifiedComplexityAnalyzer::new();
let switch_stmt = UnifiedAstNode {
kind: AstKind::Statement(StmtKind::Switch),
lang: crate::models::unified_ast::Language::Rust,
flags: NodeFlags::default(),
parent: 0,
first_child: 0,
next_sibling: 0,
source_range: 0..50,
semantic_hash: 0,
structural_hash: 0,
name_vector: 0,
metadata: crate::models::unified_ast::NodeMetadata::default(),
proof_annotations: None,
};
let mut complexity = 0;
analyzer.visit_cyclomatic(&switch_stmt, &mut complexity);
assert_eq!(complexity, 1);
}
#[test]
fn test_visit_cyclomatic_try() {
let analyzer = VerifiedComplexityAnalyzer::new();
let try_stmt = UnifiedAstNode {
kind: AstKind::Statement(StmtKind::Try),
lang: crate::models::unified_ast::Language::Rust,
flags: NodeFlags::default(),
parent: 0,
first_child: 0,
next_sibling: 0,
source_range: 0..50,
semantic_hash: 0,
structural_hash: 0,
name_vector: 0,
metadata: crate::models::unified_ast::NodeMetadata::default(),
proof_annotations: None,
};
let mut complexity = 0;
analyzer.visit_cyclomatic(&try_stmt, &mut complexity);
assert_eq!(complexity, 1);
}
#[test]
fn test_visit_cyclomatic_binary_expr() {
let analyzer = VerifiedComplexityAnalyzer::new();
let binary_expr = UnifiedAstNode {
kind: AstKind::Expression(ExprKind::Binary),
lang: crate::models::unified_ast::Language::Rust,
flags: NodeFlags::default(),
parent: 0,
first_child: 0,
next_sibling: 0,
source_range: 0..20,
semantic_hash: 0,
structural_hash: 0,
name_vector: 0,
metadata: crate::models::unified_ast::NodeMetadata::default(),
proof_annotations: None,
};
let mut complexity = 0;
analyzer.visit_cyclomatic(&binary_expr, &mut complexity);
assert_eq!(complexity, 1);
}
#[test]
fn test_compute_cognitive_if() {
let mut analyzer = VerifiedComplexityAnalyzer::new();
let if_stmt = create_if_statement();
let weight = analyzer.compute_cognitive_weight(&if_stmt);
assert!(weight >= 1);
}
#[test]
fn test_compute_cognitive_while() {
let mut analyzer = VerifiedComplexityAnalyzer::new();
let while_loop = create_while_loop();
let weight = analyzer.compute_cognitive_weight(&while_loop);
assert!(weight >= 1);
}
#[test]
fn test_compute_cognitive_nested() {
let mut analyzer = VerifiedComplexityAnalyzer::new();
analyzer.nesting_level = 2;
let if_stmt = create_if_statement();
let weight = analyzer.compute_cognitive_weight(&if_stmt);
assert_eq!(weight, 3);
}
#[test]
fn test_collect_halstead_tokens() {
let analyzer = VerifiedComplexityAnalyzer::new();
let mut operators = HashMap::new();
let mut operands = HashMap::new();
let binary = UnifiedAstNode {
kind: AstKind::Expression(ExprKind::Binary),
lang: crate::models::unified_ast::Language::Rust,
flags: NodeFlags::default(),
parent: 0,
first_child: 0,
next_sibling: 0,
source_range: 0..10,
semantic_hash: 0,
structural_hash: 0,
name_vector: 0,
metadata: crate::models::unified_ast::NodeMetadata::default(),
proof_annotations: None,
};
analyzer.collect_halstead_tokens(&binary, &mut operators, &mut operands);
assert_eq!(operators.get("binary_op"), Some(&1));
}
#[test]
fn test_collect_halstead_unary() {
let analyzer = VerifiedComplexityAnalyzer::new();
let mut operators = HashMap::new();
let mut operands = HashMap::new();
let unary = UnifiedAstNode {
kind: AstKind::Expression(ExprKind::Unary),
lang: crate::models::unified_ast::Language::Rust,
flags: NodeFlags::default(),
parent: 0,
first_child: 0,
next_sibling: 0,
source_range: 0..10,
semantic_hash: 0,
structural_hash: 0,
name_vector: 0,
metadata: crate::models::unified_ast::NodeMetadata::default(),
proof_annotations: None,
};
analyzer.collect_halstead_tokens(&unary, &mut operators, &mut operands);
assert_eq!(operators.get("unary_op"), Some(&1));
}
#[test]
fn test_collect_halstead_identifier() {
let analyzer = VerifiedComplexityAnalyzer::new();
let mut operators = HashMap::new();
let mut operands = HashMap::new();
let identifier = UnifiedAstNode {
kind: AstKind::Expression(ExprKind::Identifier),
lang: crate::models::unified_ast::Language::Rust,
flags: NodeFlags::default(),
parent: 0,
first_child: 0,
next_sibling: 0,
source_range: 0..10,
semantic_hash: 0,
structural_hash: 0,
name_vector: 0,
metadata: crate::models::unified_ast::NodeMetadata::default(),
proof_annotations: None,
};
analyzer.collect_halstead_tokens(&identifier, &mut operators, &mut operands);
assert_eq!(operands.get("identifier"), Some(&1));
}
#[test]
fn test_collect_halstead_literal() {
let analyzer = VerifiedComplexityAnalyzer::new();
let mut operators = HashMap::new();
let mut operands = HashMap::new();
let literal = UnifiedAstNode {
kind: AstKind::Expression(ExprKind::Literal),
lang: crate::models::unified_ast::Language::Rust,
flags: NodeFlags::default(),
parent: 0,
first_child: 0,
next_sibling: 0,
source_range: 0..10,
semantic_hash: 0,
structural_hash: 0,
name_vector: 0,
metadata: crate::models::unified_ast::NodeMetadata::default(),
proof_annotations: None,
};
analyzer.collect_halstead_tokens(&literal, &mut operators, &mut operands);
assert_eq!(operands.get("literal"), Some(&1));
}
#[test]
fn test_collect_halstead_call() {
let analyzer = VerifiedComplexityAnalyzer::new();
let mut operators = HashMap::new();
let mut operands = HashMap::new();
let call = UnifiedAstNode {
kind: AstKind::Expression(ExprKind::Call),
lang: crate::models::unified_ast::Language::Rust,
flags: NodeFlags::default(),
parent: 0,
first_child: 0,
next_sibling: 0,
source_range: 0..10,
semantic_hash: 0,
structural_hash: 0,
name_vector: 0,
metadata: crate::models::unified_ast::NodeMetadata::default(),
proof_annotations: None,
};
analyzer.collect_halstead_tokens(&call, &mut operators, &mut operands);
assert_eq!(operators.get("()"), Some(&1));
}
}
#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod property_tests {
use proptest::prelude::*;
proptest! {
#[test]
fn basic_property_stability(_input in ".*") {
prop_assert!(true);
}
#[test]
fn module_consistency_check(_x in 0u32..1000) {
prop_assert!(_x < 1001);
}
}
}