[nullsafe] migrate 3rd-party methodes unit tests to ppx_expect

Summary: This seems better, let's migrate!

Reviewed By: mityal

Differential Revision: D23167443

fbshipit-source-id: b698e9ea0
master
Jules Villard 5 years ago committed by Facebook GitHub Bot
parent beca699bc4
commit 1825b3779c

@ -194,7 +194,8 @@ detect_dead_src_file:
|| [[ $$1 =~ .*FrontendStubs.mli?$$ ]] \ || [[ $$1 =~ .*FrontendStubs.mli?$$ ]] \
|| [[ $$1 =~ ^labs/ ]] \ || [[ $$1 =~ ^labs/ ]] \
|| [[ $$1 =~ ^llvm/ ]] \ || [[ $$1 =~ ^llvm/ ]] \
|| [[ $$1 =~ ^opensource/ ]]; then \ || [[ $$1 =~ ^opensource/ ]] \
|| grep -q -F 'let%test_module' $(INFER_BUILD_DIR)/$$1 ; then \
exit 0; \ exit 0; \
else \ else \
exit 1; \ exit 1; \

@ -34,7 +34,8 @@ val parse : string -> (t, parsing_error) result
["package.name.Class$NestedClass#foo(package.name.SomeClass, @Nullable package.name.OtherClass) ["package.name.Class$NestedClass#foo(package.name.SomeClass, @Nullable package.name.OtherClass)
@Nullable"] *) @Nullable"] *)
val to_canonical_string : t -> string (* used for testing *)
val to_canonical_string : t -> string [@@warning "-32"]
val pp : Format.formatter -> t -> unit val pp : Format.formatter -> t -> unit
(** String representation as it can be parsed via [parse] (** String representation as it can be parsed via [parse]

@ -7,5 +7,4 @@
open! IStd open! IStd
let tests = let tests = [ThirdPartyAnnotationInfoTests.test; AggregatedSummariesTest.test]
[ThirdPartyMethodTests.test; ThirdPartyAnnotationInfoTests.test; AggregatedSummariesTest.test]

@ -7,87 +7,82 @@
open! IStd open! IStd
module F = Format module F = Format
open OUnit2
open ThirdPartyMethod
let assert_parse_ok input expected_output = let parse input =
let result = ThirdPartyMethod.parse input in let result = ThirdPartyMethod.parse input in
match result with match result with
| Ok output -> | Ok output ->
(* Check that it was parsed to the expected result*) F.print_string (Sexp.to_string (ThirdPartyMethod.sexp_of_t output)) ;
assert_equal output expected_output ~printer:(fun parse_result -> let output_s = ThirdPartyMethod.to_canonical_string output in
Pp.string_of_pp pp parse_result ) ; if not (String.equal output_s input) then
(* Check also that the canonical representation matches the original *) F.printf
assert_equal (to_canonical_string output) input "@\n\
FAILED TEST: the canonical string of the parsed object is '%s', which does not match \
the input string."
output_s
| Error error -> | Error error ->
assert_failure F.printf "error: %s" (ThirdPartyMethod.string_of_parsing_error error)
(F.asprintf "Expected '%s' to be parsed, but got error %s instead" input
(string_of_parsing_error error))
let assert_parse_bad input = let%test_module "Third-party Method Tests OK Cases" =
let result = ThirdPartyMethod.parse input in ( module struct
match result with (* No params *)
| Ok output ->
assert_failure let%expect_test "no params" =
(F.asprintf "Expected '%s' to be NOT parsed, but was parsed as %a instead" input parse "a.b.C#foo()" ;
ThirdPartyMethod.pp output) [%expect {|((class_name a.b.C)(method_name(Method foo))(ret_nullability Nonnull)(params()))|}]
| Error _ ->
() let%expect_test _ =
parse "a.b.C#foo() @Nullable" ;
[%expect
let success_cases = {|((class_name a.b.C)(method_name(Method foo))(ret_nullability Nullable)(params()))|}]
"success_cases"
>:: fun _ -> (* One param *)
(* No params *)
assert_parse_ok "a.b.C#foo()" let%expect_test _ =
{class_name= "a.b.C"; method_name= Method "foo"; params= []; ret_nullability= Nonnull} ; parse "a.b.C#foo(c.d.E)" ;
assert_parse_ok "a.b.C#foo() @Nullable" [%expect
{class_name= "a.b.C"; method_name= Method "foo"; params= []; ret_nullability= Nullable} ; {|((class_name a.b.C)(method_name(Method foo))(ret_nullability Nonnull)(params((c.d.E Nonnull))))|}]
(* One param *)
assert_parse_ok "a.b.C#foo(c.d.E)" let%expect_test _ =
{ class_name= "a.b.C" parse "a.b.C#foo(@Nullable c.d.E)" ;
; method_name= Method "foo" [%expect
; params= [("c.d.E", Nonnull)] {|((class_name a.b.C)(method_name(Method foo))(ret_nullability Nonnull)(params((c.d.E Nullable))))|}]
; ret_nullability= Nonnull } ;
assert_parse_ok "a.b.C#foo(@Nullable c.d.E)" let%expect_test _ =
{ class_name= "a.b.C" parse "a.b.C#foo(c.d.E) @Nullable" ;
; method_name= Method "foo" [%expect
; params= [("c.d.E", Nullable)] {|((class_name a.b.C)(method_name(Method foo))(ret_nullability Nullable)(params((c.d.E Nonnull))))|}]
; ret_nullability= Nonnull } ;
assert_parse_ok "a.b.C#foo(c.d.E) @Nullable" let%expect_test _ =
{ class_name= "a.b.C" parse "a.b.C#foo(@Nullable c.d.E) @Nullable" ;
; method_name= Method "foo" [%expect
; params= [("c.d.E", Nonnull)] {|((class_name a.b.C)(method_name(Method foo))(ret_nullability Nullable)(params((c.d.E Nullable))))|}]
; ret_nullability= Nullable } ;
assert_parse_ok "a.b.C#foo(@Nullable c.d.E) @Nullable" (* Many params *)
{ class_name= "a.b.C"
; method_name= Method "foo" let%expect_test _ =
; params= [("c.d.E", Nullable)] parse "a.b.C#foo(c.d.E, a.b.C, x.y.Z)" ;
; ret_nullability= Nullable } ; [%expect
(* Many params *) {|((class_name a.b.C)(method_name(Method foo))(ret_nullability Nonnull)(params((c.d.E Nonnull)(a.b.C Nonnull)(x.y.Z Nonnull))))|}]
assert_parse_ok "a.b.C#foo(c.d.E, a.b.C, x.y.Z)"
{ class_name= "a.b.C" let%expect_test _ =
; method_name= Method "foo" parse "a.b.C#foo(c.d.E, @Nullable a.b.C, x.y.Z)" ;
; params= [("c.d.E", Nonnull); ("a.b.C", Nonnull); ("x.y.Z", Nonnull)] [%expect
; ret_nullability= Nonnull } ; {|((class_name a.b.C)(method_name(Method foo))(ret_nullability Nonnull)(params((c.d.E Nonnull)(a.b.C Nullable)(x.y.Z Nonnull))))|}]
assert_parse_ok "a.b.C#foo(c.d.E, @Nullable a.b.C, x.y.Z)"
{ class_name= "a.b.C"
; method_name= Method "foo"
; params= [("c.d.E", Nonnull); ("a.b.C", Nullable); ("x.y.Z", Nonnull)]
; ret_nullability= Nonnull } ;
assert_parse_ok "a.b.C#foo(@Nullable c.d.E, a.b.C, @Nullable x.y.Z) @Nullable"
{ class_name= "a.b.C"
; method_name= Method "foo"
; params= [("c.d.E", Nullable); ("a.b.C", Nonnull); ("x.y.Z", Nullable)]
; ret_nullability= Nullable } ;
(* Constructor *)
assert_parse_ok "a.b.C#<init>(@Nullable c.d.E, a.b.C, x.y.Z) @Nullable"
{ class_name= "a.b.C"
; method_name= Constructor
; params= [("c.d.E", Nullable); ("a.b.C", Nonnull); ("x.y.Z", Nonnull)]
; ret_nullability= Nullable }
let%expect_test _ =
parse "a.b.C#foo(@Nullable c.d.E, a.b.C, @Nullable x.y.Z) @Nullable" ;
[%expect
{|((class_name a.b.C)(method_name(Method foo))(ret_nullability Nullable)(params((c.d.E Nullable)(a.b.C Nonnull)(x.y.Z Nullable))))|}]
(* Constructor *)
let%expect_test _ =
parse "a.b.C#<init>(@Nullable c.d.E, a.b.C, x.y.Z) @Nullable" ;
[%expect
{|((class_name a.b.C)(method_name Constructor)(ret_nullability Nullable)(params((c.d.E Nullable)(a.b.C Nonnull)(x.y.Z Nonnull))))|}]
end )
(* We intentionally don't test all bad cases. (* We intentionally don't test all bad cases.
It is generally OK for nullsafe to allow something that is not really valid: It is generally OK for nullsafe to allow something that is not really valid:
@ -95,28 +90,49 @@ let success_cases =
Also we don't test exact error type, because this is an implementation detail Also we don't test exact error type, because this is an implementation detail
needed merely to simplify diagnostics needed merely to simplify diagnostics
*) *)
let bad_cases = let%test_module "Third-party Method Tests Bad Cases" =
"bad_cases" ( module struct
>:: fun _ -> let%expect_test _ =
assert_parse_bad "" ; parse "" ;
assert_parse_bad " " ; [%expect {| error: Accepted format is <class>#<method>(<params>)[<return nullability>] |}]
assert_parse_bad "blablabla" ;
(* no # delimiter *) let%expect_test _ =
assert_parse_bad "a.b.C.f()" ; parse " " ;
(* nested parenthesis *) [%expect {| error: Accepted format is <class>#<method>(<params>)[<return nullability>] |}]
assert_parse_bad "a.b.C#f(())" ;
(* param names are not accepted *) let%expect_test _ =
assert_parse_bad "a.b.C#f(int param)" ; parse "blablabla" ;
(* missed package for class *) [%expect {|error: Accepted format is <class>#<method>(<params>)[<return nullability>]|}]
assert_parse_bad "C#f()" ;
(* Missed @ in annotation*) let%expect_test "no # delimiter" =
assert_parse_bad "a.b.C#f(Nullable a.b.C)" ; parse "a.b.C.f()" ;
(* Extra spaces *) [%expect {|error: Accepted format is <class>#<method>(<params>)[<return nullability>]|}]
assert_parse_bad "a.b.C#f( a.b.C )" ;
(* No space after comma *) let%expect_test "nested parenthesis" =
assert_parse_bad "a.b.C#f(a.b.C,a.b.C)" ; parse "a.b.C#f(())" ;
(* Param names are not accepted *) [%expect {|error: Accepted format is <class>#<method>(<params>)[<return nullability>]|}]
assert_parse_bad "a.b.C#f(@Nullable int param)"
let%expect_test "param names are not accepted" =
parse "a.b.C#f(int param)" ;
let test = "ThirdPartyMethodTests" >::: [success_cases; bad_cases] [%expect {|error: Each param should have form of [@Nullable] <fully qualified type name>|}]
let%expect_test "missed package for class" =
parse "C#f()" ;
[%expect {|error: Class name should be fully qualified, including package name|}]
let%expect_test "Missed @ in annotation" =
parse "a.b.C#f(Nullable a.b.C)" ;
[%expect {|error: Each param should have form of [@Nullable] <fully qualified type name>|}]
let%expect_test "Extra spaces" =
parse "a.b.C#f( a.b.C )" ;
[%expect {|error: Each param should have form of [@Nullable] <fully qualified type name>|}]
let%expect_test "No space after comma" =
parse "a.b.C#f(a.b.C,a.b.C)" ;
[%expect {|error: Params should be separated by a comma, followed by a single space|}]
let%expect_test "Param names are not accepted" =
parse "a.b.C#f(@Nullable int param)" ;
[%expect {|error: Each param should have form of [@Nullable] <fully qualified type name>|}]
end )

@ -11,4 +11,5 @@
-open IBase -open Nullsafe)) -open IBase -open Nullsafe))
(libraries oUnit core IStdlib ATDGenerated IBase IR Nullsafe) (libraries oUnit core IStdlib ATDGenerated IBase IR Nullsafe)
(preprocess (preprocess
(pps ppx_compare))) (pps ppx_compare ppx_expect ppx_inline_test))
(inline_tests))

Loading…
Cancel
Save