Search in sources :

Example 91 with BLangPackage

use of org.wso2.ballerinalang.compiler.tree.BLangPackage in project ballerina by ballerina-lang.

the class BallerinaEnumDocGenTest method testStruct.

@Test(description = "Test a Bal file with Enum")
public void testStruct() {
    try {
        Map<String, BLangPackage> docsMap = BallerinaDocGenerator.generatePackageDocsFromBallerina(sourceRoot, "enumTest.bal");
        Assert.assertNotNull(docsMap);
        Assert.assertEquals(docsMap.size(), 1);
        BLangPackage balPackage = docsMap.get(".");
        List<? extends EnumNode> enums = balPackage.getEnums();
        Assert.assertEquals(enums.size(), 1);
        BLangEnum struct = (BLangEnum) enums.iterator().next();
        Assert.assertEquals(struct.getEnumerators().size(), 13);
    } catch (IOException e) {
        Assert.fail();
    } finally {
        BallerinaDocGenTestUtils.cleanUp();
    }
}
Also used : BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) BLangEnum(org.wso2.ballerinalang.compiler.tree.BLangEnum) IOException(java.io.IOException) Test(org.testng.annotations.Test)

Example 92 with BLangPackage

use of org.wso2.ballerinalang.compiler.tree.BLangPackage in project ballerina by ballerina-lang.

the class BallerinaFunctionDocGenTest method testABalWithNativeFunction.

@Test(description = "Test a Bal file with a native function")
public void testABalWithNativeFunction() {
    try {
        Map<String, BLangPackage> docsMap = BallerinaDocGenerator.generatePackageDocsFromBallerina(sourceRoot, "natives.bal", "", true);
        Assert.assertNotNull(docsMap);
        Assert.assertEquals(docsMap.size(), 1);
        BallerinaDocGenTestUtils.printDocMap(docsMap);
        BLangPackage doc = docsMap.get(".");
        Collection<BLangFunction> functions = doc.getFunctions();
        Assert.assertEquals(functions.size(), 1);
        BLangFunction function = functions.iterator().next();
        Assert.assertEquals(function.getParameters().size(), 1);
        Assert.assertEquals(function.getReturnParameters().size(), 1);
        Assert.assertEquals(function.getAnnotationAttachments().size(), 2);
    } catch (IOException e) {
        Assert.fail();
    } finally {
        BallerinaDocGenTestUtils.cleanUp();
    }
}
Also used : BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) IOException(java.io.IOException) Test(org.testng.annotations.Test)

Example 93 with BLangPackage

use of org.wso2.ballerinalang.compiler.tree.BLangPackage in project ballerina by ballerina-lang.

the class BallerinaFunctionDocGenTest method testABalWithOneFunction.

@Test(description = "Test a Bal file with one Function")
public void testABalWithOneFunction() {
    try {
        Map<String, BLangPackage> docsMap = BallerinaDocGenerator.generatePackageDocsFromBallerina(sourceRoot, "helloWorld.bal");
        Assert.assertNotNull(docsMap);
        Assert.assertEquals(docsMap.size(), 1);
        BallerinaDocGenTestUtils.printDocMap(docsMap);
        BLangPackage doc = docsMap.get(".");
        Collection<BLangFunction> functions = doc.getFunctions();
        Assert.assertEquals(functions.size(), 1);
        BLangFunction function = functions.iterator().next();
        Assert.assertEquals(function.getParameters().size(), 1);
        Assert.assertEquals(function.getReturnParameters().size(), 1);
    } catch (IOException e) {
        Assert.fail();
    } finally {
        BallerinaDocGenTestUtils.cleanUp();
    }
}
Also used : BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) IOException(java.io.IOException) Test(org.testng.annotations.Test)

Example 94 with BLangPackage

use of org.wso2.ballerinalang.compiler.tree.BLangPackage in project ballerina by ballerina-lang.

the class BallerinaStructDocGenTest method testStruct.

@Test(description = "Test a Bal file with structs")
public void testStruct() {
    try {
        Map<String, BLangPackage> docsMap = BallerinaDocGenerator.generatePackageDocsFromBallerina(sourceRoot, "balWithStruct.bal");
        Assert.assertNotNull(docsMap);
        Assert.assertEquals(docsMap.size(), 1);
        BLangPackage balPackage = docsMap.get(".");
        List<BLangStruct> structs = balPackage.getStructs();
        Assert.assertEquals(structs.size(), 1);
        BLangStruct struct = (BLangStruct) structs.iterator().next();
        Assert.assertEquals(struct.getFields().size(), 3);
        Assert.assertEquals(struct.getAnnotationAttachments().size(), 4);
    } catch (IOException e) {
        Assert.fail();
    } finally {
        BallerinaDocGenTestUtils.cleanUp();
    }
}
Also used : BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) BLangStruct(org.wso2.ballerinalang.compiler.tree.BLangStruct) IOException(java.io.IOException) Test(org.testng.annotations.Test)

Example 95 with BLangPackage

use of org.wso2.ballerinalang.compiler.tree.BLangPackage in project ballerina by ballerina-lang.

the class TextDocumentServiceUtil method getBLangPackage.

/**
 * Get the BLangPackage for a given program.
 *
 * @param context             Language Server Context
 * @param docManager          Document manager
 * @param preserveWhitespace  Enable preserve whitespace
 * @param customErrorStrategy custom error strategy class
 * @param compileFullProject  compile full project from the source root
 * @return {@link BLangPackage} BLang Package
 */
public static List<BLangPackage> getBLangPackage(LanguageServerContext context, WorkspaceDocumentManager docManager, boolean preserveWhitespace, Class customErrorStrategy, boolean compileFullProject) {
    String uri = context.get(DocumentServiceKeys.FILE_URI_KEY);
    LSDocument document = new LSDocument(uri);
    Path filePath = CommonUtil.getPath(document);
    Path fileNamePath = filePath.getFileName();
    String fileName = "";
    if (fileNamePath != null) {
        fileName = fileNamePath.toString();
    }
    String sourceRoot = TextDocumentServiceUtil.getSourceRoot(filePath);
    String pkgName = TextDocumentServiceUtil.getPackageNameForGivenFile(sourceRoot, filePath.toString());
    LSDocument sourceDocument = new LSDocument();
    sourceDocument.setUri(uri);
    sourceDocument.setSourceRoot(sourceRoot);
    PackageRepository packageRepository = new WorkspacePackageRepository(sourceRoot, docManager);
    List<BLangPackage> packages = new ArrayList<>();
    if (compileFullProject) {
        if (!sourceRoot.isEmpty()) {
            File projectDir = new File(sourceRoot);
            File[] files = projectDir.listFiles();
            if (files != null) {
                for (File file : files) {
                    if ((file.isDirectory() && !file.getName().startsWith(".")) || (!file.isDirectory() && file.getName().endsWith(".bal"))) {
                        Compiler compiler = getCompiler(context, fileName, packageRepository, sourceDocument, preserveWhitespace, customErrorStrategy, docManager);
                        packages.add(compiler.compile(file.getName()));
                    }
                }
            }
        }
    } else {
        Compiler compiler = getCompiler(context, fileName, packageRepository, sourceDocument, preserveWhitespace, customErrorStrategy, docManager);
        if ("".equals(pkgName)) {
            packages.add(compiler.compile(fileName));
        } else {
            packages.add(compiler.compile(pkgName));
        }
    }
    return packages;
}
Also used : Path(java.nio.file.Path) Compiler(org.wso2.ballerinalang.compiler.Compiler) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) LSDocument(org.ballerinalang.langserver.common.LSDocument) ArrayList(java.util.ArrayList) PackageRepository(org.ballerinalang.repository.PackageRepository) WorkspacePackageRepository(org.ballerinalang.langserver.workspace.repository.WorkspacePackageRepository) File(java.io.File) WorkspacePackageRepository(org.ballerinalang.langserver.workspace.repository.WorkspacePackageRepository)

Aggregations

BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)78 Test (org.testng.annotations.Test)29 ArrayList (java.util.ArrayList)28 CompilerContext (org.wso2.ballerinalang.compiler.util.CompilerContext)19 Page (org.ballerinalang.docgen.model.Page)18 BLangFunction (org.wso2.ballerinalang.compiler.tree.BLangFunction)16 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)15 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)15 BLangNode (org.wso2.ballerinalang.compiler.tree.BLangNode)14 IOException (java.io.IOException)13 List (java.util.List)13 Path (java.nio.file.Path)12 Compiler (org.wso2.ballerinalang.compiler.Compiler)12 BLangStruct (org.wso2.ballerinalang.compiler.tree.BLangStruct)12 BLangService (org.wso2.ballerinalang.compiler.tree.BLangService)11 TopLevelNode (org.ballerinalang.model.tree.TopLevelNode)10 BLangConnector (org.wso2.ballerinalang.compiler.tree.BLangConnector)10 BPackageSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol)9 BLangAction (org.wso2.ballerinalang.compiler.tree.BLangAction)9 BLangEnum (org.wso2.ballerinalang.compiler.tree.BLangEnum)9