Coverage for tests/test_edge_case_coverage.py: 100%
55 statements
« prev ^ index » next coverage.py v7.10.4, created at 2025-08-22 16:03 -0700
« prev ^ index » next coverage.py v7.10.4, created at 2025-08-22 16:03 -0700
1import pytest
2from fguess import analyze_number_format, split_numeric_literals, get_test_value
5class TestEdgeCaseCoverage:
6 """Test edge cases to achieve 100% branch coverage."""
8 def test_padding_with_space_fill_no_align(self):
9 """Test case where we have padding but fill_char is space and no explicit align.
11 This should make line 490 condition `if align or fill_char != ' ':` evaluate to False.
12 """
13 # This is tricky because detect_padding returns align based on padding type
14 # Let's look for a case where we might have space padding but no alignment
16 # Try a case where we have space padding but it gets detected as no alignment
17 # This might happen in edge cases of the padding detection logic
18 formats = analyze_number_format(" ") # Only padding characters
19 # This should return no formats, but let's verify the internal logic
20 assert not formats # Should be empty
22 # Try a case with mixed spaces that might not get detected as alignment
23 formats = analyze_number_format(" 42 ") # Unequal spaces - should be literals
24 # This should trigger the literal path, not the alignment path
25 string_formats = [f for t, f in formats if t == 'str']
26 assert any(' {variable} ' in f for f in string_formats)
28 def test_unprefixed_hex_padding_edge_case(self):
29 """Test unprefixed hex with padding on exactly one side.
31 This should make line 161 condition evaluate to False.
32 The condition is: not ((left_pad and not right_pad) or (right_pad and not left_pad))
33 It's False when we have padding on exactly one side.
34 """
35 # Test case: unprefixed hex with padding only on left
36 # This should make the condition False and skip the return
37 formats = analyze_number_format(" ff") # Space + hex
38 # Should not treat as unprefixed hex due to left padding only
39 hex_formats = [f for t, f in formats if 'x' in f or 'X' in f]
40 # Might have hex format, but should be treated differently due to padding
42 def test_unprefixed_hex_padding_both_sides(self):
43 """Test unprefixed hex with padding on both sides or neither side.
45 This should make line 161 condition evaluate to True.
46 """
47 # Test case: unprefixed hex with no padding - should be treated as hex
48 formats = analyze_number_format("ff")
49 hex_formats = [f for t, f in formats if ':x}' in f]
50 assert len(hex_formats) > 0 # Should detect as hex
52 # Test case: unprefixed hex with padding on both sides - should be treated as hex
53 formats = analyze_number_format(" ff ")
54 # This should be treated as center-aligned hex or literals depending on implementation
56 def test_get_test_value_float_empty_clean(self):
57 """Test get_test_value for float type where clean becomes empty.
59 This should make line 577 condition `if clean and NUMBER_RE.fullmatch(clean):` evaluate to False.
60 """
61 # Create a case where after cleaning, we get an empty string
62 result = get_test_value("+", "float") # Just a plus sign
63 assert result == 0.0 # Should return default 0.0
65 # Test case where clean doesn't match number regex
66 result = get_test_value("abc", "float") # Non-numeric
67 assert result == 0.0 # Should return default 0.0
69 def test_get_test_value_float_percentage_empty_clean(self):
70 """Test get_test_value for percentage where clean becomes empty after removing %.
72 This should test the first line 577 condition in the percentage branch.
73 """
74 # Create a percentage case where after removing % we get empty
75 result = get_test_value("%", "float") # Just a percent sign
76 assert result == 0.0 # Should return default 0.0
78 # Test case where percentage clean doesn't match number regex
79 result = get_test_value("abc%", "float") # Non-numeric percentage
80 assert result == 0.0 # Should return default 0.0
82 def test_get_test_value_float_non_matching_clean(self):
83 """Test get_test_value for float where clean doesn't match NUMBER_RE.
85 This tests the second line 580 condition.
86 """
87 # Test with something that becomes non-numeric after cleaning
88 result = get_test_value("$$$", "float") # Multiple dollar signs, no numbers
89 assert result == 0.0
91 # Test with text that can't be split into numeric literals
92 result = get_test_value("hello", "float") # Pure text
93 assert result == 0.0
95 def test_split_numeric_literals_edge_cases(self):
96 """Test edge cases in split_numeric_literals that might affect coverage."""
97 from fguess import NumberParts
99 # Test unprefixed hex with different padding scenarios
100 result = split_numeric_literals(" af") # Left pad only
101 # Should not be treated as unprefixed hex due to asymmetric padding
103 result = split_numeric_literals("af ") # Right pad only
104 # Should not be treated as unprefixed hex due to asymmetric padding
106 result = split_numeric_literals(" af ") # Both sides
107 # Might be treated as unprefixed hex with padding
109 def test_padding_detection_edge_cases(self):
110 """Test edge cases in padding detection that affect alignment logic."""
111 # Test case where padding exists but might not create alignment
112 formats = analyze_number_format("42 ") # Right padding only
114 # Should create left alignment
115 left_align_formats = [f for t, f in formats if '<' in f]
116 assert len(left_align_formats) > 0
118 # Test case with very specific padding that might trigger edge cases
119 formats = analyze_number_format(" 42") # Left padding only
121 # Should create right alignment
122 right_align_formats = [f for t, f in formats if '>' in f]
123 assert len(right_align_formats) > 0
125 def test_complex_padding_scenarios(self):
126 """Test complex padding scenarios to hit edge cases."""
127 # Test case that might have different fill characters
128 formats = analyze_number_format("___42___") # Underscore padding both sides
130 # Should detect center alignment with underscore fill
131 center_formats = [f for t, f in formats if '^' in f and '_' in f]
132 assert len(center_formats) > 0
134 # Test mixed padding characters (should be treated as literals)
135 formats = analyze_number_format(" _42_ ") # Mixed padding
136 # Should be treated as literals, not alignment
137 literal_formats = [f for t, f in formats if 'str' == t]
138 assert len(literal_formats) > 0
140 def test_hex_detection_with_various_padding(self):
141 """Test hex detection with various padding to trigger different branches."""
142 # Test different hex padding scenarios
143 test_cases = [
144 " af", # Left pad only - should not be treated as centered hex
145 "af ", # Right pad only - should not be treated as centered hex
146 " af ", # Both sides equal - might be treated as centered hex
147 " af", # Different left padding
148 "af ", # Different right padding
149 ]
151 for test_case in test_cases:
152 formats = analyze_number_format(test_case)
153 # Each should be handled appropriately based on padding pattern