2022-03-24 12:41:31 +00:00
|
|
|
open Jest
|
|
|
|
open Reducer_TestHelpers
|
|
|
|
|
|
|
|
describe("eval", () => {
|
|
|
|
// All MathJs operators and functions are builtin for string, float and boolean
|
|
|
|
// .e.g + - / * > >= < <= == /= not and or
|
|
|
|
// See https://mathjs.org/docs/reference/functions.html
|
|
|
|
describe("expressions", () => {
|
2022-04-05 17:18:10 +00:00
|
|
|
testEvalToBe("1", "Ok(1)")
|
|
|
|
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)")
|
|
|
|
testEvalToBe("cos(10)", "Ok(-0.8390715290764524)")
|
2022-03-24 12:41:31 +00:00
|
|
|
// TODO more built ins
|
|
|
|
})
|
|
|
|
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-07-30 20:14:16 +00:00
|
|
|
test("always the same property ending", () =>
|
|
|
|
expectEvalToBe(
|
|
|
|
`{
|
|
|
|
a: 1,
|
|
|
|
b: 2,
|
|
|
|
}`,
|
|
|
|
"Ok({a: 1,b: 2})",
|
|
|
|
)
|
|
|
|
)
|
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-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-08 21:13:28 +00:00
|
|
|
"Error(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
|
|
|
})
|