paulk-asert commented on PR #3085:
URL: https://github.com/apache/groovy/pull/3085#issuecomment-6533138247
Suggested test tweaks, fine as a follow-up after merge too:
> **1. Per-instance state instead of static counters.** The counter and the
failure switch can live on each `Watched`, which drops the `@BeforeEach` reset
and any shared state between tests:
>
> ```groovy
> static class Watched {
> int metaClassCalls
> boolean failMetaClass
>
> @Override
> MetaClass getMetaClass() {
> metaClassCalls++
> if (failMetaClass) {
> throw new IllegalStateException('getMetaClass() called')
> }
> InvokerHelper.getMetaClass(Watched)
> }
> }
>
> @Test
> void 'a cast of a value that has the target type does not call
getMetaClass'() {
> Watched first = new Watched()
> Watched second = new Watched()
>
> assertSame first, castToWatched(first)
> assertSame second, castToWatched(second)
> assertEquals 0, first.metaClassCalls
> assertEquals 0, second.metaClassCalls
> }
>
> @Test
> void 'a cast succeeds when getMetaClass of the value fails'() {
> Watched watched = new Watched(failMetaClass: true)
>
> assertSame watched, castToWatched(watched)
> }
> ```
>
> **2. A proxy-shaped case.** The reported failure is a class-based proxy,
which is a subclass of the target type. The current tests cast `Watched` to
exactly `Watched`. Both go through the same identity handle, but one extra test
pins the reported shape:
>
> ```groovy
> static class WatchedProxy extends Watched {
> @Override
> MetaClass getMetaClass() {
> throw new IllegalStateException('target out of scope')
> }
> }
>
> @Test
> void 'a cast of a subclass proxy whose getMetaClass fails succeeds'() {
> WatchedProxy proxy = new WatchedProxy()
>
> assertSame proxy, castToWatched(proxy)
> }
> ```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]