squiggle/packages/squiggle-lang/__tests__/Reducer/Reducer_functionTricks_test.res

91 lines
3.5 KiB
Plaintext
Raw Normal View History

2022-05-02 09:43:09 +00:00
open Jest
open Reducer_TestHelpers
2022-05-02 10:55:28 +00:00
describe("Arity check", () => {
testEvalToBe("f(x,y) = x + y; f(1,2)", "Ok(3)")
testEvalToBe(
"f(x,y) = x + y; f(1)",
"Error(2 arguments expected. Instead 1 argument(s) were passed.)",
)
testEvalToBe(
"f(x,y) = x + y; f(1,2,3)",
"Error(2 arguments expected. Instead 3 argument(s) were passed.)",
)
testEvalToBe(
"f(x,y)=x+y; f(1,2,3,4)",
"Error(2 arguments expected. Instead 4 argument(s) were passed.)",
)
testEvalToBe(
"f(x,y)=x+y; f(1)",
"Error(2 arguments expected. Instead 1 argument(s) were passed.)",
)
testEvalToBe(
"f(x,y)=x(y); f(f)",
"Error(2 arguments expected. Instead 1 argument(s) were passed.)",
)
testEvalToBe("f(x)=x; f(f)", "Ok(lambda(x=>internal code))")
testEvalToBe(
"f(x,y)=x(y); f(z)",
"Error(2 arguments expected. Instead 1 argument(s) were passed.)",
)
})
2022-05-02 15:52:59 +00:00
describe("symbol not defined", () => {
2022-05-02 10:55:28 +00:00
testEvalToBe("f(x)=x(y); f(f)", "Error(y is not defined)")
testEvalToBe("f(x)=x; f(f)", "Ok(lambda(x=>internal code))")
testEvalToBe("f(x)=x(y); f(z)", "Error(z is not defined)")
2022-05-02 13:47:35 +00:00
testEvalToBe("f(x)=x(y); f(2)", "Error(2 is not a function)")
testEvalToBe("f(x)=x(1); f(2)", "Error(2 is not a function)")
2022-05-02 15:52:59 +00:00
})
2022-05-02 21:18:36 +00:00
describe("call and bindings", () => {
2022-05-02 16:44:35 +00:00
testEvalToBe("f(x)=x+1", "Ok({f: lambda(x=>internal code)})")
testEvalToBe("f(x)=x+1; f(1)", "Ok(2)")
2022-05-02 15:52:59 +00:00
testEvalToBe("f=1;y=2", "Ok({f: 1,y: 2})")
2022-05-02 16:44:35 +00:00
testEvalToBe("f(x)=x+1; y=f(1)", "Ok({f: lambda(x=>internal code),y: 2})")
testEvalToBe("f(x)=x+1; y=f(1); f(1)", "Ok(2)")
2022-05-02 15:52:59 +00:00
testEvalToBe("f(x)=x+1; y=f(1); z=f(1)", "Ok({f: lambda(x=>internal code),y: 2,z: 2})")
2022-05-02 16:44:35 +00:00
testEvalToBe(
"f(x)=x+1; g(x)=f(x)+1",
"Ok({f: lambda(x=>internal code),g: lambda(x=>internal code)})",
)
testParseToBe(
"f=99; g(x)=f; g(2)",
"Ok((:$$_block_$$ (:$_let_$ :f (:$$_block_$$ 99)) (:$_let_$ :g (:$$_lambda_$$ [x] (:$$_block_$$ :f))) (:g 2)))",
2022-05-02 16:44:35 +00:00
)
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+1; g(x)=f(x)+1; y=g(2)",
"Ok({f: lambda(x=>internal code),g: lambda(x=>internal code),y: 4})",
)
testEvalToBe("f(x)=x+1; g(x)=f(x)+1; g(2)", "Ok(4)")
2022-05-02 15:52:59 +00:00
})
2022-05-04 19:23:40 +00:00
describe("function tricks", () => {
parser toFunction additive multiplicative compact whitespace pow relational equality boolean whitespace separator left associative operators expression not identifier function call array constructor string indexed values ident priority block outerBlock optional final expression statement separator outerBlock innerBlock better errors note xor white space and record unary minus inner/outer block statement lambda sort lambda is a value constructor lambdaCall ternary ternary basicValue cleanup quotes chained Functions dot operators unify unary operators unify unary operatos notes notes notes notes parser priorities set white space or newline defined allow newlines notes function call has become a post operator recordElement recursive index postOperatorToFunction better integer comments notes record priority comment atom finalComment generated parser type cast format initiate test file test initiated; todo nodeCall; nodeExpression callIdentifier recover extra initiate testing initial tests pass tests pass remove function node ternary test parse passed to tests pass notes sort toExpression format notes remove unused modules remove unnecessary nodeLambdaCall notes note fix construct array comment test todo elixir pipe fix toString notes initial to expression test value test parsing records records comments ternary ifthenelse inner block passed inner block lambda lambda new parser tested now test tricks ternary in expression to test lambda as argument to test lambda in structures Use peggy Parser expectEvalError macros tested remove mathjs parse reducer test comparison operator
2022-05-05 19:45:25 +00:00
testEvalError("f(x)=f(y)=2; f(2)") //Error because chain assignment is not allowed
testEvalToBe("y=2;g(x)=y+1;g(2)", "Ok(3)")
testEvalToBe("y=2;g(x)=inspect(y)+1", "Ok({g: lambda(x=>internal code),y: 2})")
2022-05-02 22:18:23 +00:00
MySkip.testEvalToBe("f(x) = x(x); f(f)", "????") // TODO: Infinite loop. Any solution? Catching proper exception or timeout?
2022-05-02 22:30:41 +00:00
MySkip.testEvalToBe("f(x, x)=x+x; f(1,2)", "????") // TODO: Duplicate parameters
2022-05-19 18:25:18 +00:00
testEvalToBe("myadd(x,y)=x+y; z=myadd; z", "Ok(lambda(x,y=>internal code))")
testEvalToBe("myadd(x,y)=x+y; z=myadd; z(1, 1)", "Ok(2)")
parser toFunction additive multiplicative compact whitespace pow relational equality boolean whitespace separator left associative operators expression not identifier function call array constructor string indexed values ident priority block outerBlock optional final expression statement separator outerBlock innerBlock better errors note xor white space and record unary minus inner/outer block statement lambda sort lambda is a value constructor lambdaCall ternary ternary basicValue cleanup quotes chained Functions dot operators unify unary operators unify unary operatos notes notes notes notes parser priorities set white space or newline defined allow newlines notes function call has become a post operator recordElement recursive index postOperatorToFunction better integer comments notes record priority comment atom finalComment generated parser type cast format initiate test file test initiated; todo nodeCall; nodeExpression callIdentifier recover extra initiate testing initial tests pass tests pass remove function node ternary test parse passed to tests pass notes sort toExpression format notes remove unused modules remove unnecessary nodeLambdaCall notes note fix construct array comment test todo elixir pipe fix toString notes initial to expression test value test parsing records records comments ternary ifthenelse inner block passed inner block lambda lambda new parser tested now test tricks ternary in expression to test lambda as argument to test lambda in structures Use peggy Parser expectEvalError macros tested remove mathjs parse reducer test comparison operator
2022-05-05 19:45:25 +00:00
})
describe("lambda in structures", () => {
2022-05-19 18:25:18 +00:00
testEvalToBe(
"myadd(x,y)=x+y; z=[myadd]",
"Ok({myadd: lambda(x,y=>internal code),z: [lambda(x,y=>internal code)]})",
)
testEvalToBe("myadd(x,y)=x+y; z=[myadd]; z[0]", "Ok(lambda(x,y=>internal code))")
testEvalToBe("myadd(x,y)=x+y; z=[myadd]; z[0](3,2)", "Ok(5)")
testEvalToBe("myaddd(x,y)=x+y; z={x: myaddd}; z", "Ok({x: lambda(x,y=>internal code)})")
testEvalToBe("myaddd(x,y)=x+y; z={x: myaddd}; z.x", "Ok(lambda(x,y=>internal code))")
testEvalToBe("myaddd(x,y)=x+y; z={x: myaddd}; z.x(3,2)", "Ok(5)")
2022-05-02 10:55:28 +00:00
})
parser toFunction additive multiplicative compact whitespace pow relational equality boolean whitespace separator left associative operators expression not identifier function call array constructor string indexed values ident priority block outerBlock optional final expression statement separator outerBlock innerBlock better errors note xor white space and record unary minus inner/outer block statement lambda sort lambda is a value constructor lambdaCall ternary ternary basicValue cleanup quotes chained Functions dot operators unify unary operators unify unary operatos notes notes notes notes parser priorities set white space or newline defined allow newlines notes function call has become a post operator recordElement recursive index postOperatorToFunction better integer comments notes record priority comment atom finalComment generated parser type cast format initiate test file test initiated; todo nodeCall; nodeExpression callIdentifier recover extra initiate testing initial tests pass tests pass remove function node ternary test parse passed to tests pass notes sort toExpression format notes remove unused modules remove unnecessary nodeLambdaCall notes note fix construct array comment test todo elixir pipe fix toString notes initial to expression test value test parsing records records comments ternary ifthenelse inner block passed inner block lambda lambda new parser tested now test tricks ternary in expression to test lambda as argument to test lambda in structures Use peggy Parser expectEvalError macros tested remove mathjs parse reducer test comparison operator
2022-05-05 19:45:25 +00:00
describe("ternary and bindings", () => {
testEvalToBe("f(x)=x ? 1 : 0; f(true)", "Ok(1)")
testEvalToBe("f(x)=x>2 ? 1 : 0; f(3)", "Ok(1)")
})