skutter
Version:
Skutter: Opinionated BDD Browser based test framework
105 lines (104 loc) • 4.36 kB
JavaScript
;
class ListLibrary {
static findCurrentInspection(inspectionStack) {
if (inspectionStack === undefined || inspectionStack === null) {
throw new Error("[ListLibrary].[findCurrentInspection] inspectionStack is not specified.");
}
if (!inspectionStack || inspectionStack.length === 0 || typeof (inspectionStack) === "Stack") {
throw new Error(`[ListLibrary].[findCurrentInspection] Inspection object has not been set. Firstly call a step utilising the "I am Inspecting" phrase.`);
}
let inspected = inspectionStack.topItem;
if (!inspected) {
throw new Error("[ListLibrary].[findCurrentInspection] No item on inspection stack.");
}
return inspected;
}
static findCurrentListContainer(inspectionStack, throwOnWrongType) {
if (inspectionStack === undefined || inspectionStack === null || typeof (inspectionStack) === "Stack") {
throw new Error("[ListLibrary].[findCurrentListContainer] inspectionStack is not specified.");
}
let inspected = ListLibrary.findCurrentInspection(inspectionStack);
if (inspected.type === "ListContainer") {
return inspected;
}
if (throwOnWrongType) {
throw new Error(`[ListLibrary].[findCurrentListContainer] When utilising positional items, the inspection element must be a list. It is currently: "${inspected.type}"`);
}
return null;
}
static findCurrentListItem(inspectionStack, throwOnWrongType) {
if (inspectionStack === undefined || inspectionStack === null || typeof (inspectionStack) === "Stack") {
throw new Error("[ListLibrary].[findCurrentListItem] inspectionStack is not specified.");
}
let inspected = ListLibrary.findCurrentInspection(inspectionStack);
if (inspected === undefined || inspected === null) {
throw new Error("[ListLibrary].[findCurrentListItem] inspected item is null or undefined.");
}
if (inspected.type === "ListItem") {
return inspected;
}
if (throwOnWrongType) {
throw new Error('When inspecting items, the inspection element must be a list item. It is currently: "' + inspected.type + '"');
}
return null;
}
static findItemByPosition(inspected, position) {
let items = inspected.items;
if (items === undefined || items === null || items.length === 0) {
throw new Error("List items are undefined or there are no items.");
}
let positionIndex = ListLibrary.positionToItemsIndex(position, items);
if (positionIndex === -1) {
throw new Error('Positional value "' + position + '" does not resolve to a known integer.');
}
if (items.length < positionIndex) {
throw new Error(`Not enough items in target list. Desired: ${positionIndex}, available: ${items.length}`);
}
let item = items[positionIndex];
if (item === undefined || item == null) {
throw new Error(`Item at position ${positionIndex} was null or undefined.`);
}
return item;
}
static positionToItemsIndex(position, elements) {
let positionIndex = 0;
switch (position) {
case "first":
case "1st":
positionIndex = 1;
break;
case "second":
case "2nd":
positionIndex = 2;
break;
case "third":
case "3rd":
positionIndex = 3;
break;
case "fourth":
case "4th":
positionIndex = 4;
break;
case "fifth":
case "5th":
positionIndex = 5;
break;
case "sixth":
case "6th":
positionIndex = 6;
break;
case "seventh":
case "7th":
positionIndex = 7;
break;
case "last":
positionIndex = elements.length;
break;
default:
positionIndex = parseInt(position);
break;
}
return positionIndex - 1;
}
}
exports.ListLibrary = ListLibrary;