shorter tests

This commit is contained in:
Umur Ozkul 2022-04-05 19:18:10 +02:00
parent 74da6a0ae2
commit bbcd7f2640

View File

@ -1,6 +1,12 @@
open Jest open Jest
open Reducer_TestHelpers open Reducer_TestHelpers
let testParseToBe = (expr, answer) =>
test(expr, () => expectParseToBe(expr, answer))
let testEvalToBe = (expr, answer) =>
test(expr, () => expectEvalToBe(expr, answer))
describe("reducer using mathjs parse", () => { describe("reducer using mathjs parse", () => {
// Test the MathJs parser compatibility // Test the MathJs parser compatibility
// Those tests toString that there is a semantic mapping from MathJs to Expression // Those tests toString that there is a semantic mapping from MathJs to Expression
@ -10,20 +16,20 @@ describe("reducer using mathjs parse", () => {
// Those tests toString that we are converting mathjs parse tree to what we need // Those tests toString that we are converting mathjs parse tree to what we need
describe("expressions", () => { describe("expressions", () => {
test("1", () => expectParseToBe("1", "Ok(1)")) testParseToBe("1", "Ok(1)")
test("(1)", () => expectParseToBe("(1)", "Ok(1)")) testParseToBe("(1)", "Ok(1)")
test("1+2", () => expectParseToBe("1+2", "Ok((:add 1 2))")) testParseToBe("1+2", "Ok((:add 1 2))")
test("(1+2)", () => expectParseToBe("1+2", "Ok((:add 1 2))")) testParseToBe("1+2", "Ok((:add 1 2))")
test("add(1,2)", () => expectParseToBe("1+2", "Ok((:add 1 2))")) testParseToBe("1+2", "Ok((:add 1 2))")
test("1+2*3", () => expectParseToBe("1+2*3", "Ok((:add 1 (:multiply 2 3)))")) testParseToBe("1+2*3", "Ok((:add 1 (:multiply 2 3)))")
}) })
describe("arrays", () => { describe("arrays", () => {
//Note. () is a empty list in Lisp //Note. () is a empty list in Lisp
// The only builtin structure in Lisp is list. There are no arrays // The only builtin structure in Lisp is list. There are no arrays
// [1,2,3] becomes (1 2 3) // [1,2,3] becomes (1 2 3)
test("empty", () => expectParseToBe("[]", "Ok(())")) test("empty", () => expectParseToBe("[]", "Ok(())"))
test("[1, 2, 3]", () => expectParseToBe("[1, 2, 3]", "Ok((1 2 3))")) testParseToBe("[1, 2, 3]", "Ok((1 2 3))")
test("['hello', 'world']", () => expectParseToBe("['hello', 'world']", "Ok(('hello' 'world'))")) testParseToBe("['hello', 'world']", "Ok(('hello' 'world'))")
test("index", () => expectParseToBe("([0,1,2])[1]", "Ok((:$atIndex (0 1 2) (1)))")) test("index", () => expectParseToBe("([0,1,2])[1]", "Ok((:$atIndex (0 1 2) (1)))"))
}) })
describe("records", () => { describe("records", () => {
@ -45,20 +51,20 @@ describe("eval", () => {
// See https://mathjs.org/docs/expressions/syntax.html // See https://mathjs.org/docs/expressions/syntax.html
// See https://mathjs.org/docs/reference/functions.html // See https://mathjs.org/docs/reference/functions.html
describe("expressions", () => { describe("expressions", () => {
test("1", () => expectEvalToBe("1", "Ok(1)")) testEvalToBe("1", "Ok(1)")
test("1+2", () => expectEvalToBe("1+2", "Ok(3)")) testEvalToBe("1+2", "Ok(3)")
test("(1+2)*3", () => expectEvalToBe("(1+2)*3", "Ok(9)")) testEvalToBe("(1+2)*3", "Ok(9)")
test("2>1", () => expectEvalToBe("2>1", "Ok(true)")) testEvalToBe("2>1", "Ok(true)")
test("concat('a ', 'b')", () => expectEvalToBe("concat('a ', 'b')", "Ok('a b')")) testEvalToBe("concat('a ', 'b')", "Ok('a b')")
test("log(10)", () => expectEvalToBe("log(10)", "Ok(2.302585092994046)")) testEvalToBe("log(10)", "Ok(2.302585092994046)")
test("cos(10)", () => expectEvalToBe("cos(10)", "Ok(-0.8390715290764524)")) testEvalToBe("cos(10)", "Ok(-0.8390715290764524)")
// TODO more built ins // TODO more built ins
}) })
describe("arrays", () => { describe("arrays", () => {
test("empty array", () => expectEvalToBe("[]", "Ok([])")) test("empty array", () => expectEvalToBe("[]", "Ok([])"))
test("[1, 2, 3]", () => expectEvalToBe("[1, 2, 3]", "Ok([1, 2, 3])")) testEvalToBe("[1, 2, 3]", "Ok([1, 2, 3])")
test("['hello', 'world']", () => expectEvalToBe("['hello', 'world']", "Ok(['hello', 'world'])")) testEvalToBe("['hello', 'world']", "Ok(['hello', 'world'])")
test("index", () => expectEvalToBe("([0,1,2])[1]", "Ok(1)")) testEvalToBe("([0,1,2])[1]", "Ok(1)")
test("index not found", () => test("index not found", () =>
expectEvalToBe("([0,1,2])[10]", "Error(Array index not found: 10)") expectEvalToBe("([0,1,2])[10]", "Error(Array index not found: 10)")
) )