From d6e050cfd56dbd5172ba74a9fcede4a7f2d4687c Mon Sep 17 00:00:00 2001 From: Hai Nguyen Van Date: Mon, 28 Sep 2020 11:01:34 +0200 Subject: [PATCH 1/5] Testing: Better comments in lib_micheline --- src/lib_micheline/test/assert.ml | 7 ++++ src/lib_micheline/test/test_parser.ml | 50 ++++++++++++++++++++------- 2 files changed, 45 insertions(+), 12 deletions(-) diff --git a/src/lib_micheline/test/assert.ml b/src/lib_micheline/test/assert.ml index 4a7a86151384..fe8cf30de770 100644 --- a/src/lib_micheline/test/assert.ml +++ b/src/lib_micheline/test/assert.ml @@ -23,6 +23,13 @@ (* *) (*****************************************************************************) +(* Testing + ------- + Component: Micheline + Invocation: dune build @src/lib_micheline/runtest + Subject: Helpers. +*) + (* Mini compatibility layer to avoid circular dependency *) module Compat = struct let failwith fmt = Format.kasprintf (fun s -> Lwt.return_error s) fmt diff --git a/src/lib_micheline/test/test_parser.ml b/src/lib_micheline/test/test_parser.ml index 6d76e640b472..5f20c2890ce1 100644 --- a/src/lib_micheline/test/test_parser.ml +++ b/src/lib_micheline/test/test_parser.ml @@ -23,12 +23,21 @@ (* *) (*****************************************************************************) +(* Testing + ------- + Component: Micheline + Invocation: dune build @src/lib_micheline/runtest + Dependencies: src/lib_micheline/test/assert.ml + Subject: Lexing and parsing functions of Micheline terms. +*) + (****************************************************************************) (* Token value *) (****************************************************************************) open Assert.Compat +(* Asserts that an input [given] will generate some output [expected] *) let assert_tokenize ~loc given expected = match Micheline_parser.tokenize given with | (tokens, []) -> @@ -37,14 +46,19 @@ let assert_tokenize ~loc given expected = | (_, _) -> failwith "%s - Cannot tokenize %s" loc given -let assert_tokenize_error ~loc given expected = +(* Asserts that an input [given] will yield a token different from [not_expected] *) +let assert_tokenize_error ~loc given not_expected = match Micheline_parser.tokenize given with | (tokens, []) -> let tokens_got = List.map (fun x -> x.Micheline_parser.token) tokens in - Assert.not_equal_tokens ~loc tokens_got expected + Assert.not_equal_tokens ~loc tokens_got not_expected | (_, _) -> return_unit +(* Test. + Basic tokenizing of strings, bytes, integers, identifiers, + annotations, comments. +*) let test_tokenize_basic () = (* String *) assert_tokenize ~loc:__LOC__ "\"abc\"" [String "abc"] @@ -238,9 +252,9 @@ let test_tokenize_basic () = assert_tokenize_error ~loc:__LOC__ "(" [Close_paren] >>=? fun () -> assert_tokenize_error ~loc:__LOC__ ")" [Open_paren] -(*********************) -(* One line contracts *) - +(* Test. + Tokenizing simple one-line contracts (not including parsing). +*) let test_one_line_contract () = assert_tokenize ~loc:__LOC__ @@ -282,9 +296,9 @@ let test_one_line_contract () = "}{}{}{" [Close_brace; Open_brace; Close_brace; Open_brace; Close_brace; Open_brace] -(*********************************) -(* Conditional contracts *) - +(* Test. + Tokenizing conditional contracts (not including parsing). +*) let test_condition_contract () = assert_tokenize ~loc:__LOC__ @@ -388,7 +402,7 @@ let assert_toplevel_parsing ~loc source expected = >>=? fun () -> iter2_p (Assert.equal ~loc) ast expected >>=? fun () -> return_unit ) -let assert_toplevel_parsing_error ~loc source expected = +let assert_toplevel_parsing_error ~loc source not_expected = match Micheline_parser.tokenize source with | (_, _ :: _) -> return_unit @@ -398,10 +412,13 @@ let assert_toplevel_parsing_error ~loc source expected = return_unit | (ast, []) -> let ast = List.map Micheline.strip_locations ast in - let expected = List.map Micheline.strip_locations expected in - Assert.equal ~loc (List.length ast) (List.length expected) - >>=? fun () -> iter2_p (Assert.not_equal ~loc) ast expected ) + let not_expected = List.map Micheline.strip_locations not_expected in + Assert.equal ~loc (List.length ast) (List.length not_expected) + >>=? fun () -> iter2_p (Assert.not_equal ~loc) ast not_expected ) +(* Test. + Parse basic terms (not including type-checking). +*) let test_basic_parsing () = assert_toplevel_parsing ~loc:__LOC__ @@ -534,6 +551,9 @@ let test_basic_parsing () = ], [] ) ] +(* Test. + Parses a contract with conditional IF. +*) let test_condition_contract_parsing () = assert_toplevel_parsing ~loc:__LOC__ @@ -555,6 +575,9 @@ let test_condition_contract_parsing () = ] ) ], [] ) ] +(* Test. + Parses a contract which appends two lists. +*) let test_list_append_parsing () = assert_toplevel_parsing ~loc:__LOC__ @@ -653,6 +676,9 @@ let assert_expression_parsing ~loc source expected = let expected = Micheline.strip_locations expected in Assert.equal ~loc ast expected ) +(* Test. + Parses Michelson-expressions. +*) let test_parses_expression () = (* String *) assert_expression_parsing -- GitLab From 9263bda6405ad0e2de8138926da207ee0e4a99bc Mon Sep 17 00:00:00 2001 From: Hai Nguyen Van Date: Mon, 5 Oct 2020 13:23:47 +0200 Subject: [PATCH 2/5] Testing: Minor change in src/lib_micheline/test/test_parser.ml --- src/lib_micheline/test/test_parser.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib_micheline/test/test_parser.ml b/src/lib_micheline/test/test_parser.ml index 5f20c2890ce1..e1b7a5d15d11 100644 --- a/src/lib_micheline/test/test_parser.ml +++ b/src/lib_micheline/test/test_parser.ml @@ -28,7 +28,7 @@ Component: Micheline Invocation: dune build @src/lib_micheline/runtest Dependencies: src/lib_micheline/test/assert.ml - Subject: Lexing and parsing functions of Micheline terms. + Subject: Lexing and parsing functions for Micheline terms. *) (****************************************************************************) -- GitLab From 65cc7cbfe90b3807c5d64b19ae191998087c7595 Mon Sep 17 00:00:00 2001 From: Hai Nguyen Van Date: Mon, 12 Oct 2020 09:18:58 +0200 Subject: [PATCH 3/5] Testing: Variable renaming following @rafoo_'s review --- src/lib_micheline/test/test_parser.ml | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/lib_micheline/test/test_parser.ml b/src/lib_micheline/test/test_parser.ml index e1b7a5d15d11..76ec616157cf 100644 --- a/src/lib_micheline/test/test_parser.ml +++ b/src/lib_micheline/test/test_parser.ml @@ -46,12 +46,13 @@ let assert_tokenize ~loc given expected = | (_, _) -> failwith "%s - Cannot tokenize %s" loc given -(* Asserts that an input [given] will yield a token different from [not_expected] *) -let assert_tokenize_error ~loc given not_expected = +(* Asserts that the token produced by the input [given] is not present + in the [forbidden_tokens] list. *) +let assert_tokenize_error ~loc given forbidden_tokens = match Micheline_parser.tokenize given with | (tokens, []) -> let tokens_got = List.map (fun x -> x.Micheline_parser.token) tokens in - Assert.not_equal_tokens ~loc tokens_got not_expected + Assert.not_equal_tokens ~loc tokens_got forbidden_tokens | (_, _) -> return_unit @@ -402,7 +403,7 @@ let assert_toplevel_parsing ~loc source expected = >>=? fun () -> iter2_p (Assert.equal ~loc) ast expected >>=? fun () -> return_unit ) -let assert_toplevel_parsing_error ~loc source not_expected = +let assert_toplevel_parsing_error ~loc source forbidden_tokens = match Micheline_parser.tokenize source with | (_, _ :: _) -> return_unit @@ -412,9 +413,9 @@ let assert_toplevel_parsing_error ~loc source not_expected = return_unit | (ast, []) -> let ast = List.map Micheline.strip_locations ast in - let not_expected = List.map Micheline.strip_locations not_expected in - Assert.equal ~loc (List.length ast) (List.length not_expected) - >>=? fun () -> iter2_p (Assert.not_equal ~loc) ast not_expected ) + let forbidden_tokens = List.map Micheline.strip_locations forbidden_tokens in + Assert.equal ~loc (List.length ast) (List.length forbidden_tokens) + >>=? fun () -> iter2_p (Assert.not_equal ~loc) ast forbidden_tokens ) (* Test. Parse basic terms (not including type-checking). -- GitLab From 17693c71b69d77aa4687b6eb79f3533494a8f0b8 Mon Sep 17 00:00:00 2001 From: Hai Nguyen Van Date: Tue, 27 Oct 2020 10:24:48 +0100 Subject: [PATCH 4/5] Testing: Compliance with ocamldoc --- src/lib_micheline/test/assert.ml | 10 +++--- src/lib_micheline/test/test_parser.ml | 48 ++++++++++----------------- 2 files changed, 22 insertions(+), 36 deletions(-) diff --git a/src/lib_micheline/test/assert.ml b/src/lib_micheline/test/assert.ml index fe8cf30de770..5339b91f2206 100644 --- a/src/lib_micheline/test/assert.ml +++ b/src/lib_micheline/test/assert.ml @@ -23,11 +23,11 @@ (* *) (*****************************************************************************) -(* Testing - ------- - Component: Micheline - Invocation: dune build @src/lib_micheline/runtest - Subject: Helpers. +(** Testing + ------- + Component: Micheline + Invocation: dune build @src/lib_micheline/runtest + Subject: Helpers. *) (* Mini compatibility layer to avoid circular dependency *) diff --git a/src/lib_micheline/test/test_parser.ml b/src/lib_micheline/test/test_parser.ml index 76ec616157cf..c9743d48fbcc 100644 --- a/src/lib_micheline/test/test_parser.ml +++ b/src/lib_micheline/test/test_parser.ml @@ -23,12 +23,12 @@ (* *) (*****************************************************************************) -(* Testing - ------- - Component: Micheline - Invocation: dune build @src/lib_micheline/runtest - Dependencies: src/lib_micheline/test/assert.ml - Subject: Lexing and parsing functions for Micheline terms. +(** Testing + ------- + Component: Micheline + Invocation: dune build @src/lib_micheline/runtest + Dependencies: src/lib_micheline/test/assert.ml + Subject: Lexing and parsing functions for Micheline terms. *) (****************************************************************************) @@ -37,7 +37,7 @@ open Assert.Compat -(* Asserts that an input [given] will generate some output [expected] *) +(** Asserts that an input [given] will generate some output [expected] *) let assert_tokenize ~loc given expected = match Micheline_parser.tokenize given with | (tokens, []) -> @@ -46,8 +46,8 @@ let assert_tokenize ~loc given expected = | (_, _) -> failwith "%s - Cannot tokenize %s" loc given -(* Asserts that the token produced by the input [given] is not present - in the [forbidden_tokens] list. *) +(** Asserts that the token produced by the input [given] is not + present in the [forbidden_tokens] list. *) let assert_tokenize_error ~loc given forbidden_tokens = match Micheline_parser.tokenize given with | (tokens, []) -> @@ -56,10 +56,8 @@ let assert_tokenize_error ~loc given forbidden_tokens = | (_, _) -> return_unit -(* Test. - Basic tokenizing of strings, bytes, integers, identifiers, - annotations, comments. -*) +(** Basic tokenizing of strings, bytes, integers, identifiers, + annotations, comments. *) let test_tokenize_basic () = (* String *) assert_tokenize ~loc:__LOC__ "\"abc\"" [String "abc"] @@ -253,9 +251,7 @@ let test_tokenize_basic () = assert_tokenize_error ~loc:__LOC__ "(" [Close_paren] >>=? fun () -> assert_tokenize_error ~loc:__LOC__ ")" [Open_paren] -(* Test. - Tokenizing simple one-line contracts (not including parsing). -*) +(** Tokenizing simple one-line contracts (not including parsing). *) let test_one_line_contract () = assert_tokenize ~loc:__LOC__ @@ -297,9 +293,7 @@ let test_one_line_contract () = "}{}{}{" [Close_brace; Open_brace; Close_brace; Open_brace; Close_brace; Open_brace] -(* Test. - Tokenizing conditional contracts (not including parsing). -*) +(** Tokenizing conditional contracts (not including parsing). *) let test_condition_contract () = assert_tokenize ~loc:__LOC__ @@ -417,9 +411,7 @@ let assert_toplevel_parsing_error ~loc source forbidden_tokens = Assert.equal ~loc (List.length ast) (List.length forbidden_tokens) >>=? fun () -> iter2_p (Assert.not_equal ~loc) ast forbidden_tokens ) -(* Test. - Parse basic terms (not including type-checking). -*) +(** Parse basic terms (not including type-checking). *) let test_basic_parsing () = assert_toplevel_parsing ~loc:__LOC__ @@ -552,9 +544,7 @@ let test_basic_parsing () = ], [] ) ] -(* Test. - Parses a contract with conditional IF. -*) +(** Parses a contract with conditional IF. *) let test_condition_contract_parsing () = assert_toplevel_parsing ~loc:__LOC__ @@ -576,9 +566,7 @@ let test_condition_contract_parsing () = ] ) ], [] ) ] -(* Test. - Parses a contract which appends two lists. -*) +(** Parses a contract which appends two lists. *) let test_list_append_parsing () = assert_toplevel_parsing ~loc:__LOC__ @@ -677,9 +665,7 @@ let assert_expression_parsing ~loc source expected = let expected = Micheline.strip_locations expected in Assert.equal ~loc ast expected ) -(* Test. - Parses Michelson-expressions. -*) +(** Parses Michelson-expressions. *) let test_parses_expression () = (* String *) assert_expression_parsing -- GitLab From a9020906a690b87202cee82031bfdc0cf6fa7b30 Mon Sep 17 00:00:00 2001 From: Hai Nguyen Van Date: Tue, 27 Oct 2020 10:37:10 +0100 Subject: [PATCH 5/5] Testing: Compatibility with linter --- src/lib_micheline/test/test_parser.ml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/lib_micheline/test/test_parser.ml b/src/lib_micheline/test/test_parser.ml index c9743d48fbcc..5090989aacac 100644 --- a/src/lib_micheline/test/test_parser.ml +++ b/src/lib_micheline/test/test_parser.ml @@ -407,7 +407,9 @@ let assert_toplevel_parsing_error ~loc source forbidden_tokens = return_unit | (ast, []) -> let ast = List.map Micheline.strip_locations ast in - let forbidden_tokens = List.map Micheline.strip_locations forbidden_tokens in + let forbidden_tokens = + List.map Micheline.strip_locations forbidden_tokens + in Assert.equal ~loc (List.length ast) (List.length forbidden_tokens) >>=? fun () -> iter2_p (Assert.not_equal ~loc) ast forbidden_tokens ) -- GitLab