Search in sources :

Example 1 with StructDoc

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

the class HtmlDocTest method testAnonymousStructs.

@Test(description = "Tests whether anonymous structs are documented.")
public void testAnonymousStructs() {
    BLangPackage bLangPackage = createPackage("package x.y; " + "@Description { value:\"Represents a person\"}" + "@Field {value:\"id: The identification number\"}\n" + "@Field {value:\"address: The address of the person.\"}" + "public struct Person {" + "  string id;" + "  struct {" + "     string address1;" + "     string address2;" + "     string state = \"MN\";" + "  } address;" + "}");
    Page page = generatePage(bLangPackage);
    Assert.assertEquals(page.constructs.size(), 1);
    Assert.assertTrue(page.constructs.get(0) instanceof StructDoc, "Documentable of type StructDoc expected.");
    StructDoc personStructDoc = (StructDoc) page.constructs.get(0);
    Assert.assertEquals(personStructDoc.fields.size(), 2, "2 fields are expected.");
    Assert.assertEquals(personStructDoc.fields.get(0).name, "id", "Field \"id\" expected.");
    Assert.assertEquals(personStructDoc.fields.get(1).name, "address", "Field \"address\" expected.");
    Assert.assertEquals(personStructDoc.fields.get(1).description, "The address of the person.");
    Assert.assertEquals(personStructDoc.fields.get(1).dataType, "struct {string address1, string address2, string state}");
}
Also used : BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) Page(org.ballerinalang.docgen.model.Page) StructDoc(org.ballerinalang.docgen.model.StructDoc) Test(org.testng.annotations.Test)

Example 2 with StructDoc

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

the class Generator method createDocForNode.

/**
 * Create documentation for structs.
 * @param structNode ballerina struct node.
 * @return documentation for structs.
 */
public static StructDoc createDocForNode(BLangStruct structNode) {
    String structName = structNode.getName().value;
    // Check if its an anonymous struct
    if (structName.contains(ANONYMOUS_STRUCT)) {
        structName = "Anonymous Struct";
    }
    List<Field> fields = new ArrayList<>();
    // Iterate through the struct fields
    if (structNode.getFields().size() > 0) {
        for (BLangVariable param : structNode.getFields()) {
            String dataType = type(param);
            String desc = fieldAnnotation(structNode, param);
            String defaultValue = "";
            if (null != param.getInitialExpression()) {
                defaultValue = param.getInitialExpression().toString();
            }
            Field variable = new Field(param.getName().value, dataType, desc, defaultValue);
            fields.add(variable);
        }
    }
    return new StructDoc(structName, description(structNode), new ArrayList<>(), fields);
}
Also used : Field(org.ballerinalang.docgen.model.Field) ArrayList(java.util.ArrayList) StructDoc(org.ballerinalang.docgen.model.StructDoc) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Example 3 with StructDoc

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

the class HtmlDocTest method testStructPropertiesExtracted.

@Test(description = "Struct properties should be available via construct")
public void testStructPropertiesExtracted() throws Exception {
    BLangPackage bLangPackage = createPackage("package x.y; " + "@Description { value:\"Message sent by the client\"}" + "@Field {value:\"count: Number of retries\"}\n" + "@Field {value:\"interval: Retry interval in millisecond\"}" + "struct Message {int interval;int count;}");
    StructDoc structDoc = Generator.createDocForNode(bLangPackage.getStructs().get(0));
    Assert.assertEquals(structDoc.name, "Message", "Struct name should be extracted");
    Assert.assertEquals(structDoc.description, "Message sent by the client", "Description of the " + "struct should be extracted");
    // Struct fields
    Assert.assertEquals(structDoc.fields.get(0).name, "interval", "Struct field name should be extracted");
    Assert.assertEquals(structDoc.fields.get(0).dataType, "int", "Struct field type should be extracted");
    Assert.assertEquals(structDoc.fields.get(0).description, "Retry interval in millisecond", "Description of the struct field should be extracted");
}
Also used : BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) StructDoc(org.ballerinalang.docgen.model.StructDoc) Test(org.testng.annotations.Test)

Example 4 with StructDoc

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

the class HtmlDocTest method testStructDefaultValues.

@Test(description = "Tests whether default values are collected.")
public void testStructDefaultValues() {
    BLangPackage bLangPackage = createPackage("package x.y; " + "public struct Person {" + "  string id;" + "  string address = \"20,Palm Grove\";" + "}");
    Page page = generatePage(bLangPackage);
    Assert.assertEquals(page.constructs.size(), 1);
    Assert.assertTrue(page.constructs.get(0) instanceof StructDoc, "Documentable of type StructDoc expected.");
    StructDoc personStructDoc = (StructDoc) page.constructs.get(0);
    Assert.assertEquals(personStructDoc.fields.size(), 2, "2 fields are expected.");
    Assert.assertEquals(personStructDoc.fields.get(0).name, "id", "Field \"id\" expected.");
    Assert.assertEquals(personStructDoc.fields.get(1).name, "address", "Field \"address\" expected.");
    Assert.assertEquals(personStructDoc.fields.get(1).defaultValue, "20,Palm Grove", "Unexpected address value found.");
}
Also used : BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) Page(org.ballerinalang.docgen.model.Page) StructDoc(org.ballerinalang.docgen.model.StructDoc) Test(org.testng.annotations.Test)

Aggregations

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