Commit 3e1c844f authored by Giuseppe Tropea's avatar Giuseppe Tropea
Browse files

more sophisticated parsing of API spec

parent 932be110
Loading
Loading
Loading
Loading
+73 −15
Original line number Diff line number Diff line
@@ -7,7 +7,12 @@ fullapi = JSON.parse(fullapi);
var i_am_inside = false;

var testcasemetadata = {};
for(let block of fullapi.blocks) {
testcasemetadata.seqnumber = 0;
testcasemetadata.progressive_subid = 0;
number_of_blocks = fullapi.blocks.length;
current_index = 0;
while(current_index < number_of_blocks) {
    block = fullapi.blocks[current_index];
    let type = block.t;
    let value = block.c;
    if (is_a_apigroup_header(type, value)) {
@@ -15,43 +20,87 @@ for(let block of fullapi.blocks) {
            console.log("H2 group not Str!");
            process.exit();
        }
        testcasemetadata.group = value[2].slice(1).filter(el => el.t != "Space").map(el => el.c).join(" ");
        console.log("   H2 "+testcasemetadata.group);
        // we collect all the words composing the clause name, example: "5.6 Context Information Provision"
        // but skip the first token because it is the clause number
        testcasemetadata.descriptive_group = value[2].slice(1).filter(el => el.t != "Space").map(el => el.c).join(" ");
    }
    if (is_a_apioperation_header(type, value)) {
        if(value[2][0].t != "Str") {
            console.log("H3 operation not Str!");
            exit();
        }
        testcasemetadata.operation = value[2].slice(1).filter(el => el.t != "Space").map(el => el.c).join(" ");
        console.log("         H3 "+testcasemetadata.group+" "+testcasemetadata.operation);
        // we create metadata for all testcases of this operation:
        // the first token is the clause number
        testcasemetadata.clause_number = value[2][0].c;
        // the group is composed of the first two words of the group descriptive header string taken from the API document
        testcasemetadata.group_fromspec = compact_string_array(testcasemetadata.descriptive_group.split(" ").slice(0, 2));
        // the subgroup is composed of the remaining (after 2 till end) words of the group header string taken from the API document
        testcasemetadata.subgroup_fromspec = compact_string_array(testcasemetadata.descriptive_group.split(" ").slice(2));
        // for the name of the operation we collect all the words composing the sub-clause name, example "5.6.1 Create Entity"
        // but skip the first token because it is the sub-clause number  
        testcasemetadata.descriptive_operation = value[2].slice(1).filter(el => el.t != "Space").map(el => el.c).join(" ");
        console.log("         H3 - "+testcasemetadata.descriptive_group+" - "+testcasemetadata.descriptive_operation);
        // the operation is composed from the descriptive header string of the operation itself taken from the API document
        testcasemetadata.operation_fromspec = compact_string_array(testcasemetadata.descriptive_operation.split(" "));
    }
    if (is_a_behaviour_header(type, value) && i_am_inside === false) {
        i_am_inside = true;
        outputLogger.log("entering "+value[2][0].c);
        testcasemetadata.seqnumber++;
        console.log("entering "+value[2][0].c+" META "+testcasemetadata.group_fromspec+" "+testcasemetadata.subgroup_fromspec+" "+testcasemetadata.operation_fromspec);
    }
    else if (i_am_inside === true && type === "Header") { // i get out once i reach whatever Header follows
        i_am_inside = false;
        outputLogger.log("exiting "+value[2][0].c);
        // ressetting the subindex
        testcasemetadata.progressive_subid = 0;
        console.log("exiting "+value[2][0].c);
        current_index--; // parse the current exit block again because we might have two good blocks back-to-back
    }
    else if (i_am_inside === true) {
        if (type === "BulletList") {
            outputLogger.log("..collecting BL of "+value.length);
        }
        else if (type === "Para") {
            outputLogger.log("..collecting PARA of "+value.length);
        if (type === "BulletList" || type === "Para") {
            testcasemetadata.progressive_subid++;
            testcasemetadata.tp_id = testcasemetadata.seqnumber.toString().padStart(3, '0') + "_" + testcasemetadata.progressive_subid.toString().padStart(2, '0')
            testcasemetadata.description = value.filter(el => el.t === "Str").map(el => el.c).join(" ");
            console.log("..collecting BLock "+current_index+" of "+value.length+" in "+testcasemetadata.tp_id);
            outputLogger.log(testcasemetadata.tp_id)
            outputLogger.log(testcasemetadata.group_fromspec+"/"+testcasemetadata.subgroup_fromspec+"/"+testcasemetadata.operation_fromspec+"/");
            outputLogger.log(testcasemetadata.clause_number);
            outputLogger.log(testcasemetadata.description);
            outputLogger.log("");
        }
        else {
            outputLogger.log("..not collecting "+type+": "+value);
            console.log("..not collecting "+type+": "+value);
        }
    }
    current_index++;
}

// check if we are entering a level-4 header
// check if we are entering a level-4 header that spells "5.x.x.x Behaviour"
// or a level-3 common Behaviour greater that 5.5.2
function is_a_behaviour_header(type, value) {
    if (type === "Header" && value[0] === 4 && value[2][0].c.startsWith("5.") && value[2][2].c === "Behaviour") {
        return true;
    }

    if(is_a_apioperation_header(type, value) && value[2][0].c.startsWith("5.5.")) {
        subclause_of_clause_five = parseInt(value[2][0].c.substring(4)); // from the 4th char, i.e. skipping "5.5." at the beginning
        console.log(subclause_of_clause_five);
        if(subclause_of_clause_five >= 4 && subclause_of_clause_five <= 99) {
            // if we are in subclause 5.5.4 onwards, but not 5.6.X
            console.log(subclause_of_clause_five);
            return true;
        }
    }

    if(is_a_apioperation_header(type, value) && value[2][0].c.startsWith("5.13.")) {
        subclause_of_clause_five = parseInt(value[2][0].c.substring(5)); // from the 4th char, i.e. skipping "5.13." at the beginning
        console.log(subclause_of_clause_five);
        if(subclause_of_clause_five >= 2 && subclause_of_clause_five <= 99) {
            // if we are in subclause 5.5.4 onwards, but not 5.6.X
            console.log(subclause_of_clause_five);
            return true;
        }
    }

    return false;
}

@@ -63,10 +112,19 @@ function is_a_apigroup_header(type, value) {
    return false;
}

// check if we are parsing a level-2 API group header
// check if we are parsing a level-2 API operation header
function is_a_apioperation_header(type, value) {
    if (type === "Header" && value[0] === 3 && value[2][0].c.startsWith("5.")) {
        return true;
    }
    return false;
}

// creates an identifier out of a sequence of words composing the name of an API operation
function compact_string_array(array_of_strings) {
    identifier = array_of_strings.join("");
    if(identifier === "") {
        return "Generic"
    }
    return identifier;
}
 No newline at end of file