From 824f2987d47e87d58ee2a4a96d7be417aad6aeab Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Wed, 31 Jan 2018 13:20:20 +0100 Subject: API refactoring : made the GADT abstract, provide contructor for each case, and deported the expression with evaluation with module functors --- tests/expressionParser_test.ml | 17 ++++---- tests/expression_test.ml | 45 +++++++++++-------- tests/odf/odf_ExpressionParser_test.ml | 54 ++++++++++++----------- tests/sheet_test.ml | 80 +++++++++++++++++----------------- tests/test.ml | 2 +- 5 files changed, 104 insertions(+), 94 deletions(-) (limited to 'tests') diff --git a/tests/expressionParser_test.ml b/tests/expressionParser_test.ml index 12ceeb0..481ed6d 100755 --- a/tests/expressionParser_test.ml +++ b/tests/expressionParser_test.ml @@ -19,7 +19,7 @@ let test_num ctx = begin let expected = Expression.Formula ( Expression.Expression ( - ScTypes.Value (ScTypes.number ( + ScTypes.Expr.value (ScTypes.Type.number ( DataType.Num.of_int 1 ) ))) in @@ -35,8 +35,7 @@ let test_call ctx = begin let expected = Expression.Formula ( Expression.Expression ( - ScTypes.Call ( - u"sum", []))) in + ScTypes.Expr.call0 (u"sum"))) in let result = load_expr "=sum()" in assert_equal @@ -52,10 +51,10 @@ let test_call2 ctx = begin let expected = Expression.Formula ( Expression.Expression ( - ScTypes.Call ( - u"foo2", [ScTypes.Value (ScTypes.number ( - DataType.Num.of_int 4 - ))]))) in + ScTypes.Expr.call1 + (u"foo2") (ScTypes.Expr.value (ScTypes.Type.number ( + DataType.Num.of_int 4) + )))) in let result = load_expr "=foo2(4)" in assert_equal @@ -68,8 +67,8 @@ let test_ref ctx = begin let expected = Expression.Formula ( Expression.Expression ( - ScTypes.Ref( - ScTypes.Cell ((1, 3), (false, false))))) in + ScTypes.Expr.ref( + ScTypes.Refs.cell ((1, 3), (false, false))))) in let result = load_expr "=A3" in assert_equal diff --git a/tests/expression_test.ml b/tests/expression_test.ml index 5def730..6743a09 100755 --- a/tests/expression_test.ml +++ b/tests/expression_test.ml @@ -5,18 +5,9 @@ let u = UTF8.from_utf8string let _msg ~expected ~result = - let get_type = function - | Expression.Basic ScTypes.Num (ScTypes.Number, _) -> "N" - | Expression.Basic ScTypes.Num (ScTypes.Date, _) -> "D" - | Expression.Basic ScTypes.Str _ -> "S" - | Expression.Basic ScTypes.Bool _ -> "B" - | Expression.Formula _ -> "F" in - - Printf.sprintf "Expected %s:%s but got %s:%s" + Printf.sprintf "Expected %s but got %s" (UTF8.raw_encode @@ Expression.show expected) - (get_type expected) (UTF8.raw_encode @@ Expression.show result) - (get_type result) let assert_equal expected result = OUnit2.assert_equal @@ -28,7 +19,7 @@ let assert_equal expected result = let test_str ctx = begin let result = Expression.load @@ u"cafe" in let expected = Expression.load_expr @@ Expression.Basic ( - ScTypes.string (u"cafe")) in + ScTypes.Type.string (u"cafe")) in assert_equal expected result end @@ -36,7 +27,7 @@ end let test_str_space ctx = begin let result = Expression.load @@ u" =cafe" in let expected = Expression.load_expr @@ Expression.Basic ( - ScTypes.string (u" =cafe")) in + ScTypes.Type.string (u" =cafe")) in assert_equal expected result end @@ -44,15 +35,15 @@ let test_formula_str ctx = begin let result = Expression.load @@ u"=\"cafe\"" in let expected = Expression.load_expr @@ Expression.Formula ( Expression.Expression ( - ScTypes.Value ( - ScTypes.string (u"cafe")))) in + ScTypes.Expr.value ( + ScTypes.Type.string (u"cafe")))) in assert_equal expected result end let test_num ctx = begin let result = Expression.load @@ u"123" in let expected = Expression.load_expr @@ Expression.Basic ( - ScTypes.number ( + ScTypes.Type.number ( DataType.Num.of_int 123 )) in assert_equal expected result @@ -61,7 +52,7 @@ end let test_float ctx = begin let result = Expression.load @@ u"12.45" in let expected = Expression.load_expr @@ Expression.Basic ( - ScTypes.number ( + ScTypes.Type.number ( DataType.Num.of_float @@ float_of_string "12.45" )) in assert_equal expected result @@ -70,7 +61,7 @@ end let test_relative ctx = begin let result = Expression.load @@ u"-123" in let expected = Expression.load_expr @@ Expression.Basic ( - ScTypes.number ( + ScTypes.Type.number ( DataType.Num.of_int (-123) )) in assert_equal expected result @@ -79,7 +70,7 @@ end let test_date ctx = begin let result = Expression.load @@ u"1900/01/01" and expected = Expression.load_expr @@ Expression.Basic ( - ScTypes.date ( + ScTypes.Type.date ( DataType.Date.get_julian_day 1900 01 01 )) in assert_equal expected result @@ -96,6 +87,22 @@ let test_sources ctx = begin (UTF8.raw_encode @@ Tools.String.print_buffer Cell.Set.printb result) in OUnit2.assert_equal ~msg expected result +end + +let test_sources2 ctx = begin + let result = Expression.load @@ u"=if($A$1>0;rand()*10+1;0)" + |> Expression.collect_sources + and expected = Cell.Set.singleton (1, 1) in + + let msg_buffer = UTF8.Buffer.create 16 in + UTF8.Printf.bprintf msg_buffer "Expected %a but got %a" + Cell.Set.printb expected + Cell.Set.printb result; + + OUnit2.assert_equal + ~msg:(UTF8.to_utf8string @@ UTF8.Buffer.contents msg_buffer) + expected + result end @@ -114,5 +121,5 @@ let tests = "expression_test">::: [ "test_date" >:: test_date; "test_sources" >:: test_sources; - + "test_sources2" >:: test_sources2; ] diff --git a/tests/odf/odf_ExpressionParser_test.ml b/tests/odf/odf_ExpressionParser_test.ml index 18efe96..3d6c4fa 100755 --- a/tests/odf/odf_ExpressionParser_test.ml +++ b/tests/odf/odf_ExpressionParser_test.ml @@ -2,11 +2,13 @@ open OUnit2 let u = UTF8.from_utf8string -let _msg ~(expected:ScTypes.expression) ~(result:ScTypes.expression) = +module Show = ScTypes.Expr.Eval(Show_expr.Show_Expr(Show_ref)(Show_type)) + +let _msg ~(expected:ScTypes.Expr.t) ~(result:ScTypes.Expr.t) = let b1 = UTF8.Buffer.create 16 and b2 = UTF8.Buffer.create 16 in - ScTypes.show_expr b1 expected; - ScTypes.show_expr b2 result; + Show.eval expected () b1; + Show.eval result () b2; Printf.sprintf "Expected \n\t%s but got \n\t%s" (UTF8.raw_encode @@ UTF8.Buffer.contents b1) @@ -14,7 +16,7 @@ let _msg ~(expected:ScTypes.expression) ~(result:ScTypes.expression) = -let build_num value = ScTypes.number ( +let build_num value = ScTypes.Type.number ( DataType.Num.of_int value ) @@ -28,22 +30,22 @@ let test_formula ctx = begin let expected = ScTypes.( - Call(u"CONCATENATE", [ - Call (u"SUM", [ - Ref (Range (((6, 16), (false, false)), (((36, 16), (false, false)))))]); - Value (string (u"/")); - Call(u"*", [ - Value (build_num 8); - Call(u"NETWORKDAYS", [ - Ref (Cell ((6, 6), (false, false))); - Call(u"+", [ - Ref (Cell ((6, 6), (false, false))); - Expression ( - Call( u"-", [ - Call(u"ORG.OPENOFFICE.DAYSINMONTH", [ - Ref (Cell ((6, 6), (false, false)))]); - Value (build_num 1); - ]))])])])])) in + Expr.call3 (u"CONCATENATE") + (Expr.call1 (u"SUM") + (Expr.ref (Refs.range ((6, 16), (false, false)) (((36, 16), (false, false)))))) + (Expr.value (Type.string (u"/"))) + (Expr.call2 (u"*") + (Expr.value (build_num 8)) + (Expr.call2 (u"NETWORKDAYS") + (Expr.ref (Refs.cell ((6, 6), (false, false)))) + (Expr.call2 (u"+") + (Expr.ref (Refs.cell ((6, 6), (false, false)))) + (Expr.expression + (Expr.call2 (u"-") + (Expr.call1 (u"ORG.OPENOFFICE.DAYSINMONTH") + (Expr.ref (Refs.cell ((6, 6), (false, false))))) + (Expr.value (build_num 1)); + )))))) in assert_equal ~msg:(_msg ~expected ~result) @@ -59,11 +61,11 @@ let test_formula2 ctx = begin let expected = ScTypes.( - Call (u"+", [ - Call(u"*", [ - Ref (Cell ((8, 51), (false, false))); - Ref (Cell ((7, 52), (false, false))) - ])])) in + Expr.call1 (u"+") + (Expr.call2 (u"*") + (Expr.ref (Refs.cell ((8, 51), (false, false)))) + (Expr.ref (Refs.cell ((7, 52), (false, false)))) + )) in assert_equal ~msg:(_msg ~expected ~result) @@ -72,7 +74,7 @@ let test_formula2 ctx = begin end -let tests = "odf_ExpressionParser_test">::: [ +let tests = "odf_ExpressionParser_test" >::: [ "test_formula" >:: test_formula; "test_formula2" >:: test_formula2; diff --git a/tests/sheet_test.ml b/tests/sheet_test.ml index 3960c4b..dfa8da4 100755 --- a/tests/sheet_test.ml +++ b/tests/sheet_test.ml @@ -4,6 +4,8 @@ module Option = Tools.Option let u = UTF8.from_utf8string +let catalog = Functions.C.compile @@ Functions.built_in Functions.C.empty + let _msg ~expected ~result = begin let get_string v = match v with @@ -16,17 +18,17 @@ let _msg ~expected ~result = begin (get_string result) end -let build_num value = ScTypes.number @@ DataType.Num.of_int value +let build_num value = ScTypes.Type.number @@ DataType.Num.of_int value (** Test a simple references between two cells *) let test_create_ref_1 ctx = begin let s = Sheet.Raw.empty - |> Sheet.Raw.add (3,3) @@ Expression.load @@ u"=-1" - |> snd |> Sheet.Raw.add (0,0) @@ Expression.load @@ u"=C3" + |> Sheet.Raw.add (3,3) (Expression.load @@ u"=-1") catalog + |> snd |> Sheet.Raw.add (0,0) (Expression.load @@ u"=C3") catalog |> snd in let result = (Sheet.Raw.get_value (0, 0) s) in - let expected = Some (ScTypes.Result (build_num (-1))) in + let expected = Some (ScTypes.Result.Ok (build_num (-1))) in assert_equal ~msg:(_msg ~expected ~result) @@ -37,14 +39,14 @@ end let test_create_ref_2 ctx = begin let s = Sheet.Raw.empty - |> Sheet.Raw.add (2,2) @@ Expression.load @@ u"=C3" - |> snd |> Sheet.Raw.add (3,3) @@ Expression.load @@ u"=A1" - |> snd |> Sheet.Raw.add (1,1) @@ Expression.load @@ u"123" + |> Sheet.Raw.add (2,2) (Expression.load @@ u"=C3") catalog + |> snd |> Sheet.Raw.add (3,3) (Expression.load @@ u"=A1") catalog + |> snd |> Sheet.Raw.add (1,1) (Expression.load @@ u"123") catalog |> snd in let result = (Sheet.Raw.get_value (2, 2) s) in - let expected = Some (ScTypes.Result (build_num 123)) in + let expected = Some (ScTypes.Result.Ok (build_num 123)) in assert_equal ~msg:(_msg ~expected ~result) @@ -55,10 +57,10 @@ end let test_create_direct_cycle ctx = begin let s = Sheet.Raw.empty - |> Sheet.Raw.add (2,2) @@ Expression.load @@ u"=B2 + 1" + |> Sheet.Raw.add (2,2) (Expression.load @@ u"=B2 + 1") catalog |> snd in let result = (Sheet.Raw.get_value (2, 2) s) in - let expected = Some (ScTypes.Error Errors.TypeError) in + let expected = Some (ScTypes.Result.Error Errors.TypeError) in assert_equal ~msg:(_msg ~expected ~result) @@ -70,11 +72,11 @@ end let test_recover_from_cycle ctx = begin let s = Sheet.Raw.empty - |> Sheet.Raw.add (2,2) @@ Expression.load @@ u"=B2 + 1" - |> snd |> Sheet.Raw.add (2,2) @@ Expression.load @@ u"=6" + |> Sheet.Raw.add (2,2) (Expression.load @@ u"=B2 + 1") catalog + |> snd |> Sheet.Raw.add (2,2) (Expression.load @@ u"=6") catalog |> snd in let result = (Sheet.Raw.get_value (2, 2) s) in - let expected = Some (ScTypes.Result (build_num (6))) in + let expected = Some (ScTypes.Result.Ok (build_num (6))) in assert_equal ~msg:(_msg ~expected ~result) @@ -85,13 +87,13 @@ end let test_create_indirect_cycle ctx = begin let s = Sheet.Raw.empty - |> Sheet.Raw.add (2,2) @@ Expression.load @@ u"=A1" - |> snd |> Sheet.Raw.add (1,1) @@ Expression.load @@ u"=2" - |> snd |> Sheet.Raw.add (1,1) @@ Expression.load @@ u"=B2+1" - |> snd |> Sheet.Raw.add (0,0) @@ Expression.load @@ u"=A1" + |> Sheet.Raw.add (2,2) (Expression.load @@ u"=A1") catalog + |> snd |> Sheet.Raw.add (1,1) (Expression.load @@ u"=2") catalog + |> snd |> Sheet.Raw.add (1,1) (Expression.load @@ u"=B2+1") catalog + |> snd |> Sheet.Raw.add (0,0) (Expression.load @@ u"=A1") catalog |> snd in let result = (Sheet.Raw.get_value (0, 0) s) in - let expected = Some (ScTypes.Error Errors.Cycle) in + let expected = Some (ScTypes.Result.Error Errors.Cycle) in assert_equal ~msg:(_msg ~expected ~result) @@ -103,18 +105,18 @@ let test_check_cycle3 ctx = begin let s = Sheet.Raw.empty (* First set A1 to 3 *) - |> Sheet.Raw.add (1,1) @@ Expression.load @@ u"=3" - |> snd |> Sheet.Raw.add (1,2) @@ Expression.load @@ u"=A1" - |> snd |> Sheet.Raw.add (2,2) @@ Expression.load @@ u"=A1" - |> snd |> Sheet.Raw.add (5,5) @@ Expression.load @@ u"=B2" + |> Sheet.Raw.add (1,1) (Expression.load @@ u"=3") catalog + |> snd |> Sheet.Raw.add (1,2) (Expression.load @@ u"=A1") catalog + |> snd |> Sheet.Raw.add (2,2) (Expression.load @@ u"=A1") catalog + |> snd |> Sheet.Raw.add (5,5) (Expression.load @@ u"=B2") catalog (* A3 = A1 + A1 = 6 *) - |> snd |> Sheet.Raw.add (1,3) @@ Expression.load @@ u"=A2 + E5" + |> snd |> Sheet.Raw.add (1,3) (Expression.load @@ u"=A2 + E5") catalog (* Then set A1 to 2 *) - |> snd |> Sheet.Raw.add (1,1) @@ Expression.load @@ u"=2" + |> snd |> Sheet.Raw.add (1,1) (Expression.load @@ u"=2") catalog |> snd in let result = (Sheet.Raw.get_value (1, 3) s) in (* A3 = A1 + A1 = 4 *) - let expected = Some (ScTypes.Result (build_num 4)) in + let expected = Some (ScTypes.Result.Ok (build_num 4)) in assert_equal ~msg:(_msg ~expected ~result) @@ -125,10 +127,10 @@ end let test_delete ctx = begin let s = Sheet.Raw.empty - |> Sheet.Raw.add (2,2) @@ Expression.load @@ u"=C3" - |> snd |> Sheet.Raw.add (3,3) @@ Expression.load @@ u"=A1" - |> snd |> Sheet.Raw.remove (2,2) - |> snd |> Sheet.Raw.remove (3,3) + |> Sheet.Raw.add (2,2) (Expression.load @@ u"=C3") catalog + |> snd |> Sheet.Raw.add (3,3) (Expression.load @@ u"=A1") catalog + |> snd |> Sheet.Raw.remove (2,2) catalog + |> snd |> Sheet.Raw.remove (3,3) catalog |> snd in let result = (Sheet.Raw.get_value (3, 3) s) in let expected = None in @@ -142,10 +144,10 @@ end let test_update_succs1 ctx = begin let result = Sheet.Raw.empty - |> Sheet.Raw.add (1,1) @@ Expression.load @@ u" =1" - |> snd |> Sheet.Raw.add (2,2) @@ Expression.load @@ u"=A2" - |> snd |> Sheet.Raw.add (1,2) @@ Expression.load @@ u"=A1/1" - |> snd |> Sheet.Raw.add (1,1) @@ Expression.load @@ u"=2" + |> Sheet.Raw.add (1,1) (Expression.load @@ u" =1") catalog + |> snd |> Sheet.Raw.add (2,2) (Expression.load @@ u"=A2") catalog + |> snd |> Sheet.Raw.add (1,2) (Expression.load @@ u"=A1/1") catalog + |> snd |> Sheet.Raw.add (1,1) (Expression.load @@ u"=2") catalog |> fst in (* All the cells are updated by the change *) @@ -163,10 +165,10 @@ end let test_update_succs2 ctx = begin let result = Sheet.Raw.empty - |> Sheet.Raw.add (1,1) @@ Expression.load @@ u"=1" - |> snd |> Sheet.Raw.add (2,2) @@ Expression.load @@ u"=A2" - |> snd |> Sheet.Raw.add (1,2) @@ Expression.load @@ u"=A1/0" - |> snd |> Sheet.Raw.add (1,1) @@ Expression.load @@ u"=2" + |> Sheet.Raw.add (1,1) (Expression.load @@ u"=1") catalog + |> snd |> Sheet.Raw.add (2,2) (Expression.load @@ u"=A2") catalog + |> snd |> Sheet.Raw.add (1,2) (Expression.load @@ u"=A1/0") catalog + |> snd |> Sheet.Raw.add (1,1) (Expression.load @@ u"=2") catalog |> fst in (* Only (1, 1) is updated ; (2, 2) does not change, neither (2, 2) *) let expected = Cell.Set.of_list [(1,1)] in @@ -178,10 +180,10 @@ end let test_paste_undo ctx = begin - let empty = Sheet.create Sheet.Raw.empty in + let empty = Sheet.create catalog Sheet.Raw.empty in (* The expected result for the whole test *) - let expected = Some (ScTypes.Result (ScTypes.number (DataType.Num.of_int 6))) in + let expected = Some (ScTypes.Result.Ok (ScTypes.Type.number (DataType.Num.of_int 6))) in let sheet = empty |> Tools.Option.test @@ Sheet.move (Actions.Absolute (2, 1)) diff --git a/tests/test.ml b/tests/test.ml index ee71cb0..0c5af82 100755 --- a/tests/test.ml +++ b/tests/test.ml @@ -1,6 +1,6 @@ let () = - Evaluator.set_catalog (Functions.C.compile @@ Functions.built_in @@ Functions.C.empty); + (*Evaluator.set_catalog (Functions.C.compile @@ Functions.built_in @@ Functions.C.empty);*) let tests = OUnit2.test_list [ Tools_test.tests; -- cgit v1.2.3