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
2.x class method visibility #916
Conversation
@epeee, just a quick comment (I haven't reviewed the PR yet) can you put |
* assertThat(String.class).isPublic(); | ||
* assertThat(Math.class).isPublic(); | ||
* | ||
* // This assertion fail: |
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.
typo: fails
* | ||
* <pre><code class='java'> public class MyClass { } | ||
* | ||
* // This assertion succeed: |
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.
succeeds
@@ -284,4 +329,111 @@ public SELF hasDeclaredFields(String... fields) { | |||
classes.assertHasDeclaredFields(info, actual, fields); | |||
return myself; | |||
} | |||
|
|||
/** | |||
* Verifies that the actual {@code Class} has the {@code methods}. |
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.
reword: has the given methods.
I use {@code
for something that denotes a java element like {@code true}
or {@code equals}
* | ||
* @since 2.7.0 / 3.7.0 | ||
*/ | ||
public SELF hasMethods(String... methods) { |
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.
minor refactoring: I think we should rename methods
to methodNames
so that it is clear that we are not dealing with the Method
type.
The example would be better with a superclass for MyClass
having a public method and use it in the successful assertion example.
* assertThat(MyClass.class).hasDeclaredMethods("methodOne", "methodTwo"); | ||
* | ||
* // This one should fail : | ||
* assertThat(MyClass.class).hasDeclaredMethods("methodThree");</code></pre> |
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.
I think a better example would show a superclass for MyClass
with a public method and use it in the failed assertion example (keep the example of the non existent method)
* assertThat(MyClass.class).hasPublicMethods("methodOne"); | ||
* assertThat(MyClass.class).hasPublicMethods("methodOne", "methodTwo"); | ||
* | ||
* // This one should fail : |
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.
plural
/** | ||
* Verifies that the actual {@code Class} has the declared public {@code methods}. | ||
* | ||
* <pre><code class='java'> class MyClass { |
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.
same comment as before: introduce a superclass
public class ClassModifierShouldBe extends BasicErrorMessageFactory { | ||
|
||
private ClassModifierShouldBe(Class<?> actual, boolean positive, String modifier) { | ||
super("%nExpecting:%n <%s>%n" + (positive ? "to" : "not to") + " be a %s class.", actual, modifier); |
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.
suggestion: finish the error message with but was %
and add actual's modifier value ?
*/ | ||
public void assertHasDeclaredMethods(AssertionInfo info, Class<?> actual, String... methods) { | ||
assertNotNull(info, actual); | ||
doAssertHasMethods(info, actual, actual.getDeclaredMethods(), methods); |
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.
how do you now in doAssertHasMethods
that the error message is about declared methods and not methods ?
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.
thx for pointing that out. You are right, this one was still missing but should be fixed now.
private static boolean noNonMatchingModifier(Set<String> expectedMethodNames, Map<String, Integer> actualMethods, Map<String, String> nonMatchingModifiers, int modifier) { | ||
for (String method : actualMethods.keySet()) { | ||
if(expectedMethodNames.contains(method)) { | ||
if ((actualMethods.get(method) & modifier) == 0) { |
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.
I believe you can combine if
conditions
} | ||
} | ||
|
||
private static boolean noNonMatchingModifier(Set<String> expectedMethodNames, Map<String, Integer> actualMethods, Map<String, String> nonMatchingModifiers, int modifier) { |
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.
rename actualMethods
to better express what this map contains (methodsModifer
?)
} | ||
|
||
@Test | ||
public void should_pass_if_no_methods_are_expected() { |
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.
When no methods are given, the assertion should only pass if the actual class has not methods of the expected category (that is consistent with contains()
assertion that succeeds it the actual iterable/array is empty).
This should be true whatever the category of expected methods is (public, declared, ...)
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.
k, thx. I will add this one.
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.
To sum it up:
When no methods are given
hasPublicMethods
will always fail because of methods inherit fromObject
hasDeclaredPublicMethods
will pass if no public methods are declared in the given classhasDeclaredMethods
will pass if no methods are declared in the given classhasMethods
also will always fail because of methods inherit fromObject
Am I right?
…eclared public methods" assertion error message for hasDeclaredPublicMethods - handle no methods are given
@joel-costigliola thx for the feedback. I have integrated your suggestions. |
Thanks @epeee, I'm currently polishing the code, will be integrated soonish.
|
Another thing, can you (in the future) rebase your branches instead of merging the main branch (2.x in our case) into yours ? |
@joel-costigliola k, I will do so in the future (rebase). ad |
@epeee thanks for the additional commit. I was trying to run the tests and found some were flaky because thrown.expectAssertionError(shouldNotHaveMethods(actual, true,
newLinkedHashSet("publicMethod", "privateMethod", "protectedMethod")));
classes.assertHasDeclaredMethods(someInfo(), actual); because the order of methods is not always the same (a LinkedHashSet ensure). The fix is simple, use One more thing, could you add some tests for One last thing (promised !) could you format the constructors in private ShouldHaveSuppressedException(Throwable actual, Throwable expectedSuppressedException) {
super("%n" +
"Expecting:%n" +
" <%s>%n" +
"to have a suppressed exception with the following type and message:%n" +
" <%s> / <%s>%n" +
"but could not find any in actual's suppressed exceptions:%n" +
" <%s>.",
actual, expectedSuppressedException.getClass().getName(), expectedSuppressedException.getMessage(),
actual.getSuppressed());
} it makes the error message easier to read. thanks ! |
using SortedSet now
@joel-costigliola thx for your feedback. I've already updated the pr. |
Integrated thanks ! |
@epeee I think it would be better to align fields and methods assertions, I'm thinking of reducing methods assertions to:
I might consider renaming WDYT ? |
@joel-costigliola: 👍 |
Check List:
a very first draft for class and method visibility.
e.g.
would end up in: