File tree Expand file tree Collapse file tree 2 files changed +27
-4
lines changed
main/java/ru/untitled_devs/core/routers/filters
test/java/ru/untitleddevs/core/routers/filters Expand file tree Collapse file tree 2 files changed +27
-4
lines changed Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff 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 );
You can’t perform that action at this time.
0 commit comments