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

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

2

3 from analyzers.python import analyze_source

4

5

6 def test_halstead_simple_function():

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

8 code = """

9 def 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

21 def test_halstead_empty_source():

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

23 result = analyze_source("")

24 assert len(result.functions) == 0

25

26

27 def test_halstead_operators_counted():

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

29 code = """

30 def 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

40 def 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