Package dtool.engine.operations

Source Code of dtool.engine.operations.FindDefinitionOperation_Test

/*******************************************************************************
* Copyright (c) 2014, 2014 Bruno Medeiros and other Contributors.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
*     Bruno Medeiros - initial API and implementation
*******************************************************************************/
package dtool.engine.operations;

import static melnorme.utilbox.core.Assert.AssertNamespace.assertTrue;

import java.nio.file.Path;

import melnorme.lang.tooling.ast.SourceRange;

import org.junit.Test;

import dtool.engine.operations.FindDefinitionResult.FindDefinitionResultEntry;

public class FindDefinitionOperation_Test extends CommonDToolOperation_Test {
 
  public static final Path BASIC_FOO_FilePath = BUNDLE_FOO__SRC_FOLDER.resolve("basic_foo.d");
  public static final String BASIC_FOO_Contents = readStringFromFile(BASIC_FOO_FilePath);
 
  protected FindDefinitionResult doOperation(Path filePath, int offset) throws Exception {
    return dtoolEngine.doFindDefinition(filePath, offset);
  }
 
  @Test
  public void testALL() throws Exception { testALL$(); }
  public void testALL$() throws Exception {
   
    testFindDefinition(BASIC_FOO_FilePath, indexOf(BASIC_FOO_Contents, "/*defA_REF1*/"),
      new FindDefinitionResultEntry(
        "defA", false, BASIC_FOO_FilePath, new SourceRange(indexOf(BASIC_FOO_Contents, "defA/*DEF*/"), 4))
    );
   
    // Test implicit source module
    testFindDefinition(BASIC_FOO_FilePath, indexOf(BASIC_FOO_Contents, "implicit_name/*MARKER*/"),
      new FindDefinitionResultEntry(
        "implicit_name", false, BUNDLE_FOO__SRC_FOLDER.resolve("basic_pack/implicit_name.d"),
        new SourceRange(0, 0))
    );
   
    // Test intrinsic
    testFindDefinition(BASIC_FOO_FilePath, indexOf(BASIC_FOO_Contents, "int/*int_ref*/"),
      new FindDefinitionResultEntry(
        "int", true, null, null)
    );
   
    // Test error cases
    testFindDefinition(BASIC_FOO_FilePath, indexOf(BASIC_FOO_Contents, "intvar ="),
      FindDefinitionOperation.FIND_DEF_PickedElementAlreadyADefinition
    );
    testFindDefinition(BASIC_FOO_FilePath, indexOf(BASIC_FOO_Contents, "123"),
      FindDefinitionOperation.FIND_DEF_NoReferenceFoundAtCursor
    );
    testFindDefinition(BASIC_FOO_FilePath, indexOf(BASIC_FOO_Contents, "not_found"),
      FindDefinitionOperation.FIND_DEF_ReferenceResolveFailed
    );
   
  }
 
  protected void testFindDefinition(Path modulePath, int offset, FindDefinitionResultEntry... expectedResults)
      throws Exception {
    testFindDefinition(modulePath, offset, null, expectedResults);
  }
 
  protected void testFindDefinition(Path modulePath, int offset, String errorMsg,
      FindDefinitionResultEntry... expectedResults) throws Exception {
    FindDefinitionResult opResult = doOperation(modulePath, offset);
   
    if(opResult.errorMessage == null) {
      assertTrue(errorMsg == null);
    } else {
      assertTrue(opResult.errorMessage.startsWith(errorMsg));
      assertTrue(expectedResults.length == 0);
      return;
    }
   
    assertTrue(expectedResults.length == opResult.results.size());
   
    for (int i = 0; i < expectedResults.length; i++) {
      FindDefinitionResultEntry expected = expectedResults[i];
      FindDefinitionResultEntry nameResult = opResult.results.get(i);
     
      assertAreEqual(nameResult.extendedName, expected.extendedName);
      assertAreEqual(nameResult.modulePath, expected.modulePath);
      assertAreEqual(nameResult.sourceRange, expected.sourceRange);
      assertAreEqual(nameResult.isLanguageIntrinsic, expected.isLanguageIntrinsic);
    }
  }
 
}
TOP

Related Classes of dtool.engine.operations.FindDefinitionOperation_Test

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.