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

1"""Halstead integration tests for TypeScript."""

2

3 from analyzers.typescript import analyze_source

4

5

6 def test_halstead_basic():

7 """Basic TypeScript function produces Halstead metrics."""

8 code = """

9 function add(a, b) {

10 return a + b;

11}

12"""

13 result = analyze_source(code)

14 h = result.functions[0].halstead

15 assert h is not None

16 assert h.N1 > 0

17 assert h.N2 > 0

18 assert h.volume > 0

19

20

21 def test_halstead_strict_equality():

22 """=== and !== are counted as operators."""

23 code = """

24 function f() {

25 const x = a === b;

26 const y = c !== d;

27}

28"""

29 result = analyze_source(code)

30 h = result.functions[0].halstead

31 assert h.n1 >= 2 # at least === and !==

32

33

34 def test_halstead_optional_chaining_and_nullish():

35 """?. and ?? are counted as operators."""

36 code = """

37 function f() {

38 const x = a?.b ?? c;

39}

40"""

41 result = analyze_source(code)

42 h = result.functions[0].halstead

43 assert h.N1 > 0

44 assert h.volume > 0

45

46

47 def test_halstead_empty_source():

48 """Empty TypeScript source has no functions."""

49 result = analyze_source("")

50 assert len(result.functions) == 0