Repository navigation
Add unit tests for ProblemTextFilter #126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| package org.overengineer.inlineproblems.utils; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.CsvSource; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| class ProblemTextFilterTest { | ||
|
|
||
| @Test | ||
| void prefixMatchesTextBegin() { | ||
| assertTrue(ProblemTextFilter.isFiltered("Field 'x' is never used", "field")); | ||
| assertTrue(ProblemTextFilter.isFiltered("Unused import statement", "unu")); | ||
| assertFalse(ProblemTextFilter.isFiltered("Field 'x' is never used", "never")); | ||
| assertFalse(ProblemTextFilter.isFiltered("Unused import statement", "import")); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @CsvSource({ | ||
| "Field 'x' is never used,todo;fIELD", | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: |
||
| "Unused import statement,UNU", | ||
| "Field 'x' is never used,re:NEVER", | ||
| "Unused import statement,*sTaT*;todo" | ||
| }) | ||
| void caseIsIgnored(String problemText, String rawFilterList) { | ||
| assertTrue(ProblemTextFilter.isFiltered(problemText, rawFilterList)); | ||
| } | ||
|
|
||
| @Test | ||
| void blankFilterTextIsIgnored() { | ||
| assertFalse(ProblemTextFilter.isFiltered("Unused import statement", "")); | ||
| assertFalse(ProblemTextFilter.isFiltered("Unused import statement", ";;")); | ||
| assertFalse(ProblemTextFilter.isFiltered("Unused import statement", "; ;")); | ||
| assertFalse(ProblemTextFilter.isFiltered("Cannot resolve method 'foo(int)'", "re:")); | ||
| } | ||
|
|
||
| @Test | ||
| void leadingSpacesAreIgnored() { | ||
| assertTrue(ProblemTextFilter.isFiltered("Field 'x' is never used", " field")); | ||
| assertTrue(ProblemTextFilter.isFiltered("Unused import statement", " *use*")); | ||
| assertTrue(ProblemTextFilter.isFiltered(" Unused import statement", "Unu")); | ||
| } | ||
|
|
||
| @Test | ||
| void globMatchesTextPattern() { | ||
| assertTrue(ProblemTextFilter.isFiltered("Field 'x' is never used", "fie*")); | ||
| assertTrue(ProblemTextFilter.isFiltered("Unused import statement", "*t st*")); | ||
| assertFalse(ProblemTextFilter.isFiltered("Field 'x' is never used", "field?")); | ||
| assertTrue(ProblemTextFilter.isFiltered("Unused import statement", "*statement")); | ||
| } | ||
|
|
||
| @Test | ||
| void globIgnoresRegexSymbols() { | ||
| assertTrue(ProblemTextFilter.isFiltered("Cannot resolve method 'foo(int)'", "*foo(int)?")); | ||
| assertTrue(ProblemTextFilter.isFiltered("Cannot resolve method 'foo(int)'", "*foo(*")); | ||
| assertTrue(ProblemTextFilter.isFiltered("'Thread.sleep()' called in loop", "*ad.sleep*")); | ||
| assertFalse(ProblemTextFilter.isFiltered("'Thread.sleep' called in loop", "*sleep()*")); | ||
| assertFalse(ProblemTextFilter.isFiltered("'ThreadXsleep()' called in loop", "*ad.sleep*")); | ||
| } | ||
|
|
||
| @Test | ||
| void regexMatchesTextPattern() { | ||
| assertTrue(ProblemTextFilter.isFiltered("'ThreadXsleep()' called in loop", "re:ad.sleep")); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no test for an entry that both starts with |
||
| } | ||
|
|
||
| @Test | ||
| void regexIdentifierCaseIsIgnored() { | ||
| assertTrue(ProblemTextFilter.isFiltered("'ThreadXsleep()' called in loop", "RE:ad.sleep")); | ||
| } | ||
|
|
||
| @Test | ||
| void invalidRegexIsIgnored() { | ||
| assertDoesNotThrow(() -> ProblemTextFilter.isFiltered("Cannot resolve method 'foo(int)'", "re:foo(")); | ||
| assertTrue(ProblemTextFilter.isFiltered("Unused import statement", "re:imp(;unused")); | ||
| } | ||
|
|
||
| @Test | ||
| void nullTextIsIgnored() { | ||
| assertTrue(ProblemTextFilter.isFiltered(null, ";;")); | ||
| } | ||
|
|
||
| @Test | ||
| void nullFilterListIsIgnored() { | ||
| assertFalse(ProblemTextFilter.isFiltered("Unused import statement", null)); | ||
| } | ||
|
|
||
| @Test | ||
| void changedFilterListIsApplied() { | ||
| assertTrue(ProblemTextFilter.isFiltered("Field 'x' is never used", "field")); | ||
| assertFalse(ProblemTextFilter.isFiltered("Field 'x' is never used", "*fiel")); | ||
| assertFalse(ProblemTextFilter.isFiltered("Field 'x' is never used", "*fiel")); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the same assertion as the line above, so the sequence checks less than it looks like. Was this meant to use a different list or text? With only two lists that alternate, the test would also pass with a cache that is rebuilt on every call, so a third distinct list (e.g. |
||
| assertTrue(ProblemTextFilter.isFiltered("Field 'x' is never used", "field")); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All tests share the static cache in
ProblemTextFilter(cachedRawFilterList/cachedFilters) and never reset it, so what a test actually exercises depends on what the previous test left behind. For example, ifnullFilterListIsIgnoredruns first,Objects.equals(null, null)is true on the initial state andparse(null)is never called. A small package-privateresetCache()(@VisibleForTesting) called from a@BeforeEachwould make the tests independent of the method order.