Inspection - inner enums

Issue #882 resolved
Derek Wiers created an issue

I have:

public class MyClass {
    @TestVisible private enum MyPrivateEnum {
        VALUE_1,
        VALUE_2
    }
}

and

@IsTest private class MyClassTest {
    private static testMethod void testSomethingWithEnum {
        System.assertEquals(MyClass.MyPrivateEnum.VALUE_1, someMethodToTest());
    }
}

Trouble is, "VALUE_1" in the test class is showing as not able to be resolved. "MyClass.MyPrivateEnum" is fine, but the value itself is a no-go. Code compiles fine.

I would also like to remind you that you do excellent work and I'm very happy I started going with IJ/IC.

You said in my last ticket that I marked trivial that inspections should be taken seriously, so I'm bumping this up to "minor" lol

Comments (13)

  1. Scott Wells repo owner

    This was interesting...I hadn't thought about the fact that enum constants would need to be exposed via their containing type. Done and will be included in the next build.

  2. Scott Wells repo owner

    Delivered in 1.8.2.0 (@TestVisible handling only) and 2.0.1.1 (@TestVisible handling and inspection).

  3. Jon Wu

    Is this expected to fix MyClass.MyPrivateEnum.VALUE_1.name() as well? IC tells me Cannot resolve symbol 'name' for code just like this, which also uses .name() on the enum. If this is unrelated, I can open another bug.

  4. Scott Wells repo owner

    Hi, Jon. Note that name() is not a real method on an enum constant, though. IC adds it for purposes of code completion since it is part of the language, but there's no physical declaration of name() to which you can navigate. That's why you're getting Cannot resolve symbol 'name' when you try to navigate to it. The same thing is true of other implicit symbols such as the class constant for all Apex types (e.g., String.class). IC includes those in the body declarations of the parent types even though they're not explicitly part of the type definition.

    Regards, Scott

  5. Jon Wu

    Hi Scott, understood that you can't necessarily navigate to a declaration, but since it's valid to call name() on the constant, it's unexpected that name() shows up in red and adds to the count of errors in the file. Happy to open a separate issue here if that's appropriate.

    Thanks!

  6. Scott Wells repo owner

    Ah, okay. Based on the error message you provided I assumed you were talking about IC1. You should not see this flagged as an unresolvable reference in IC2, though. I just tried to reproduce this and am not seeing any errors in the following:

    String enumConstant1Value = UnusedDeclarationTest.InnerEnum.ENUM_CONSTANT1.name();
    

    If you are seeing errors in code like that, could you please create a standalone reproducible example and provide it here so I can try that?

  7. Jon Wu

    Basically the example in this ticket I would imagine would recreate the issue if it used .name() in the test. However, I've recreated from scratch with this pair of classes.

    public class TestClass {
        @TestVisible private enum TestEnum{A}
    }
    

    And the error is in the tests for that (poorly named) class:

    @isTest private class TestClassTest {
        @isTest static void test() {
            // name is red here. On hover, I see `Cannot resolve symbol 'name'` in IC 2.
            TestClass.TestEnum.A.name();
        }
    }
    
  8. Scott Wells repo owner

    Ah, okay. I'm being dumb...I wasn't tying together the @TestVisible aspect that drove this original ticket. Good lord...I should pay better attention! Okay, it's easily reproduced when I do that. I'll take a look for the next build as I work through some other false negatives. Thanks for bearing with me!

  9. Derek Wiers reporter

    Excellent! I also wanted you to know that every time I got an email for this issue - I read "Inner Eardrums" lol.

  10. Log in to comment