Skip to content

Commit 58e987d

Browse files
справлен RegexFilter.java
1 parent 0b49e29 commit 58e987d

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

src/main/java/ru/untitled_devs/core/routers/filters/RegexFilter.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,33 @@ public RegexFilter(String pattern) {
1414

1515
@Override
1616
public boolean check(Update update) {
17-
String text = update.hasMessage() ? update.getMessage().getText()
18-
: update.hasCallbackQuery() ? update.getCallbackQuery().getData()
19-
: null;
17+
String text = extractTextFromUpdate(update);
2018

2119
if (text == null) return false;
2220

2321
return this.pattern.matcher(text).find();
2422
}
2523

24+
private String extractTextFromUpdate(Update update) {
25+
if (update.hasMessage() && update.getMessage().hasText()) {
26+
return update.getMessage().getText();
27+
}
28+
if (update.hasEditedMessage() && update.getEditedMessage().hasText()) {
29+
return update.getEditedMessage().getText();
30+
}
31+
if (update.hasChannelPost() && update.getChannelPost().hasText()) {
32+
return update.getChannelPost().getText();
33+
}
34+
if (update.hasEditedChannelPost() && update.getEditedChannelPost().hasText()) {
35+
return update.getEditedChannelPost().getText();
36+
}
37+
if (update.hasCallbackQuery()) {
38+
return update.getCallbackQuery().getData();
39+
}
40+
if (update.hasInlineQuery()) {
41+
return update.getInlineQuery().getQuery();
42+
}
43+
return null;
44+
}
2645

2746
}

src/test/java/ru/untitleddevs/core/routers/filters/RegexFilterTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ void setUp() {
2929
@Test
3030
void checkShouldReturnTrueWhenPatternMatchesText() {
3131
RegexFilter filter = new RegexFilter("hello");
32+
when(updateMock.hasMessage()).thenReturn(true);
33+
when(messageMock.hasText()).thenReturn(true);
3234
when(messageMock.getText()).thenReturn("say hello to the world");
3335

3436
boolean result = filter.check(updateMock);
@@ -48,7 +50,9 @@ void checkShouldReturnFalseWhenPatternDoesNotMatchText() {
4850

4951
@Test
5052
void checkShouldHonorUnicodeFlagWhenMatchingUnicodeCharacters() {
51-
RegexFilter filter = new RegexFilter("привет");
53+
RegexFilter filter = new RegexFilter("\\p{L}+");
54+
when(updateMock.hasMessage()).thenReturn(true);
55+
when(messageMock.hasText()).thenReturn(true);
5256
when(messageMock.getText()).thenReturn("Скажи привет, пожалуйста");
5357

5458
boolean result = filter.check(updateMock);

0 commit comments

Comments
 (0)