From 627484a8f7e532a548ca939582e48fd568493256 Mon Sep 17 00:00:00 2001 From: Pierre Surer Date: Mon, 10 Jan 2022 14:47:14 +0100 Subject: [PATCH 1/2] Contextual tests (wip2) --- doc/contextual_checks.md | 10 +++--- test/context.ml | 67 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 67 insertions(+), 10 deletions(-) diff --git a/doc/contextual_checks.md b/doc/contextual_checks.md index 34ee970..8924fc8 100644 --- a/doc/contextual_checks.md +++ b/doc/contextual_checks.md @@ -35,8 +35,8 @@ * No Return instruction ✔ * No reserved keyword in params ✔ -* Constructor name and class name are equal -* Constructor parameters and class parameters are equal +* Constructor name and class name are equal ✔ +* Constructor parameters and class parameters are equal ✔ * Constructor calls the right super constructor if class is derived * Constructor does not call any super constructor if class is base @@ -44,15 +44,15 @@ * Perform expression checks for Expr, Ite, Return and Assign * -- -* No reserved keyword declared in Block instructions +* No reserved keyword declared in Block instructions ✔ * Can only Assign to idents, attributes or static attributes * Assign rhs is compatible with lhs * Expression in an Ite instruction is of type Integer ## Expressions -* Called method exists -* Called method params are compatible with declaration +* Called method exists ✔ +* Called method params are compatible with declaration ✔ * Called static method exists in static class * Called static method params are compatible with declaration * Call to New exists diff --git a/test/context.ml b/test/context.ml index 53e3ac3..ecb0f03 100644 --- a/test/context.ml +++ b/test/context.ml @@ -134,7 +134,7 @@ let%test "no-duplicate-instance-method-declaration" = let%test "herited-class-exists" = expects_ctx_err {| class Point1() extends Point2 is { - def Point1() is {} + def Point1() : Point2() is {} } {} |} @@ -142,10 +142,10 @@ let%test "herited-class-exists" = let%test "no-cycle-in-inheritance-graph" = expects_ctx_err {| class Point1() extends Point2 is { - def Point1() is {} + def Point1() : Point2() is {} } class Point2() extends Point1 is { - def Point2() is {} + def Point2() : Point1() is {} } {} |} @@ -166,7 +166,7 @@ let%test "override-methods-have-the-override-keyword" = def test() is {} } class Point2() extends Point1 is { - def Point2() is {} + def Point2() : Point1() is {} def test() is {} } {} @@ -179,7 +179,7 @@ let%test "override-methods-match-the-overriden-method-signature" = def test(i : Integer) is {} } class Point2() extends Point1 is { - def Point2() is {} + def Point2() : Point1() is {} def override test(i1, i2 : Integer) is {} } {} @@ -205,3 +205,60 @@ let%test "no-reserved-keyword-in-constructor-params" = |} in List.for_all reserved ~f:(fun r -> expects_ctx_err (code r r)) +let%test "constructor-name-and-class-name-are-equal" = + expects_ctx_err {| + class Point1() is { + def Point2() is {} + } + {} + |} + +let%test "constructor-parameters-and-class-parameters-are-equal" = + expects_ctx_err {| + class Point1(i : Integer) is { + def Point1(i : String) is {} + } + {} + |} + + +let%test "no-reserved-keyword-declared-in-Block-instructions" = + let reserved = [ "this"; "super"; "result" ] + in let code = Printf.sprintf {| + { + %s : Integer is {} + } + |} + in List.for_all reserved ~f:(fun r -> expects_ctx_err (code r)) + + +let%test "called-method-exists" = + expects_ctx_err {| + class Point1() is { + def Point1() is {} + } + {p : Point1 + is + p.test();} + |} + +let%test "called-method-exists" = + expects_ctx_err {| + class Point1() is { + def Point1() is {} + } + {p : Point1 + is + p.test();} + |} + +let%test "called-method-params-are-compatible-with-declaration" = + expects_ctx_err {| + class Point1() is { + def Point1() is {} + def test(i : Integer) is {} + } + {p : Point1 + is + p.test();} + |} \ No newline at end of file From 39931cdcfa2b66a8848e483b4e16d67e9ddffee6 Mon Sep 17 00:00:00 2001 From: Mathis Brossier Date: Mon, 10 Jan 2022 15:11:51 +0100 Subject: [PATCH 2/2] fixed some tests --- test/context.ml | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/test/context.ml b/test/context.ml index ecb0f03..374517c 100644 --- a/test/context.ml +++ b/test/context.ml @@ -14,12 +14,12 @@ let%test "no-class-inherits-reserved" = |} in List.for_all reserved ~f:(fun r -> expects_ctx_err (code r)) -let%test "underclared_variable" = +let%test "no-undeclared-variable" = expects_ctx_err {| {p1 := 51;} |} - -let%test "unmatched-type" = + +let%test "no-unmatched-type" = expects_ctx_err {| class Point1() is { def Point1() is {} @@ -37,16 +37,35 @@ let%test "unmatched-type" = } |} +let%test "compatible-types" = + expects_ast {| + class Base() is { + def Base() is {} + } + + class Derived() extends Base is { + def Derived() : Base() is {} + } + + { + p1: Base + p2: Derived + is + p1 := p2; + } + |} + let%test "no-reserved-keyword-in-method-params" = let reserved = [ "this"; "super"; "result" ] in let code = Printf.sprintf {| class Test() is { def Test() is {} - def testMethod(%s : Integer) : Integer is {} + def testMethod(%s : Integer) : Integer is { return 0; } } {} |} in List.for_all reserved ~f:(fun r -> expects_ctx_err (code r)) + && expects_ast (code "foo") let%test "no-reserved-keyword-in-attributes" = let reserved = [ "this"; "super"; "result" ] @@ -59,6 +78,7 @@ let%test "no-reserved-keyword-in-attributes" = |} in List.for_all reserved ~f:(fun r -> expects_ctx_err (code r)) + && expects_ast (code "foo") let%test "no-reserved-keyword-in-instructions" = let reserved = [ "this"; "super"; "result" ] @@ -66,6 +86,7 @@ let%test "no-reserved-keyword-in-instructions" = {%s : Integer is {}} |} in List.for_all reserved ~f:(fun r -> expects_ctx_err (code r)) + && expects_ast (code "foo") let%test "no-duplicate-class-declaration" = expects_ctx_err {| @@ -89,6 +110,7 @@ let%test "no-reserved-class-name" = {} |} in List.for_all reserved ~f:(fun r -> expects_ctx_err (code r r)) + && expects_ast (code "Foo" "Foo") let%test "no-duplicate-static-attribute-declaration" = expects_ctx_err {| @@ -105,8 +127,8 @@ let%test "no-duplicate-static-method-declaration" = expects_ctx_err {| class Point1() is { def Point1() is {} - def static static1() : Integer is {return 0;} - def static static1() : String is {return "";} + def static static1() : Integer is { return 0; } + def static static1() : String is { return ""; } } {} |}