Coverage for tests / test_halstead / test_python.py: 100%

25 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-08 15:04 -0800

1"""Halstead integration tests for Python.""" 

2 

3from analyzers.python import analyze_source 

4 

5 

6def test_halstead_simple_function(): 

7 """Simple function produces non-zero Halstead metrics.""" 

8 code = """ 

9def add(a, b): 

10 return a + b 

11""" 

12 result = analyze_source(code) 

13 h = result.functions[0].halstead 

14 assert h is not None 

15 assert h.N1 > 0 

16 assert h.N2 > 0 

17 assert h.vocabulary > 0 

18 assert h.volume > 0 

19 

20 

21def test_halstead_empty_source(): 

22 """Empty source has no functions.""" 

23 result = analyze_source("") 

24 assert len(result.functions) == 0 

25 

26 

27def test_halstead_operators_counted(): 

28 """Operators like +, -, = are counted.""" 

29 code = """ 

30def f(): 

31 x = 1 + 2 

32 y = x - 3 

33""" 

34 result = analyze_source(code) 

35 h = result.functions[0].halstead 

36 assert h.n1 >= 2 # at least = and +, - 

37 assert h.N1 >= 4 # at least two = and one + and one - 

38 

39 

40def test_halstead_volume_scales_with_code(): 

41 """More code should produce higher volume.""" 

42 small = "def f():\n x = 1\n" 

43 big = "def g():\n x = 1\n y = 2\n z = x + y\n w = z * x\n" 

44 h_small = analyze_source(small).functions[0].halstead 

45 h_big = analyze_source(big).functions[0].halstead 

46 assert h_big.volume > h_small.volume