Coverage for tests/test_mi/test_go.py: 100%

1"""Tests for Maintainability Index (Go)."""

2

3 from analyzers.go import analyze_source

4

5

6 def _mi(code: str) -> float:

7 """Return MI of the first function in code."""

8 result = analyze_source(code)

9 assert result.functions, f"No functions found in:\n{code}"

10 return result.functions[0].mi

11

12

13# --- Trivial function (high MI) ---

14

15 def test_trivial_function():

16 code = """

17 package main

18

19 func f() int {

20 return 1

21}

22"""

23 mi = _mi(code)

24 assert mi > 50, f"Trivial function should have high MI, got {mi}"

25

26

27 def test_simple_add():

28 code = """

29 package main

30

31 func add(a int, b int) int {

32 return a + b

33}

34"""

35 mi = _mi(code)

36 assert mi > 50, f"Simple function should have high MI, got {mi}"

37

38

39# --- Complex function (low MI) ---

40

41 def test_complex_function():

42 code = """

43 package main

44

45 import "fmt"

46

47 func process(data []int, flag bool, mode bool, opt bool, extra bool) []int {

48 result := make([]int, 0)

49 total := 0

50 count := 0

51 for _, item := range data {

52 if flag && mode {

53 if item > 0 {

54 for _, sub := range data {

55 if sub < 100 {

56 if opt || extra {

57 result = append(result, sub*2+total)

58 count += 1

59 } else {

60 result = append(result, sub-1)

61 }

62 } else if sub > 200 {

63 total += sub

64 count -= 1

65 }

66 }

67 } else if item < 0 {

68 val := item

69 if val > 0 && val < 50 {

70 result = append(result, val)

71 } else if val >= 50 || val == 0 {

72 result = append(result, -val)

73 }

74 }

75 } else if mode || opt {

76 for _, sub := range data {

77 if sub != item {

78 result = append(result, sub+item)

79 total += sub

80 }

81 }

82 }

83 }

84 if count > 0 {

85 return result[:count]

86 }

87 fmt.Println(total)

88 return result

89}

90"""

91 mi = _mi(code)

92 assert mi < 50, f"Complex function should have low MI, got {mi}"

93

94

95# --- Edge case: empty body ---

96

97 def test_empty_body():

98 code = """

99 package main

100

101 func noop() {}

102"""

103 mi = _mi(code)

104 assert mi > 50, f"Empty body function should have high MI, got {mi}"

105

106

107# --- Edge case: no operators ---

108

109 def test_no_operators():

110 code = """

111 package main

112

113 func f() {

114 x := 1

115 _ = x

116}

117"""

118 mi = _mi(code)

119 assert mi > 50, f"No-operator function should have high MI, got {mi}"

120

121

122# --- MI is in valid range ---

123

124 def test_mi_range():

125 code = """

126 package main

127

128 func f(x bool) int {

129 if x {

130 return 1

131 }

132 return 0

133}

134"""

135 mi = _mi(code)

136 assert 0 <= mi <= 100, f"MI should be in [0, 100], got {mi}"

137

138

139# --- Multiple functions ---

140

141 def test_multiple_functions():

142 code = """

143 package main

144

145 func simple() int {

146 return 42

147}

148

149 func moderate(x int, y int) int {

150 if x > y {

151 return x

152 } else if x < y {

153 return y

154 } else {

155 return x + y

156 }

157}

158"""

159 result = analyze_source(code)

160 assert len(result.functions) == 2

161 mi_simple = result.functions[0].mi

162 mi_moderate = result.functions[1].mi

163 assert mi_simple > mi_moderate, f"Simple function should have higher MI: {mi_simple} vs {mi_moderate}"