Coverage for tests/test_specific_branches.py: 89%

68 statements  

« prev     ^ index     » next       coverage.py v7.10.4, created at 2025-08-22 16:03 -0700

1import pytest 

2from fguess import analyze_number_format, get_test_value 

3 

4 

5class TestSpecificBranches: 

6 """Test specific branches to achieve 100% coverage.""" 

7 

8 def test_space_fill_with_no_alignment_condition(self): 

9 """Test the condition on line 490: if align or fill_char != ' ': 

10  

11 We need to find a case where we have padding but align='' and fill_char=' '. 

12 This happens when detect_padding finds space padding but doesn't set alignment. 

13 """ 

14 # Looking at detect_padding function, it should return align='' and fill_char=' '  

15 # when there are only space characters that don't create alignment 

16 

17 # Try to create a scenario where we have spaces but no alignment is detected 

18 # This might happen in very specific edge cases of the padding detection 

19 

20 # Let's check what happens with just spaces 

21 from fguess import detect_padding 

22 result = detect_padding(" ", " _*") # Only spaces 

23 core, left_pad, right_pad, char = result 

24 print(f"detect_padding(' '): core={repr(core)}, left={repr(left_pad)}, right={repr(right_pad)}, char={repr(char)}") 

25 

26 # The condition should be false when align='' and fill_char==' ' 

27 # This might not be reachable in normal flow, indicating dead code 

28 

29 def test_unprefixed_hex_single_side_padding(self): 

30 """Test line 161: if not ((left_pad and not right_pad) or (right_pad and not left_pad)): 

31  

32 This condition is False when we have padding on exactly one side. 

33 """ 

34 # Test unprefixed hex with left padding only 

35 formats = analyze_number_format(" af") 

36 # With left padding only, the condition should be False and we skip the return 

37 

38 # Test unprefixed hex with right padding only  

39 formats = analyze_number_format("af ") 

40 # With right padding only, the condition should be False and we skip the return 

41 

42 # Verify these don't get treated as simple unprefixed hex 

43 hex_formats = [f for t, f in formats if ':x}' in f and 'af' in f] 

44 # Should have different behavior due to asymmetric padding 

45 

46 def test_get_test_value_edge_case_clean_empty(self): 

47 """Test line 577/580: if clean and NUMBER_RE.fullmatch(clean): 

48  

49 Need to find cases where clean becomes empty or doesn't match NUMBER_RE. 

50 """ 

51 # Case 1: Percentage where removing % leaves empty string 

52 result = get_test_value("%", "float") 

53 assert result == 0.0 

54 

55 # Case 2: Input that becomes empty after cleaning  

56 result = get_test_value("+", "float") # Just plus sign 

57 assert result == 0.0 

58 

59 # Case 3: Input that doesn't match NUMBER_RE after cleaning 

60 result = get_test_value("abc", "float") # Pure text 

61 assert result == 0.0 

62 

63 # Case 4: Percentage with non-numeric content 

64 result = get_test_value("abc%", "float") 

65 assert result == 0.0 

66 

67 def test_manual_branch_checking(self): 

68 """Manually test the specific conditions mentioned.""" 

69 

70 # Test condition: align or fill_char != ' ' 

71 # This is in the context of padding detection 

72 from fguess import detect_padding 

73 

74 # Check various padding scenarios 

75 test_cases = [ 

76 " ", # Only spaces 

77 "_", # Single underscore  

78 "*", # Single asterisk 

79 " 42 ", # Spaces around number 

80 "__42__", # Underscores around number 

81 ] 

82 

83 for case in test_cases: 

84 result = detect_padding(case) 

85 core, left_pad, right_pad, fill_char = result 

86 

87 # The problematic condition is evaluated when left_pad or right_pad exists 

88 if left_pad or right_pad: 

89 # Determine alignment 

90 if left_pad and right_pad: 90 ↛ 92line 90 didn't jump to line 92 because the condition on line 90 was always true

91 align = '^' if len(left_pad) == len(right_pad) else '' 

92 elif left_pad: 

93 align = '>' 

94 elif right_pad: 

95 align = '<' 

96 else: 

97 align = '' 

98 

99 # This is the condition we're testing 

100 condition_result = align or fill_char != ' ' 

101 print(f"Case: {repr(case)}, align: {repr(align)}, fill_char: {repr(fill_char)}, condition: {condition_result}") 

102 

103 def test_literal_detection_edge_cases(self): 

104 """Test edge cases in literal detection that might trigger uncovered branches.""" 

105 

106 # Test cases that might trigger different code paths 

107 test_cases = [ 

108 " af", # Unprefixed hex with left padding only 

109 "af ", # Unprefixed hex with right padding only  

110 " af ", # Unprefixed hex with equal padding 

111 " af", # Unprefixed hex with different left padding 

112 "af ", # Unprefixed hex with different right padding 

113 ] 

114 

115 for case in test_cases: 

116 formats = analyze_number_format(case) 

117 print(f"Case {repr(case)}: {len(formats)} formats") 

118 for t, f in formats: 

119 print(f" {t}: {f}") 

120 

121 def test_get_test_value_percentage_edge_cases(self): 

122 """Test specific edge cases in percentage handling in get_test_value.""" 

123 

124 # Test percentage cases that might not match NUMBER_RE 

125 test_cases = [ 

126 "%", # Just percent 

127 "abc%", # Non-numeric with percent 

128 "+%", # Plus and percent 

129 ".%", # Just decimal point and percent 

130 ] 

131 

132 for case in test_cases: 

133 result = get_test_value(case, "float") 

134 print(f"get_test_value({repr(case)}, 'float') = {result}") 

135 assert result == 0.0 # Should return default 

136 

137 def test_number_regex_edge_cases(self): 

138 """Test edge cases for NUMBER_RE matching.""" 

139 

140 from fguess import NUMBER_RE 

141 

142 # Test strings that might not match NUMBER_RE 

143 test_cases = [ 

144 "", # Empty string 

145 ".", # Just decimal point 

146 "+", # Just plus 

147 "-", # Just minus 

148 "abc", # Pure text 

149 "1.2.3", # Multiple decimal points 

150 ] 

151 

152 for case in test_cases: 

153 match = NUMBER_RE.fullmatch(case) 

154 print(f"NUMBER_RE.fullmatch({repr(case)}) = {match}") 

155 

156 # Test in get_test_value context 

157 result = get_test_value(case, "float") 

158 print(f"get_test_value({repr(case)}, 'float') = {result}") 

159 

160 def test_unprefixed_hex_padding_combinations(self): 

161 """Test all combinations of padding for unprefixed hex to hit the branch.""" 

162 

163 from fguess import split_numeric_literals 

164 

165 # Test the specific condition: not ((left_pad and not right_pad) or (right_pad and not left_pad)) 

166 # This is False when: (left_pad and not right_pad) or (right_pad and not left_pad) 

167 # i.e., when padding is on exactly one side 

168 

169 test_cases = [ 

170 " af", # left_pad=True, right_pad=False -> condition should be False 

171 "af ", # left_pad=False, right_pad=True -> condition should be False  

172 " af ", # left_pad=True, right_pad=True -> condition should be True 

173 "af", # left_pad=False, right_pad=False -> condition should be True 

174 ] 

175 

176 for case in test_cases: 

177 # First check if it matches unprefixed hex pattern 

178 from fguess import UNPREFIXED_HEX_RE 

179 if UNPREFIXED_HEX_RE.fullmatch(case): 

180 result = split_numeric_literals(case) 

181 print(f"split_numeric_literals({repr(case)}) = {result}") 

182 

183 formats = analyze_number_format(case) 

184 print(f"analyze_number_format({repr(case)}) has {len(formats)} formats")