2022-03-24 12:41:31 +00:00
open Jest
open Reducer_TestHelpers
describe("eval", () => {
describe("expressions", () => {
2022-04-05 17:18:10 +00:00
testEvalToBe("1", "Ok(1)")
2022-09-19 17:46:37 +00:00
testEvalToBe("-1", "Ok(-1)")
testEvalToBe("1-1", "Ok(0)")
2022-04-05 17:18:10 +00:00
testEvalToBe("1+2", "Ok(3)")
testEvalToBe("(1+2)*3", "Ok(9)")
testEvalToBe("2>1", "Ok(true)")
testEvalToBe("concat('a ', 'b')", "Ok('a b')")
2022-09-17 23:15:58 +00:00
testEvalToBe("concat([3,4], [5,6,7])", "Ok([3,4,5,6,7])")
2022-04-05 17:18:10 +00:00
testEvalToBe("log(10)", "Ok(2.302585092994046)")
2022-09-19 17:46:37 +00:00
testEvalToBe("Math.cos(10)", "Ok(-0.8390715290764524)")
2022-03-24 12:41:31 +00:00
// TODO more built ins
})
2022-09-19 17:46:37 +00:00
describe("missing function", () => {
testEvalToBe("testZadanga(1)", "Error(testZadanga is not defined)")
testEvalToBe(
"arr = [normal(3,2)]; map(arr, zarathsuzaWasHere)",
"Error(zarathsuzaWasHere is not defined)",
)
})
2022-03-24 12:41:31 +00:00
describe("arrays", () => {
2022-03-29 09:09:59 +00:00
test("empty array", () => expectEvalToBe("[]", "Ok([])"))
2022-04-28 16:35:09 +00:00
testEvalToBe("[1, 2, 3]", "Ok([1,2,3])")
testEvalToBe("['hello', 'world']", "Ok(['hello','world'])")
2022-04-05 17:18:10 +00:00
testEvalToBe("([0,1,2])[1]", "Ok(1)")
2022-04-12 14:19:50 +00:00
testDescriptionEvalToBe("index not found", "([0,1,2])[10]", "Error(Array index not found: 10)")
2022-03-24 12:41:31 +00:00
})
describe("records", () => {
2022-04-28 16:35:09 +00:00
test("define", () => expectEvalToBe("{a: 1, b: 2}", "Ok({a: 1,b: 2})"))
2022-05-05 19:45:25 +00:00
test("index", () => expectEvalToBe("r = {a: 1}; r.a", "Ok(1)"))
test("index", () => expectEvalToBe("r = {a: 1}; r.b", "Error(Record property not found: b)"))
2022-05-19 18:25:18 +00:00
testEvalError("{a: 1}.b") // invalid syntax
2022-10-12 16:11:28 +00:00
test(
"always the same property ending",
() =>
expectEvalToBe(
`{
2022-07-30 20:14:16 +00:00
a: 1,
b: 2,
}`,
2022-10-12 16:11:28 +00:00
"Ok({a: 1,b: 2})",
),
2022-07-30 20:14:16 +00:00
)
2022-03-24 12:41:31 +00:00
})
2022-04-08 12:14:37 +00:00
describe("multi-line", () => {
2022-05-05 19:45:25 +00:00
testEvalError("1; 2")
testEvalError("1+1; 2+1")
2022-04-08 12:14:37 +00:00
})
describe("assignment", () => {
testEvalToBe("x=1; x", "Ok(1)")
testEvalToBe("x=1+1; x+1", "Ok(3)")
testEvalToBe("x=1; y=x+1; y+1", "Ok(3)")
2022-05-05 19:45:25 +00:00
testEvalError("1; x=1")
testEvalError("1; 1")
2022-07-25 14:34:33 +00:00
testEvalToBe("x=1; x=1; x", "Ok(1)")
2022-04-08 12:14:37 +00:00
})
2022-09-19 17:46:37 +00:00
describe("blocks", () => {
testEvalToBe("x = { y = { z = 5; z * 2 }; y + 3 }; x", "Ok(13)")
})
2022-03-24 12:41:31 +00:00
})
describe("test exceptions", () => {
2022-04-12 14:19:50 +00:00
testDescriptionEvalToBe(
2022-04-05 18:54:51 +00:00
"javascript exception",
2022-04-08 12:14:37 +00:00
"javascriptraise('div by 0')",
2022-09-26 00:44:08 +00:00
"Error(JS Exception: Error: 'div by 0')",
2022-04-05 18:54:51 +00:00
)
2022-05-05 19:45:25 +00:00
// testDescriptionEvalToBe(
// "rescript exception",
// "rescriptraise()",
// "Error(TODO: unhandled rescript exception)",
// )
2022-03-24 12:41:31 +00:00
})
2022-10-05 14:07:37 +00:00
describe("stacktraces", () => {
test("nested calls", () => {
open Expect
let error =
Expression.BackCompatible.evaluateString(`
f(x) = {
y = "a"
x + y
}
g = {|x| f(x)}
h(x) = g(x)
h(5)
`)
->E.R.getError
->E.O2.toExn("oops")
->SqError.toStringWithStackTrace
expect(
error,
)->toBe(`Error: There are function matches for add(), but with different arguments: [add(number, number)]; [add(distribution, number)]; [add(number, distribution)]; [add(distribution, distribution)]; [add(date, duration)]; [add(duration, duration)]
Stack trace:
f at line 4, column 5
2022-10-05 15:19:22 +00:00
g at line 6, column 12
2022-10-05 14:07:37 +00:00
h at line 7, column 10
<top> at line 8, column 3
`)
})
})