Search in sources :

Example 56 with BLangFunction

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

the class DocumentationTest method testDocConnectorFunction.

@Test(description = "Test doc connector/function.", enabled = false)
public void testDocConnectorFunction() {
    CompileResult compileResult = BCompileUtil.compile("test-src/documentation/connector_function.bal");
    Assert.assertEquals(0, compileResult.getWarnCount());
    PackageNode packageNode = compileResult.getAST();
    BLangConnector connector = (BLangConnector) packageNode.getConnectors().get(0);
    List<BLangDocumentation> docNodes = connector.docAttachments;
    BLangDocumentation dNode = docNodes.get(0);
    Assert.assertNotNull(dNode);
    Assert.assertEquals(dNode.getAttributes().size(), 2);
    Assert.assertEquals(dNode.documentationText, "Test Connector\n");
    Assert.assertEquals(dNode.getAttributes().get(0).documentationField.getValue(), "url");
    Assert.assertEquals(dNode.getAttributes().get(0).documentationText, " url for endpoint\n");
    Assert.assertEquals(dNode.getAttributes().get(1).documentationField.getValue(), "path");
    Assert.assertEquals(dNode.getAttributes().get(1).documentationText, " path for endpoint\n");
    dNode = connector.getActions().get(0).docAttachments.get(0);
    Assert.assertEquals(dNode.getAttributes().size(), 1);
    Assert.assertEquals(dNode.documentationText, "Test Connector action testAction ");
    Assert.assertEquals(dNode.getAttributes().get(0).documentationField.getValue(), "s");
    Assert.assertEquals(dNode.getAttributes().get(0).documentationText, " which represent successful or not");
    dNode = connector.getActions().get(1).docAttachments.get(0);
    Assert.assertEquals(dNode.documentationText, "Test Connector action testSend ");
    Assert.assertEquals(dNode.getAttributes().size(), 2);
    Assert.assertEquals(dNode.getAttributes().get(0).documentationField.getValue(), "ep");
    Assert.assertEquals(dNode.getAttributes().get(0).documentationText, " which represent successful or not ");
    Assert.assertEquals(dNode.getAttributes().get(1).documentationField.getValue(), "s");
    Assert.assertEquals(dNode.getAttributes().get(1).documentationText, " which represent successful or not");
    docNodes = ((BLangFunction) packageNode.getFunctions().get(0)).docAttachments;
    dNode = docNodes.get(0);
    Assert.assertNotNull(dNode);
    Assert.assertEquals(dNode.documentationText, "\n" + "Gets a access parameter value (`true` or `false`) for a given key. " + "Please note that #foo will always be bigger than #bar.\n" + "Example:\n" + "``SymbolEnv pkgEnv = symbolEnter.packageEnvs.get(pkgNode.symbol);``\n");
    Assert.assertEquals(dNode.getAttributes().size(), 3);
    Assert.assertEquals(dNode.getAttributes().get(0).documentationField.getValue(), "file");
    Assert.assertEquals(dNode.getAttributes().get(0).documentationText, " file path ``C:\\users\\OddThinking\\Documents\\My Source\\Widget\\foo.src``\n");
    Assert.assertEquals(dNode.getAttributes().get(1).documentationField.getValue(), "accessMode");
    Assert.assertEquals(dNode.getAttributes().get(1).documentationText, " read or write mode\n");
    Assert.assertEquals(dNode.getAttributes().get(2).documentationField.getValue(), "successful");
    Assert.assertEquals(dNode.getAttributes().get(2).documentationText, " boolean `true` or `false`\n");
    docNodes = ((BLangStruct) packageNode.getStructs().get(0)).docAttachments;
    dNode = docNodes.get(0);
    Assert.assertNotNull(dNode);
    Assert.assertEquals(dNode.documentationText, " Documentation for File struct\n");
    Assert.assertEquals(dNode.getAttributes().size(), 1);
    Assert.assertEquals(dNode.getAttributes().get(0).documentationField.getValue(), "path");
    Assert.assertEquals(dNode.getAttributes().get(0).documentationText, " struct `field path` documentation\n");
}
Also used : BLangDocumentation(org.wso2.ballerinalang.compiler.tree.BLangDocumentation) CompileResult(org.ballerinalang.launcher.util.CompileResult) PackageNode(org.ballerinalang.model.tree.PackageNode) BLangConnector(org.wso2.ballerinalang.compiler.tree.BLangConnector) Test(org.testng.annotations.Test)

Example 57 with BLangFunction

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

the class DocumentationTest method testDeprecated.

@Test(description = "Test doc deprecated.", enabled = false)
public void testDeprecated() {
    CompileResult compileResult = BCompileUtil.compile("test-src/documentation/deprecated.bal");
    Assert.assertEquals(compileResult.getWarnCount(), 0);
    PackageNode packageNode = compileResult.getAST();
    List<BLangDeprecatedNode> dNodes = ((BLangFunction) packageNode.getFunctions().get(0)).deprecatedAttachments;
    BLangDeprecatedNode dNode = dNodes.get(0);
    Assert.assertNotNull(dNode);
    Assert.assertEquals(dNode.documentationText, "\n" + "  This function is deprecated use `openFile(string accessMode){}` instead.\n");
    dNodes = ((BLangStruct) packageNode.getStructs().get(0)).deprecatedAttachments;
    dNode = dNodes.get(0);
    Assert.assertNotNull(dNode);
    Assert.assertEquals(dNode.documentationText, "\n" + "  This Struct is deprecated use `File2` instead.\n");
    dNodes = ((BLangEnum) packageNode.getEnums().get(0)).deprecatedAttachments;
    dNode = dNodes.get(0);
    Assert.assertNotNull(dNode);
    Assert.assertEquals(dNode.documentationText, "\n" + "  This Enum is deprecated use `Enum2` instead.\n");
    dNodes = ((BLangEnum) packageNode.getEnums().get(0)).deprecatedAttachments;
    dNode = dNodes.get(0);
    Assert.assertNotNull(dNode);
    Assert.assertEquals(dNode.documentationText, "\n" + "  This Enum is deprecated use `Enum2` instead.\n");
    dNodes = ((BLangVariable) packageNode.getGlobalVariables().get(0)).deprecatedAttachments;
    dNode = dNodes.get(0);
    Assert.assertNotNull(dNode);
    Assert.assertEquals(dNode.documentationText, "use ```const string testConst = " + "\"TestConstantDocumentation\";``` instead");
    dNodes = ((BLangConnector) packageNode.getConnectors().get(0)).deprecatedAttachments;
    dNode = dNodes.get(0);
    Assert.assertNotNull(dNode);
    Assert.assertEquals(dNode.documentationText, "\n" + "  This Connector is deprecated use `Connector(string url2){}` instead.\n");
    dNodes = ((BLangConnector) packageNode.getConnectors().get(0)).getActions().get(0).deprecatedAttachments;
    dNode = dNodes.get(0);
    Assert.assertNotNull(dNode);
    Assert.assertEquals(dNode.documentationText, "\n" + "      This action is deprecated use `Connector.test(string url2){}` instead.\n" + "    ");
    dNodes = ((BLangService) packageNode.getServices().get(0)).deprecatedAttachments;
    dNode = dNodes.get(0);
    Assert.assertNotNull(dNode);
    Assert.assertEquals(dNode.documentationText, "\n" + "  This Service is deprecated use `PizzaHutService{}` instead.\n");
    dNodes = ((BLangService) packageNode.getServices().get(0)).getResources().get(0).deprecatedAttachments;
    dNode = dNodes.get(0);
    Assert.assertNotNull(dNode);
    Assert.assertEquals(dNode.documentationText, "This Resource is deprecated use `PizzaHutService.orderFromPizza()` instead.");
}
Also used : BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) CompileResult(org.ballerinalang.launcher.util.CompileResult) BLangDeprecatedNode(org.wso2.ballerinalang.compiler.tree.BLangDeprecatedNode) PackageNode(org.ballerinalang.model.tree.PackageNode) Test(org.testng.annotations.Test)

Example 58 with BLangFunction

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

the class DocumentationTest method testDocNativeFunction.

@Test(description = "Test doc native function.")
public void testDocNativeFunction() {
    CompileResult compileResult = BCompileUtil.compile("test-src/documentation/native_function.bal", CompilerPhase.TYPE_CHECK);
    Assert.assertEquals(1, compileResult.getWarnCount());
    BAssertUtil.validateWarning(compileResult, 0, "no such documentable attribute 'successful' with doc prefix 'P'", 6, 1);
    PackageNode packageNode = compileResult.getAST();
    List<BLangDocumentation> docNodes = ((BLangFunction) packageNode.getFunctions().get(0)).docAttachments;
    BLangDocumentation dNode = docNodes.get(0);
    Assert.assertNotNull(dNode);
    Assert.assertEquals(dNode.documentationText, "\n" + "Gets a access parameter value (`true` or `false`) for a given key. " + "Please note that #foo will always be bigger than #bar.\n" + "Example:\n" + "``SymbolEnv pkgEnv = symbolEnter.packageEnvs.get(pkgNode.symbol);``\n");
    Assert.assertEquals(dNode.getAttributes().size(), 2);
    Assert.assertEquals(dNode.getAttributes().get(0).documentationField.getValue(), "accessMode");
    Assert.assertEquals(dNode.getAttributes().get(0).documentationText, " read or write mode\n");
    Assert.assertEquals(dNode.getAttributes().get(1).documentationField.getValue(), "successful");
    Assert.assertEquals(dNode.getAttributes().get(1).documentationText, " boolean `true` or `false`\n");
}
Also used : BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) BLangDocumentation(org.wso2.ballerinalang.compiler.tree.BLangDocumentation) CompileResult(org.ballerinalang.launcher.util.CompileResult) PackageNode(org.ballerinalang.model.tree.PackageNode) Test(org.testng.annotations.Test)

Example 59 with BLangFunction

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

the class CommandUtil method getFunctionNodeDocumentation.

static DocAttachmentInfo getFunctionNodeDocumentation(FunctionNode bLangFunction, int replaceFrom) {
    DiagnosticPos functionPos = CommonUtil.toZeroBasedPosition((DiagnosticPos) bLangFunction.getPosition());
    int offset = functionPos.getStartColumn();
    List<String> attributes = new ArrayList<>();
    if (bLangFunction.getReceiver() != null && bLangFunction.getReceiver() instanceof BLangVariable) {
        BLangVariable receiverNode = (BLangVariable) bLangFunction.getReceiver();
        attributes.add(getDocumentationAttribute(receiverNode.docTag.getValue(), receiverNode.getName().getValue(), offset));
    }
    bLangFunction.getParameters().forEach(bLangVariable -> attributes.add(getDocAttributeFromBLangVariable((BLangVariable) bLangVariable, offset)));
    bLangFunction.getReturnParameters().forEach(bLangVariable -> {
        if (!bLangVariable.getName().getValue().isEmpty()) {
            attributes.add(getDocAttributeFromBLangVariable((BLangVariable) bLangVariable, offset));
        }
    });
    return new DocAttachmentInfo(getDocumentationAttachment(attributes, functionPos.getStartColumn()), replaceFrom);
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) ArrayList(java.util.ArrayList) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Example 60 with BLangFunction

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

the class CommandUtil method getFunctionDocumentationByPosition.

/**
 * Get the Documentation attachment for the function.
 * @param bLangPackage      BLangPackage built
 * @param line              Start line of the function in the source
 * @return {@link String}   Documentation attachment for the function
 */
static DocAttachmentInfo getFunctionDocumentationByPosition(BLangPackage bLangPackage, int line) {
    List<FunctionNode> filteredFunctions = new ArrayList<>();
    for (TopLevelNode topLevelNode : bLangPackage.topLevelNodes) {
        if (topLevelNode instanceof BLangFunction) {
            filteredFunctions.add((BLangFunction) topLevelNode);
        } else if (topLevelNode instanceof BLangObject) {
            filteredFunctions.addAll(((BLangObject) topLevelNode).getFunctions());
        }
        for (FunctionNode filteredFunction : filteredFunctions) {
            DiagnosticPos functionPos = CommonUtil.toZeroBasedPosition((DiagnosticPos) filteredFunction.getPosition());
            int functionStart = functionPos.getStartLine();
            if (functionStart == line) {
                return getFunctionNodeDocumentation(filteredFunction, line);
            }
        }
    }
    return null;
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) BLangObject(org.wso2.ballerinalang.compiler.tree.BLangObject) FunctionNode(org.ballerinalang.model.tree.FunctionNode) ArrayList(java.util.ArrayList) TopLevelNode(org.ballerinalang.model.tree.TopLevelNode)

Aggregations

BLangFunction (org.wso2.ballerinalang.compiler.tree.BLangFunction)37 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)24 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)16 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)16 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)15 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)15 ArrayList (java.util.ArrayList)14 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)14 BLangStruct (org.wso2.ballerinalang.compiler.tree.BLangStruct)13 List (java.util.List)11 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)10 BLangConnector (org.wso2.ballerinalang.compiler.tree.BLangConnector)10 BLangBlockStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt)10 BInvokableSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol)9 BLangEnum (org.wso2.ballerinalang.compiler.tree.BLangEnum)9 BLangObject (org.wso2.ballerinalang.compiler.tree.BLangObject)9 BLangReturn (org.wso2.ballerinalang.compiler.tree.statements.BLangReturn)9 CompilerContext (org.wso2.ballerinalang.compiler.util.CompilerContext)9 Collectors (java.util.stream.Collectors)8 NodeKind (org.ballerinalang.model.tree.NodeKind)8