Skip to content

Commit

Permalink
Added array length function
Browse files Browse the repository at this point in the history
  • Loading branch information
valkjsaaa committed Nov 12, 2023
1 parent ae3f356 commit e0fcfa3
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 79 deletions.
156 changes: 82 additions & 74 deletions lib/dsl/__test__/dsl-interpreter.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { DslInterpreter } from "../dsl-interpreter";
import {
allDescriptors,
recentBooking,
Restaurant,
} from "../../__test__/example_descriptor";
import { allDescriptors, recentBooking, Restaurant } from "../../__test__/example_descriptor";
import { initGenie } from "../../decorators";

initGenie();

test("Basic function call", async () => {
Expand All @@ -16,7 +13,7 @@ test("Basic function call", async () => {
expect(funcCallResult).toEqual({
objectType: "void",
type: "object",
value: undefined,
value: undefined
});
});

Expand All @@ -26,7 +23,7 @@ test("Indexing", async () => {
expect(funcCallResult).toEqual({
objectType: "string",
type: "object",
value: "KFC",
value: "KFC"
});
});

Expand All @@ -41,26 +38,26 @@ test("Array", async () => {
{
objectType: "string",
type: "object",
value: "KFC",
value: "KFC"
},
{
objectType: "string",
type: "object",
value: "McDonald's",
},
],
value: "McDonald's"
}
]
});
});

test("Array matching", async () => {
const interpreter = new DslInterpreter(allDescriptors);
const funcCallResult = await interpreter.interpret(
'Restaurant.current().menu.matching(field: .name, value: "hamburger")[0].name'
"Restaurant.current().menu.matching(field: .name, value: \"hamburger\")[0].name"
);
expect(funcCallResult).toEqual({
objectType: "string",
type: "object",
value: "Hamburger",
value: "Hamburger"
});
});

Expand All @@ -72,7 +69,7 @@ test("Array between", async () => {
expect(funcCallResult).toEqual({
objectType: "string",
type: "object",
value: "McDonald's",
value: "McDonald's"
});
});

Expand All @@ -84,7 +81,7 @@ test("Array equals", async () => {
expect(funcCallResult).toEqual({
objectType: "string",
type: "object",
value: "KFC",
value: "KFC"
});
});

Expand All @@ -96,19 +93,31 @@ test("Array sort", async () => {
expect(funcCallResult).toEqual({
objectType: "string",
type: "object",
value: "Taste",
value: "Taste"
});
});

test("Array.Array", async () => {
test("Array in array", async () => {
const interpreter = new DslInterpreter(allDescriptors);
const funcCallResult = await interpreter.interpret(
"Restaurant.all().menu.matching(field:.name, value:\"Hamburger\")[0][0].name"
);
expect(funcCallResult).toEqual({
objectType: "string",
type: "object",
value: "Hamburger",
value: "Hamburger"
});
});

test("length", async () => {
const interpreter = new DslInterpreter(allDescriptors);
const funcCallResult = await interpreter.interpret(
"Restaurant.all()[0].menu.matching(field:.name, value:\"Hamburger\").length()"
);
expect(funcCallResult).toEqual({
"objectType": "int",
"type": "object",
"value": 1
});
});

Expand All @@ -127,19 +136,19 @@ test("Array.Array", async () => {
test("find burger name", async () => {
const interpreter = new DslInterpreter(allDescriptors);
const funcCallResult = await interpreter.interpret(
'Restaurant.current().menu.matching(field: .name, value: "hamburger")[0].name'
"Restaurant.current().menu.matching(field: .name, value: \"hamburger\")[0].name"
);
expect(funcCallResult).toEqual({
objectType: "string",
type: "object",
value: "Hamburger",
value: "Hamburger"
});
});

test("best restaurant", async () => {
const interpreter = new DslInterpreter(allDescriptors);
const funcCallResult = await interpreter.interpret(
'Restaurant.all().matching(field: .address, value: "palo alto").sort(field: .rating, ascending: false)[0]'
"Restaurant.all().matching(field: .address, value: \"palo alto\").sort(field: .rating, ascending: false)[0]"
);
expect(await interpreter.describe(funcCallResult)).toEqual({
type: "object",
Expand All @@ -148,15 +157,15 @@ test("best restaurant", async () => {
cuisine: "Chinese",
name: "Taste",
priceGrade: 3,
rating: 5,
},
rating: 5
}
});
});

test("find cheap chinese restaurant", async () => {
const interpreter = new DslInterpreter(allDescriptors);
const funcCallResult = await interpreter.interpret(
'Restaurant.all().matching(field: .cuisine, value: "chinese").sort(field: .priceGrade, ascending: true)[0]'
"Restaurant.all().matching(field: .cuisine, value: \"chinese\").sort(field: .priceGrade, ascending: true)[0]"
);
expect(await interpreter.describe(funcCallResult)).toEqual({
type: "object",
Expand All @@ -165,19 +174,19 @@ test("find cheap chinese restaurant", async () => {
cuisine: "Chinese",
name: "Steam",
priceGrade: 2,
rating: 4,
},
rating: 4
}
});
});

test("add hamburger to the order", async () => {
const interpreter = new DslInterpreter(allDescriptors);
const funcCallResult = await interpreter.interpret(
'Order.current().addFoods(foods: [Restaurant.current().menu.matching(field: .name, value: "hamburger")[0]])'
"Order.current().addFoods(foods: [Restaurant.current().menu.matching(field: .name, value: \"hamburger\")[0]])"
);
expect(await interpreter.describe(funcCallResult)).toEqual({
type: "object",
value: "undefined",
value: "undefined"
});
});

Expand All @@ -193,8 +202,8 @@ test("add hamburger to the order (steps)", async () => {
cuisine: "Fast Food",
name: "KFC",
priceGrade: 1,
rating: 3,
},
rating: 3
}
});
});

Expand All @@ -208,7 +217,7 @@ test("DateTime object comparison", async () => {
expect(funcCallResult).toEqual({
objectType: "string",
type: "object",
value: "McDonald's",
value: "McDonald's"
});
});

Expand All @@ -219,67 +228,66 @@ test("Order containing burger", async () => {
// 'Order.all().containing(field: .foods, value: Restaurant.current().menu.matching(field: .name, value: "hamburger")[0])[0].restaurant.name'
// )
const funcCallResult = await interpreter.interpret(
'Order.all().contains(field: .foods, value: Restaurant.current().menu.matching(field: .name, value: "hamburger")[0])[0].restaurant.name'
"Order.all().contains(field: .foods, value: Restaurant.current().menu.matching(field: .name, value: \"hamburger\")[0])[0].restaurant.name"
);
expect(funcCallResult).toEqual({
objectType: "string",
type: "object",
value: "McDonald's",
value: "McDonald's"
});
});

test("Order array distribution field", async () => {
Restaurant.all();
const interpreter = new DslInterpreter(allDescriptors);
const funcCallResult = await interpreter.interpret(
'Order.all().dateTime[0].dayOfWeek'
);
expect(funcCallResult).toEqual({
"objectType": "string",
"type": "object",
"value": "Wednesday"
});
const funcCallResult = await interpreter.interpret(
"Order.all().dateTime[0].dayOfWeek"
);
expect(funcCallResult).toEqual({
"objectType": "string",
"type": "object",
"value": "Wednesday"
});
});

test("Order array distribution function", async () => {
Restaurant.all();
const interpreter = new DslInterpreter(allDescriptors);
const funcCallResult = await interpreter.interpret(
'Order.all().placeOrder()'
);
expect(funcCallResult["objectType"]).toEqual("void");
Restaurant.all();
const interpreter = new DslInterpreter(allDescriptors);
const funcCallResult = await interpreter.interpret(
"Order.all().placeOrder()"
);
expect(funcCallResult["objectType"]).toEqual("void");
});

test("Multiple actions", async () => {
Restaurant.all();
const interpreter = new DslInterpreter(allDescriptors);
const funcCallResult = await interpreter.interpret(
'Order.all().placeOrder(); Order.all().dateTime[0].dayOfWeek'
);
expect(funcCallResult["objectType"]).toEqual("string");
Restaurant.all();
const interpreter = new DslInterpreter(allDescriptors);
const funcCallResult = await interpreter.interpret(
"Order.all().placeOrder(); Order.all().dateTime[0].dayOfWeek"
);
expect(funcCallResult["objectType"]).toEqual("string");
});

test("Single actions with semicolon", async () => {
Restaurant.all();
const interpreter = new DslInterpreter(allDescriptors);
const funcCallResult = await interpreter.interpret(
'Order.all().dateTime[0].dayOfWeek;'
);
expect(funcCallResult["objectType"]).toEqual("string");
Restaurant.all();
const interpreter = new DslInterpreter(allDescriptors);
const funcCallResult = await interpreter.interpret(
"Order.all().dateTime[0].dayOfWeek;"
);
expect(funcCallResult["objectType"]).toEqual("string");
});



test("[dry run] Order containing burger", async () => {
Restaurant.all();
const interpreter = new DslInterpreter(allDescriptors, true);
const funcCallResult = await interpreter.interpret(
'Order.all().contains(field: .foods, value: Restaurant.current().menu.matching(field: .name, value: "hamburger")[0])[0].restaurant.name'
"Order.all().contains(field: .foods, value: Restaurant.current().menu.matching(field: .name, value: \"hamburger\")[0])[0].restaurant.name"
);
expect(funcCallResult).toEqual({
objectType: "string",
type: "object",
value: null,
value: null
});
});

Expand All @@ -288,10 +296,10 @@ test("[dry run][error] Restaurant noise level", async () => {
const interpreter = new DslInterpreter(allDescriptors, true);
try {
await interpreter.interpret(
'Restaurant.All().matching(field: .noiseLevel, value: "quiet")'
"Restaurant.All().matching(field: .noiseLevel, value: \"quiet\")"
);
} catch (e) {
expect(e.message).toEqual("Field Restaurant.noiseLevel is missing");
expect(e.message).toEqual("Field Restaurant.noiseLevel is missing");
}
});

Expand All @@ -300,10 +308,10 @@ test("[dry run][error] Restaurant noise level 2", async () => {
const interpreter = new DslInterpreter(allDescriptors, true);
try {
await interpreter.interpret(
'Restaurant.All()[0].noiseLevel'
"Restaurant.All()[0].noiseLevel"
);
} catch (e) {
expect(e.message).toEqual("Field Restaurant.noiseLevel is missing");
expect(e.message).toEqual("Field Restaurant.noiseLevel is missing");
}
});

Expand All @@ -312,10 +320,10 @@ test("[dry run][error] Restaurant notify when near", async () => {
const interpreter = new DslInterpreter(allDescriptors, true);
try {
await interpreter.interpret(
'Order.current().notifyWhenNearLocation()'
"Order.current().notifyWhenNearLocation()"
);
} catch (e) {
expect(e.message).toEqual("Function Order.notifyWhenNearLocation is missing");
expect(e.message).toEqual("Function Order.notifyWhenNearLocation is missing");
}
});

Expand All @@ -324,10 +332,10 @@ test("[dry run][error] Login", async () => {
const interpreter = new DslInterpreter(allDescriptors, true);
try {
await interpreter.interpret(
'User.login()'
"User.login()"
);
} catch (e) {
expect(e.message).toEqual("Class User is missing");
expect(e.message).toEqual("Class User is missing");
}
});

Expand All @@ -337,10 +345,10 @@ test("[dry run][error] String", async () => {
const interpreter = new DslInterpreter(allDescriptors, true);
try {
await interpreter.interpret(
'Restaurant.All().matching(field: .location, value: "near me")'
"Restaurant.All().matching(field: .location, value: \"near me\")"
);
} catch (e) {
console.log(e)
console.log(e);
expect(e.message).toEqual("Field Restaurant.location is missing");
}
});
Expand All @@ -350,10 +358,10 @@ test("GetObject", async () => {
const interpreter = new DslInterpreter(allDescriptors, true);
try {
await interpreter.interpret(
'Restaurant.GetObject(id:0)'
"Restaurant.GetObject(id:0)"
);
} catch (e) {
console.log(e)
console.log(e);
expect(e.message).toEqual("Function Restaurant.GetObject is missing");
}
});
Expand Down
Loading

0 comments on commit e0fcfa3

Please sign in to comment.