open OUnit2 let u = UTF8.from_utf8string let _msg ~(expected:Expression.t) ~(result:Expression.t) = Printf.sprintf "Expected %s but got %s" (UTF8.raw_encode @@ Expression.show @@ Expression.load_expr expected) (UTF8.raw_encode @@ Expression.show @@ Expression.load_expr result) let load_expr str = Expression.Formula ( Expression.Expression( ExpressionParser.value ExpressionLexer.read @@ Lexing.from_string str ) ) let test_num ctx = begin let expected = Expression.Formula ( Expression.Expression ( ScTypes.Value (ScTypes.number ( DataType.Num.of_int 1 ) ))) in let result = load_expr "=1" in assert_equal ~msg:(_msg ~expected ~result) expected result end let test_call ctx = begin let expected = Expression.Formula ( Expression.Expression ( ScTypes.Call ( u"sum", []))) in let result = load_expr "=sum()" in assert_equal ~msg:(_msg ~expected ~result) expected result end let test_call2 ctx = begin (* The expression "foo2(" has to be parsed as a function call, and not as a reference to cell "FOO2", followed by another expression. *) let expected = Expression.Formula ( Expression.Expression ( ScTypes.Call ( u"foo2", [ScTypes.Value (ScTypes.number ( DataType.Num.of_int 4 ))]))) in let result = load_expr "=foo2(4)" in assert_equal ~msg:(_msg ~expected ~result) expected result end let test_ref ctx = begin let expected = Expression.Formula ( Expression.Expression ( ScTypes.Ref( ScTypes.Cell ((1, 3), (false, false))))) in let result = load_expr "=A3" in assert_equal ~msg:(_msg ~expected ~result) expected result end let tests = "expression_parser_test">::: [ "test_num" >:: test_num; "test_call" >:: test_call; "test_call2" >:: test_call2; "test_ref" >:: test_ref; ]