Skip to content

Commit

Permalink
Update tests to use test_lib for extended testing and verify type map…
Browse files Browse the repository at this point in the history
…ping
  • Loading branch information
andersfugmann committed Jan 31, 2024
1 parent fbd8f8c commit ada5af4
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 35 deletions.
7 changes: 7 additions & 0 deletions test/extensions_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ let%expect_test _ =
let foo = Extensions.Foo.{ bar = Some 5; extensions' = Ocaml_protoc_plugin.Extensions.default } in
let foo = Extensions.Baz'.set foo (Some 8) in
let foo = Extensions.Baz'.set foo (Some 7) in
Test_lib.test_encode ~protoc:false (module Extensions.Foo) foo;
let baz = Extensions.Baz'.get foo in
print_endline ([%show: Extensions.Foo.t] foo);
print_endline ([%show: Extensions.Baz.t Ocaml_protoc_plugin.Result.t] baz);
Expand All @@ -34,6 +35,8 @@ let%expect_test _ =
let foo = Extensions.Baz'.set foo (Some 0) in
let foo = Extensions.B2.set foo ([6;7;8]) in
let foo = Extensions.B2.set foo ([]) in
Test_lib.test_encode ~protoc:false (module Extensions.Foo) foo;

print_endline ([%show: Extensions.Foo.t] foo);
();
[%expect {|
Expand All @@ -42,6 +45,8 @@ let%expect_test _ =
let%expect_test _ =
let foo = Extensions.Foo.{ bar = Some 5; extensions' = Ocaml_protoc_plugin.Extensions.default } in
let foo = Extensions.Baz'.set foo (Some 7) in
Test_lib.test_encode ~protoc:false (module Extensions.Foo) foo;

let foo' =
Extensions.Foo.to_proto foo
|> Ocaml_protoc_plugin.Writer.contents
Expand All @@ -61,6 +66,7 @@ let%expect_test _ =
let%expect_test _ =
let v = [6;7;8;9] in
let foo = Extensions.Foo.{ bar = Some 5; extensions' = Ocaml_protoc_plugin.Extensions.default } in
Test_lib.test_encode ~protoc:false (module Extensions.Foo) foo;
let foo = Extensions.R_baz.set foo v in
let foo' =
Extensions.Foo.to_proto foo
Expand Down Expand Up @@ -100,6 +106,7 @@ let%expect_test _ =

let foo = Extensions.B.set foo 0 in
Printf.printf "Set B = 0: %d\n" (Extensions.B.get foo |> Ocaml_protoc_plugin.Result.get ~msg:"No Value");
Test_lib.test_encode ~protoc:false (module Extensions.Foo) foo;
print_endline ([%show: Extensions.Foo.t] foo);
();
[%expect {|
Expand Down
15 changes: 2 additions & 13 deletions test/large_test.ml
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
open Large
open Ocaml_protoc_plugin

let%expect_test "Test very large message type" =
let large = Large.make ~x7:7 () in
let writer = Large.to_proto large in
let contents = Writer.contents writer in
Printf.printf "Size of large message: %d\n" (String.length contents);
let reader = Reader.create contents in
let large' = Large.from_proto_exn reader in
Printf.printf "Serialization works: %b\n" (large = large');
Printf.printf "x7: %d = %d\n" large.x7 large'.x7;
Printf.printf "x5: %d = %d\n" large.x5 large'.x5;
Test_lib.test_encode (module Large) large;
();
[%expect {|
Size of large message: 2
Serialization works: true
x7: 7 = 7
x5: 0 = 0 |}]
x7: 7 |}]
1 change: 0 additions & 1 deletion test/merge_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ open Merge.Merge

let test_merge (type t) (module T: Test_lib.T with type t = t) (init : t) (ts: t list) =
let open Ocaml_protoc_plugin in

let writer = Writer.init () in
let expect =
List.fold_left ~init ~f:(fun acc t ->
Expand Down
22 changes: 2 additions & 20 deletions test/primitive_types_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,8 @@ let%expect_test _ =

let%expect_test _ =
let module T = Primitive_types.Types in
let t =
T.
{
int64 = 0;
sint64 = 0;
uint64 = 0;
int32 = 0;
sint32 = 0;
uint32 = 0;
double = 0.0;
float = 0.0;
fixed64 = 0L;
fixed32 = 0l;
sfixed64 = 0L;
sfixed32 = 0l;
bool = false;
string = "";
bytes = Bytes.of_string "";
}
in
let t = T.make () in
Test_lib.test_encode (module T) t;
let bin = T.to_proto t in
Printf.printf "Size: %d%!" (Ocaml_protoc_plugin.Writer.contents bin |> String.length);
[%expect {| Size: 0 |}]
Expand Down
1 change: 1 addition & 0 deletions test/proto2.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ message A {
message Message {
message Required {
optional uint64 a = 1;
optional uint64 b = 2;
}
enum E {
A = 0;
Expand Down
7 changes: 6 additions & 1 deletion test/proto2_test.ml
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
open Proto2

let%expect_test _ =
let module T = Proto2.Message in
let t = T.{enum = Some E.B; i = 0; j = 5; required = Some 7; k = Some 5 } in
(* Verify signature that required messages are mapped as mandatory arguments *)
let make: ?enum:T.E.t -> ?i:int -> j:int -> required:T.Required.t -> ?k:int -> unit -> T.t = T.make in
let t' = make ~enum:T.E.B ~i:0 ~j:5 ~required:(T.Required.make ~a:7 ()) ~k:5 () in
let t = T.{enum = Some E.B; i = 0; j = 5; required = T.Required.make ~a:7 (); k = Some 5 } in
if (not (T.equal t t')) then Printf.eprintf "Error: Type does not match";
Test_lib.test_encode (module T) t;
[%expect {|
enum: B
Expand Down

0 comments on commit ada5af4

Please sign in to comment.