From 05b3868f44c125b4ca7fcdb3d9dd7d92954d8178 Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Fri, 30 Aug 2019 20:24:31 +0200 Subject: Added tests --- tests/expressions/buildExpression.ml | 27 +++++++++++++ tests/expressions/evaluate_test.ml | 55 ++++++++++++++++++++++++++ tests/expressions/show_expr_test.ml | 75 ++++++++++++++++++++++++++++++++++++ 3 files changed, 157 insertions(+) create mode 100644 tests/expressions/buildExpression.ml create mode 100644 tests/expressions/evaluate_test.ml create mode 100644 tests/expressions/show_expr_test.ml (limited to 'tests/expressions') diff --git a/tests/expressions/buildExpression.ml b/tests/expressions/buildExpression.ml new file mode 100644 index 0000000..e8a32c6 --- /dev/null +++ b/tests/expressions/buildExpression.ml @@ -0,0 +1,27 @@ +let u = UTF8.from_utf8string + +module M(E:Sym_expr.SYM_EXPR) = struct + + let string = + let v = E.value (E.T.str @@ u "This is a test string") in + E.observe v + + let date0 = + let zero = E.value (E.T.date @@ DataType.Num.zero) in + E.observe zero + + let true0 = + let v = E.call0 (u "true") in + E.observe v + + let f3 = + let zero = E.value (E.T.num @@ DataType.Num.zero) in + let v = E.call3 (u "register3") zero zero zero in + E.observe v + + let calln = + let zero = E.value (E.T.num @@ DataType.Num.zero) in + let one = E.value (E.T.num @@ DataType.Num.one) in + E.observe (E.callN (u "calln") [zero; one]) + +end diff --git a/tests/expressions/evaluate_test.ml b/tests/expressions/evaluate_test.ml new file mode 100644 index 0000000..e571831 --- /dev/null +++ b/tests/expressions/evaluate_test.ml @@ -0,0 +1,55 @@ +open OUnit2 + +open Functions +let f_string = ScTypes.ReturnType.f_string + +let u = UTF8.from_utf8string + +let catalog = Functions.C.compile ( + Functions.built_in Functions.C.empty + |> Functions.C.register3 "register3" (t_int, t_int, t_int) f_string + (fun a b c -> u "register3") +) + +let _msg ~(expected:ScTypes.Result.t) ~(result:ScTypes.Result.t) = + Printf.sprintf "Expected %s but got %s" + (UTF8.raw_encode @@ ScTypes.Result.show expected) + (UTF8.raw_encode @@ ScTypes.Result.show result) + +module M = BuildExpression.M(Evaluate) + +let test_value_string e = begin + assert_equal + (ScTypes.Result.Ok (ScTypes.Type.string @@ u "This is a test string")) + (M.string (catalog, fun _ -> None)) +end + +let test_value_date e = begin + + let expected = ScTypes.Result.Ok (ScTypes.Type.date DataType.Num.zero) + and result = M.date0 (catalog, fun _ -> None) in + + assert_equal + ~msg:(_msg ~expected ~result) + expected result +end + +let test_evaluate0 e = begin + assert_equal + (ScTypes.Result.Ok (ScTypes.Type.boolean true)) + (M.true0 (catalog, fun _ -> None)) +end + +let test_evaluate3 e = begin + assert_equal + (ScTypes.Result.Ok (ScTypes.Type.string @@ u "register3")) + (M.f3 (catalog, fun _ -> None)) +end + +let tests = "evaluate_test">::: [ + "test_value_string" >:: test_value_string; + "test_value_date" >:: test_value_date; + "test_evaluate0" >:: test_evaluate0; + "test_evaluate3" >:: test_evaluate3; +] + diff --git a/tests/expressions/show_expr_test.ml b/tests/expressions/show_expr_test.ml new file mode 100644 index 0000000..aa07685 --- /dev/null +++ b/tests/expressions/show_expr_test.ml @@ -0,0 +1,75 @@ +open OUnit2 + +open Functions +let f_string = ScTypes.ReturnType.f_string + +let u = UTF8.from_utf8string + +let _msg ~expected ~result = + Printf.sprintf "Expected %s but got %s" + (UTF8.raw_encode expected) + (UTF8.raw_encode result) + +module M = BuildExpression.M(Show_expr.Show_Expr(Show_ref)(Show_type)) + +let test_value_string e = begin + let buffer = UTF8.Buffer.create 16 in + + let expected = u "This is a test string" + and result = M.string buffer; UTF8.Buffer.contents buffer in + + assert_equal + ~msg:(_msg ~expected ~result) + expected result + +end + +let test_value_date e = begin + + let buffer = UTF8.Buffer.create 16 in + let expected = u "1899/12/30" + and result = M.date0 buffer; UTF8.Buffer.contents buffer in + + assert_equal + ~msg:(_msg ~expected ~result) + expected result +end + +let test_evaluate0 e = begin + let buffer = UTF8.Buffer.create 16 in + let expected = u "true()" + and result = M.true0 buffer; UTF8.Buffer.contents buffer in + assert_equal + ~msg:(_msg ~expected ~result) + expected result +end + +let test_evaluate3 e = begin + let buffer = UTF8.Buffer.create 16 in + let expected = u "register3(0;0;0)" + and result = M.f3 buffer; UTF8.Buffer.contents buffer in + + assert_equal + ~msg:(_msg ~expected ~result) + expected result +end + +let test_calln e = begin + let buffer = UTF8.Buffer.create 16 in + let expected = u "calln(0;1)" + and result = M.calln buffer; UTF8.Buffer.contents buffer in + + assert_equal + ~msg:(_msg ~expected ~result) + expected result +end + +let tests = "show_expr_test">::: [ + "test_value_string" >:: test_value_string; + "test_value_date" >:: test_value_date; + "test_evaluate0" >:: test_evaluate0; + "test_evaluate3" >:: test_evaluate3; + "test_calln" >:: test_calln; +] + + -- cgit v1.2.3