Set filetype association for LightJavaCodeInsightFixtureTestCase

Answered

I have succesfully created an extension that creates references from 'Angular HTML Template' elements to my DSL. When I want to unit-test these references the fixture always considers the file a Plain HTML file so the PsiTree is not as expected and I cannot test my references.

Is there anyway to set the file type association for a LightJavaCodeInsightFixtureTestCase so something with '*.component.html' is considered an Angular HTML Template FileType. 

6 comments
Comment actions Permalink

Did you define a dependency on Angular plugin in your plugin.xml?

0
Comment actions Permalink

Yes, as an optional dependency. Everything works fine when I use it in runIde or when actually deploying the plugin. The only situation where it doesn't work is in a LightJavaCodeInsightFixtureTestCase where the PsiFile is identified as a plain HTML file instead of Angular HTML template

0
Comment actions Permalink

Tim, the Angular HTML language replaces the regular HTML under certain circumstances. One of the easiest ways to reproduce the regular project setup is to add `package.json` file with `@angular/core` dependency in it. E.g.:

myFixture.configureByText("package.json","{\"dependencies\":{\"@angular/core\":\"*\"}}")

You can also register your own context provider (`Angular2ContextProvider` in 2020.3 and older or `WebFrameworkContext` in 2021.1 and newer) in `setUp()` method, which always returns `true` for the test duration. E.g.:

Angular2ContextProvider.ANGULAR_CONTEXT_PROVIDER_EP
.getPoint()
.registerExtension(psiDir -> CachedValueProvider.Result.create(true, ModificationTracker.NEVER_CHANGED),
myFixture.getTestRootDisposable());

Let me know if you have any questions!

0
Comment actions Permalink

Hi Piotr,

thank you for your answer. Unfortunately it didn't do the trick for me. I have upgraded to 2021.1 and to my surprise now my .ts files in my unit-tests are considered plain-text. Also, the icon for the Angular HTML file type is missing. Don't know if this might be a bug in the release or something went wrong during my upgrade.

I have tried your package.json example, that one didn't work. The WebFrameworkContext example I couldn't get to work, I'm not sure what I should register, the line in your example isn't working (probably also deprecated) and the interface for registerExtension shows little to no documentation. I do see a lot of references to a PlatformTestUtil, does this mean these tests should not setup using a LightJavaCodeInsightFixtureTestCase but rather a heavier one?

Kr, Tim

0
Comment actions Permalink

Tim, I am sorry, I haven't payed enough attention to the class you mentioned. Indeed, these tests should be based on `BasePlatformTestCase`. You can see Angular tests here and take the most appropriate as an example: https://github.com/JetBrains/intellij-plugins/tree/master/AngularJS/test/org/angular2 .

0
Comment actions Permalink

Hi Piotr,

thanks for your response. I have tried it quickly but so far nothing seems to get it working in a test environment. I have created a very simpel setup with 2 tests: 

import com.intellij.lang.javascript.TypeScriptFileType;
import com.intellij.psi.PsiFile;
import com.intellij.testFramework.fixtures.BasePlatformTestCase;
import org.angular2.lang.html.Angular2HtmlFileType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class Angular2TSReferenceTest extends BasePlatformTestCase {

@Override
@BeforeEach
protected void setUp() throws Exception {
super.setUp();
myFixture.configureByText("package.json","{\"dependencies\":{\"@angular/core\":\"*\"}}");
}

@Override
@AfterEach
protected void tearDown() throws Exception {
super.tearDown();
}

@Test
void testTSFile() {
String content = "@Component({\n" +
" templateUrl: './test.html',\n" +
"})";
final PsiFile psiFile = myFixture.configureByText("myFile.ts", content);
assertEquals(TypeScriptFileType.INSTANCE, psiFile.getFileType());
}

@Test
void testHTMLFile() {
String content = "<div (click)=\"domeSomeActionInTheTsComponent();\"></div>";
final PsiFile psiFile = myFixture.configureByText("test.html", content);
assertEquals(Angular2HtmlFileType.INSTANCE, psiFile.getFileType());
}
}

Both tests fail on their assertion:

testTSFile:

Expected :com.intellij.lang.javascript.TypeScriptFileType@658dcf73
Actual :com.intellij.openapi.fileTypes.PlainTextFileType@22ac8c64

Second

Expected :org.angular2.lang.html.Angular2HtmlFileType@1b2f727a
Actual :com.intellij.ide.highlighter.HtmlFileType@7d291106

In a few days I'll give it another go when I have some more time, perhaps you can have a quick look to see if you can reproduce the above to see if I'm missing something in my setup. The fact that my typescript file is recognized as plain text makes me think that it somehow is running against a community edition.

0

Please sign in to leave a comment.