Search in sources :

Example 1 with FunctionDoc

use of org.ballerinalang.docgen.model.FunctionDoc in project ballerina by ballerina-lang.

the class Generator method createDocForNode.

/**
 * Create documentation for functions.
 * @param functionNode ballerina function node.
 * @return documentation for functions.
 */
public static FunctionDoc createDocForNode(BLangFunction functionNode) {
    String functionName = functionNode.getName().value;
    List<Variable> parameters = new ArrayList<>();
    List<Variable> returnParams = new ArrayList<>();
    // Iterate through the parameters
    if (functionNode.getParameters().size() > 0) {
        for (BLangVariable param : functionNode.getParameters()) {
            String dataType = type(param);
            String desc = paramAnnotation(functionNode, param);
            Variable variable = new Variable(param.getName().value, dataType, desc);
            parameters.add(variable);
        }
    }
    // Iterate through the return types
    if (functionNode.getReturnParameters().size() > 0) {
        for (int i = 0; i < functionNode.getReturnParameters().size(); i++) {
            BLangVariable returnParam = functionNode.getReturnParameters().get(i);
            String dataType = type(returnParam);
            String desc = returnParamAnnotation(functionNode, i);
            Variable variable = new Variable(returnParam.getName().value, dataType, desc);
            returnParams.add(variable);
        }
    }
    return new FunctionDoc(functionName, description(functionNode), new ArrayList<>(), parameters, returnParams);
}
Also used : BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) Variable(org.ballerinalang.docgen.model.Variable) FunctionDoc(org.ballerinalang.docgen.model.FunctionDoc) ArrayList(java.util.ArrayList) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Example 2 with FunctionDoc

use of org.ballerinalang.docgen.model.FunctionDoc in project ballerina by ballerina-lang.

the class HtmlDocTest method testFunctions.

@Test(description = "Functions in a package should be shown in the constructs")
public void testFunctions() throws Exception {
    BLangPackage bLangPackage = createPackage("package x.y; public function hello(string name) returns (string){}");
    Page page = generatePage(bLangPackage);
    Assert.assertEquals(page.constructs.size(), 1);
    Assert.assertEquals(page.constructs.get(0).name, "hello");
    Assert.assertTrue(page.constructs.get(0) instanceof FunctionDoc, "Invalid documentable type");
    FunctionDoc functionDoc = (FunctionDoc) page.constructs.get(0);
    Assert.assertEquals(functionDoc.parameters.get(0).toString(), "string name", "Invalid parameter string value");
    Assert.assertEquals(functionDoc.returnParams.get(0).toString(), "string", "Invalid return type");
}
Also used : BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) FunctionDoc(org.ballerinalang.docgen.model.FunctionDoc) Page(org.ballerinalang.docgen.model.Page) Test(org.testng.annotations.Test)

Example 3 with FunctionDoc

use of org.ballerinalang.docgen.model.FunctionDoc in project ballerina by ballerina-lang.

the class HtmlDocTest method testFunctionsPropertiesExtracted.

@Test(description = "Function properties should be available via construct")
public void testFunctionsPropertiesExtracted() throws Exception {
    BLangPackage bLangPackage = createPackage("package x.y; " + "@Description { value:\"This function would say hello\"}" + "@Param { value:\"message: The message sent\" }" + "@Return { value:\"int representation of the message\" }" + "public function sayHello(string message) returns (int){}");
    FunctionDoc functionDoc = Generator.createDocForNode(bLangPackage.getFunctions().get(0));
    Assert.assertEquals(functionDoc.name, "sayHello", "Function name should be extracted");
    Assert.assertEquals(functionDoc.description, "This function would say hello", "Description of the " + "function should be extracted");
    Assert.assertEquals(functionDoc.parameters.get(0).name, "message", "Parameter name should be extracted");
    Assert.assertEquals(functionDoc.parameters.get(0).dataType, "string", "Parameter type should be extracted");
    Assert.assertEquals(functionDoc.parameters.get(0).description, "The message sent", "Description of the " + "parameter should be extracted");
    Assert.assertEquals(functionDoc.returnParams.get(0).dataType, "int", "Return parameter type " + "should be extracted");
    Assert.assertEquals(functionDoc.returnParams.get(0).description, "int representation of the message", "Description of the return parameter should be extracted");
}
Also used : BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) FunctionDoc(org.ballerinalang.docgen.model.FunctionDoc) Test(org.testng.annotations.Test)

Aggregations

FunctionDoc (org.ballerinalang.docgen.model.FunctionDoc)3 Test (org.testng.annotations.Test)2 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)2 ArrayList (java.util.ArrayList)1 Page (org.ballerinalang.docgen.model.Page)1 Variable (org.ballerinalang.docgen.model.Variable)1 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)1