open OUnit2 module T = Tools 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" (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 ~cmp:(Expression.(=)) ~msg:(_msg ~expected ~result) 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 assert_equal expected result end (** If the string start with space, it should be interpreted as a litteral *) 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 assert_equal expected result end 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 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 ( DataType.Num.of_int 123 )) in assert_equal expected result end let test_float ctx = begin let result = Expression.load @@ u"12.45" in let expected = Expression.load_expr @@ Expression.Basic ( ScTypes.number ( DataType.Num.of_float @@ float_of_string "12.45" )) in assert_equal expected result end let test_relative ctx = begin let result = Expression.load @@ u"-123" in let expected = Expression.load_expr @@ Expression.Basic ( ScTypes.number ( DataType.Num.of_int (-123) )) in assert_equal expected result end let test_date ctx = begin let result = Expression.load @@ u"1900/01/01" and expected = Expression.load_expr @@ Expression.Basic ( ScTypes.date ( DataType.Date.get_julian_day 1900 01 01 )) in assert_equal expected result end let test_sources ctx = begin let result = Expression.load @@ u"=A1" |> Expression.collect_sources in let expected = Cell.Set.singleton (1, 1) in let msg = Printf.sprintf "Expected %s but got %s" (UTF8.raw_encode @@ Tools.String.print_buffer Cell.Set.printb expected) (UTF8.raw_encode @@ Tools.String.print_buffer Cell.Set.printb result) in OUnit2.assert_equal ~msg expected result end let tests = "expression_test">::: [ (* Test litteral input *) "test_str" >:: test_str; "test_str_space" >:: test_str_space; "test_formula_str" >:: test_formula_str; (* Test numeric input *) "test_num" >:: test_num; "test_float" >:: test_float; "test_relative" >:: test_relative; "test_date" >:: test_date; "test_sources" >:: test_sources; ]