Merge pull request #601 from quantified-uncertainty/reducer-dev
More readable language tests
This commit is contained in:
commit
9320e831ff
|
@ -88,17 +88,13 @@ describe("block", () => {
|
||||||
)
|
)
|
||||||
testMacroEval([], eBlock(list{exampleStatementY, exampleStatementZ}), "Ok({y: 1,z: 1})")
|
testMacroEval([], eBlock(list{exampleStatementY, exampleStatementZ}), "Ok({y: 1,z: 1})")
|
||||||
// Block inside a block
|
// Block inside a block
|
||||||
testMacro(
|
testMacro([], eBlock(list{eBlock(list{exampleExpression})}), "Ok((:$$_bindExpression_$$ {1}))")
|
||||||
[],
|
|
||||||
eBlock(list{eBlock(list{exampleExpression})}),
|
|
||||||
"Ok((:$$_bindExpression_$$ (:$$_block_$$ 1)))",
|
|
||||||
)
|
|
||||||
testMacroEval([], eBlock(list{eBlock(list{exampleExpression})}), "Ok(1)")
|
testMacroEval([], eBlock(list{eBlock(list{exampleExpression})}), "Ok(1)")
|
||||||
// Block assigned to a variable
|
// Block assigned to a variable
|
||||||
testMacro(
|
testMacro(
|
||||||
[],
|
[],
|
||||||
eBlock(list{eLetStatement("z", eBlock(list{eBlock(list{exampleExpressionY})}))}),
|
eBlock(list{eLetStatement("z", eBlock(list{eBlock(list{exampleExpressionY})}))}),
|
||||||
"Ok((:$$_bindExpression_$$ (:$_let_$ :z (:$$_block_$$ (:$$_block_$$ :y)))))",
|
"Ok((:$$_bindExpression_$$ (:$_let_$ :z {{:y}})))",
|
||||||
)
|
)
|
||||||
testMacroEval(
|
testMacroEval(
|
||||||
[],
|
[],
|
||||||
|
@ -116,7 +112,7 @@ describe("block", () => {
|
||||||
eSymbol("y"),
|
eSymbol("y"),
|
||||||
}),
|
}),
|
||||||
}),
|
}),
|
||||||
"Ok((:$$_bindExpression_$$ (:$$_block_$$ (:$_let_$ :y (:add :x 1)) :y)))",
|
"Ok((:$$_bindExpression_$$ {(:$_let_$ :y (:add :x 1)); :y}))",
|
||||||
)
|
)
|
||||||
testMacroEval(
|
testMacroEval(
|
||||||
[("x", EvNumber(1.))],
|
[("x", EvNumber(1.))],
|
||||||
|
|
|
@ -40,111 +40,101 @@ module MyOnly = {
|
||||||
describe("Peggy to Expression", () => {
|
describe("Peggy to Expression", () => {
|
||||||
describe("literals operators parenthesis", () => {
|
describe("literals operators parenthesis", () => {
|
||||||
// Note that there is always an outer block. Otherwise, external bindings are ignrored at the first statement
|
// Note that there is always an outer block. Otherwise, external bindings are ignrored at the first statement
|
||||||
testToExpression("1", "(:$$_block_$$ 1)", ~v="1", ())
|
testToExpression("1", "{1}", ~v="1", ())
|
||||||
testToExpression("'hello'", "(:$$_block_$$ 'hello')", ~v="'hello'", ())
|
testToExpression("'hello'", "{'hello'}", ~v="'hello'", ())
|
||||||
testToExpression("true", "(:$$_block_$$ true)", ~v="true", ())
|
testToExpression("true", "{true}", ~v="true", ())
|
||||||
testToExpression("1+2", "(:$$_block_$$ (:add 1 2))", ~v="3", ())
|
testToExpression("1+2", "{(:add 1 2)}", ~v="3", ())
|
||||||
testToExpression("add(1,2)", "(:$$_block_$$ (:add 1 2))", ~v="3", ())
|
testToExpression("add(1,2)", "{(:add 1 2)}", ~v="3", ())
|
||||||
testToExpression("(1)", "(:$$_block_$$ 1)", ())
|
testToExpression("(1)", "{1}", ())
|
||||||
testToExpression("(1+2)", "(:$$_block_$$ (:add 1 2))", ())
|
testToExpression("(1+2)", "{(:add 1 2)}", ())
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("unary", () => {
|
describe("unary", () => {
|
||||||
testToExpression("-1", "(:$$_block_$$ (:unaryMinus 1))", ~v="-1", ())
|
testToExpression("-1", "{(:unaryMinus 1)}", ~v="-1", ())
|
||||||
testToExpression("!true", "(:$$_block_$$ (:not true))", ~v="false", ())
|
testToExpression("!true", "{(:not true)}", ~v="false", ())
|
||||||
testToExpression("1 + -1", "(:$$_block_$$ (:add 1 (:unaryMinus 1)))", ~v="0", ())
|
testToExpression("1 + -1", "{(:add 1 (:unaryMinus 1))}", ~v="0", ())
|
||||||
testToExpression("-a[0]", "(:$$_block_$$ (:unaryMinus (:$_atIndex_$ :a 0)))", ())
|
testToExpression("-a[0]", "{(:unaryMinus (:$_atIndex_$ :a 0))}", ())
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("multi-line", () => {
|
describe("multi-line", () => {
|
||||||
testToExpression("x=1; 2", "(:$$_block_$$ (:$_let_$ :x (:$$_block_$$ 1)) 2)", ~v="2", ())
|
testToExpression("x=1; 2", "{(:$_let_$ :x {1}); 2}", ~v="2", ())
|
||||||
testToExpression(
|
testToExpression("x=1; y=2", "{(:$_let_$ :x {1}); (:$_let_$ :y {2})}", ~v="{x: 1,y: 2}", ())
|
||||||
"x=1; y=2",
|
|
||||||
"(:$$_block_$$ (:$_let_$ :x (:$$_block_$$ 1)) (:$_let_$ :y (:$$_block_$$ 2)))",
|
|
||||||
~v="{x: 1,y: 2}",
|
|
||||||
(),
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("variables", () => {
|
describe("variables", () => {
|
||||||
testToExpression("x = 1", "(:$$_block_$$ (:$_let_$ :x (:$$_block_$$ 1)))", ~v="{x: 1}", ())
|
testToExpression("x = 1", "{(:$_let_$ :x {1})}", ~v="{x: 1}", ())
|
||||||
testToExpression("x", "(:$$_block_$$ :x)", ~v=":x", ()) //TODO: value should return error
|
testToExpression("x", "{:x}", ~v=":x", ()) //TODO: value should return error
|
||||||
testToExpression("x = 1; x", "(:$$_block_$$ (:$_let_$ :x (:$$_block_$$ 1)) :x)", ~v="1", ())
|
testToExpression("x = 1; x", "{(:$_let_$ :x {1}); :x}", ~v="1", ())
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("functions", () => {
|
describe("functions", () => {
|
||||||
testToExpression(
|
testToExpression(
|
||||||
"identity(x) = x",
|
"identity(x) = x",
|
||||||
"(:$$_block_$$ (:$_let_$ :identity (:$$_lambda_$$ [x] (:$$_block_$$ :x))))",
|
"{(:$_let_$ :identity (:$$_lambda_$$ [x] {:x}))}",
|
||||||
~v="{identity: lambda(x=>internal code)}",
|
~v="{identity: lambda(x=>internal code)}",
|
||||||
(),
|
(),
|
||||||
) // Function definitions become lambda assignments
|
) // Function definitions become lambda assignments
|
||||||
testToExpression("identity(x)", "(:$$_block_$$ (:identity :x))", ()) // Note value returns error properly
|
testToExpression("identity(x)", "{(:identity :x)}", ()) // Note value returns error properly
|
||||||
testToExpression(
|
testToExpression(
|
||||||
"f(x) = x> 2 ? 0 : 1; f(3)",
|
"f(x) = x> 2 ? 0 : 1; f(3)",
|
||||||
"(:$$_block_$$ (:$_let_$ :f (:$$_lambda_$$ [x] (:$$_block_$$ (:$$_ternary_$$ (:larger :x 2) 0 1)))) (:f 3))",
|
"{(:$_let_$ :f (:$$_lambda_$$ [x] {(:$$_ternary_$$ (:larger :x 2) 0 1)})); (:f 3)}",
|
||||||
~v="0",
|
~v="0",
|
||||||
(),
|
(),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("arrays", () => {
|
describe("arrays", () => {
|
||||||
testToExpression("[]", "(:$$_block_$$ (:$_constructArray_$ ()))", ~v="[]", ())
|
testToExpression("[]", "{(:$_constructArray_$ ())}", ~v="[]", ())
|
||||||
testToExpression("[0, 1, 2]", "(:$$_block_$$ (:$_constructArray_$ (0 1 2)))", ~v="[0,1,2]", ())
|
testToExpression("[0, 1, 2]", "{(:$_constructArray_$ (0 1 2))}", ~v="[0,1,2]", ())
|
||||||
testToExpression(
|
testToExpression(
|
||||||
"['hello', 'world']",
|
"['hello', 'world']",
|
||||||
"(:$$_block_$$ (:$_constructArray_$ ('hello' 'world')))",
|
"{(:$_constructArray_$ ('hello' 'world'))}",
|
||||||
~v="['hello','world']",
|
~v="['hello','world']",
|
||||||
(),
|
(),
|
||||||
)
|
)
|
||||||
testToExpression(
|
testToExpression("([0,1,2])[1]", "{(:$_atIndex_$ (:$_constructArray_$ (0 1 2)) 1)}", ~v="1", ())
|
||||||
"([0,1,2])[1]",
|
|
||||||
"(:$$_block_$$ (:$_atIndex_$ (:$_constructArray_$ (0 1 2)) 1))",
|
|
||||||
~v="1",
|
|
||||||
(),
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("records", () => {
|
describe("records", () => {
|
||||||
testToExpression(
|
testToExpression(
|
||||||
"{a: 1, b: 2}",
|
"{a: 1, b: 2}",
|
||||||
"(:$$_block_$$ (:$_constructRecord_$ (('a' 1) ('b' 2))))",
|
"{(:$_constructRecord_$ (('a' 1) ('b' 2)))}",
|
||||||
~v="{a: 1,b: 2}",
|
~v="{a: 1,b: 2}",
|
||||||
(),
|
(),
|
||||||
)
|
)
|
||||||
testToExpression(
|
testToExpression(
|
||||||
"{1+0: 1, 2+0: 2}",
|
"{1+0: 1, 2+0: 2}",
|
||||||
"(:$$_block_$$ (:$_constructRecord_$ (((:add 1 0) 1) ((:add 2 0) 2))))",
|
"{(:$_constructRecord_$ (((:add 1 0) 1) ((:add 2 0) 2)))}",
|
||||||
(),
|
(),
|
||||||
) // key can be any expression
|
) // key can be any expression
|
||||||
testToExpression("record.property", "(:$$_block_$$ (:$_atIndex_$ :record 'property'))", ())
|
testToExpression("record.property", "{(:$_atIndex_$ :record 'property')}", ())
|
||||||
testToExpression(
|
testToExpression(
|
||||||
"record={property: 1}; record.property",
|
"record={property: 1}; record.property",
|
||||||
"(:$$_block_$$ (:$_let_$ :record (:$$_block_$$ (:$_constructRecord_$ (('property' 1))))) (:$_atIndex_$ :record 'property'))",
|
"{(:$_let_$ :record {(:$_constructRecord_$ (('property' 1)))}); (:$_atIndex_$ :record 'property')}",
|
||||||
~v="1",
|
~v="1",
|
||||||
(),
|
(),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("comments", () => {
|
describe("comments", () => {
|
||||||
testToExpression("1 # This is a line comment", "(:$$_block_$$ 1)", ~v="1", ())
|
testToExpression("1 # This is a line comment", "{1}", ~v="1", ())
|
||||||
testToExpression("1 // This is a line comment", "(:$$_block_$$ 1)", ~v="1", ())
|
testToExpression("1 // This is a line comment", "{1}", ~v="1", ())
|
||||||
testToExpression("1 /* This is a multi line comment */", "(:$$_block_$$ 1)", ~v="1", ())
|
testToExpression("1 /* This is a multi line comment */", "{1}", ~v="1", ())
|
||||||
testToExpression("/* This is a multi line comment */ 1", "(:$$_block_$$ 1)", ~v="1", ())
|
testToExpression("/* This is a multi line comment */ 1", "{1}", ~v="1", ())
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("ternary operator", () => {
|
describe("ternary operator", () => {
|
||||||
testToExpression("true ? 1 : 0", "(:$$_block_$$ (:$$_ternary_$$ true 1 0))", ~v="1", ())
|
testToExpression("true ? 1 : 0", "{(:$$_ternary_$$ true 1 0)}", ~v="1", ())
|
||||||
testToExpression("false ? 1 : 0", "(:$$_block_$$ (:$$_ternary_$$ false 1 0))", ~v="0", ())
|
testToExpression("false ? 1 : 0", "{(:$$_ternary_$$ false 1 0)}", ~v="0", ())
|
||||||
testToExpression(
|
testToExpression(
|
||||||
"true ? 1 : false ? 2 : 0",
|
"true ? 1 : false ? 2 : 0",
|
||||||
"(:$$_block_$$ (:$$_ternary_$$ true 1 (:$$_ternary_$$ false 2 0)))",
|
"{(:$$_ternary_$$ true 1 (:$$_ternary_$$ false 2 0))}",
|
||||||
~v="1",
|
~v="1",
|
||||||
(),
|
(),
|
||||||
) // nested ternary
|
) // nested ternary
|
||||||
testToExpression(
|
testToExpression(
|
||||||
"false ? 1 : false ? 2 : 0",
|
"false ? 1 : false ? 2 : 0",
|
||||||
"(:$$_block_$$ (:$$_ternary_$$ false 1 (:$$_ternary_$$ false 2 0)))",
|
"{(:$$_ternary_$$ false 1 (:$$_ternary_$$ false 2 0))}",
|
||||||
~v="0",
|
~v="0",
|
||||||
(),
|
(),
|
||||||
) // nested ternary
|
) // nested ternary
|
||||||
|
@ -152,21 +142,21 @@ describe("Peggy to Expression", () => {
|
||||||
testToExpression(
|
testToExpression(
|
||||||
// expression binding
|
// expression binding
|
||||||
"f(a) = a > 5 ? 1 : 0; f(6)",
|
"f(a) = a > 5 ? 1 : 0; f(6)",
|
||||||
"(:$$_block_$$ (:$_let_$ :f (:$$_lambda_$$ [a] (:$$_block_$$ (:$$_ternary_$$ (:larger :a 5) 1 0)))) (:f 6))",
|
"{(:$_let_$ :f (:$$_lambda_$$ [a] {(:$$_ternary_$$ (:larger :a 5) 1 0)})); (:f 6)}",
|
||||||
~v="1",
|
~v="1",
|
||||||
(),
|
(),
|
||||||
)
|
)
|
||||||
testToExpression(
|
testToExpression(
|
||||||
// when true binding
|
// when true binding
|
||||||
"f(a) = a > 5 ? a : 0; f(6)",
|
"f(a) = a > 5 ? a : 0; f(6)",
|
||||||
"(:$$_block_$$ (:$_let_$ :f (:$$_lambda_$$ [a] (:$$_block_$$ (:$$_ternary_$$ (:larger :a 5) :a 0)))) (:f 6))",
|
"{(:$_let_$ :f (:$$_lambda_$$ [a] {(:$$_ternary_$$ (:larger :a 5) :a 0)})); (:f 6)}",
|
||||||
~v="6",
|
~v="6",
|
||||||
(),
|
(),
|
||||||
)
|
)
|
||||||
testToExpression(
|
testToExpression(
|
||||||
// when false binding
|
// when false binding
|
||||||
"f(a) = a < 5 ? 1 : a; f(6)",
|
"f(a) = a < 5 ? 1 : a; f(6)",
|
||||||
"(:$$_block_$$ (:$_let_$ :f (:$$_lambda_$$ [a] (:$$_block_$$ (:$$_ternary_$$ (:smaller :a 5) 1 :a)))) (:f 6))",
|
"{(:$_let_$ :f (:$$_lambda_$$ [a] {(:$$_ternary_$$ (:smaller :a 5) 1 :a)})); (:f 6)}",
|
||||||
~v="6",
|
~v="6",
|
||||||
(),
|
(),
|
||||||
)
|
)
|
||||||
|
@ -174,31 +164,23 @@ describe("Peggy to Expression", () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("if then else", () => {
|
describe("if then else", () => {
|
||||||
testToExpression(
|
testToExpression("if true then 2 else 3", "{(:$$_ternary_$$ true {2} {3})}", ())
|
||||||
"if true then 2 else 3",
|
testToExpression("if true then {2} else {3}", "{(:$$_ternary_$$ true {2} {3})}", ())
|
||||||
"(:$$_block_$$ (:$$_ternary_$$ true (:$$_block_$$ 2) (:$$_block_$$ 3)))",
|
|
||||||
(),
|
|
||||||
)
|
|
||||||
testToExpression(
|
|
||||||
"if true then {2} else {3}",
|
|
||||||
"(:$$_block_$$ (:$$_ternary_$$ true (:$$_block_$$ 2) (:$$_block_$$ 3)))",
|
|
||||||
(),
|
|
||||||
)
|
|
||||||
testToExpression(
|
testToExpression(
|
||||||
"if false then {2} else if false then {4} else {5}",
|
"if false then {2} else if false then {4} else {5}",
|
||||||
"(:$$_block_$$ (:$$_ternary_$$ false (:$$_block_$$ 2) (:$$_ternary_$$ false (:$$_block_$$ 4) (:$$_block_$$ 5))))",
|
"{(:$$_ternary_$$ false {2} (:$$_ternary_$$ false {4} {5}))}",
|
||||||
(),
|
(),
|
||||||
) //nested if
|
) //nested if
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("pipe", () => {
|
describe("pipe", () => {
|
||||||
testToExpression("1 -> add(2)", "(:$$_block_$$ (:add 1 2))", ~v="3", ())
|
testToExpression("1 -> add(2)", "{(:add 1 2)}", ~v="3", ())
|
||||||
testToExpression("-1 -> add(2)", "(:$$_block_$$ (:add (:unaryMinus 1) 2))", ~v="1", ()) // note that unary has higher priority naturally
|
testToExpression("-1 -> add(2)", "{(:add (:unaryMinus 1) 2)}", ~v="1", ()) // note that unary has higher priority naturally
|
||||||
testToExpression("1 -> add(2) * 3", "(:$$_block_$$ (:multiply (:add 1 2) 3))", ~v="9", ())
|
testToExpression("1 -> add(2) * 3", "{(:multiply (:add 1 2) 3)}", ~v="9", ())
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("elixir pipe", () => {
|
describe("elixir pipe", () => {
|
||||||
testToExpression("1 |> add(2)", "(:$$_block_$$ (:add 1 2))", ~v="3", ())
|
testToExpression("1 |> add(2)", "{(:add 1 2)}", ~v="3", ())
|
||||||
})
|
})
|
||||||
|
|
||||||
// see testParse for priorities of to and credibleIntervalToDistribution
|
// see testParse for priorities of to and credibleIntervalToDistribution
|
||||||
|
@ -208,34 +190,29 @@ describe("Peggy to Expression", () => {
|
||||||
// Like lambdas they have a local scope.
|
// Like lambdas they have a local scope.
|
||||||
testToExpression(
|
testToExpression(
|
||||||
"y=99; x={y=1; y}",
|
"y=99; x={y=1; y}",
|
||||||
"(:$$_block_$$ (:$_let_$ :y (:$$_block_$$ 99)) (:$_let_$ :x (:$$_block_$$ (:$_let_$ :y (:$$_block_$$ 1)) :y)))",
|
"{(:$_let_$ :y {99}); (:$_let_$ :x {(:$_let_$ :y {1}); :y})}",
|
||||||
~v="{x: 1,y: 99}",
|
~v="{x: 1,y: 99}",
|
||||||
(),
|
(),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("lambda", () => {
|
describe("lambda", () => {
|
||||||
testToExpression(
|
testToExpression("{|x| x}", "{(:$$_lambda_$$ [x] {:x})}", ~v="lambda(x=>internal code)", ())
|
||||||
"{|x| x}",
|
|
||||||
"(:$$_block_$$ (:$$_lambda_$$ [x] (:$$_block_$$ :x)))",
|
|
||||||
~v="lambda(x=>internal code)",
|
|
||||||
(),
|
|
||||||
)
|
|
||||||
testToExpression(
|
testToExpression(
|
||||||
"f={|x| x}",
|
"f={|x| x}",
|
||||||
"(:$$_block_$$ (:$_let_$ :f (:$$_block_$$ (:$$_lambda_$$ [x] (:$$_block_$$ :x)))))",
|
"{(:$_let_$ :f {(:$$_lambda_$$ [x] {:x})})}",
|
||||||
~v="{f: lambda(x=>internal code)}",
|
~v="{f: lambda(x=>internal code)}",
|
||||||
(),
|
(),
|
||||||
)
|
)
|
||||||
testToExpression(
|
testToExpression(
|
||||||
"f(x)=x",
|
"f(x)=x",
|
||||||
"(:$$_block_$$ (:$_let_$ :f (:$$_lambda_$$ [x] (:$$_block_$$ :x))))",
|
"{(:$_let_$ :f (:$$_lambda_$$ [x] {:x}))}",
|
||||||
~v="{f: lambda(x=>internal code)}",
|
~v="{f: lambda(x=>internal code)}",
|
||||||
(),
|
(),
|
||||||
) // Function definitions are lambda assignments
|
) // Function definitions are lambda assignments
|
||||||
testToExpression(
|
testToExpression(
|
||||||
"f(x)=x ? 1 : 0",
|
"f(x)=x ? 1 : 0",
|
||||||
"(:$$_block_$$ (:$_let_$ :f (:$$_lambda_$$ [x] (:$$_block_$$ (:$$_ternary_$$ :x 1 0)))))",
|
"{(:$_let_$ :f (:$$_lambda_$$ [x] {(:$$_ternary_$$ :x 1 0)}))}",
|
||||||
~v="{f: lambda(x=>internal code)}",
|
~v="{f: lambda(x=>internal code)}",
|
||||||
(),
|
(),
|
||||||
)
|
)
|
||||||
|
|
|
@ -5,7 +5,7 @@ open Reducer_TestHelpers
|
||||||
describe("Eval with Bindings", () => {
|
describe("Eval with Bindings", () => {
|
||||||
testEvalBindingsToBe("x", list{("x", ExpressionValue.EvNumber(1.))}, "Ok(1)")
|
testEvalBindingsToBe("x", list{("x", ExpressionValue.EvNumber(1.))}, "Ok(1)")
|
||||||
testEvalBindingsToBe("x+1", list{("x", ExpressionValue.EvNumber(1.))}, "Ok(2)")
|
testEvalBindingsToBe("x+1", list{("x", ExpressionValue.EvNumber(1.))}, "Ok(2)")
|
||||||
testParseToBe("y = x+1; y", "Ok((:$$_block_$$ (:$_let_$ :y (:$$_block_$$ (:add :x 1))) :y))")
|
testParseToBe("y = x+1; y", "Ok({(:$_let_$ :y {(:add :x 1)}); :y})")
|
||||||
testEvalBindingsToBe("y = x+1; y", list{("x", ExpressionValue.EvNumber(1.))}, "Ok(2)")
|
testEvalBindingsToBe("y = x+1; y", list{("x", ExpressionValue.EvNumber(1.))}, "Ok(2)")
|
||||||
testEvalBindingsToBe("y = x+1", list{("x", ExpressionValue.EvNumber(1.))}, "Ok({x: 1,y: 2})")
|
testEvalBindingsToBe("y = x+1", list{("x", ExpressionValue.EvNumber(1.))}, "Ok({x: 1,y: 2})")
|
||||||
})
|
})
|
||||||
|
|
|
@ -2,11 +2,8 @@ open Jest
|
||||||
open Reducer_TestHelpers
|
open Reducer_TestHelpers
|
||||||
|
|
||||||
describe("Parse function assignment", () => {
|
describe("Parse function assignment", () => {
|
||||||
testParseToBe("f(x)=x", "Ok((:$$_block_$$ (:$_let_$ :f (:$$_lambda_$$ [x] (:$$_block_$$ :x)))))")
|
testParseToBe("f(x)=x", "Ok({(:$_let_$ :f (:$$_lambda_$$ [x] {:x}))})")
|
||||||
testParseToBe(
|
testParseToBe("f(x)=2*x", "Ok({(:$_let_$ :f (:$$_lambda_$$ [x] {(:multiply 2 :x)}))})")
|
||||||
"f(x)=2*x",
|
|
||||||
"Ok((:$$_block_$$ (:$_let_$ :f (:$$_lambda_$$ [x] (:$$_block_$$ (:multiply 2 :x))))))",
|
|
||||||
)
|
|
||||||
//MathJs does not allow blocks in function definitions
|
//MathJs does not allow blocks in function definitions
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -51,7 +51,7 @@ describe("call and bindings", () => {
|
||||||
)
|
)
|
||||||
testParseToBe(
|
testParseToBe(
|
||||||
"f=99; g(x)=f; g(2)",
|
"f=99; g(x)=f; g(2)",
|
||||||
"Ok((:$$_block_$$ (:$_let_$ :f (:$$_block_$$ 99)) (:$_let_$ :g (:$$_lambda_$$ [x] (:$$_block_$$ :f))) (:g 2)))",
|
"Ok({(:$_let_$ :f {99}); (:$_let_$ :g (:$$_lambda_$$ [x] {:f})); (:g 2)})",
|
||||||
)
|
)
|
||||||
testEvalToBe("f=99; g(x)=f; g(2)", "Ok(99)")
|
testEvalToBe("f=99; g(x)=f; g(2)", "Ok(99)")
|
||||||
testEvalToBe("f(x)=x; g(x)=f(x); g(2)", "Ok(2)")
|
testEvalToBe("f(x)=x; g(x)=f(x); g(2)", "Ok(2)")
|
||||||
|
|
|
@ -2,7 +2,7 @@ open Jest
|
||||||
open Reducer_TestHelpers
|
open Reducer_TestHelpers
|
||||||
|
|
||||||
describe("Parse ternary operator", () => {
|
describe("Parse ternary operator", () => {
|
||||||
testParseToBe("true ? 'YES' : 'NO'", "Ok((:$$_block_$$ (:$$_ternary_$$ true 'YES' 'NO')))")
|
testParseToBe("true ? 'YES' : 'NO'", "Ok({(:$$_ternary_$$ true 'YES' 'NO')})")
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("Evaluate ternary operator", () => {
|
describe("Evaluate ternary operator", () => {
|
||||||
|
|
|
@ -123,13 +123,13 @@ describe("eval on distribution functions", () => {
|
||||||
|
|
||||||
describe("parse on distribution functions", () => {
|
describe("parse on distribution functions", () => {
|
||||||
describe("power", () => {
|
describe("power", () => {
|
||||||
testParse("normal(5,2) ^ normal(5,1)", "Ok((:$$_block_$$ (:pow (:normal 5 2) (:normal 5 1))))")
|
testParse("normal(5,2) ^ normal(5,1)", "Ok({(:pow (:normal 5 2) (:normal 5 1))})")
|
||||||
testParse("3 ^ normal(5,1)", "Ok((:$$_block_$$ (:pow 3 (:normal 5 1))))")
|
testParse("3 ^ normal(5,1)", "Ok({(:pow 3 (:normal 5 1))})")
|
||||||
testParse("normal(5,2) ^ 3", "Ok((:$$_block_$$ (:pow (:normal 5 2) 3)))")
|
testParse("normal(5,2) ^ 3", "Ok({(:pow (:normal 5 2) 3)})")
|
||||||
})
|
})
|
||||||
describe("subtraction", () => {
|
describe("subtraction", () => {
|
||||||
testParse("10 - normal(5,1)", "Ok((:$$_block_$$ (:subtract 10 (:normal 5 1))))")
|
testParse("10 - normal(5,1)", "Ok({(:subtract 10 (:normal 5 1))})")
|
||||||
testParse("normal(5,1) - 10", "Ok((:$$_block_$$ (:subtract (:normal 5 1) 10)))")
|
testParse("normal(5,1) - 10", "Ok({(:subtract (:normal 5 1) 10)})")
|
||||||
})
|
})
|
||||||
describe("pointwise arithmetic expressions", () => {
|
describe("pointwise arithmetic expressions", () => {
|
||||||
testParse(~skip=true, "normal(5,2) .+ normal(5,1)", "Ok((:dotAdd (:normal 5 2) (:normal 5 1)))")
|
testParse(~skip=true, "normal(5,2) .+ normal(5,1)", "Ok((:dotAdd (:normal 5 2) (:normal 5 1)))")
|
||||||
|
@ -137,23 +137,14 @@ describe("parse on distribution functions", () => {
|
||||||
~skip=true,
|
~skip=true,
|
||||||
"normal(5,2) .- normal(5,1)",
|
"normal(5,2) .- normal(5,1)",
|
||||||
"Ok((:$$_block_$$ (:dotSubtract (:normal 5 2) (:normal 5 1))))",
|
"Ok((:$$_block_$$ (:dotSubtract (:normal 5 2) (:normal 5 1))))",
|
||||||
// TODO: !!! returns "Ok((:$$_block_$$ (:dotPow (:normal 5 2) (:normal 5 1))))"
|
// TODO: !!! returns "Ok({(:dotPow (:normal 5 2) (:normal 5 1))})"
|
||||||
)
|
|
||||||
testParse(
|
|
||||||
"normal(5,2) .* normal(5,1)",
|
|
||||||
"Ok((:$$_block_$$ (:dotMultiply (:normal 5 2) (:normal 5 1))))",
|
|
||||||
)
|
|
||||||
testParse(
|
|
||||||
"normal(5,2) ./ normal(5,1)",
|
|
||||||
"Ok((:$$_block_$$ (:dotDivide (:normal 5 2) (:normal 5 1))))",
|
|
||||||
)
|
|
||||||
testParse(
|
|
||||||
"normal(5,2) .^ normal(5,1)",
|
|
||||||
"Ok((:$$_block_$$ (:dotPow (:normal 5 2) (:normal 5 1))))",
|
|
||||||
)
|
)
|
||||||
|
testParse("normal(5,2) .* normal(5,1)", "Ok({(:dotMultiply (:normal 5 2) (:normal 5 1))})")
|
||||||
|
testParse("normal(5,2) ./ normal(5,1)", "Ok({(:dotDivide (:normal 5 2) (:normal 5 1))})")
|
||||||
|
testParse("normal(5,2) .^ normal(5,1)", "Ok({(:dotPow (:normal 5 2) (:normal 5 1))})")
|
||||||
})
|
})
|
||||||
describe("equality", () => {
|
describe("equality", () => {
|
||||||
testParse("5 == normal(5,2)", "Ok((:$$_block_$$ (:equal 5 (:normal 5 2))))")
|
testParse("5 == normal(5,2)", "Ok({(:equal 5 (:normal 5 2))})")
|
||||||
})
|
})
|
||||||
describe("pointwise adding two normals", () => {
|
describe("pointwise adding two normals", () => {
|
||||||
testParse(~skip=true, "normal(5,2) .+ normal(5,1)", "Ok((:dotAdd (:normal 5 2) (:normal 5 1)))")
|
testParse(~skip=true, "normal(5,2) .+ normal(5,1)", "Ok((:dotAdd (:normal 5 2) (:normal 5 1)))")
|
||||||
|
|
|
@ -28,6 +28,11 @@ type reducerFn = (
|
||||||
*/
|
*/
|
||||||
let rec toString = expression =>
|
let rec toString = expression =>
|
||||||
switch expression {
|
switch expression {
|
||||||
|
| EList(list{EValue(EvCall("$$_block_$$")), ...statements}) =>
|
||||||
|
`{${Belt.List.map(statements, aValue => toString(aValue))
|
||||||
|
->Extra.List.interperse("; ")
|
||||||
|
->Belt.List.toArray
|
||||||
|
->Js.String.concatMany("")}}`
|
||||||
| EList(aList) =>
|
| EList(aList) =>
|
||||||
`(${Belt.List.map(aList, aValue => toString(aValue))
|
`(${Belt.List.map(aList, aValue => toString(aValue))
|
||||||
->Extra.List.interperse(" ")
|
->Extra.List.interperse(" ")
|
||||||
|
|
Loading…
Reference in New Issue
Block a user