Skip to content

Commit b2b64ad

Browse files
graycreateclaude
andauthored
fix: correct parsing of consecutive sign-in days (#113)
* fix: correct parsing of consecutive sign-in days Previously, the extractDigits() method extracted all digits from the string, causing incorrect display when the string contained user IDs or dates. Now uses regex to specifically extract the number before "天" (days). This fixes the issue where sign-in days displayed incorrectly as "21612902025091812天" instead of the actual consecutive days count. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * fix: improve test code based on Copilot suggestions - Use specific exception types instead of generic Exception - Throw RuntimeException instead of just printing stack trace to make test failures explicit - Improves test maintainability and failure debugging 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent c60f34c commit b2b64ad

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

app/src/main/java/me/ghui/v2er/network/bean/DailyInfo.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,22 @@ public boolean hadCheckedIn() {
2626
}
2727

2828
public String getCheckinDays() {
29+
if (Check.isEmpty(continuousLoginDayStr)) return "";
30+
31+
// Extract consecutive days from strings like:
32+
// "您已连续登录 123 天"
33+
// "ghui 已连续签到 12 天 2024/12/25"
34+
// "用户161290已连续签到 5 天"
35+
36+
// Use regex to find number followed by "天" (days)
37+
java.util.regex.Pattern pattern = java.util.regex.Pattern.compile("(\\d+)\\s*天");
38+
java.util.regex.Matcher matcher = pattern.matcher(continuousLoginDayStr);
39+
40+
if (matcher.find()) {
41+
return matcher.group(1);
42+
}
43+
44+
// Fallback to extracting all digits if pattern not found
2945
return Utils.extractDigits(continuousLoginDayStr);
3046
}
3147

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package me.ghui.v2er.network.bean;
2+
3+
import org.junit.Test;
4+
import static org.junit.Assert.*;
5+
6+
public class DailyInfoTest {
7+
8+
@Test
9+
public void testGetCheckinDays_withValidFormat() {
10+
// Test case 1: Standard format
11+
DailyInfo dailyInfo = new DailyInfo();
12+
dailyInfo.continuousLoginDayStr = "您已连续登录 123 天";
13+
assertEquals("123", dailyInfo.getCheckinDays());
14+
}
15+
16+
@Test
17+
public void testGetCheckinDays_withDateInString() {
18+
// Test case 2: String contains date and other numbers
19+
DailyInfo dailyInfo = new DailyInfo();
20+
// Simulate the actual problematic string that might appear
21+
dailyInfo.continuousLoginDayStr = "ghui 已连续签到 12 天 2024/12/25";
22+
String result = dailyInfo.getCheckinDays();
23+
// After fix, it should correctly extract "12"
24+
assertEquals("12", result);
25+
}
26+
27+
@Test
28+
public void testGetCheckinDays_withUserId() {
29+
// Test case 3: String contains user ID and days
30+
DailyInfo dailyInfo = new DailyInfo();
31+
dailyInfo.continuousLoginDayStr = "用户161290已连续签到 5 天";
32+
String result = dailyInfo.getCheckinDays();
33+
// After fix, it should correctly extract "5"
34+
assertEquals("5", result);
35+
}
36+
37+
@Test
38+
public void testGetCheckinDays_withNull() {
39+
DailyInfo dailyInfo = new DailyInfo();
40+
dailyInfo.continuousLoginDayStr = null;
41+
assertEquals("", dailyInfo.getCheckinDays());
42+
}
43+
44+
@Test
45+
public void testGetCheckinDays_withEmpty() {
46+
DailyInfo dailyInfo = new DailyInfo();
47+
dailyInfo.continuousLoginDayStr = "";
48+
assertEquals("", dailyInfo.getCheckinDays());
49+
}
50+
51+
// Make continuousLoginDayStr accessible for testing
52+
private static class DailyInfo extends me.ghui.v2er.network.bean.DailyInfo {
53+
String continuousLoginDayStr;
54+
55+
@Override
56+
public String getCheckinDays() {
57+
// Use reflection to set the field for testing purposes
58+
try {
59+
java.lang.reflect.Field field = me.ghui.v2er.network.bean.DailyInfo.class.getDeclaredField("continuousLoginDayStr");
60+
field.setAccessible(true);
61+
field.set(this, continuousLoginDayStr);
62+
} catch (NoSuchFieldException | IllegalAccessException e) {
63+
// Fail the test explicitly if reflection fails
64+
throw new RuntimeException("Failed to access continuousLoginDayStr via reflection: " + e.getMessage(), e);
65+
}
66+
return super.getCheckinDays();
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)