// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.intellij.findUsages;

import ai.vespa.intellij.PluginTestBase;
import ai.vespa.intellij.schema.SdUtil;
import ai.vespa.intellij.schema.findUsages.SdFindUsagesHandler;
import ai.vespa.intellij.schema.model.Schema;
import ai.vespa.intellij.schema.utils.Path;
import com.intellij.find.findUsages.FindUsagesOptions;
import com.intellij.usageView.UsageInfo;
import com.intellij.util.Processor;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * @author bratseth
 */
public class FindUsagesTest extends PluginTestBase {

    @Test
    public void testFindUsages() {
        useDir("src/test/applications/rankprofilemodularity");
        Schema schema = Schema.fromProjectFile(getProject(), Path.fromString("test.sd"));
        var handler = new SdFindUsagesHandler(schema.definition());
        var function = SdUtil.functionsIn(schema.rankProfile("in_schema2").get()).get("ff1").get(0).definition();
        var usageProcessor = new MockUsageProcessor();
        var options = new FindUsagesOptions(getProject());
        options.isUsages = true;
        handler.processElementUsages(function, usageProcessor, options);
        assertEquals(3, usageProcessor.usages.size());
    }

    private static class MockUsageProcessor implements Processor<UsageInfo> {

        List<UsageInfo> usages = new ArrayList<>();

        @Override
        public boolean process(UsageInfo usageInfo) {
            usages.add(usageInfo);
            return true;
        }

    }

}

@@ -427,16 +427,61 @@
 ptions;%0A
+import com.intellij.openapi.project.Project;%0A
 import c
@@ -513,16 +513,16 @@
 geInfo;%0A
-
 import c
@@ -827,15 +827,428 @@
     
-Schema 
+var tester = new UsagesTester(%22test.sd%22, getProject());%0A        tester.assertFunctionUsages(%22Self reference + 2 local references%22, 3, %22in_schema2%22, %22ff1%22);%0A    %7D%0A%0A    private static class UsagesTester %7B%0A%0A        final Project project;%0A        final Schema schema;%0A        final SdFindUsagesHandler handler;%0A%0A        UsagesTester(String schemaName, Project project) %7B%0A            this.project = project;%0A            this.
 sche
@@ -1275,28 +1275,23 @@
 ectFile(
-getP
+p
 roject
-()
 , Path.f
@@ -1300,25 +1300,26 @@
 mString(
-%22test.sd%22
+schemaName
 ));%0A    
@@ -1322,20 +1322,25 @@
         
-var 
+    this.
 handler 
@@ -1383,24 +1383,156 @@
 inition());%0A
+        %7D%0A%0A        void assertFunctionUsages(String explanation, int expectedUsages, String profileName, String functionName) %7B%0A    
         var 
@@ -1580,28 +1580,27 @@
 Profile(
-%22in_schema2%22
+profileName
 ).get())
@@ -1604,21 +1604,28 @@
 ()).get(
-%22ff1%22
+functionName
 ).get(0)
@@ -1643,24 +1643,28 @@
 ();%0A        
+   
+ 
 var usagePro
@@ -1702,24 +1702,28 @@
 ();%0A        
+    
 var options 
@@ -1746,31 +1746,30 @@
 Options(
-getP
+p
 roject
-()
 );%0A
+    
         
@@ -1789,24 +1789,28 @@
 ges = true;%0A
+    
         hand
@@ -1878,16 +1878,20 @@
         
+ 
+   
 assertEq
@@ -1895,17 +1895,43 @@
 tEquals(
-3
+explanation, expectedUsages
 , usageP
@@ -1951,24 +1951,35 @@
 es.size());%0A
+        %7D%0A%0A
     %7D%0A%0A    p

Extract into tester