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();
}
}
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();
}
}
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();
}
}
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();
}
}
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;
}
Aggregations